To JSON or not to JSON?

15 ביוני 2008

תגיות: , ,
3 תגובות

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.

kick it on DotNetKicks.com

Technorati Tags: ,,
הוסף תגובה
facebook linkedin twitter email

כתיבת תגובה

האימייל לא יוצג באתר. (*) שדות חובה מסומנים

3 תגובות

  1. Avi Pinto16 ביוני 2008 ב 7:59

    Thanks for this post, some points favoring JSON:
    Also don't forget the bandwidth that you save as a result of JSON

    The parsing at the Client side, as you said – you didn't take into consideration now, and this is usually where the perfoemence tweaks do count

    And the readability of the JS code you write when using JSON id much better

    להגיב
  2. jw18 ביוני 2008 ב 19:30

    I really like using json, but I seem to have to go too far out of my way to present the data that it returns.

    Most of the time I find that any additional XML overhead is worth it to be able to perform an XML transformation on the fly.

    להגיב
  3. Mike18 ביוני 2008 ב 21:20

    I found similar things with JSON and Flex, though the format can be quite a bit more concise, the libraries to serialize/deserialize XML seem to be significantly faster than those for JSON.

    להגיב