To JSON or not to JSON?
To JSON or not to JSON?
I want to introduce some performance testing about DataContractJsonSerializer against DataContractSerializer.
But first, I want to explain what is JSON and DataContractJsonSerializer?
What is JSON?
JSON (JavaScript Object Notation) is a lightweight syntax for representing data that enabling fast exchanges of small amount of data between client browsers and AJAX-enabled Web services.
JSON encodes data using subset of the JavaScript object literals, and it easier to "parse" for JavaScript client code, and used as alternative to XML in AJAX applications.
The following sample illustrates how easy you can parse JSON in JavaScript, using 'eval' method:
var jsonText = “someVar = { 'color' : 'blue' }”;
eval( ‘(‘ + jsonText + ‘)’ );
alert(someVar.color);
DataContractJsonSerializer
With DataContractJsonSerializer introduced with WCF 3.5 you can serialize instance of .NET type into a JSON document, and de-serialize JSON document into an instance of a .NET type.
.NET Framework 3.5 include reach support of JSON in WCF. You can specify for each method if it accept/return JSON format or XML.
The DataContractJsonSerializer can be plugged by using WebScriptEnablingBehavior or by using WebHttpBehavior.
For WebScriptEnablingBehavior see the following post about WCF and ASP.NET AJAX Integration.
JsonReaderWriterFactory
WCF processes JSON messages using mapping between JSON data and the XML info-set. For that WCF use the XmlDictionaryReader and XmlDictionaryWriter produced by the JsonReaderWriterFactory, which provide an XML API over JSON.
The following JSON object:
{ “product” : “pencil”, “price” : 12 }
Translates into the following XML info-set:
<root type=“object”>
<product type=“string”>pencil</product>
<price type=“number”>12</price>
</root>
The Performance Test
I'm including two tests:
1) One customer with simple data.
| |
JSON |
XML |
JSON / XML |
| CustomerList Length |
275 |
471 |
0.5838 |
| Serialization Time |
0.18 |
0.02 |
9 |
| Deserialization Time |
0.3 |
0.1 |
3 |
2) List of 13 Customers, each contains 20 sales orders, each contains 75 sales order items.
| |
JSON |
XML |
JSON / XML |
| CustomerList Length |
1587031 |
3007061 |
0.5277 |
| Serialization Time |
294.3 |
181.4 |
1.6223 |
| Deserialization Time |
416 |
323 |
1.2879 |
Conclusion
It seems that DataContractJsonSerializer doesn't implemented in a way that adhere to the JSON as a lightweight syntax advantages over XML. But, the test should be completed, and with the end-to-end results, by parsing the data also at the browser side (JavaScript), the final results should be different.
Technorati Tags:
WCF 3.5,
AJAX,
JSON