28

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

Shawn Miller
  • 7,082
  • 6
  • 46
  • 54
  • Useful linked SO question: http://stackoverflow.com/questions/536359/why-microsoft-made-javascriptserializer-obsolete-prior-to-net-3-5-sp1-and-again – James Wiseman Jan 16 '12 at 11:15

3 Answers3

14

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.

Kamarey
  • 10,832
  • 7
  • 57
  • 70
  • The link is broken since live spaces moved to WordPress.com ... and there's no redirect either. Is this article available anywhere else? – EAMann Oct 10 '11 at 20:13
  • Who is so stupid to downvote 6 year answer without specifying a reason? – Kamarey Feb 01 '15 at 11:24
9

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

James Newton-King
  • 48,174
  • 24
  • 109
  • 130
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
1

The JavaScriptSerializer is marked as obsolete in framework 3.5. You shouldn't use it for that reason. However, back to your question. The JavaScriptSerializer doesn't require classes to be marked as [Serializable] or as [DataContract] but the DataContractJsonSerializer does. If you have compiled classes that you can't mark with attributes, you may want to use the older JSON serializer.