JsonManager.cs 1000 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using LitJson;
  5. #if !COMBAT_SERVER
  6. using UnityEngine;
  7. #else
  8. using System.Text.Json;
  9. #endif
  10. public class JsonManager
  11. {
  12. public static T FromJson<T>(string json)
  13. {
  14. return JsonMapper.ToObject<T>(json);
  15. #if !COMBAT_SERVER
  16. return JsonUtility.FromJson<T>(json);
  17. #else
  18. return JsonSerializer.Deserialize<T>(json);
  19. #endif
  20. }
  21. public static object FromJson(string json, Type type)
  22. {
  23. return JsonMapper.ToObject(json,type);
  24. #if !COMBAT_SERVER
  25. return JsonUtility.FromJson(json, type);
  26. #else
  27. JsonSerializerOptions jo = new JsonSerializerOptions();
  28. // jo.IgnoreNullValues = true;
  29. return JsonSerializer.Deserialize(json,type,jo);
  30. #endif
  31. }
  32. public static string ToJson(object obj)
  33. {
  34. return JsonMapper.ToJson(obj);
  35. #if !COMBAT_SERVER
  36. return JsonUtility.ToJson(obj);
  37. #else
  38. return JsonSerializer.Serialize(obj);
  39. #endif
  40. }
  41. }