fastJSON
Introduction
This is the smallest and fastest polymorphic JSON serializer, smallest because it's only 18kb when compiled, fastest because most of the time it is (see performance test section) and polymorphic because it can serialize and deserialize the following situation correctly at run-time with what ever object you throw at it:
class animal { public string Name { get; set;} }
class cat: animal { public int legs { get; set;} }
class dog : animal { public bool tail { get; set;} }
class zoo { public List<animal> animals { get; set;} }
var zoo1 = new zoo();
zoo1.animals = new List<animal>();
zoo1.animals.Add(new cat());
zoo1.animals.Add(new dog());
This is a very important point because it simplifies your coding immensely and is a cornerstone of object orientated programming, strangely few serializers handle this situation, even the XmlSerializer
in .NET doesn't do this and you have to jump through hoops to get it to work. Also this is a must if you want to replace the BinaryFormatter serializer which what most transport protocols use in applications and can handle any .NET object structure (see my WCF Killer article).
The What and Why of JSON
JSON (Java Script Object Notation) is a text or human readable format invented by Douglas Crockford around 1999 primarily as a data exchange format for web applications (see www.JSON.org). The benefits of which are ( in regards to XML which was used before):
- Structured data format like XML
- High signal to noise ratio in other words it does away with extra characters which are not conclusive to the data ( angle brackets and slashes in XML)
- Compact data format
- Simple parsing rules which makes the processing of data easy and fast
So its good for the following scenarios:
- Data exchange between same or different platforms like Java, .NET services over the wire.
- Data storage: MongoDB (www.mongodb.org) uses JSON as an internal storage format.
Features of this implementation
- Just 3 classes + 2 helpers : 883 lines of code
- JSON standard compliant with the following additions
- "
$type
" is used to denote object type information [ Json.NET does this as well ]. - "
$schema
" is used to denote the dataset schema information - Works on .NET 2.0+ : some implementations in the list of alternatives below require at least .NET 3.5
- Extremely small size : 18kb when compiled
- Blazingly fast (see the performance tests section)
- Can dynamically create types
- Handles
Guid
,Dataset
,Dictionary
,Hashtable
andGeneric
lists - Handles Nullable types
- Handles byte arrays as base64 strings
- Handles polymorphic collections of objects
- Thread safe
Limitations
- Currently can't deserialize value type array properties (e.g.
int[]
char[]
etc.)
What's out there
In this section I will discuss some of the JSON alternatives that I have personally used. Although I can't say it is a comprehensive list, it does however showcase the best of what is out there.
XML
If you are using XML, then don't. It's too slow and bloated, it does deserve an honorable mention as being the first thing everyone uses, but seriously don't. It's about 50 times slower than the slowest JSON in this list. The upside is that you can convert to and from JSON easily.
BinaryFormatter
Probably the most robust format for computer to computer data transfer. It has a pretty good performance although some implementation here beat it.
Pros | Cons |
|
|
Json.NET
The most referenced JSON serializer for the .NET framework is Json.NET from (http://JSON.codeplex.com/) and the blog site (http://james.newtonking.com/pages/JSON-net.aspx). It was the first JSON implementation I used in my own applications.
Pros | Cons |
|
|
LitJSON
I had to look around a lot to find this gem (http://litjson.sourceforge.NET/), which is still at version 0.5 since 2007. This was what I was using before my own implementation and it replaced the previous JSON serializer which was Json.NET. Admittedly I had to change the original to fit the requirements stated above.
Pros | Cons |
|
|
ServiceStack Serializer
An amazingly fast JSON serializer from Demis Bellot found at (http://www.servicestack.NET/mythz_blog/?p=344). The serializer speed is astonishing, although it does not support what is needed from the serializer. I have included it here as a measure of performance.
Pros | Cons |
|
|
Using the code
To use the code do the following:
// to serialize an object to string
string jsonText = fastJSON.JSON.Instance.ToJSON(c);
// to deserialize a string to an object
var newobj = fastJSON.JSON.Instance.ToObject(jsonText);
The main class is JSON which is implemented as a singleton so it can cache type and property information for speed.
Performance Tests
All test were run on the following computer:
- AMD K625 1.5Ghz Processor
- 4Gb Ram DDR2
- Windows 7 Home Premium 64bit
- Windows Rating of 3.9
The tests were conducted under three different .NET compilation versions
- .NET 3.5
- .NET 4 with processor type set to auto
- .NET 4 with processor type set to x86
The Excel screen shots below are the results of these test with the following descriptions:
- The numbers are elapsed time in milliseconds.
- The more red the background the slower the times
- The more green the background the faster the times.
- 5 tests were conducted for each serializer.
- The "AVG" column is the average for the last 4 tests excluding the first test which is basically the serializer setting up its internal caching structures, and the times are off.
- The "min" row is the minimum numbers in the respective columns below.
- The Json.NET serializer was tested with two version of 3.5r6 and 4.0r1 which is the current one.
- "bin" is the BinaryFormatter tests which for reference.
- The test structure is the code below which is a 5 time loop with an inner processing of 1000 objects.
- Some data types were removed from the test data structure so all serializers could work.
The test code template
The following is the basic test code template, as you can see it is a loop of 5 tests of what we want to test each done count time (1000 times). The elapsed time is written out to the console with tab formatting so you can pipe it to a file for easier viewing in an Excel spreadsheet.
int count = 1000;
private static void fastjson_serialize()
{
Console.WriteLine();
Console.Write("fastjson serialize");
for (int tests = 0; tests < 5; tests++)
{
DateTime st = DateTime.Now;
colclass c;
string jsonText = null;
c = CreateObject();
for (int i = 0; i < count; i++)
{
jsonText = fastJSON.JSON.Instance.ToJSON(c);
}
Console.Write("\t" + DateTime.Now.Subtract(st).TotalMilliseconds + "\t");
}
}
The test data structure
The test data are the following classes which show the polymorphic nature we want to test. The "colclass" is a collection of these data structures. In the attached source files more exotic data structures like Hashtables, Dictionaries, Datasets etc. are included.
[Serializable()]
public class baseclass
{
public string Name { get; set; }
public string Code { get; set; }
}
[Serializable()]
public class class1 : baseclass
{
public Guid guid { get; set; }
}
[Serializable()]
public class class2 : baseclass
{
public string description { get; set; }
}
[Serializable()]
public class colclass
{
public colclass()
{
items = new
List<baseclass>();
date = DateTime.Now;
multilineString = @"
AJKLjaskljLA
ahjksjkAHJKS
AJKHSKJhaksjhAHSJKa
AJKSHajkhsjkHKSJKash
ASJKhasjkKASJKahsjk
";
gggg = Guid.NewGuid();
//hash = new Hashtable();
isNew = true;
done= true;
}
public bool done { get; set; }
public DateTime date {get; set;}
//public DataSet ds { get; set; }
public string multilineString { get; set; }
public List<baseclass> items { get; set; }
public Guid gggg {get; set;}
public decimal? dec {get; set;}
public bool isNew { get; set; }
//public Hashtable hash { get; set; }
}
.NET 3.5 Serialize
- fastJSON is second place in this test by a margin of nearly 35% slower than Stacks.
- fastJSON is nearly 2.9x faster than binary formatter.
- Json.NET is nearly 1.9x slower in the new version 4.0r1 against its previous version of 3.5r6
- Json.NET v3.5r6 is nearly 20% faster than binary formatter.
.NET 3.5 Deserialize
- fastJSON is first place in this test to Stacks by a margin of 10%.
- fastJSON is nearly 4x faster than nearest other JSON.
- Json.NET is nearly 1.5x faster in version 4.0r1 than its previous version of 3.5r6
.NET 4 Auto Serialize
- fastJSON is first place in this test by a margin of nearly 20% against Stacks.
- fastJSON is nearly 4.9x faster than binary formatter.
- Json.NET v3.5r6 is on par with binary formatter.
.NET 4 Auto Deserialize

- fastJSON is first place by a margin of 11%.
- fastJSON is 1.7x faster than binary formatter.
- Json.NET v4 1.5x faster than its previous version.
.NET 4 x86 Serialize

- fastJSON is first place in this test by a margin of nearly 21% against Stacks.
- fastJSON is 4x faster than binary formatter.
- Json.NET v3.5r6 1.7x faster than the previuos version.
.NET 4 x86 Deserialize

- fastJSON is first place by a margin of 5% against Stacks.
- fastJSON is 1.7x faster than binary formatter which is third.
Exotic data type tests
In this section we will see the performance results for exotic data types like datasets, hash tables, dictionaries, etc.. The comparison is between fastJSON and the BinaryFormatter as most of the other serializers can't handle these data types. These include the following:
- Datasets
- Nullable types
- Hashtables
- Dictionaries
- fastJSON is 5x faster than BinaryFormatter in serialization
- fastJSON is 20% faster than BinaryFormatter in deserialization
- Datasets are performance killers by a factor of 10
Performance Conclusions
- fastJSON is faster in all test except the when running the serializer under .NET 3.5 for which Stacks is faster by only 35% (note must be made that Stacks is not polymorphic and can't handle all types so it is not outputting data correctly within the tests).
- .NET 4 is faster than .NET 3.5 by around 15% in these test except for the fastJSON serializer which is 90% faster..
- You can replace BinaryFormatter with fastJSON with a huge performance boost ( this lean way lends it self to compression techniques on the text output also).
- Start up costs for fastJSON is on average 2x faster than Stacks and consistently faster than everyone else.
Points of Interest
I did a lot of performance tuning with a profiler and here are my results:
- Always use a StringBuilder and never strings concats.
- Never do the following stringbuilder.append("string1 + "string2") because it kills performance, replace it with two stringbuilder appends. This point blew my mind and was 50% faster in my tests with the profiler.
- Never give the stringbuilder a capacity value to start with e.g. var stringbuilder = new StringBuilder(4096); . Strange but it is faster without it.
- I tried replacing the StringBuiler with a MemoryStream but it was too slow (100% slower).
- The simplest and the most direct way is probably the fastest as well, case in point reading values as opposed to lexer parser implementations.
- Always use cached reflection properties on objects.
History
- Initial Release : 2011/02/20
- Update v1.1 : 26% performance boost on dataset deserialization, corrected ServiceStack name
发表评论
Thank you for your blog article. this site
Looking forward to reading more. Great article post.Really thank you! Much obliged.
This is very interesting, You are a very skilled blogger. I ave joined your rss feed and look forward to seeking more of your wonderful post. Also, I have shared your site in my social networks!
You have remarked very interesting details ! ps decent website.
Wow, incredible blog format! How lengthy have you been blogging for? you made running a blog look easy. The total glance of your site is fantastic, as well as the content material!
I truly appreciate this post. I ave been looking all over for this! Thank goodness I found it on Bing. You have made my day! Thx again..
really pleasant piece of writing on building up new weblog.
Thanks again for the post.Much thanks again. Really Great.
Louis Vuitton Monogram Galliera Pm Handbag Bag
Sign up form for Joomla without all the bells and whistles?
Thank you for your very fantastically clear fine points and feedback from you. sedan dealers san jose
You need to take part in a contest for the most effective blogs on the web. I will suggest this website!
very good post, i certainly love this web site, keep on it
It as not that I want to copy your web-site, but I really like the style and design. Could you let me know which design are you using? Or was it especially designed?
louis vuitton outlet yorkdale the moment exploring the best tips and hints
Super-Duper site! I am loving it!! Will be back later to read some more. I am taking your feeds also
Major thanks for the article post.Really thank you! Really Great.
written about for many years. Great stuff, just excellent!
Pretty! This was an incredibly wonderful post. Thank you for providing this info.
This particular blog is without a doubt awesome additionally informative. I have picked up a lot of helpful tips out of this source. I ad love to come back again soon. Thanks a lot!
relating to this article. I wish to read even more issues about it!
Simply wanna input on few general things, The website pattern is perfect, the content is rattling fantastic. аЂаThe way you treat yourself sets the standard for others.аЂа by Sonya Friedman.
Looking forward to reading more. Great blog.Really thank you! Keep writing.
Right away I am going away to do my breakfast, later than having my breakfast coming over again to read more news.
Im grateful for the article post.Really looking forward to read more. Really Cool.
You have noted very interesting points! ps decent site.
Thank you for sharing your info. I truly appreciate your efforts and I am waiting for your further post thanks once again.
You have made some really good points there. I looked on the net for more information about the issue and found most people will go along with your views on this site.
very nice post, i certainly love this web site, keep on it
what you are stating and the way in which you say it.
I was able to find good info from your articles.
It as hard to find knowledgeable people on this topic, however, you seem like you know what you are talking about! Thanks
Nice weblog right here! Also your site rather a lot up very fast! What host are you the usage of? Can I get your associate hyperlink in your host? I want my website loaded up as quickly as yours lol
Link exchange is nothing else but it is only placing the other person as webpage link on your page at suitable place and other person will also do similar in support of you.
iXyLYK Regards for this terrific post, I am glad I discovered this web site on yahoo.
Piece of writing writing is also a excitement, if you be acquainted with afterward you can write or else it is complicated to write.
It as really a nice and helpful piece of information. I am glad that you shared this useful info with us. Please keep us informed like this. Thanks for sharing.
What i do not realize is in fact how you are now not actually much more well-favored than you may be right now.
visiting this web site and be updated with the hottest information posted here.
Thanks for the news! Just was thinking about it! By the way Happy New Year to all of you:D
Woman of Alien Perfect work you might have finished, this site is admittedly awesome with fantastic info. Time is God as way of maintaining everything from happening at once.
Im thankful for the article.Thanks Again. Will read on
I truly appreciate this blog.Thanks Again. Really Great.
Major thankies for the blog post.Really looking forward to read more.
I think this internet site holds some very great info for everyone .
Paragraph writing is also a fun, if you be familiar with then you can write
Really informative blog post.Thanks Again. Really Great.
Thank you for some other wonderful post. Where else may anyone get that type of info in such a perfect means of writing? I ave a presentation subsequent week, and I am on the look for such info.
You made some decent points there. I looked on the internet for the issue and found most persons will go along with with your site.
Muchos Gracias for your blog article.Really looking forward to read more. Fantastic.