AliyunJsonParser.cs 6.6 KB

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