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

Is .NET 4 enabled with any class that serializes / deserializes JSON data?

  • I know there are third-party libraries like JSON.NET , but I'm looking for something built directly. NET

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

+56
json serialization jsonserializer
Jul 18 2018-10-18T00:
source share
3 answers

You can use the DataContractJsonSerializer class anywhere, it's just a .net class and is not limited to WCF. Learn more about how to use it here and here .

+37
Jul 18 '10 at 14:27
source share

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

+27
Jul 18 '10 at 14:25
source share

Use this common class to serialize / deserialize JSON. You can easily serialize a complex data structure as follows:

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

into a JSON string and then save it in the application settings or

 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, serializedObj); 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; } } 
0
Nov 24 '17 at 0:51
source share



All Articles