64

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.

stakx - no longer contributing
  • 80,142
  • 18
  • 159
  • 256
Cheung
  • 14,843
  • 19
  • 61
  • 90
  • 4
    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 http://www.hanselman.com/blog/OnTheNightmareThatIsJSONDatesPlusJSONNETAndASPNETWebAPI.aspx – Liam May 31 '13 at 15:42
  • Just be aware of the embedded library for JSon serializing's **performance** in .Net! – Babak Sep 25 '13 at 08:06
  • 2
    @Babak what do you mean beware? Please elaborate. – Eriawan Kusumawardhono Jan 10 '14 at 11:01
  • @EriawanKusumawardhono, It has not a very good performance. I'm using SimpleJSON not very easy to use but it has much better performance. – Babak Jan 11 '14 at 19:09
  • 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! – mike Nov 20 '15 at 13:36

4 Answers4

40

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.

Ben Robinson
  • 21,319
  • 5
  • 61
  • 78
  • 1
    Thanks,MSDN said DataContractJsonSerializer class in Assembly: System.Runtime.Serialization (in System.Runtime.Serialization.dll). However, VS2010 show error, cannot find DataContractJsonSerializer . – Cheung Jul 18 '10 at 14:33
  • @TatMing That's because, IIRC, pre .Net V4 it resides in System.ServiceModel.Web – Psytronic Jul 18 '10 at 14:38
  • 1
    Find that~ It about Target Framework problem, see : http://stackoverflow.com/questions/1825417/where-is-system-servicemodel-web-dll – Cheung Jul 18 '10 at 14:48
  • 1
    The only problem with this is that you have to decorate your classes with attributes. – DonO Dec 01 '16 at 15:52
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.

Darin Dimitrov
  • 994,864
  • 265
  • 3,241
  • 2,902
  • 6
    FYI: Comparison between JsonSerializer and JavaScriptSerializer can be found [http://stackoverflow.com/questions/9301878/whats-the-difference-between-datacontractjsonserializer-and-javascriptserialize](here). – LosManos Oct 22 '14 at 20:08
  • 1
    Also, JavaScriptSerializer is buried in the namespace System.Web.Script.Serialization.JavaScriptSerializer for those looking for it. – Brain2000 Oct 28 '16 at 19:58
4

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 aObject) where T : new()
    {
        T serializedObj = new T();
        MemoryStream ms = new MemoryStream(); 
        DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
        ser.WriteObject(ms, aObject);
        byte[] json = ms.ToArray();
        ms.Close();
        return Encoding.UTF8.GetString(json, 0, json.Length);
    }

    public T Deserialize<T>(string aJSON) where T : new()
    {
        T deserializedObj = new T();
        MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(aJSON));
        DataContractJsonSerializer ser = new DataContractJsonSerializer(aJSON.GetType());
        deserializedObj = (T)ser.ReadObject(ms);
        ms.Close();
        return deserializedObj;
    }
}
vinsa
  • 992
  • 10
  • 24
  • 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 }` – PandaWood Aug 06 '18 at 01:36
  • WARNING!! Replace this line ser.WriteObject(ms, serializedObj); with this ser.WriteObject(ms, aObject); – Riccardo Bassilichi Nov 15 '18 at 10:34
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

Zane
  • 1
  • 1