AgoraUtil.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using System.Text;
  4. namespace Agora.Rtc
  5. {
  6. using LitJson;
  7. public class AgoraJson
  8. {
  9. private const string ErrorTag = "AgoraJsonError";
  10. internal static object GetData<T>(string data, string key)
  11. {
  12. string jValue = "";
  13. try
  14. {
  15. var jData = JsonMapper.ToObject(data);
  16. if (jData[key] == null) return null;
  17. jValue = jData[key].ToString();
  18. }
  19. catch (System.Exception)
  20. {
  21. AgoraLog.LogError(ErrorTag + data);
  22. }
  23. switch (typeof(T).Name)
  24. {
  25. case "Boolean":
  26. return bool.Parse(jValue);
  27. case "Byte":
  28. return byte.Parse(jValue);
  29. case "Decimal":
  30. return decimal.Parse(jValue);
  31. case "Double":
  32. return double.Parse(jValue);
  33. case "Int16":
  34. return short.Parse(jValue);
  35. case "Int32":
  36. return int.Parse(jValue);
  37. case "Int64":
  38. return long.Parse(jValue);
  39. case "SByte":
  40. return sbyte.Parse(jValue);
  41. case "Single":
  42. return float.Parse(jValue);
  43. case "String":
  44. return jValue;
  45. case "UInt16":
  46. return ushort.Parse(jValue);
  47. case "UInt32":
  48. return uint.Parse(jValue);
  49. case "UInt64":
  50. return ulong.Parse(jValue);
  51. default:
  52. return jValue;
  53. }
  54. }
  55. internal static object GetData<T>(char[] data, string key)
  56. {
  57. var str = new string(data, 0, Array.IndexOf(data, '\0'));
  58. return AgoraJson.GetData<T>(str, key);
  59. }
  60. internal static object GetData<T>(JsonData data, string key)
  61. {
  62. var jValue = data[key].ToString();
  63. switch (typeof(T).Name)
  64. {
  65. case "Boolean":
  66. return bool.Parse(jValue);
  67. case "Byte":
  68. return byte.Parse(jValue);
  69. case "Decimal":
  70. return decimal.Parse(jValue);
  71. case "Double":
  72. return double.Parse(jValue);
  73. case "Int16":
  74. return short.Parse(jValue);
  75. case "Int32":
  76. return int.Parse(jValue);
  77. case "Int64":
  78. return long.Parse(jValue);
  79. case "SByte":
  80. return sbyte.Parse(jValue);
  81. case "Single":
  82. return float.Parse(jValue);
  83. case "String":
  84. return jValue;
  85. case "UInt16":
  86. return ushort.Parse(jValue);
  87. case "UInt32":
  88. return uint.Parse(jValue);
  89. case "UInt64":
  90. return ulong.Parse(jValue);
  91. default:
  92. return jValue;
  93. }
  94. }
  95. internal static T JsonToStruct<T>(char[] data) where T : new()
  96. {
  97. var str = new string(data, 0, Array.IndexOf(data, '\0'));
  98. return AgoraJson.JsonToStruct<T>(str);
  99. }
  100. internal static T JsonToStruct<T>(char[] data, string key) where T : new()
  101. {
  102. var str = new string(data, 0, Array.IndexOf(data, '\0'));
  103. return AgoraJson.JsonToStruct<T>(str, key);
  104. //var jValue = AgoraJson.ToJson(JsonMapper.ToObject(new string(data, 0, Array.IndexOf(data, '\0')))[key]);
  105. //return JsonMapper.ToObject<T>(jValue ?? string.Empty);
  106. }
  107. internal static T[] JsonToStructArray<T>(char[] data, string key = null, uint length = 0) where T : new()
  108. {
  109. var str = new string(data, 0, Array.IndexOf(data, '\0'));
  110. return AgoraJson.JsonToStructArray<T>(str, key, length);
  111. //var jValueArray = key == null
  112. // ? JsonMapper.ToObject(new string(data, 0, Array.IndexOf(data, '\0')))
  113. // : JsonMapper.ToObject(new string(data, 0, Array.IndexOf(data, '\0')))[key];
  114. //length = length != 0 ? length : (uint)jValueArray.Count;
  115. //var ret = new T[length];
  116. //for (var i = 0; i < length; i++)
  117. //{
  118. // ret[i] = JsonMapper.ToObject<T>(jValueArray[i].ToJson());
  119. //}
  120. //return ret;
  121. }
  122. internal static T JsonToStruct<T>(string data) where T : new()
  123. {
  124. return JsonMapper.ToObject<T>(data);
  125. }
  126. internal static T JsonToStruct<T>(string data, string key) where T : new()
  127. {
  128. var jValue = AgoraJson.ToJson(JsonMapper.ToObject(data)[key]);
  129. return AgoraJson.JsonToStruct<T>(jValue ?? string.Empty);
  130. }
  131. internal static T JsonToStruct<T>(JsonData data, string key) where T : new()
  132. {
  133. var jValue = AgoraJson.ToJson(data[key]);
  134. return AgoraJson.JsonToStruct<T>(jValue ?? string.Empty);
  135. }
  136. internal static T[] JsonToStructArray<T>(string data, string key = null, uint length = 0) where T : new()
  137. {
  138. var jValueArray = key == null ? JsonMapper.ToObject(data) : JsonMapper.ToObject(data)[key];
  139. if (jValueArray == null)
  140. return new T[0];
  141. length = length != 0 ? length : (uint)jValueArray.Count;
  142. var ret = new T[length];
  143. for (var i = 0; i < length; i++)
  144. {
  145. ret[i] = AgoraJson.JsonToStruct<T>(jValueArray[i].ToJson());
  146. }
  147. return ret;
  148. }
  149. internal static T[] JsonToStructArray<T>(JsonData data, string key = null, uint length = 0) where T : new()
  150. {
  151. var jValueArray = key == null ? data : data[key];
  152. if (jValueArray == null)
  153. return new T[0];
  154. length = length != 0 ? length : (uint)jValueArray.Count;
  155. var ret = new T[length];
  156. for (var i = 0; i < length; i++)
  157. {
  158. ret[i] = AgoraJson.JsonToStruct<T>(jValueArray[i].ToJson());
  159. }
  160. return ret;
  161. }
  162. internal static string ToJson<T>(T param)
  163. {
  164. return LitJson.JsonMapper.ToJson(param);
  165. }
  166. internal static JsonData ToObject(string data)
  167. {
  168. return JsonMapper.ToObject(data);
  169. }
  170. }
  171. [StructLayout(LayoutKind.Sequential)]
  172. internal struct CharAssistant
  173. {
  174. internal CharAssistant(int param = 0) {
  175. resultChar = new byte[65536];
  176. }
  177. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 65536)]
  178. private byte[] resultChar;
  179. public string Result
  180. {
  181. get
  182. {
  183. var re = Encoding.UTF8.GetString(resultChar);
  184. var index = re.IndexOf('\0');
  185. return re.Substring(0, index);
  186. }
  187. }
  188. }
  189. }