JSON.h 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988
  1. #pragma once
  2. #include <functional>
  3. #include "Array.h"
  4. #include "ReferenceCounter.h"
  5. #include "Text.h"
  6. #include "Writer.h"
  7. #include "XML.h"
  8. namespace Framework
  9. {
  10. namespace JSON
  11. {
  12. enum class JSONType
  13. {
  14. NULL_,
  15. BOOLEAN,
  16. NUMBER,
  17. STRING,
  18. ARRAY,
  19. OBJECT
  20. };
  21. class JSONArray;
  22. class JSONObject;
  23. class JSONBool;
  24. class JSONNumber;
  25. class JSONString;
  26. class JSONValue : public virtual ReferenceCounter
  27. {
  28. private:
  29. JSONType type;
  30. protected:
  31. __declspec(dllexport) JSONValue(JSONType type);
  32. public:
  33. __declspec(dllexport) JSONValue();
  34. __declspec(dllexport) virtual ~JSONValue();
  35. __declspec(dllexport) JSONType getType() const;
  36. __declspec(dllexport) virtual Text toString() const;
  37. __declspec(dllexport) virtual JSONValue* clone() const;
  38. __declspec(dllexport) JSONBool* asBool() const;
  39. __declspec(dllexport) JSONNumber* asNumber() const;
  40. __declspec(dllexport) JSONString* asString() const;
  41. __declspec(dllexport) JSONArray* asArray() const;
  42. __declspec(dllexport) JSONObject* asObject() const;
  43. };
  44. class JSONBool : public JSONValue
  45. {
  46. private:
  47. bool b;
  48. public:
  49. __declspec(dllexport) JSONBool(bool b);
  50. __declspec(dllexport) bool getBool() const;
  51. __declspec(dllexport) Text toString() const override;
  52. __declspec(dllexport) JSONValue* clone() const override;
  53. };
  54. class JSONNumber : public JSONValue
  55. {
  56. private:
  57. double number;
  58. public:
  59. __declspec(dllexport) JSONNumber(double num);
  60. __declspec(dllexport) double getNumber() const;
  61. __declspec(dllexport) Text toString() const override;
  62. __declspec(dllexport) JSONValue* clone() const override;
  63. };
  64. class JSONString : public JSONValue
  65. {
  66. private:
  67. Text string;
  68. public:
  69. __declspec(dllexport) JSONString(Text string);
  70. __declspec(dllexport) Text getString() const;
  71. __declspec(dllexport) Text toString() const override;
  72. __declspec(dllexport) JSONValue* clone() const override;
  73. };
  74. class JSONArray : public JSONValue
  75. {
  76. private:
  77. RCArray<JSONValue>* array;
  78. public:
  79. __declspec(dllexport) JSONArray();
  80. __declspec(dllexport) JSONArray(Text string);
  81. __declspec(dllexport) JSONArray(const JSONArray& arr);
  82. __declspec(dllexport) ~JSONArray();
  83. __declspec(dllexport) JSONArray& operator=(const JSONArray& arr);
  84. __declspec(dllexport) void addValue(JSONValue* value);
  85. __declspec(dllexport) void setValue(int i, JSONValue* value);
  86. __declspec(dllexport) void removeValue(int i);
  87. __declspec(dllexport) JSONValue* getValue(int i) const;
  88. __declspec(dllexport) JSONValue* zValue(int i) const;
  89. __declspec(dllexport) int getLength() const;
  90. __declspec(dllexport) bool isValueOfType(
  91. int i, JSONType type) const;
  92. //! Gibt einen Iterator zurück.
  93. //! Mit ++ kann durch die Liste iteriert werden
  94. __declspec(dllexport) Iterator<JSONValue*> begin() const;
  95. __declspec(dllexport) Iterator<JSONValue*> end() const;
  96. template<class T>
  97. RCArray<T>* toRCArray(std::function<T*(JSONValue&)> map) const
  98. {
  99. return toRCArray([](JSONValue& v) { return 1; }, map);
  100. }
  101. template<class T>
  102. RCArray<T>* toRCArray(std::function<bool(JSONValue&)> filter,
  103. std::function<T*(JSONValue&)> map) const
  104. {
  105. RCArray<T>* result = new RCArray<T>();
  106. for (auto v : *array)
  107. {
  108. if (filter(*v))
  109. {
  110. result->add(map(*v));
  111. }
  112. }
  113. return result;
  114. }
  115. template<class T>
  116. Array<T>* toArray(std::function<T(JSONValue&)> map) const
  117. {
  118. return toArray([](JSONValue& v) { return 1; }, map);
  119. ;
  120. }
  121. template<class T>
  122. Array<T>* toArray(std::function<bool(JSONValue&)> filter,
  123. std::function<T(JSONValue&)> map) const
  124. {
  125. Array<T>* result = new Array<T>();
  126. for (auto v : *array)
  127. {
  128. if (filter(*v))
  129. {
  130. result->add(map(*v));
  131. }
  132. }
  133. return result;
  134. }
  135. __declspec(dllexport) Text toString() const override;
  136. __declspec(dllexport) JSONValue* clone() const override;
  137. template<class T> static JSONArray* fromRCArray(
  138. Framework::RCArray<T>& arr, std::function<JSONValue*(T&)> map)
  139. {
  140. JSONArray* array = new JSONArray();
  141. for (T* v : arr)
  142. {
  143. array->addValue(map(*v));
  144. }
  145. return array;
  146. }
  147. template<class T> static JSONArray* fromArray(
  148. Framework::Array<T>& arr, std::function<JSONValue*(T)> map)
  149. {
  150. JSONArray* array = new JSONArray();
  151. for (T v : arr)
  152. {
  153. array->addValue(map(v));
  154. }
  155. return array;
  156. }
  157. };
  158. class JSONObject : public JSONValue
  159. {
  160. private:
  161. Array<Text>* fields;
  162. RCArray<JSONValue>* values;
  163. public:
  164. __declspec(dllexport) JSONObject();
  165. __declspec(dllexport) JSONObject(Text string);
  166. __declspec(dllexport) JSONObject(const JSONObject& obj);
  167. __declspec(dllexport) ~JSONObject();
  168. __declspec(dllexport) JSONObject& operator=(const JSONObject& obj);
  169. __declspec(dllexport) bool addValue(Text field, JSONValue* value);
  170. __declspec(dllexport) bool removeValue(Text field);
  171. __declspec(dllexport) bool hasValue(Text field);
  172. __declspec(dllexport) JSONValue* getValue(Text field);
  173. __declspec(dllexport) JSONValue* zValue(Text field);
  174. __declspec(dllexport) Iterator<Text> getFields();
  175. __declspec(dllexport) Iterator<JSONValue*> getValues();
  176. __declspec(dllexport) int getFieldCount() const;
  177. __declspec(dllexport) bool isValueOfType(
  178. Text field, JSONType type) const;
  179. template<class T> T* parseTo(T* initialState,
  180. std::function<void(
  181. T* obj, Text fieldName, JSONValue& fieldValue)> parser)
  182. const
  183. {
  184. auto fieldsI = fields->begin();
  185. auto valuesI = values->begin();
  186. while (fieldsI && valuesI)
  187. {
  188. parser(initialState, fieldsI, *(JSONValue*)valuesI);
  189. fieldsI++;
  190. valuesI++;
  191. }
  192. return initialState;
  193. }
  194. __declspec(dllexport) Text toString() const override;
  195. __declspec(dllexport) JSONValue* clone() const override;
  196. };
  197. __declspec(dllexport) JSONValue* loadJSONFromFile(Text path);
  198. namespace Parser
  199. {
  200. __declspec(dllexport) int findObjectEndInArray(const char* str);
  201. __declspec(dllexport) Text removeWhitespace(const char* str);
  202. __declspec(dllexport) JSONValue* getValue(const char* str);
  203. __declspec(dllexport) int findFieldEndInObject(const char* str);
  204. __declspec(dllexport) int findValueEndInObject(const char* str);
  205. }; // namespace Parser
  206. namespace Validator
  207. {
  208. class JSONValidationResult : public Framework::ReferenceCounter
  209. {
  210. public:
  211. __declspec(dllexport) JSONValidationResult();
  212. __declspec(dllexport) virtual ~JSONValidationResult();
  213. virtual bool isValid() const = 0;
  214. virtual void printInvalidInfo(int indent = 0) const = 0;
  215. virtual JSONValue* getValidPart(RCArray<JSONValidationResult>*
  216. zRemovedPartsValidationResults)
  217. = 0;
  218. virtual Text getPath() const = 0;
  219. virtual bool isDifferent(const JSONValidationResult* zResult,
  220. Text additionalPath) const
  221. = 0;
  222. };
  223. class JSONTypeMissmatch : public JSONValidationResult
  224. {
  225. private:
  226. Text path;
  227. JSONValue* foundValue;
  228. XML::Element* expected;
  229. JSONValidationResult* reason;
  230. public:
  231. __declspec(dllexport) JSONTypeMissmatch(Text path,
  232. JSONValue* foundValue,
  233. XML::Element* expected,
  234. JSONValidationResult* reason);
  235. __declspec(dllexport) ~JSONTypeMissmatch();
  236. __declspec(dllexport) bool isValid() const override;
  237. __declspec(dllexport) void printInvalidInfo(
  238. int indent) const override;
  239. __declspec(dllexport) JSONValue* getValidPart(
  240. RCArray<JSONValidationResult>*
  241. zRemovedPartsValidationResults) override;
  242. __declspec(dllexport) Text getPath() const override;
  243. bool isDifferent(const JSONValidationResult* zResult,
  244. Text additionalPath) const override;
  245. };
  246. class JSONUnknownValue : public JSONValidationResult
  247. {
  248. private:
  249. Text path;
  250. JSONValue* foundValue;
  251. public:
  252. __declspec(dllexport)
  253. JSONUnknownValue(Text path, JSONValue* foundValue);
  254. __declspec(dllexport) ~JSONUnknownValue();
  255. __declspec(dllexport) bool isValid() const override;
  256. __declspec(dllexport) void printInvalidInfo(
  257. int indent) const override;
  258. __declspec(dllexport) JSONValue* getValidPart(
  259. RCArray<JSONValidationResult>*
  260. zRemovedPartsValidationResults) override;
  261. __declspec(dllexport) Text getPath() const override;
  262. __declspec(dllexport) bool isDifferent(
  263. const JSONValidationResult* zResult,
  264. Text additionalPath) const override;
  265. };
  266. class JSONMissingValue : public JSONValidationResult
  267. {
  268. private:
  269. Text path;
  270. XML::Element* expected;
  271. public:
  272. __declspec(dllexport)
  273. JSONMissingValue(Text path, XML::Element* expected);
  274. __declspec(dllexport) ~JSONMissingValue();
  275. __declspec(dllexport) bool isValid() const override;
  276. __declspec(dllexport) void printInvalidInfo(
  277. int indent) const override;
  278. __declspec(dllexport) JSONValue* getValidPart(
  279. RCArray<JSONValidationResult>*
  280. zRemovedPartsValidationResults) override;
  281. __declspec(dllexport) Text getPath() const override;
  282. __declspec(dllexport) bool isDifferent(
  283. const JSONValidationResult* zResult,
  284. Text additionalPath) const override;
  285. };
  286. class JSONMissingOneOf : public JSONValidationResult
  287. {
  288. private:
  289. Text path;
  290. RCArray<XML::Element> expected;
  291. public:
  292. __declspec(dllexport)
  293. JSONMissingOneOf(Text path, XML::Editor expected);
  294. __declspec(dllexport) ~JSONMissingOneOf();
  295. __declspec(dllexport) bool isValid() const override;
  296. __declspec(dllexport) void printInvalidInfo(
  297. int indent) const override;
  298. __declspec(dllexport) JSONValue* getValidPart(
  299. RCArray<JSONValidationResult>*
  300. zRemovedPartsValidationResults) override;
  301. __declspec(dllexport) Text getPath() const override;
  302. __declspec(dllexport) bool isDifferent(
  303. const JSONValidationResult* zResult,
  304. Text additionalPath) const override;
  305. };
  306. class JSONNoTypeMatching : public JSONValidationResult
  307. {
  308. private:
  309. Text path;
  310. JSONValue* foundValue;
  311. RCArray<XML::Element> expected;
  312. RCArray<JSONValidationResult> reasons;
  313. public:
  314. __declspec(dllexport) JSONNoTypeMatching(Text path,
  315. JSONValue* foundValue,
  316. RCArray<XML::Element>& expected,
  317. RCArray<JSONValidationResult>& reasons);
  318. __declspec(dllexport) ~JSONNoTypeMatching();
  319. __declspec(dllexport) bool isValid() const override;
  320. __declspec(dllexport) void printInvalidInfo(
  321. int indent) const override;
  322. __declspec(dllexport) JSONValue* getValidPart(
  323. RCArray<JSONValidationResult>*
  324. zRemovedPartsValidationResults) override;
  325. __declspec(dllexport) Text getPath() const override;
  326. __declspec(dllexport) bool isDifferent(
  327. const JSONValidationResult* zResult,
  328. Text additionalPath) const override;
  329. };
  330. class JSONValidValue : public JSONValidationResult
  331. {
  332. private:
  333. Text path;
  334. JSONValue* value;
  335. public:
  336. __declspec(dllexport)
  337. JSONValidValue(Text path, JSONValue* value);
  338. __declspec(dllexport) ~JSONValidValue();
  339. __declspec(dllexport) bool isValid() const override;
  340. __declspec(dllexport) void printInvalidInfo(
  341. int indent) const override;
  342. __declspec(dllexport) JSONValue* getValidPart(
  343. RCArray<JSONValidationResult>*
  344. zRemovedPartsValidationResults) override;
  345. __declspec(dllexport) Text getPath() const override;
  346. __declspec(dllexport) bool isDifferent(
  347. const JSONValidationResult* zResult,
  348. Text additionalPath) const override;
  349. };
  350. template<typename T> class StringValidationBuilder;
  351. template<typename T> class NumberValidationBuilder;
  352. template<typename T> class BoolValidationBuilder;
  353. template<typename T> class ObjectValidationBuilder;
  354. template<typename T> class ArrayValidationBuilder;
  355. class JSONValidator : public Framework::ReferenceCounter
  356. {
  357. private:
  358. XML::Element* constraints;
  359. public:
  360. __declspec(dllexport) JSONValidator(XML::Element* constraints);
  361. __declspec(dllexport) ~JSONValidator();
  362. __declspec(dllexport) JSONValidationResult* validate(
  363. JSONValue* zValue) const;
  364. __declspec(dllexport) bool isValid(JSONValue* zValue) const;
  365. /**
  366. * returns the valid part of the json by performing the
  367. * following operations in the specified order untill no further
  368. * changes are made:
  369. * - invalid or unknown object properties are removed
  370. * - missing object properties with default values are added if
  371. * missing
  372. * - invalid array elements are removed
  373. *
  374. * @param zValue the json value to to extract the valid part
  375. * without increased reference counter
  376. * @return the valid part or 0 if no valid part exists
  377. */
  378. __declspec(dllexport) JSONValue* getValidParts(
  379. JSONValue* zValue,
  380. RCArray<JSONValidationResult>*
  381. zRemovedPartsValidationResults) const;
  382. __declspec(dllexport) XML::Element* zConstraints();
  383. private:
  384. __declspec(dllexport) JSONValidationResult* validate(
  385. JSONValue* zValue,
  386. XML::Element* zConstraints,
  387. Text path) const;
  388. __declspec(dllexport)
  389. JSONValidationResult* validateMultipleTypes(
  390. JSONValue* zChildValue,
  391. XML::Element* zPossibleChildConstraints,
  392. Text childPath) const;
  393. public:
  394. __declspec(dllexport) static StringValidationBuilder<
  395. JSONValidator>* buildForString();
  396. __declspec(dllexport) static NumberValidationBuilder<
  397. JSONValidator>* buildForNumber();
  398. __declspec(dllexport) static BoolValidationBuilder<
  399. JSONValidator>* buildForBool();
  400. __declspec(dllexport) static ObjectValidationBuilder<
  401. JSONValidator>* buildForObject();
  402. __declspec(dllexport) static ArrayValidationBuilder<
  403. JSONValidator>* buildForArray();
  404. __declspec(
  405. dllexport) static JSONValidator* buildForObjectReference(Text
  406. objectId);
  407. };
  408. template<typename T> class StringValidationBuilder
  409. {
  410. private:
  411. XML::Element element;
  412. std::function<T*(XML::Element& element)> builder;
  413. public:
  414. StringValidationBuilder(
  415. std::function<T*(XML::Element& element)> builder)
  416. : element("<string/>"),
  417. builder(builder)
  418. {}
  419. StringValidationBuilder<T>* withExactMatch(Text value)
  420. {
  421. element.setAttribute("equals", value);
  422. return this;
  423. }
  424. StringValidationBuilder<T>* whichContainsMatch(Text value)
  425. {
  426. element.setAttribute("contains", value);
  427. return this;
  428. }
  429. StringValidationBuilder<T>* whichStartsWithMatch(Text value)
  430. {
  431. element.setAttribute("startsWith", value);
  432. return this;
  433. }
  434. StringValidationBuilder<T>* whichEndsWithMatch(Text value)
  435. {
  436. element.setAttribute("endsWith", value);
  437. return this;
  438. }
  439. StringValidationBuilder<T>* whichIsOneOf(RCArray<Text>& values)
  440. {
  441. JSONArray arr;
  442. for (Text* str : values)
  443. arr.addValue(new JSONString(str->getText()));
  444. element.setAttribute("oneOf", arr.toString());
  445. return this;
  446. }
  447. StringValidationBuilder<T>* withDefault(Text value)
  448. {
  449. element.setAttribute(
  450. "default", JSONString(value).toString());
  451. return this;
  452. }
  453. StringValidationBuilder<T>* withDefaultNull()
  454. {
  455. element.setAttribute("default", "null");
  456. return this;
  457. }
  458. StringValidationBuilder<T>* whichCanBeNull()
  459. {
  460. element.setAttribute("nullable", "true");
  461. return this;
  462. }
  463. StringValidationBuilder<T>* whichIsOptional()
  464. {
  465. element.setAttribute("optional", "true");
  466. return this;
  467. }
  468. T* finishString()
  469. {
  470. T* result = builder(element);
  471. delete this;
  472. return result;
  473. }
  474. };
  475. template<typename T> class NumberValidationBuilder
  476. {
  477. private:
  478. XML::Element element;
  479. std::function<T*(XML::Element& element)> builder;
  480. public:
  481. NumberValidationBuilder(
  482. std::function<T*(XML::Element& element)> builder)
  483. : element("<number/>"),
  484. builder(builder)
  485. {}
  486. NumberValidationBuilder<T>* whichIs(double value)
  487. {
  488. element.setAttribute("equals", Text(value));
  489. return this;
  490. }
  491. NumberValidationBuilder<T>* whichIsLessOrEqual(double value)
  492. {
  493. element.setAttribute("lessOrEqual", Text(value));
  494. return this;
  495. }
  496. NumberValidationBuilder<T>* whichIsGreaterOrEqual(double value)
  497. {
  498. element.setAttribute("greaterOrEqual", Text(value));
  499. return this;
  500. }
  501. NumberValidationBuilder<T>* whichIsLessThen(double value)
  502. {
  503. element.setAttribute("less", Text(value));
  504. return this;
  505. }
  506. NumberValidationBuilder<T>* whichIsGreaterThen(double value)
  507. {
  508. element.setAttribute("greater", Text(value));
  509. return this;
  510. }
  511. NumberValidationBuilder<T>* withDefault(double value)
  512. {
  513. element.setAttribute(
  514. "default", JSONNumber(value).toString());
  515. return this;
  516. }
  517. NumberValidationBuilder<T>* withDefaultNull()
  518. {
  519. element.setAttribute("default", "null");
  520. return this;
  521. }
  522. NumberValidationBuilder<T>* whichCanBeNull()
  523. {
  524. element.setAttribute("nullable", "true");
  525. return this;
  526. }
  527. NumberValidationBuilder<T>* whichIsOptional()
  528. {
  529. element.setAttribute("optional", "true");
  530. return this;
  531. }
  532. T* finishNumber()
  533. {
  534. T* result = builder(element);
  535. delete this;
  536. return result;
  537. }
  538. };
  539. template<typename T> class BoolValidationBuilder
  540. {
  541. private:
  542. XML::Element element;
  543. std::function<T*(XML::Element& element)> builder;
  544. public:
  545. BoolValidationBuilder(
  546. std::function<T*(XML::Element& element)> builder)
  547. : element("<bool/>"),
  548. builder(builder)
  549. {}
  550. BoolValidationBuilder<T>* whichIs(bool value)
  551. {
  552. element.setAttribute("equals", value ? "true" : "false");
  553. return this;
  554. }
  555. BoolValidationBuilder<T>* withDefault(bool value)
  556. {
  557. element.setAttribute("default", JSONBool(value).toString());
  558. return this;
  559. }
  560. BoolValidationBuilder<T>* withDefaultNull()
  561. {
  562. element.setAttribute("default", "null");
  563. return this;
  564. }
  565. BoolValidationBuilder<T>* whichCanBeNull()
  566. {
  567. element.setAttribute("nullable", "true");
  568. return this;
  569. }
  570. BoolValidationBuilder<T>* whichIsOptional()
  571. {
  572. element.setAttribute("optional", "true");
  573. return this;
  574. }
  575. T* finishBool()
  576. {
  577. T* result = builder(element);
  578. delete this;
  579. return result;
  580. }
  581. };
  582. template<typename T> class ArrayValidationBuilder;
  583. template<typename T> class ObjectValidationBuilder
  584. {
  585. private:
  586. XML::Element element;
  587. std::function<T*(XML::Element& element)> builder;
  588. public:
  589. ObjectValidationBuilder(
  590. std::function<T*(XML::Element& element)> builder)
  591. : element("<object></object>"),
  592. builder(builder)
  593. {}
  594. ObjectValidationBuilder<T>* setObjectReferenceId(Text id)
  595. {
  596. element.setAttribute("id", id);
  597. return this;
  598. }
  599. NumberValidationBuilder<ObjectValidationBuilder<T>>*
  600. withRequiredNumber(Text name)
  601. {
  602. return new NumberValidationBuilder<
  603. ObjectValidationBuilder<T>>(
  604. [this, name](XML::Element& e) {
  605. if (!element.selectChildsByAttribute("name", name)
  606. .exists())
  607. {
  608. XML::Element* attr
  609. = new XML::Element("<value></value>");
  610. attr->setAttribute("name", name);
  611. element.addChild(attr);
  612. }
  613. element.selectChildsByAttribute("name", name)
  614. .addChild(e.dublicate());
  615. return this;
  616. });
  617. }
  618. StringValidationBuilder<ObjectValidationBuilder<T>>*
  619. withRequiredString(Text name)
  620. {
  621. return new StringValidationBuilder<
  622. ObjectValidationBuilder<T>>(
  623. [this, name](XML::Element& e) {
  624. if (!element.selectChildsByAttribute("name", name)
  625. .exists())
  626. {
  627. XML::Element* attr
  628. = new XML::Element("<value></value>");
  629. attr->setAttribute("name", name);
  630. element.addChild(attr);
  631. }
  632. element.selectChildsByAttribute("name", name)
  633. .addChild(e.dublicate());
  634. return this;
  635. });
  636. }
  637. BoolValidationBuilder<ObjectValidationBuilder<T>>*
  638. withRequiredBool(Text name)
  639. {
  640. return new BoolValidationBuilder<
  641. ObjectValidationBuilder<T>>(
  642. [this, name](XML::Element& e) {
  643. if (!element.selectChildsByAttribute("name", name)
  644. .exists())
  645. {
  646. XML::Element* attr
  647. = new XML::Element("<value></value>");
  648. attr->setAttribute("name", name);
  649. element.addChild(attr);
  650. }
  651. element.selectChildsByAttribute("name", name)
  652. .addChild(e.dublicate());
  653. return this;
  654. });
  655. }
  656. ArrayValidationBuilder<ObjectValidationBuilder<T>>*
  657. withRequiredArray(Text name)
  658. {
  659. return new ArrayValidationBuilder<
  660. ObjectValidationBuilder<T>>(
  661. [this, name](XML::Element& e) {
  662. if (!element.selectChildsByAttribute("name", name)
  663. .exists())
  664. {
  665. XML::Element* attr
  666. = new XML::Element("<value></value>");
  667. attr->setAttribute("name", name);
  668. element.addChild(attr);
  669. }
  670. element.selectChildsByAttribute("name", name)
  671. .addChild(e.dublicate());
  672. return this;
  673. });
  674. }
  675. ObjectValidationBuilder<ObjectValidationBuilder<T>>*
  676. withRequiredObject(Text name)
  677. {
  678. return new ObjectValidationBuilder<
  679. ObjectValidationBuilder<T>>(
  680. [this, name](XML::Element& e) {
  681. if (!element.selectChildsByAttribute("name", name)
  682. .exists())
  683. {
  684. XML::Element* attr
  685. = new XML::Element("<value></value>");
  686. attr->setAttribute("name", name);
  687. element.addChild(attr);
  688. }
  689. element.selectChildsByAttribute("name", name)
  690. .addChild(e.dublicate());
  691. return this;
  692. });
  693. }
  694. ObjectValidationBuilder<T>* withRequiredAttribute(
  695. Text name, JSONValidator* validator)
  696. {
  697. if (!element.selectChildsByAttribute("name", name).exists())
  698. {
  699. XML::Element* attr
  700. = new XML::Element("<value></value>");
  701. attr->setAttribute("name", name);
  702. element.addChild(attr);
  703. }
  704. element.selectChildsByAttribute("name", name)
  705. .addChild(validator->zConstraints()->dublicate());
  706. validator->release();
  707. return this;
  708. }
  709. ObjectValidationBuilder<T>* withDefault(JSONObject* obj)
  710. {
  711. element.setAttribute("default", obj->toString());
  712. obj->release();
  713. return this;
  714. }
  715. ObjectValidationBuilder<T>* withDefaultNull()
  716. {
  717. element.setAttribute("default", "null");
  718. return this;
  719. }
  720. ObjectValidationBuilder<T>* whichCanBeNull()
  721. {
  722. element.setAttribute("nullable", "true");
  723. return this;
  724. }
  725. ObjectValidationBuilder<T>* whichIsOptional()
  726. {
  727. element.setAttribute("optional", "true");
  728. return this;
  729. }
  730. ArrayValidationBuilder<T>* typeOfValuesSpecifiedByAttribute(
  731. Text valueName, Text attributeName)
  732. {
  733. if (!element.selectChildsByAttribute("name", valueName)
  734. .exists())
  735. {
  736. XML::Element* attr
  737. = new XML::Element("<value></value>");
  738. attr->setAttribute("name", valueName);
  739. element.addChild(attr);
  740. }
  741. element.selectChildsByAttribute("name", valueName)
  742. .setAttribute("typeSpecifiedBy", attributeName);
  743. return this;
  744. }
  745. ObjectValidationBuilder<T>* removeInvalidEntries()
  746. {
  747. element.setAttribute("removeInvalidEntries", "true");
  748. return this;
  749. }
  750. T* finishObject()
  751. {
  752. T* result = builder(element);
  753. delete this;
  754. return result;
  755. }
  756. };
  757. template<typename T> class ArrayValidationBuilder
  758. {
  759. private:
  760. XML::Element element;
  761. std::function<T*(XML::Element& element)> builder;
  762. public:
  763. ArrayValidationBuilder(
  764. std::function<T*(XML::Element& element)> builder)
  765. : element("<array></array>"),
  766. builder(builder)
  767. {}
  768. StringValidationBuilder<ArrayValidationBuilder<T>>*
  769. addAcceptedStringInArray()
  770. {
  771. return new StringValidationBuilder<
  772. ArrayValidationBuilder<T>>([this](XML::Element& e) {
  773. element.addChild(e.dublicate());
  774. return this;
  775. });
  776. }
  777. NumberValidationBuilder<ArrayValidationBuilder<T>>*
  778. addAcceptedNumberInArray()
  779. {
  780. return new NumberValidationBuilder<
  781. ArrayValidationBuilder<T>>([this](XML::Element& e) {
  782. element.addChild(e.dublicate());
  783. return this;
  784. });
  785. }
  786. BoolValidationBuilder<ArrayValidationBuilder<T>>*
  787. addAcceptedBooleanInArray()
  788. {
  789. return new BoolValidationBuilder<ArrayValidationBuilder<T>>(
  790. [this](XML::Element& e) {
  791. element.addChild(e.dublicate());
  792. return this;
  793. });
  794. }
  795. ObjectValidationBuilder<ArrayValidationBuilder<T>>*
  796. addAcceptedObjectInArray()
  797. {
  798. return new ObjectValidationBuilder<
  799. ArrayValidationBuilder<T>>([this](XML::Element& e) {
  800. element.addChild(e.dublicate());
  801. return this;
  802. });
  803. }
  804. ArrayValidationBuilder<ArrayValidationBuilder<T>>*
  805. addAcceptedArrayInArray()
  806. {
  807. return new ArrayValidationBuilder<
  808. ArrayValidationBuilder<T>>([this](XML::Element& e) {
  809. element.addChild(e.dublicate());
  810. return this;
  811. });
  812. }
  813. ArrayValidationBuilder<T>* addAcceptedTypeInArray(
  814. JSONValidator* validator)
  815. {
  816. element.addChild(validator->zConstraints()->dublicate());
  817. validator->release();
  818. return this;
  819. }
  820. ArrayValidationBuilder<T>* acceptNullsInArray()
  821. {
  822. element.setAttribute("nullsEnabled", "true");
  823. return this;
  824. }
  825. ArrayValidationBuilder<T>* withDefault(JSONArray* array)
  826. {
  827. element.setAttribute("default", array->toString());
  828. array->release();
  829. return this;
  830. }
  831. ArrayValidationBuilder<T>* withDefaultNull()
  832. {
  833. element.setAttribute("default", "null");
  834. return this;
  835. }
  836. ArrayValidationBuilder<T>* whichCanBeNull()
  837. {
  838. element.setAttribute("nullable", "true");
  839. return this;
  840. }
  841. ArrayValidationBuilder<T>* whichIsOptional()
  842. {
  843. element.setAttribute("optional", "true");
  844. return this;
  845. }
  846. ArrayValidationBuilder<T>* typeSpecifiedByAttribute(Text name)
  847. {
  848. element.setAttribute("typeSpecifiedBy", name);
  849. return this;
  850. }
  851. ArrayValidationBuilder<T>* removeInvalidEntries()
  852. {
  853. element.setAttribute("removeInvalidEntries", "true");
  854. return this;
  855. }
  856. T* finishArray()
  857. {
  858. T* result = builder(element);
  859. delete this;
  860. return result;
  861. }
  862. };
  863. } // namespace Validator
  864. } // namespace JSON
  865. } // namespace Framework