Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The <strong>Microsoft</strong> recommended <strong>JSON serializer</strong> is <a href="http://msdn.microsoft.com/en-us/library/bb410770.aspx" rel="noreferrer"><code>DataContractJsonSerializer</code></a> This class exists within the <strong>System.Runtime.Serialization</strong> assembly</p> <p>The sample demonstrates deserializing from JSON data into an object. </p> <pre><code>MemoryStream stream1 = new MemoryStream(); Person p2 = (Person)ser.ReadObject(stream1); </code></pre> <p>To serialize an instance of the Person type to JSON, create the DataContractJsonSerializer first and use the WriteObject method to write JSON data to a stream.</p> <pre><code>Person p = new Person(); //Set up Person object... MemoryStream stream1 = new MemoryStream(); DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Person)); ser.WriteObject(stream1, p); </code></pre> <p><strong>Update:</strong> Added Helper class</p> <p>Here is a sample helper class that you can use for simple To/From Json serialization:</p> <pre><code>public static class JsonHelper { public static string ToJson&lt;T&gt;(T instance) { var serializer = new DataContractJsonSerializer(typeof(T)); using (var tempStream = new MemoryStream()) { serializer.WriteObject(tempStream, instance); return Encoding.Default.GetString(tempStream.ToArray()); } } public static T FromJson&lt;T&gt;(string json) { var serializer = new DataContractJsonSerializer(typeof(T)); using (var tempStream = new MemoryStream(Encoding.Unicode.GetBytes(json))) { return (T)serializer.ReadObject(tempStream); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload