JsonNode.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872
  1. #include "StdInc.h"
  2. #include "JsonNode.h"
  3. const JsonNode JsonNode::nullNode;
  4. JsonNode::JsonNode(JsonType Type):
  5. type(DATA_NULL)
  6. {
  7. setType(Type);
  8. }
  9. JsonNode::JsonNode(const char *data, size_t datasize):
  10. type(DATA_NULL)
  11. {
  12. JsonParser parser(data, datasize, *this);
  13. JsonValidator validator(*this);
  14. }
  15. JsonNode::JsonNode(std::string filename):
  16. type(DATA_NULL)
  17. {
  18. FILE * file = fopen(filename.c_str(), "rb");
  19. if (!file)
  20. {
  21. tlog1 << "Failed to open file " << filename << "\n";
  22. perror("Last system error was ");
  23. return;
  24. }
  25. fseek(file, 0, SEEK_END);
  26. size_t datasize = ftell(file);
  27. fseek(file, 0, SEEK_SET);
  28. char *input = new char[datasize];
  29. datasize = fread((void*)input, 1, datasize, file);
  30. fclose(file);
  31. JsonParser parser(input, datasize, *this);
  32. JsonValidator validator(*this);
  33. delete [] input;
  34. }
  35. JsonNode::JsonNode(const JsonNode &copy):
  36. type(DATA_NULL)
  37. {
  38. setType(copy.getType());
  39. switch(type)
  40. {
  41. break; case DATA_NULL:
  42. break; case DATA_BOOL: Bool() = copy.Bool();
  43. break; case DATA_FLOAT: Float() = copy.Float();
  44. break; case DATA_STRING: String() = copy.String();
  45. break; case DATA_VECTOR: Vector() = copy.Vector();
  46. break; case DATA_STRUCT: Struct() = copy.Struct();
  47. }
  48. }
  49. JsonNode::~JsonNode()
  50. {
  51. setType(DATA_NULL);
  52. }
  53. void JsonNode::swap(JsonNode &b)
  54. {
  55. using std::swap;
  56. swap(data, b.data);
  57. swap(type, b.type);
  58. }
  59. JsonNode & JsonNode::operator =(JsonNode node)
  60. {
  61. swap(node);
  62. return *this;
  63. }
  64. bool JsonNode::operator == (const JsonNode &other) const
  65. {
  66. if (getType() == other.getType())
  67. {
  68. switch(type)
  69. {
  70. break; case DATA_NULL: return true;
  71. break; case DATA_BOOL: return Bool() == other.Bool();
  72. break; case DATA_FLOAT: return Float() == other.Float();
  73. break; case DATA_STRING: return String() == other.String();
  74. break; case DATA_VECTOR: return Vector() == other.Vector();
  75. break; case DATA_STRUCT: return Struct() == other.Struct();
  76. }
  77. }
  78. return false;
  79. }
  80. bool JsonNode::operator != (const JsonNode &other) const
  81. {
  82. return !(*this == other);
  83. }
  84. void JsonNode::minimize(const JsonNode& schema)
  85. {
  86. JsonValidator validator(*this, schema, true);
  87. }
  88. void JsonNode::validate(const JsonNode& schema)
  89. {
  90. JsonValidator validator(*this, schema, false);
  91. }
  92. JsonNode::JsonType JsonNode::getType() const
  93. {
  94. return type;
  95. }
  96. void JsonNode::setType(JsonType Type)
  97. {
  98. if (type == Type)
  99. return;
  100. //Reset node to NULL
  101. if (Type != DATA_NULL)
  102. setType(DATA_NULL);
  103. switch (type)
  104. {
  105. break; case DATA_STRING: delete data.String;
  106. break; case DATA_VECTOR: delete data.Vector;
  107. break; case DATA_STRUCT: delete data.Struct;
  108. break; default:
  109. break;
  110. }
  111. //Set new node type
  112. type = Type;
  113. switch(type)
  114. {
  115. break; case DATA_NULL:
  116. break; case DATA_BOOL: data.Bool = false;
  117. break; case DATA_FLOAT: data.Float = 0;
  118. break; case DATA_STRING: data.String = new std::string;
  119. break; case DATA_VECTOR: data.Vector = new JsonVector;
  120. break; case DATA_STRUCT: data.Struct = new JsonMap;
  121. }
  122. }
  123. bool JsonNode::isNull() const
  124. {
  125. return type == DATA_NULL;
  126. }
  127. bool & JsonNode::Bool()
  128. {
  129. setType(DATA_BOOL);
  130. return data.Bool;
  131. }
  132. double & JsonNode::Float()
  133. {
  134. setType(DATA_FLOAT);
  135. return data.Float;
  136. }
  137. std::string & JsonNode::String()
  138. {
  139. setType(DATA_STRING);
  140. return *data.String;
  141. }
  142. JsonVector & JsonNode::Vector()
  143. {
  144. setType(DATA_VECTOR);
  145. return *data.Vector;
  146. }
  147. JsonMap & JsonNode::Struct()
  148. {
  149. setType(DATA_STRUCT);
  150. return *data.Struct;
  151. }
  152. const bool boolDefault = false;
  153. const bool & JsonNode::Bool() const
  154. {
  155. if (type == DATA_NULL)
  156. return boolDefault;
  157. assert(type == DATA_BOOL);
  158. return data.Bool;
  159. }
  160. const double floatDefault = 0;
  161. const double & JsonNode::Float() const
  162. {
  163. if (type == DATA_NULL)
  164. return floatDefault;
  165. assert(type == DATA_FLOAT);
  166. return data.Float;
  167. }
  168. const std::string stringDefault = std::string();
  169. const std::string & JsonNode::String() const
  170. {
  171. if (type == DATA_NULL)
  172. return stringDefault;
  173. assert(type == DATA_STRING);
  174. return *data.String;
  175. }
  176. const JsonVector vectorDefault = JsonVector();
  177. const JsonVector & JsonNode::Vector() const
  178. {
  179. if (type == DATA_NULL)
  180. return vectorDefault;
  181. assert(type == DATA_VECTOR);
  182. return *data.Vector;
  183. }
  184. const JsonMap mapDefault = JsonMap();
  185. const JsonMap & JsonNode::Struct() const
  186. {
  187. if (type == DATA_NULL)
  188. return mapDefault;
  189. assert(type == DATA_STRUCT);
  190. return *data.Struct;
  191. }
  192. JsonNode & JsonNode::operator[](std::string child)
  193. {
  194. return Struct()[child];
  195. }
  196. const JsonNode & JsonNode::operator[](std::string child) const
  197. {
  198. JsonMap::const_iterator it = Struct().find(child);
  199. if (it != Struct().end())
  200. return it->second;
  201. return nullNode;
  202. }
  203. ////////////////////////////////////////////////////////////////////////////////
  204. template<typename Iterator>
  205. void JsonWriter::writeContainer(Iterator begin, Iterator end)
  206. {
  207. if (begin == end)
  208. return;
  209. prefix += '\t';
  210. end--;
  211. while (begin != end)
  212. {
  213. writeEntry(begin++);
  214. out<<",\n";
  215. }
  216. writeEntry(begin);
  217. out<<"\n";
  218. prefix.resize(prefix.size()-1);
  219. }
  220. void JsonWriter::writeEntry(JsonMap::const_iterator entry)
  221. {
  222. out << prefix;
  223. writeString(entry->first);
  224. out << " : ";
  225. writeNode(entry->second);
  226. }
  227. void JsonWriter::writeEntry(JsonVector::const_iterator entry)
  228. {
  229. out << prefix;
  230. writeNode(*entry);
  231. }
  232. void JsonWriter::writeString(const std::string &string)
  233. {
  234. static const std::string escaped = "\"\\/\b\f\n\r\t";
  235. out <<'\"';
  236. size_t pos=0, start=0;
  237. for (; pos<string.size(); pos++)
  238. {
  239. size_t escapedChar = escaped.find(string[pos]);
  240. if (escapedChar != std::string::npos)
  241. {
  242. out.write(string.data()+start, pos - start);
  243. out << '\\' << escaped[escapedChar];
  244. start = pos;
  245. }
  246. }
  247. out.write(string.data()+start, pos - start);
  248. out <<'\"';
  249. }
  250. void JsonWriter::writeNode(const JsonNode &node)
  251. {
  252. switch(node.getType())
  253. {
  254. break; case JsonNode::DATA_NULL:
  255. out << "null";
  256. break; case JsonNode::DATA_BOOL:
  257. if (node.Bool())
  258. out << "true";
  259. else
  260. out << "false";
  261. break; case JsonNode::DATA_FLOAT:
  262. out << node.Float();
  263. break; case JsonNode::DATA_STRING:
  264. writeString(node.String());
  265. break; case JsonNode::DATA_VECTOR:
  266. out << "[" << "\n";
  267. writeContainer(node.Vector().begin(), node.Vector().end());
  268. out << prefix << "]";
  269. break; case JsonNode::DATA_STRUCT:
  270. out << "{" << "\n";
  271. writeContainer(node.Struct().begin(), node.Struct().end());
  272. out << prefix << "}";
  273. }
  274. }
  275. JsonWriter::JsonWriter(std::ostream &output, const JsonNode &node):
  276. out(output)
  277. {
  278. writeNode(node);
  279. }
  280. std::ostream & operator<<(std::ostream &out, const JsonNode &node)
  281. {
  282. JsonWriter(out, node);
  283. return out << "\n";
  284. }
  285. ////////////////////////////////////////////////////////////////////////////////
  286. JsonParser::JsonParser(const char * inputString, size_t stringSize, JsonNode &root):
  287. input(inputString, stringSize),
  288. lineCount(1),
  289. lineStart(0),
  290. pos(0)
  291. {
  292. extractValue(root);
  293. extractWhitespace(false);
  294. //Warn if there are any non-whitespace symbols left
  295. if (pos < input.size())
  296. error("Not all file was parsed!", true);
  297. //TODO: better way to show errors (like printing file name as well)
  298. tlog3<<errors;
  299. }
  300. bool JsonParser::extractSeparator()
  301. {
  302. if (!extractWhitespace())
  303. return false;
  304. if ( input[pos] !=':')
  305. return error("Separator expected");
  306. pos++;
  307. return true;
  308. }
  309. bool JsonParser::extractValue(JsonNode &node)
  310. {
  311. if (!extractWhitespace())
  312. return false;
  313. switch (input[pos])
  314. {
  315. case '\"': return extractString(node);
  316. case 'n' : return extractNull(node);
  317. case 't' : return extractTrue(node);
  318. case 'f' : return extractFalse(node);
  319. case '{' : return extractStruct(node);
  320. case '[' : return extractArray(node);
  321. case '-' : return extractFloat(node);
  322. default:
  323. {
  324. if (input[pos] >= '0' && input[pos] <= '9')
  325. return extractFloat(node);
  326. return error("Value expected!");
  327. }
  328. }
  329. }
  330. bool JsonParser::extractWhitespace(bool verbose)
  331. {
  332. while (true)
  333. {
  334. while (pos < input.size() && (ui8)input[pos] <= ' ')
  335. {
  336. if (input[pos] == '\n')
  337. {
  338. lineCount++;
  339. lineStart = pos+1;
  340. }
  341. pos++;
  342. }
  343. if (pos >= input.size() || input[pos] != '/')
  344. break;
  345. pos++;
  346. if (pos == input.size())
  347. break;
  348. if (input[pos] == '/')
  349. pos++;
  350. else
  351. error("Comments must consist from two slashes!", true);
  352. while (pos < input.size() && input[pos] != '\n')
  353. pos++;
  354. }
  355. if (pos >= input.size() && verbose)
  356. return error("Unexpected end of file!");
  357. return true;
  358. }
  359. bool JsonParser::extractEscaping(std::string &str)
  360. {
  361. switch(input[pos++])
  362. {
  363. break; case '\"': str += '\"';
  364. break; case '\\': str += '\\';
  365. break; case '/': str += '/';
  366. break; case '\b': str += '\b';
  367. break; case '\f': str += '\f';
  368. break; case '\n': str += '\n';
  369. break; case '\r': str += '\r';
  370. break; case '\t': str += '\t';
  371. break; default: return error("Unknown escape sequence!", true);
  372. };
  373. return true;
  374. }
  375. bool JsonParser::extractString(std::string &str)
  376. {
  377. if (input[pos] != '\"')
  378. return error("String expected!");
  379. pos++;
  380. size_t first = pos;
  381. while (pos != input.size())
  382. {
  383. if (input[pos] == '\"') // Correct end of string
  384. {
  385. str.append( &input[first], pos-first);
  386. pos++;
  387. return true;
  388. }
  389. if (input[pos] == '\\') // Escaping
  390. {
  391. str.append( &input[first], pos-first);
  392. first = pos++;
  393. if (pos == input.size())
  394. break;
  395. extractEscaping(str);
  396. }
  397. if (input[pos] == '\n') // end-of-line
  398. {
  399. str.append( &input[first], pos-first);
  400. return error("Closing quote not found!", true);
  401. }
  402. if (input[pos] < ' ') // control character
  403. {
  404. str.append( &input[first], pos-first);
  405. first = pos+1;
  406. error("Illegal character in the string!", true);
  407. }
  408. pos++;
  409. }
  410. return error("Unterminated string!");
  411. }
  412. bool JsonParser::extractString(JsonNode &node)
  413. {
  414. std::string str;
  415. if (!extractString(str))
  416. return false;
  417. node.setType(JsonNode::DATA_STRING);
  418. node.String() = str;
  419. return true;
  420. }
  421. bool JsonParser::extractLiteral(const std::string &literal)
  422. {
  423. if (literal.compare(0, literal.size(), &input[pos], literal.size()) != 0)
  424. {
  425. while (pos < input.size() && ((input[pos]>'a' && input[pos]<'z')
  426. || (input[pos]>'A' && input[pos]<'Z')))
  427. pos++;
  428. return error("Unknown literal found", true);
  429. }
  430. pos += literal.size();
  431. return true;
  432. }
  433. bool JsonParser::extractNull(JsonNode &node)
  434. {
  435. if (!extractLiteral("null"))
  436. return false;
  437. node.setType(JsonNode::DATA_NULL);
  438. return true;
  439. }
  440. bool JsonParser::extractTrue(JsonNode &node)
  441. {
  442. if (!extractLiteral("true"))
  443. return false;
  444. node.Bool() = true;
  445. return true;
  446. }
  447. bool JsonParser::extractFalse(JsonNode &node)
  448. {
  449. if (!extractLiteral("false"))
  450. return false;
  451. node.Bool() = false;
  452. return true;
  453. }
  454. bool JsonParser::extractStruct(JsonNode &node)
  455. {
  456. node.setType(JsonNode::DATA_STRUCT);
  457. pos++;
  458. if (!extractWhitespace())
  459. return false;
  460. //Empty struct found
  461. if (input[pos] == '}')
  462. {
  463. pos++;
  464. return true;
  465. }
  466. while (true)
  467. {
  468. if (!extractWhitespace())
  469. return false;
  470. std::string key;
  471. if (!extractString(key))
  472. return false;
  473. if (node.Struct().find(key) != node.Struct().end())
  474. error("Dublicated element encountered!", true);
  475. if (!extractSeparator())
  476. return false;
  477. if (!extractElement(node.Struct()[key], '}'))
  478. return false;
  479. if (input[pos] == '}')
  480. {
  481. pos++;
  482. return true;
  483. }
  484. }
  485. }
  486. bool JsonParser::extractArray(JsonNode &node)
  487. {
  488. pos++;
  489. node.setType(JsonNode::DATA_VECTOR);
  490. if (!extractWhitespace())
  491. return false;
  492. //Empty array found
  493. if (input[pos] == ']')
  494. {
  495. pos++;
  496. return true;
  497. }
  498. while (true)
  499. {
  500. //NOTE: currently 50% of time is this vector resizing.
  501. //May be useful to use list during parsing and then swap() all items to vector
  502. node.Vector().resize(node.Vector().size()+1);
  503. if (!extractElement(node.Vector().back(), ']'))
  504. return false;
  505. if (input[pos] == ']')
  506. {
  507. pos++;
  508. return true;
  509. }
  510. }
  511. }
  512. bool JsonParser::extractElement(JsonNode &node, char terminator)
  513. {
  514. if (!extractValue(node))
  515. return false;
  516. if (!extractWhitespace())
  517. return false;
  518. bool comma = (input[pos] == ',');
  519. if (comma )
  520. {
  521. pos++;
  522. if (!extractWhitespace())
  523. return false;
  524. }
  525. if (input[pos] == terminator)
  526. return true;
  527. if (!comma)
  528. error("Comma expected!", true);
  529. return true;
  530. }
  531. bool JsonParser::extractFloat(JsonNode &node)
  532. {
  533. assert(input[pos] == '-' || (input[pos] >= '0' && input[pos] <= '9'));
  534. bool negative=false;
  535. double result=0;
  536. if (input[pos] == '-')
  537. {
  538. pos++;
  539. negative = true;
  540. }
  541. if (input[pos] < '0' || input[pos] > '9')
  542. return error("Number expected!");
  543. //Extract integer part
  544. while (input[pos] >= '0' && input[pos] <= '9')
  545. {
  546. result = result*10+(input[pos]-'0');
  547. pos++;
  548. }
  549. if (input[pos] == '.')
  550. {
  551. //extract fractional part
  552. pos++;
  553. double fractMult = 0.1;
  554. if (input[pos] < '0' || input[pos] > '9')
  555. return error("Decimal part expected!");
  556. while (input[pos] >= '0' && input[pos] <= '9')
  557. {
  558. result = result + fractMult*(input[pos]-'0');
  559. fractMult /= 10;
  560. pos++;
  561. }
  562. }
  563. //TODO: exponential part
  564. if (negative)
  565. result = -result;
  566. node.setType(JsonNode::DATA_FLOAT);
  567. node.Float() = result;
  568. return true;
  569. }
  570. bool JsonParser::error(const std::string &message, bool warning)
  571. {
  572. std::ostringstream stream;
  573. std::string type(warning?" warning: ":" error: ");
  574. stream << "At line " << lineCount << ", position "<<pos-lineStart
  575. << type << message <<"\n";
  576. errors += stream.str();
  577. return warning;
  578. }
  579. static const std::map<std::string, JsonNode::JsonType> stringToType =
  580. boost::assign::map_list_of
  581. ("null", JsonNode::DATA_NULL) ("bool", JsonNode::DATA_BOOL)
  582. ("number", JsonNode::DATA_FLOAT) ("string", JsonNode::DATA_STRING)
  583. ("array", JsonNode::DATA_VECTOR) ("object", JsonNode::DATA_STRUCT);
  584. //Check current schema entry for validness and converts "type" string to JsonType
  585. bool JsonValidator::validateSchema(JsonNode::JsonType &type, const JsonNode &schema)
  586. {
  587. if (schema.isNull())
  588. return addMessage("Missing schema for current entry!");
  589. const JsonNode &nodeType = schema["type"];
  590. if (nodeType.isNull())
  591. return addMessage("Entry type is not defined in schema!");
  592. if (nodeType.getType() != JsonNode::DATA_STRING)
  593. return addMessage("Entry type must be string!");
  594. std::map<std::string, JsonNode::JsonType>::const_iterator iter = stringToType.find(nodeType.String());
  595. if (iter == stringToType.end())
  596. return addMessage("Unknown entry type found!");
  597. type = iter->second;
  598. return true;
  599. }
  600. //Replaces node with default value if needed and calls type-specific validators
  601. bool JsonValidator::validateType(JsonNode &node, const JsonNode &schema, JsonNode::JsonType type)
  602. {
  603. if (node.isNull())
  604. {
  605. const JsonNode & defaultValue = schema["default"];
  606. if (defaultValue.isNull())
  607. return addMessage("Null entry without default entry!");
  608. else
  609. node = defaultValue;
  610. }
  611. if (minimize && node == schema["default"])
  612. {
  613. node.setType(JsonNode::DATA_NULL);
  614. return false;
  615. }
  616. if (type != node.getType())
  617. {
  618. node.setType(JsonNode::DATA_NULL);
  619. return addMessage("Type mismatch!");
  620. }
  621. if (type == JsonNode::DATA_VECTOR)
  622. return validateItems(node, schema["items"]);
  623. if (type == JsonNode::DATA_STRUCT)
  624. return validateProperties(node, schema["properties"]);
  625. return true;
  626. }
  627. // Basic checks common for any nodes
  628. bool JsonValidator::validateNode(JsonNode &node, const JsonNode &schema, const std::string &name)
  629. {
  630. currentPath.push_back(name);
  631. JsonNode::JsonType type = JsonNode::DATA_NULL;
  632. if (!validateSchema(type, schema)
  633. || !validateType(node, schema, type))
  634. {
  635. node.setType(JsonNode::DATA_NULL);
  636. currentPath.pop_back();
  637. return false;
  638. }
  639. currentPath.pop_back();
  640. return true;
  641. }
  642. //Checks "items" entry from schema (type-specific check for Vector)
  643. bool JsonValidator::validateItems(JsonNode &node, const JsonNode &schema)
  644. {
  645. JsonNode::JsonType type = JsonNode::DATA_NULL;
  646. if (!validateSchema(type, schema))
  647. return false;
  648. bool result = true;
  649. BOOST_FOREACH(JsonNode &entry, node.Vector())
  650. {
  651. if (!validateType(entry, schema, type))
  652. {
  653. result = false;
  654. entry.setType(JsonNode::DATA_NULL);
  655. }
  656. }
  657. return result;
  658. }
  659. //Checks "propertries" entry from schema (type-specific check for Struct)
  660. //Function is similar to merging of two sorted lists - check every entry that present in one of the input nodes
  661. bool JsonValidator::validateProperties(JsonNode &node, const JsonNode &schema)
  662. {
  663. if (schema.isNull())
  664. return addMessage("Properties entry is missing for struct in schema");
  665. JsonMap::iterator nodeIter = node.Struct().begin();
  666. JsonMap::const_iterator schemaIter = schema.Struct().begin();
  667. while (nodeIter != node.Struct().end() && schemaIter != schema.Struct().end())
  668. {
  669. if (nodeIter->first < schemaIter->first) //No schema for entry
  670. {
  671. validateNode(nodeIter->second, JsonNode::nullNode, nodeIter->first);
  672. JsonMap::iterator toRemove = nodeIter++;
  673. node.Struct().erase(toRemove);
  674. }
  675. else
  676. if (schemaIter->first < nodeIter->first) //No entry
  677. {
  678. if (!validateNode(node[schemaIter->first], schemaIter->second, schemaIter->first))
  679. node.Struct().erase(schemaIter->first);
  680. schemaIter++;
  681. }
  682. else //both entry and schema are present
  683. {
  684. JsonMap::iterator current = nodeIter++;
  685. if (!validateNode(current->second, schemaIter->second, current->first))
  686. node.Struct().erase(current);
  687. schemaIter++;
  688. }
  689. }
  690. while (nodeIter != node.Struct().end())
  691. {
  692. validateNode(nodeIter->second, JsonNode::nullNode, nodeIter->first);
  693. JsonMap::iterator toRemove = nodeIter++;
  694. node.Struct().erase(toRemove);
  695. }
  696. while (schemaIter != schema.Struct().end())
  697. {
  698. if (!validateNode(node[schemaIter->first], schemaIter->second, schemaIter->first))
  699. node.Struct().erase(schemaIter->first);
  700. schemaIter++;
  701. }
  702. return true;
  703. }
  704. bool JsonValidator::addMessage(const std::string &message)
  705. {
  706. std::ostringstream stream;
  707. stream << "At ";
  708. BOOST_FOREACH(const std::string &path, currentPath)
  709. stream << path<<"/";
  710. stream << "\t Error: " << message <<"\n";
  711. errors += stream.str();
  712. return false;
  713. }
  714. JsonValidator::JsonValidator(JsonNode &root, bool Minimize):
  715. minimize(Minimize)
  716. {
  717. JsonNode schema;
  718. schema.swap(root["schema"]);
  719. root.Struct().erase("schema");
  720. if (!schema.isNull())
  721. {
  722. validateProperties(root, schema);
  723. }
  724. //This message is quite annoying now - most files do not have schemas. May be re-enabled later
  725. //else
  726. // addMessage("Schema not found!", true);
  727. //TODO: better way to show errors (like printing file name as well)
  728. tlog3<<errors;
  729. }
  730. JsonValidator::JsonValidator(JsonNode &root, const JsonNode &schema, bool Minimize):
  731. minimize(Minimize)
  732. {
  733. validateProperties(root, schema);
  734. if (schema.isNull())
  735. addMessage("Schema not found!");
  736. tlog3<<errors;
  737. }