JsonHelper.cs 797 B

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