title: How to fix OutOfMemoryException when serializing to JSON string?
body: <p>I have a Class object which contains a property as  byte array in it. This class is a Data Contract to my REST service. The byte array property will take any document whose Max Size is limited to 500MB. When I was trying to consume this service and serializing the object I am getting the Memory Out of exception error. Please find the below image <img src="https://i.stack.imgur.com/NNsoO.jpg" alt="enter image description here"></p>
  
  <p>Below is the code snippet </p>
  
  <pre><code>  public static string SerializeJSon&lt;T&gt;(T t)
          {
              MemoryStream stream = new MemoryStream();
              DataContractJsonSerializer ds = new DataContractJsonSerializer(typeof(T));
              DataContractJsonSerializerSettings s = new DataContractJsonSerializerSettings();
              DateTimeFormat dt = new DateTimeFormat("MM/dd/yyyy");
              s.DateTimeFormat = dt;
              **ds.WriteObject(stream, t);**
              string jsonString = Encoding.UTF8.GetString(stream.ToArray());
              stream.Close();
              return jsonString;
          }
  </code></pre>