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

后端 未结 3 1697
太阳男子
太阳男子 2021-02-05 03:41

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

相关标签:
3条回答
  • 2021-02-05 03:49

    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.

    0 讨论(0)
  • 2021-02-05 03:53

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

    0 讨论(0)
  • 2021-02-05 04:03

    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.

    0 讨论(0)
提交回复
热议问题