AopModelParser.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Aop.Api;
  6. using System.Reflection;
  7. using System.Collections;
  8. using System.Xml.Serialization;
  9. using Newtonsoft.Json;
  10. namespace Aop.Api2.Parser
  11. {
  12. public class AopModelParser
  13. {
  14. /// <summary>
  15. /// Json序列化AopObject对象
  16. /// </summary>
  17. /// <param name="res"></param>
  18. /// <returns></returns>
  19. public IDictionary<object, object> serializeAopObject(AopObject res)
  20. {
  21. PropertyInfo[] pis = res.GetType().GetProperties();
  22. IDictionary<object, object> jo = new Dictionary<object, object>();
  23. foreach (PropertyInfo pi in pis)
  24. {
  25. if (!pi.CanRead)
  26. {
  27. continue;
  28. }
  29. String elementName = getElementName(pi);
  30. Object value = getPiValue(res, pi);
  31. if (!String.IsNullOrEmpty(elementName))
  32. {
  33. Object serialized = serializeValue(value);
  34. if (serialized != null)
  35. {
  36. jo[elementName] = serialized;
  37. }
  38. }
  39. }
  40. return jo;
  41. }
  42. /// <summary>
  43. /// 序列化某个对象
  44. /// </summary>
  45. /// <param name="value"></param>
  46. /// <returns></returns>
  47. private Object serializeValue(Object value)
  48. {
  49. if (value == null)
  50. {
  51. return null;
  52. }
  53. Object result = null;
  54. if (value is IDictionary)
  55. {
  56. result = serializeDicValue((IDictionary)value);
  57. }
  58. else if (value is ICollection)
  59. {
  60. result = serializeArrayValue((ICollection)value);
  61. }
  62. else
  63. {
  64. result = serializeElementValue(value);
  65. }
  66. return result;
  67. }
  68. /// <summary>
  69. /// 序列化一个元素
  70. /// </summary>
  71. /// <param name="value"></param>
  72. /// <returns></returns>
  73. private Object serializeElementValue(Object value)
  74. {
  75. if (value == null)
  76. {
  77. return null;
  78. }
  79. Object result = null;
  80. if (value is AopObject)
  81. {
  82. result = serializeAopObject((AopObject)value);
  83. }
  84. else
  85. {
  86. result = value;
  87. }
  88. return result;
  89. }
  90. /// <summary>
  91. /// 序列化一个字典
  92. /// </summary>
  93. /// <param name="dic"></param>
  94. /// <returns></returns>
  95. private Object serializeDicValue(IDictionary dic)
  96. {
  97. if (dic == null)
  98. {
  99. return null;
  100. }
  101. IDictionary<object, object> jo = new Dictionary<object, object>();
  102. foreach (String key in dic.Keys)
  103. {
  104. Object dicValue = dic[key];
  105. Object serializedValue = serializeValue(dicValue);
  106. jo[key] = serializedValue;
  107. }
  108. return jo;
  109. }
  110. /// <summary>
  111. /// 序列化一个数组
  112. /// </summary>
  113. /// <param name="collection"></param>
  114. /// <returns></returns>
  115. public Object serializeArrayValue(ICollection collection)
  116. {
  117. if (collection == null)
  118. {
  119. return null;
  120. }
  121. List<object> ja = new List<object>();
  122. foreach (var item in collection)
  123. {
  124. ja.Add(serializeValue(item));
  125. }
  126. return ja;
  127. }
  128. /// <summary>
  129. /// 获取对象的属性值
  130. /// </summary>
  131. /// <param name="res"></param>
  132. /// <param name="pi"></param>
  133. /// <returns></returns>
  134. private Object getPiValue(Object res, PropertyInfo pi)
  135. {
  136. if (!pi.CanRead)
  137. {
  138. return null;
  139. }
  140. Object value = pi.GetValue(res, null);
  141. return value;
  142. }
  143. /// <summary>
  144. /// 获取Xml属性名
  145. /// </summary>
  146. /// <param name="pi"></param>
  147. /// <returns></returns>
  148. private String getElementName(PropertyInfo pi)
  149. {
  150. if (pi == null)
  151. {
  152. return null;
  153. }
  154. // 获取XmlElement属性
  155. XmlElementAttribute[] xeas = pi.GetCustomAttributes(typeof(XmlElementAttribute), true) as XmlElementAttribute[];
  156. String elementName = null;
  157. if (xeas != null && xeas.Length > 0)
  158. {
  159. elementName = xeas[0].ElementName;
  160. }
  161. // 如果获取XmlElement属性为空,则去获取XmlArray属性
  162. if (String.IsNullOrEmpty(elementName))
  163. {
  164. XmlArrayAttribute[] xaas = pi.GetCustomAttributes(typeof(XmlArrayAttribute), true) as XmlArrayAttribute[];
  165. if (xaas != null && xaas.Length > 0)
  166. {
  167. elementName = xaas[0].ElementName;
  168. }
  169. }
  170. return elementName;
  171. }
  172. }
  173. }