ToJson

public static string ToJson<T>(this T item, System.Text.Encoding encoding = null, System.Runtime.Serialization.Json.DataContractJsonSerializer serializer=null)
{
  encoding = encoding ?? Encoding.Default;
  serializer = serializer ?? new DataContractJsonSerializer(typeof(T));

  using (var stream = new System.IO.MemoryStream())
  {
    serializer.WriteObject(stream, item);
    var json = encoding.GetString((stream.ToArray()));

    return json;
  }        
}
Example:
[Authorize]
public ActionResult Process(MyLovelyModel model)
{
  processor.Process(model);
  Json(model.JsonDeserialize());
}

Description

Json Conversion, uses DataContractJsonSerializer to deserialize item

Details

  • Author: Arek Bal
  • Submitted on: 10/30/2012 10:21:39 AM
  • Language: C#
  • Type: T
  • Views: 4365

Double click on the code to select all.

 

;