TopJsonParser.cs 8.0 KB

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