AliyunJsonReader.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using Top.Api.Parser;
  5. namespace Aliyun.Api.Parser
  6. {
  7. /// <summary>
  8. /// TOP JSON响应通用读取器。
  9. /// </summary>
  10. public class AliyunJsonReader : ITopReader
  11. {
  12. private IDictionary json;
  13. public AliyunJsonReader(IDictionary json)
  14. {
  15. this.json = json;
  16. }
  17. public bool HasReturnField(object name)
  18. {
  19. return json.Contains(name);
  20. }
  21. public object GetPrimitiveObject(object name)
  22. {
  23. return json[name];
  24. }
  25. public object GetReferenceObject(object name, Type type, DTopConvert convert)
  26. {
  27. IDictionary dict = json[name] as IDictionary;
  28. if (dict != null && dict.Count > 0)
  29. {
  30. return convert(new TopJsonReader(dict), type);
  31. }
  32. else
  33. {
  34. return null;
  35. }
  36. }
  37. public IList GetListObjects(string listName, string itemName, Type type, DTopConvert convert)
  38. {
  39. IList listObjs = null;
  40. IDictionary jsonMap = json[listName] as IDictionary;
  41. if (jsonMap != null && jsonMap.Count > 0)
  42. {
  43. IList jsonList = jsonMap[itemName] as IList;
  44. if (jsonList != null && jsonList.Count > 0)
  45. {
  46. Type listType = typeof(List<>).MakeGenericType(new Type[] { type });
  47. listObjs = Activator.CreateInstance(listType) as IList;
  48. foreach (object item in jsonList)
  49. {
  50. if (typeof(IDictionary).IsAssignableFrom(item.GetType())) // object
  51. {
  52. IDictionary subMap = item as IDictionary;
  53. object subObj = convert(new TopJsonReader(subMap), type);
  54. if (subObj != null)
  55. {
  56. listObjs.Add(subObj);
  57. }
  58. }
  59. else if (typeof(IList).IsAssignableFrom(item.GetType())) // list or array
  60. {
  61. // TODO not support yet
  62. }
  63. else // string, bool, long, double, null, other
  64. {
  65. listObjs.Add(item);
  66. }
  67. }
  68. }
  69. }
  70. return listObjs;
  71. }
  72. }
  73. }