QimenCloudJsonParser.cs 7.8 KB

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