JsonSchema.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. using System;
  2. using System.Collections.Generic;
  3. namespace FastReport.Data.JsonConnection.JsonParser
  4. {
  5. internal class JsonSchema
  6. {
  7. #region Private Fields
  8. private string description;
  9. private JsonSchema items;
  10. private Dictionary<string, JsonSchema> properties;
  11. private string type;
  12. #endregion Private Fields
  13. #region Public Properties
  14. public Type DataType
  15. {
  16. get
  17. {
  18. switch (type)
  19. {
  20. case "object": return typeof(JsonBase);
  21. case "array": return typeof(JsonBase);
  22. case "integer": return typeof(int);
  23. case "number": return typeof(double);
  24. case "string": return typeof(string);
  25. case "boolean": return typeof(bool);
  26. default: return typeof(object);
  27. }
  28. }
  29. }
  30. public string Description { get { return description; } set { description = value; } }
  31. public JsonSchema Items
  32. {
  33. get
  34. {
  35. return items;
  36. }
  37. set
  38. {
  39. items = value;
  40. }
  41. }
  42. public Dictionary<string, JsonSchema> Properties
  43. {
  44. get
  45. {
  46. return properties;
  47. }
  48. }
  49. public string Type { get { return type; } set { type = value; } }
  50. #endregion Public Properties
  51. #region Public Methods
  52. public static JsonSchema FromJson(object json)
  53. {
  54. JsonSchema result = new JsonSchema();
  55. if (json is JsonObject)
  56. {
  57. result.type = "object";
  58. result.properties = new Dictionary<string, JsonSchema>();
  59. foreach (KeyValuePair<string, object> kv in (json as JsonObject))
  60. {
  61. result.Properties[kv.Key] = FromJson(kv.Value);
  62. }
  63. }
  64. else if (json is JsonArray)
  65. {
  66. result.Type = "array";
  67. result.items = null;
  68. foreach (object obj in (json as JsonArray))
  69. {
  70. JsonSchema sub = FromJson(obj);
  71. if (result.items == null)
  72. result.items = sub;
  73. else result.items.Union(sub);
  74. }
  75. if (result.items == null)
  76. result.items = new JsonSchema();
  77. }
  78. else if (json is string)
  79. {
  80. result.Type = "string";
  81. }
  82. else if (json is double)
  83. {
  84. result.Type = "number";
  85. }
  86. else if (json is bool)
  87. {
  88. result.Type = "boolean";
  89. }
  90. else
  91. {
  92. result.Type = "null";
  93. }
  94. return result;
  95. }
  96. public static JsonSchema Load(JsonObject obj)
  97. {
  98. if (obj == null)
  99. {
  100. throw new NullReferenceException("Unable to load schema from non-object");
  101. }
  102. JsonSchema result = new JsonSchema();
  103. if (obj != null)
  104. {
  105. result.Type = obj.ReadString("type");
  106. result.Description = obj.ReadString("description");
  107. switch (result.Type)
  108. {
  109. case "object":
  110. result.properties = new Dictionary<string, JsonSchema>();
  111. if (obj.ContainsKey("properties"))
  112. {
  113. JsonObject child = obj["properties"] as JsonObject;
  114. if (child != null)
  115. {
  116. foreach (KeyValuePair<string, object> kv in child)
  117. {
  118. if (kv.Value is JsonObject)
  119. {
  120. result.Properties[kv.Key] = Load(kv.Value as JsonObject);
  121. }
  122. else
  123. {
  124. result.Properties[kv.Key] = new JsonSchema();
  125. }
  126. }
  127. }
  128. }
  129. break;
  130. case "array":
  131. if (obj.ContainsKey("items"))
  132. result.items = Load(obj["items"] as JsonObject);
  133. else
  134. result.items = new JsonSchema();
  135. break;
  136. }
  137. }
  138. return result;
  139. }
  140. public void Save(JsonObject obj)
  141. {
  142. if (Type != null)
  143. {
  144. obj["type"] = Type;
  145. }
  146. if (Description != null)
  147. {
  148. obj["description"] = Description;
  149. }
  150. if (Items != null)
  151. {
  152. JsonObject child = new JsonObject();
  153. Items.Save(child);
  154. obj["items"] = child;
  155. }
  156. if (Properties != null && Properties.Count > 0)
  157. {
  158. JsonObject child = new JsonObject();
  159. obj["properties"] = child;
  160. foreach (KeyValuePair<string, JsonSchema> kv in Properties)
  161. {
  162. JsonObject sub_child = new JsonObject();
  163. kv.Value.Save(sub_child);
  164. child[kv.Key] = sub_child;
  165. }
  166. }
  167. }
  168. #endregion Public Methods
  169. #region Private Methods
  170. private void Union(JsonSchema sub)
  171. {
  172. if (sub == null || type != sub.type)
  173. {
  174. items = null;
  175. properties = null;
  176. type = "null";
  177. }
  178. else if (type == "object")
  179. {
  180. if (properties == null)
  181. properties = new Dictionary<string, JsonSchema>();
  182. if (sub.Properties != null)
  183. foreach (KeyValuePair<string, JsonSchema> kv in sub.Properties)
  184. {
  185. JsonSchema child;
  186. if (Properties.TryGetValue(kv.Key, out child))
  187. {
  188. child.Union(kv.Value);
  189. }
  190. else
  191. {
  192. Properties[kv.Key] = kv.Value;
  193. }
  194. }
  195. }
  196. else if (type == "array")
  197. {
  198. if (items == null)
  199. items = sub.items;
  200. else
  201. {
  202. items.Union(sub.items);
  203. }
  204. }
  205. }
  206. #endregion Private Methods
  207. }
  208. }