Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.Web.Script.Serialization.JavaScriptSerializer or System.Runtime.Serialization.Json.DataContractJsonSerializer?

Tags:

json

c#

.net

What's the difference between the two? Why would you use one over the other?

like image 285
Shawn Miller Avatar asked May 06 '09 20:05

Shawn Miller


People also ask

What is DataContractJsonSerializer?

DataContractJsonSerializer(Type, String, IEnumerable<Type>) Initializes a new instance of the DataContractJsonSerializer class to serialize or deserialize an object of a specified type using the XML root element specified by a parameter, with a collection of known types that may be present in the object graph.

What is system runtime serialization?

Serialization is the process of converting an object or a graph of objects into a linear sequence of bytes for either storage or transmission to another location.

What is the use of JavaScriptSerializer in C #?

Converts a JSON-formatted string to an object of the specified type. Converts the specified JSON string to an object of type T .

What is the default serialization used by ASP NET AJAX?

Complex types used as an input parameter or return type on an AJAX-enabled Web Method are automatically serialized into JSON before being sent to the client-side. However, nested complex types (those defined internally within another type) are not made available to the client as standalone objects by default.


2 Answers

Found here: http://aaron-powell.spaces.live.com/blog/cns!91A824220E2BF369!150.entry

DataContractJsonSerializer The primary purpose of the DataContractJsonSerializer is to be used with WCF, since one serialization is a big focus of WCF. Also, it is also better equipped to handle complex classes which have only certain properties available for serialization. This class is more strongly typed, has more knowledge about the type(s) it's handling and better error handling for badly-formed JSON.

JavaScriptSerializer This class on the other hand is much better equipped for quick serialization, it's a more cowboy approach. There's less error checking and less control over what properties which are serialized.

Update

As the above link is dead, here is another link: http://kb.cnblogs.com/a/1454030.

like image 165
Kamarey Avatar answered Sep 29 '22 15:09

Kamarey


Personally, I'd look at Json.NET - this has the advantage of being .NET 2.0 compatible

like image 28
Marc Gravell Avatar answered Sep 29 '22 13:09

Marc Gravell