What's the difference between DataContractJsonSerializer and JavaScriptSerializer?
Asked Answered
H

3

88

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.

Huffish answered 15/2, 2012 at 21:41 Comment(5)
use Json.Net json.codeplex.com . You will have much more control over serialization/deserialization process.Superintend
Depends on how you're using it. DataContractJsonSerializer tends to work well with WCF. JavaScriptSerializer is much simpler. What are you trying to do?Ballroom
Use ServiceStack.JsonSerializer - it's the fastest. But it doesn't honor [ScriptIgnore] attribute. Not an issue if you doesn't need it. See a long discussion about it here: #9151420Angelynanger
Why does everybody put their answers in comments? It makes it hard to vote on the answers or comment on them.Huffish
@JustinR. perhaps because of some police here who would downvote one line answers and say: "this should be a comment".Feinberg
A
68

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.

2015-05-26 UPDATE: If your company requires the use of commercially licensable products, or you need every last bit of performance, you may also want to check out https://servicestack.net/.

Aurum answered 22/2, 2012 at 21:53 Comment(1)
What are the serialization performance differences between the two of them? Lets say they will serialize the same number of entities with the same number of attributes?Zeba
B
21

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

Broucek answered 27/2, 2012 at 12:32 Comment(0)
R
2

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).

Ruthanneruthe answered 15/2, 2012 at 22:10 Comment(1)
Json.Net is everywhere. .Net 2.0, 3.5, 4.0, Silverlight, WindowsPhoneSuperintend

© 2022 - 2024 — McMap. All rights reserved.