andrew4582

Serialize/ Deserialize JSON

Jun 1st, 2011
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.66 KB | None | 0 0
  1. static string SerializeJSON(object graph) {
  2.             using(MemoryStream ms = new MemoryStream()) {
  3.                 new DataContractJsonSerializer(graph.GetType()).WriteObject(ms,graph);
  4.                 ms.Position = 0;
  5.                 using(StreamReader reader = new StreamReader(ms)) {
  6.                     return reader.ReadToEnd();
  7.                 }
  8.             }
  9.         }
  10.  
  11.         static T DeSerializeJSON<T>(string json) {
  12.             using(MemoryStream ms = new MemoryStream(Encoding.Default.GetBytes(json))) {
  13.                 ms.Position = 0;
  14.                 return (T)new DataContractJsonSerializer(typeof(T)).ReadObject(ms);
  15.             }
  16.         }
Advertisement
Add Comment
Please, Sign In to add comment