Does .NET 4 have a built-in JSON serializer/deserializer?
Asked Answered
R

4

74

Does .NET 4 come with any class that serializes/deserializes JSON data?

  • I know there are 3rd-party libraries, such as JSON.NET, but I am looking for something built right into .NET.

  • I found Data Contracts on MSDN, but it is for WCF, not for Winforms or WPF.

Roundtree answered 18/7, 2010 at 14:23 Comment(5)
JSON.Net is well supported and it appears that Microsoft intend to adopt it themselves "We on the web team will be including JSON.NET as the default JSON Serializer in Web API when it releases, so that'll be nice." from hanselman.com/blog/…Aggrade
Just be aware of the embedded library for JSon serializing's performance in .Net!Sentimentalize
@Sentimentalize what do you mean beware? Please elaborate.Narine
@EriawanKusumawardhono, It has not a very good performance. I'm using SimpleJSON not very easy to use but it has much better performance.Sentimentalize
FWIW: I haven't tried SimpleJSON, but Newtonsoft's library (= JSON.NET) is easy to use, fairly well documented and - as far as I have experienced, and I use it extensively for de/serializing - very performant!Retorsion
S
44

You can use the DataContractJsonSerializer class anywhere you want, it is just a .net class and is not limited to WCF. More info on how to use it here and here.

Susie answered 18/7, 2010 at 14:27 Comment(4)
Thanks,MSDN said DataContractJsonSerializer class in Assembly: System.Runtime.Serialization (in System.Runtime.Serialization.dll). However, VS2010 show error, cannot find DataContractJsonSerializer .Roundtree
@TatMing That's because, IIRC, pre .Net V4 it resides in System.ServiceModel.WebCajolery
Find that~ It about Target Framework problem, see : #1825917Roundtree
The only problem with this is that you have to decorate your classes with attributes.Plumb
M
31

There's the JavaScriptSerializer class (although you will need to reference the System.Web.Extensions assembly the class works perfectly fine in WinForms/WPF applications). Also even if the DataContractJsonSerializer class was designed for WCF it works fine in client applications.

Meitner answered 18/7, 2010 at 14:25 Comment(2)
FYI: Comparison between JsonSerializer and JavaScriptSerializer can be found [#9302378.Rescript
Also, JavaScriptSerializer is buried in the namespace System.Web.Script.Serialization.JavaScriptSerializer for those looking for it.Lastly
S
13

Use this generic class in order to serialize / deserialize JSON. You can easy serialize complex data structure like this:

Dictionary<string, Tuple<int, int[], bool, string>>

to JSON string and then to save it in application setting or else

public class JsonSerializer
{
    public string Serialize<T>(T Obj)
    {
        using (var ms = new MemoryStream())
        {
            DataContractJsonSerializer serialiser = new DataContractJsonSerializer(typeof(T));
            serialiser.WriteObject(ms, Obj);
            byte[] json = ms.ToArray();
            return Encoding.UTF8.GetString(json, 0, json.Length);
        }
    }

    public T Deserialize<T>(string Json)
    {
        using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(Json)))
        {
            DataContractJsonSerializer serialiser = new DataContractJsonSerializer(typeof(T));
            var deserializedObj = (T)serialiser.ReadObject(ms);
            return deserializedObj;
        }
    }
}
Southpaw answered 24/11, 2017 at 0:51 Comment(3)
If WriteObject throws an error - there's a memory leak in this code. Better to wrap MemoryStream in a using statement eg using (var ms = new MemoryStream()) { // code in here }Thoth
WARNING!! Replace this line ser.WriteObject(ms, serializedObj); with this ser.WriteObject(ms, aObject);Katerinekates
Small bug on the Deserialize method. Replace for: DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));Latif
L
0

.NET4 has a built-in JSON Class,such as DataContractJsonSerializer ,but it is very weak,it doesn't support multidimentional array. I suggest you use JSON.Net

Lunette answered 1/11, 2018 at 11:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.