ApiDictionary.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using Newtonsoft.Json;
  8. namespace cuidian.OpenApi.Utils
  9. {
  10. public class ApiDictionary : DictionaryBase
  11. {
  12. #region DictionaryBase members
  13. public void Add(string name, object value)
  14. {
  15. base.InnerHashtable.Add(name, value);
  16. }
  17. public void Remove(string name)
  18. {
  19. base.InnerHashtable.Remove(name);
  20. }
  21. public ICollection Keys()
  22. {
  23. return base.InnerHashtable.Keys;
  24. }
  25. public ICollection Values()
  26. {
  27. return base.InnerHashtable.Values;
  28. }
  29. public bool ContainsName(string name)
  30. {
  31. return base.InnerHashtable.ContainsKey(name);
  32. }
  33. public bool ContainsValue(object value)
  34. {
  35. return base.InnerHashtable.ContainsValue(value);
  36. }
  37. public object this[string name]
  38. {
  39. get { return base.InnerHashtable[name]; }
  40. set { base.InnerHashtable[name] = value; }
  41. }
  42. #endregion
  43. public bool IsArray(string name)
  44. {
  45. Type t = base.InnerHashtable[name].GetType();
  46. return t.FullName.Equals("Newtonsoft.Json.Linq.JArray", StringComparison.CurrentCultureIgnoreCase);
  47. }
  48. public object Get(string name)
  49. {
  50. if (base.InnerHashtable[name] == null)
  51. {
  52. return null;
  53. }
  54. Type t = base.InnerHashtable[name].GetType();
  55. if (t.FullName.Equals("Newtonsoft.Json.Linq.JArray", StringComparison.CurrentCultureIgnoreCase))
  56. {
  57. return new ApiList((Newtonsoft.Json.Linq.JArray)base.InnerHashtable[name]);
  58. }
  59. if (t.FullName.Equals("Newtonsoft.Json.Linq.JObject", StringComparison.CurrentCultureIgnoreCase))
  60. {
  61. return JsonConvert.DeserializeObject<ApiDictionary>(base.InnerHashtable[name].ToString());
  62. }
  63. return base.InnerHashtable[name].ToString(); ;
  64. }
  65. public ApiList GetArray(string name)
  66. {
  67. if (base.InnerHashtable[name] == null)
  68. {
  69. return null;
  70. }
  71. Type t = base.InnerHashtable[name].GetType();
  72. if (t.FullName.Equals("Newtonsoft.Json.Linq.JArray", StringComparison.CurrentCultureIgnoreCase))
  73. {
  74. return new ApiList((Newtonsoft.Json.Linq.JArray)base.InnerHashtable[name]);
  75. }
  76. return null;
  77. }
  78. public ApiDictionary GetObject(string name)
  79. {
  80. if (base.InnerHashtable[name] == null)
  81. {
  82. return null;
  83. }
  84. Type t = base.InnerHashtable[name].GetType();
  85. if (t.FullName.Equals("Newtonsoft.Json.Linq.JObject", StringComparison.CurrentCultureIgnoreCase))
  86. {
  87. return JsonConvert.DeserializeObject<ApiDictionary>(base.InnerHashtable[name].ToString());
  88. }
  89. return null;
  90. }
  91. public object GetValue(string name)
  92. {
  93. if (base.InnerHashtable[name] == null)
  94. {
  95. return null;
  96. }
  97. Type t = base.InnerHashtable[name].GetType();
  98. if (!t.FullName.Equals("Newtonsoft.Json.Linq.JObject", StringComparison.CurrentCultureIgnoreCase)
  99. && !t.FullName.Equals("Newtonsoft.Json.Linq.JArray", StringComparison.CurrentCultureIgnoreCase))
  100. {
  101. return base.InnerHashtable[name].ToString();
  102. }
  103. return null;
  104. }
  105. public override string ToString()
  106. {
  107. return JsonConvert.SerializeObject(this);
  108. }
  109. }
  110. }