retesere20

Untitled

Apr 20th, 2018
64
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public static class JsonHelper
  2. {
  3. public static string ToJson<T>(T instance)
  4. {
  5. var serializer = new DataContractJsonSerializer(typeof(T));
  6. using (var tempStream = new MemoryStream())
  7. {
  8. serializer.WriteObject(tempStream, instance);
  9. return System.Text.Encoding.Default.GetString(tempStream.ToArray());
  10. }
  11. }
  12.  
  13. public static T FromJson<T>(string json)
  14. {
  15. var serializer = new DataContractJsonSerializer(typeof(T));
  16. using (var tempStream = new MemoryStream(System.Text.Encoding.Unicode.GetBytes(json)))
  17. {
  18. return (T)serializer.ReadObject(tempStream);
  19. }
  20. }
  21. }
RAW Paste Data