71

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
  • 83,039
  • 20
  • 168
  • 268
Cheung
  • 15,293
  • 19
  • 63
  • 93
  • 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

43

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,601
  • 5
  • 62
  • 79
  • 2
    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
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 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
11

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;
        }
    }
}
chossenger
  • 613
  • 6
  • 15
vinsa
  • 1,132
  • 12
  • 25
  • 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
  • Small bug on the Deserialize method. Replace for: DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T)); – hmojica Sep 13 '22 at 15:43
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