TopSimplifyJsonReader.cs 2.3 KB

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