JsonHelper.cs 825 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. using System;
  2. using System.ComponentModel;
  3. using LitJson;
  4. namespace ETModel
  5. {
  6. public static class JsonHelper
  7. {
  8. public static string ToJson(object obj)
  9. {
  10. return JsonMapper.ToJson(obj);
  11. }
  12. public static T FromJson<T>(string str)
  13. {
  14. T t = JsonMapper.ToObject<T>(str);
  15. ISupportInitialize iSupportInitialize = t as ISupportInitialize;
  16. if (iSupportInitialize == null)
  17. {
  18. return t;
  19. }
  20. iSupportInitialize.EndInit();
  21. return t;
  22. }
  23. public static object FromJson(Type type, string str)
  24. {
  25. object t = JsonMapper.ToObject(type, str);
  26. ISupportInitialize iSupportInitialize = t as ISupportInitialize;
  27. if (iSupportInitialize == null)
  28. {
  29. return t;
  30. }
  31. iSupportInitialize.EndInit();
  32. return t;
  33. }
  34. public static T Clone<T>(T t)
  35. {
  36. return FromJson<T>(ToJson(t));
  37. }
  38. }
  39. }