JsonHelper.cs 747 B

1234567891011121314151617181920212223242526272829303132333435
  1. using System;
  2. using Newtonsoft.Json;
  3. namespace Model
  4. {
  5. public static class JsonHelper
  6. {
  7. public static string ToJson(object obj)
  8. {
  9. return JsonConvert.SerializeObject(obj);
  10. }
  11. public static T FromJson<T>(string str)
  12. {
  13. return JsonConvert.DeserializeObject<T>(str);
  14. }
  15. public static object FromJson(Type type, string str)
  16. {
  17. return JsonConvert.DeserializeObject(str, type);
  18. }
  19. public static T FromJson<T>(byte[] bytes, int index, int count)
  20. {
  21. string str = bytes.ToStr();
  22. return JsonConvert.DeserializeObject<T>(str);
  23. }
  24. public static object FromJson(Type type, byte[] bytes, int index, int count)
  25. {
  26. string str = bytes.ToStr(index, count);
  27. return JsonConvert.DeserializeObject(str, type);
  28. }
  29. }
  30. }