DingTalkJsonParser.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. using FastJSON;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Reflection;
  6. using System.Threading;
  7. using System.Xml.Serialization;
  8. namespace Top.Api.Parser
  9. {
  10. /// <summary>
  11. /// TOP JSON响应通用解释器。
  12. /// </summary>
  13. public class DingTalkJsonParser<T> : ITopParser<T> where T : TopResponse
  14. {
  15. private static readonly ReaderWriterLock rwLock = new ReaderWriterLock();
  16. private static readonly Dictionary<string, Dictionary<string, TopAttribute>> attrs = new Dictionary<string, Dictionary<string, TopAttribute>>();
  17. #region ITopParser Members
  18. public virtual T Parse(string body)
  19. {
  20. return Parse(body, typeof(T));
  21. }
  22. public T Parse(string body, Type type)
  23. {
  24. T rsp = null;
  25. IDictionary json = JSON.Parse(body) as IDictionary;
  26. if (json != null)
  27. {
  28. IDictionary data = json;
  29. if (data != null)
  30. {
  31. ITopReader reader = new TopSimplifyJsonReader(data);
  32. rsp = (T)FromJson(reader, type);
  33. }
  34. }
  35. if (rsp == null)
  36. {
  37. rsp = Activator.CreateInstance(type) as T;
  38. }
  39. if (rsp != null)
  40. {
  41. rsp.Body = body;
  42. }
  43. return rsp;
  44. }
  45. #endregion
  46. private Dictionary<string, TopAttribute> GetTopAttributes(Type type)
  47. {
  48. Dictionary<string, TopAttribute> tas = null;
  49. bool inc = false;
  50. rwLock.AcquireReaderLock(50);
  51. try
  52. {
  53. if (rwLock.IsReaderLockHeld)
  54. {
  55. inc = attrs.TryGetValue(type.FullName, out tas);
  56. }
  57. }
  58. finally
  59. {
  60. if (rwLock.IsReaderLockHeld)
  61. {
  62. rwLock.ReleaseReaderLock();
  63. }
  64. }
  65. if (inc && tas != null) // 从缓存中获取类属性元数据
  66. {
  67. return tas;
  68. }
  69. else // 创建新的类属性元数据缓存
  70. {
  71. tas = new Dictionary<string, TopAttribute>();
  72. }
  73. PropertyInfo[] pis = type.GetProperties();
  74. foreach (PropertyInfo pi in pis)
  75. {
  76. TopAttribute ta = new TopAttribute();
  77. ta.Method = pi.GetSetMethod();
  78. // 获取对象属性名称
  79. XmlElementAttribute[] xeas = pi.GetCustomAttributes(typeof(XmlElementAttribute), true) as XmlElementAttribute[];
  80. if (xeas != null && xeas.Length > 0)
  81. {
  82. ta.ItemName = xeas[0].ElementName;
  83. }
  84. // 获取列表属性名称
  85. if (ta.ItemName == null)
  86. {
  87. XmlArrayItemAttribute[] xaias = pi.GetCustomAttributes(typeof(XmlArrayItemAttribute), true) as XmlArrayItemAttribute[];
  88. if (xaias != null && xaias.Length > 0)
  89. {
  90. ta.ItemName = xaias[0].ElementName;
  91. }
  92. XmlArrayAttribute[] xaas = pi.GetCustomAttributes(typeof(XmlArrayAttribute), true) as XmlArrayAttribute[];
  93. if (xaas != null && xaas.Length > 0)
  94. {
  95. ta.ListName = xaas[0].ElementName;
  96. }
  97. if (ta.ListName == null)
  98. {
  99. continue;
  100. }
  101. }
  102. // 获取属性类型
  103. if (pi.PropertyType.IsGenericType)
  104. {
  105. Type[] types = pi.PropertyType.GetGenericArguments();
  106. ta.ListType = types[0];
  107. }
  108. else
  109. {
  110. ta.ItemType = pi.PropertyType;
  111. }
  112. tas.Add(pi.Name, ta);
  113. }
  114. rwLock.AcquireWriterLock(50);
  115. try
  116. {
  117. if (rwLock.IsWriterLockHeld)
  118. {
  119. attrs[type.FullName] = tas;
  120. }
  121. }
  122. finally
  123. {
  124. if (rwLock.IsWriterLockHeld)
  125. {
  126. rwLock.ReleaseWriterLock();
  127. }
  128. }
  129. return tas;
  130. }
  131. public object FromJson(ITopReader reader, Type type)
  132. {
  133. object rsp = null;
  134. Dictionary<string, TopAttribute> pas = GetTopAttributes(type);
  135. Dictionary<string, TopAttribute>.Enumerator em = pas.GetEnumerator();
  136. while (em.MoveNext())
  137. {
  138. KeyValuePair<string, TopAttribute> kvp = em.Current;
  139. TopAttribute ta = kvp.Value;
  140. string itemName = ta.ItemName;
  141. string listName = ta.ListName;
  142. if (!reader.HasReturnField(itemName) && (string.IsNullOrEmpty(listName) || !reader.HasReturnField(listName)))
  143. {
  144. continue;
  145. }
  146. object value = null;
  147. if (ta.ListType != null)
  148. {
  149. value = reader.GetListObjects(listName, itemName, ta.ListType, FromJson);
  150. }
  151. else
  152. {
  153. if (typeof(string) == ta.ItemType)
  154. {
  155. object tmp = reader.GetPrimitiveObject(itemName);
  156. if (typeof(string) == tmp.GetType())
  157. {
  158. value = tmp;
  159. }
  160. else
  161. {
  162. if (tmp != null)
  163. {
  164. value = JSON.ToJSON(tmp);
  165. }
  166. }
  167. }
  168. else if (typeof(long) == ta.ItemType)
  169. {
  170. object tmp = reader.GetPrimitiveObject(itemName);
  171. if (typeof(long) == tmp.GetType())
  172. {
  173. value = tmp;
  174. }
  175. else
  176. {
  177. if (tmp != null)
  178. {
  179. value = long.Parse(tmp.ToString());
  180. }
  181. }
  182. }
  183. else if (typeof(bool) == ta.ItemType)
  184. {
  185. object tmp = reader.GetPrimitiveObject(itemName);
  186. if (typeof(bool) == tmp.GetType())
  187. {
  188. value = tmp;
  189. }
  190. else
  191. {
  192. if (tmp != null)
  193. {
  194. value = bool.Parse(tmp.ToString());
  195. }
  196. }
  197. }
  198. else
  199. {
  200. value = reader.GetReferenceObject(itemName, ta.ItemType, FromJson);
  201. }
  202. }
  203. if (value != null)
  204. {
  205. if (rsp == null)
  206. {
  207. rsp = Activator.CreateInstance(type);
  208. }
  209. ta.Method.Invoke(rsp, new object[] { value });
  210. }
  211. }
  212. return rsp;
  213. }
  214. }
  215. }