What’s the difference between DataContractJsonSerializer and JavaScriptSerializer?

The .NET Framework ships with System.Runtime.Serialization.Json.DataContractJsonSerializer and System.Web.Script.Serialization.JavaScriptSerializer, both of which de/serialize JSON. How do I know when to choose one of these types over the other? MSDN doesn’t make it clear what their relative advantages are.

We have several projects that consume or emit JSON, and the class selected for each thus far has depended on the opinion of the primary dev on each project. Some are simple, two have complex logic regarding producing managed types from JSON (the types do not map closely to the streams) but don’t have any emphasis on speed, one requires speed. None interact with WCF, at least as of now.

While I’m interested in alternative libraries, I am hoping that somebody might have an answer to my question too.

What is the difference between JSON.NET DataContractJsonSerializer and the Newtonsoft JSON serializer

Can someone help me. What’s the difference between the built in JSON.NET DataContractJsonSerializer and the Newtonsoft JSON serializer? Is it correct that I can use one or the other with Web API and

Whats the difference between flush and commit in Hibernate?

Whats the difference between flush and commit in Hibernate ? Thanks GNR

Whats the difference between CVImage and IplImage?

I want to know whats the difference between CvImage and IplImage? Because I’m trying to integrate OpenCV with ARToolKit, but I’m getting a error here: CvImage *image; This say that is expected ‘=’,’

Whats the difference between THsHaServer and TNonblockingServer?

Whats the difference between those two ? And where do I find a good doc about what is what ? I went through their JavaDoc, and it doesn’t explain a lot of questions [ which I am going to haunt you guy

whats the difference between C strings and C++ strings?

whats the difference between C Strings and C++ strings. Specially while doing dynamic memory allocation

Whats the difference between the 2 url routes in Django?

In Django, we define url mapping in urls.py. Whats the difference between these two? /app/kill/$ /app/kill$

Whats the difference between ? : and ||

What difference is there between using the ?: conditional operator and the || Logical OR. I am finding that my code works with: $screenpixelratio = !empty($_COOKIE[‘screenpixelratio’]) || $_COOKIE[‘sc

Whats the difference between a GUID and a UUID? [duplicate]

Possible Duplicate: Is there any difference between a GUID and a UUID? Whats the difference between a GUID and a UUID, and which should I use for true uniqueness? Update: What things do I need for u

Whats the difference between using .on(‘click’ and .click? [duplicate]

Possible Duplicate: Difference between .on(‘click’) vs .click() When handling a click of a div whats the difference between using .on and .click : $(‘#myDiv’).on(‘click’ , function(e) { }); $(‘#my

whats is difference between text/javascript and application/javascript [duplicate]

Possible Duplicate: When serving JavaScript files, is it better to use the application/javascript or application/x-javascript whats is difference between text/javascript and application/javascript?

Answers

Personally, I think that DataContractJsonSerializer reeks of over-engineering. I’d skip it and go with JavaScriptSerializer. In the event where JavaScriptSerializer isn’t available, you can use FridayThe13th (a library I wrote ;p).

The DataContractJsonSerializer is intended for use with WCF client applications where the serialized types are typically POCO classes with the DataContract attribute applied to them. No DataContract, no serialization. The mapping mechanism of WCF makes the sending and receiving very simple, but only if your platform is homogeneous. If you start mixing in different toolsets, your program might go sideways.

The JavaScriptSerializer can serialize any type, including anonymous types (one way), and does so in a more conformant way. You lose the “automagic” of WCF, but you gain more integration options.

As you can see by the comments, there are a lot of options out there for AJAX serialization, and to address your speed vs. maintainability questions, it might be worth investigating them to find a solution that meets the needs of all the teams, to reduce maintainability issues in the long term as everybody does things their own way.

2014-04-07 UPDATE: I suggest using JSON.NET if you can. See http://james.newtonking.com/json Feature Comparison for a review of the 3 libraries considered in this question.

Both do approximately the same but using very different infrastructure thus applying different restrictions on the classes you want to serialize/deserialize and providing different degree of flexibility in tuning the serialization/deserialization process.

For DataContractJsonSerializer you must mark all classes you want to serialize using DataContract atrtibute and all members using DataMember attribute. As well as if some of you classes have enum members, then the enums also must be marked as DataContract and each enum member – with EnumMember attribute. Also DataContractJsonSerializer allows you fine control over the whole process of serialization/deserialization by altering types resolution logic and replacing the types you serialize with surrogates.

For JavaScriptSerializer you must provide parameterless constructor if you plan on deserializing objects from json string.

For me, I usually use JavaScriptSerializer in presentation logic, where there’s a simple model I want to render in Json together with page, without additional ajax requests. And I even usually don’t have to deserialize them back to c# – so there’s no overhead at all. But if it’s persistence logic, where I want to save objects into a data store (usually no-sql storage), to load them later, I prefer using DataContractJsonSerializer because the overhead of putting attributes is worth of flexibility in the serialization/deserialization process tuning, especially when it comes to loading of serialized data into the objects of the newer version, with updated definitions