Utility.Json.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. //------------------------------------------------------------
  2. // Game Framework
  3. // Copyright © 2013-2021 loyalsoft. All rights reserved.
  4. // Homepage: http://www.game7000.com/
  5. // Feedback: http://www.game7000.com/
  6. //------------------------------------------------------------
  7. using System;
  8. namespace GameFramework
  9. {
  10. public static partial class Utility
  11. {
  12. /// <summary>
  13. /// JSON 相关的实用函数。
  14. /// </summary>
  15. public static partial class Json
  16. {
  17. private static IJsonHelper s_JsonHelper = null;
  18. /// <summary>
  19. /// 设置 JSON 辅助器。
  20. /// </summary>
  21. /// <param name="jsonHelper">要设置的 JSON 辅助器。</param>
  22. public static void SetJsonHelper(IJsonHelper jsonHelper)
  23. {
  24. s_JsonHelper = jsonHelper;
  25. }
  26. /// <summary>
  27. /// 将对象序列化为 JSON 字符串。
  28. /// </summary>
  29. /// <param name="obj">要序列化的对象。</param>
  30. /// <returns>序列化后的 JSON 字符串。</returns>
  31. public static string ToJson(object obj)
  32. {
  33. if (s_JsonHelper == null)
  34. {
  35. throw new GameFrameworkException("JSON helper is invalid.");
  36. }
  37. try
  38. {
  39. return s_JsonHelper.ToJson(obj);
  40. }
  41. catch (Exception exception)
  42. {
  43. if (exception is GameFrameworkException)
  44. {
  45. throw;
  46. }
  47. throw new GameFrameworkException(Text.Format("Can not convert to JSON with exception '{0}'.", exception), exception);
  48. }
  49. }
  50. /// <summary>
  51. /// 将 JSON 字符串反序列化为对象。
  52. /// </summary>
  53. /// <typeparam name="T">对象类型。</typeparam>
  54. /// <param name="json">要反序列化的 JSON 字符串。</param>
  55. /// <returns>反序列化后的对象。</returns>
  56. public static T ToObject<T>(string json)
  57. {
  58. if (s_JsonHelper == null)
  59. {
  60. throw new GameFrameworkException("JSON helper is invalid.");
  61. }
  62. try
  63. {
  64. return s_JsonHelper.ToObject<T>(json);
  65. }
  66. catch (Exception exception)
  67. {
  68. if (exception is GameFrameworkException)
  69. {
  70. throw;
  71. }
  72. throw new GameFrameworkException(Text.Format("Can not convert to object with exception '{0}'.", exception), exception);
  73. }
  74. }
  75. /// <summary>
  76. /// 将 JSON 字符串反序列化为对象。
  77. /// </summary>
  78. /// <param name="objectType">对象类型。</param>
  79. /// <param name="json">要反序列化的 JSON 字符串。</param>
  80. /// <returns>反序列化后的对象。</returns>
  81. public static object ToObject(Type objectType, string json)
  82. {
  83. if (s_JsonHelper == null)
  84. {
  85. throw new GameFrameworkException("JSON helper is invalid.");
  86. }
  87. if (objectType == null)
  88. {
  89. throw new GameFrameworkException("Object type is invalid.");
  90. }
  91. try
  92. {
  93. return s_JsonHelper.ToObject(objectType, json);
  94. }
  95. catch (Exception exception)
  96. {
  97. if (exception is GameFrameworkException)
  98. {
  99. throw;
  100. }
  101. throw new GameFrameworkException(Text.Format("Can not convert to object with exception '{0}'.", exception), exception);
  102. }
  103. }
  104. }
  105. }
  106. }