JsonNode.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089
  1. /*
  2. * JsonNode.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "JsonNode.h"
  12. #include "HeroBonus.h"
  13. #include "Filesystem/CResourceLoader.h"
  14. using namespace JsonDetail;
  15. static const JsonNode nullNode;
  16. JsonNode::JsonNode(JsonType Type):
  17. type(DATA_NULL)
  18. {
  19. setType(Type);
  20. }
  21. JsonNode::JsonNode(const char *data, size_t datasize):
  22. type(DATA_NULL)
  23. {
  24. JsonParser parser(data, datasize, *this);
  25. JsonValidator validator(*this);
  26. }
  27. JsonNode::JsonNode(ResourceID && fileURI):
  28. type(DATA_NULL)
  29. {
  30. std::string filename = CResourceHandler::get()->getResourceName(fileURI);
  31. FILE * file = fopen(filename.c_str(), "rb");
  32. if (!file)
  33. {
  34. tlog1 << "Failed to open file " << filename << "\n";
  35. perror("Last system error was ");
  36. return;
  37. }
  38. fseek(file, 0, SEEK_END);
  39. size_t datasize = ftell(file);
  40. fseek(file, 0, SEEK_SET);
  41. char *input = new char[datasize];
  42. datasize = fread((void*)input, 1, datasize, file);
  43. fclose(file);
  44. JsonParser parser(input, datasize, *this);
  45. JsonValidator validator(*this);
  46. delete [] input;
  47. }
  48. JsonNode::JsonNode(const JsonNode &copy):
  49. type(DATA_NULL)
  50. {
  51. setType(copy.getType());
  52. switch(type)
  53. {
  54. break; case DATA_NULL:
  55. break; case DATA_BOOL: Bool() = copy.Bool();
  56. break; case DATA_FLOAT: Float() = copy.Float();
  57. break; case DATA_STRING: String() = copy.String();
  58. break; case DATA_VECTOR: Vector() = copy.Vector();
  59. break; case DATA_STRUCT: Struct() = copy.Struct();
  60. }
  61. }
  62. JsonNode::~JsonNode()
  63. {
  64. setType(DATA_NULL);
  65. }
  66. void JsonNode::swap(JsonNode &b)
  67. {
  68. using std::swap;
  69. swap(data, b.data);
  70. swap(type, b.type);
  71. }
  72. JsonNode & JsonNode::operator =(JsonNode node)
  73. {
  74. swap(node);
  75. return *this;
  76. }
  77. bool JsonNode::operator == (const JsonNode &other) const
  78. {
  79. if (getType() == other.getType())
  80. {
  81. switch(type)
  82. {
  83. break; case DATA_NULL: return true;
  84. break; case DATA_BOOL: return Bool() == other.Bool();
  85. break; case DATA_FLOAT: return Float() == other.Float();
  86. break; case DATA_STRING: return String() == other.String();
  87. break; case DATA_VECTOR: return Vector() == other.Vector();
  88. break; case DATA_STRUCT: return Struct() == other.Struct();
  89. }
  90. }
  91. return false;
  92. }
  93. bool JsonNode::operator != (const JsonNode &other) const
  94. {
  95. return !(*this == other);
  96. }
  97. JsonNode::JsonType JsonNode::getType() const
  98. {
  99. return type;
  100. }
  101. void JsonNode::setType(JsonType Type)
  102. {
  103. if (type == Type)
  104. return;
  105. //Reset node to NULL
  106. if (Type != DATA_NULL)
  107. setType(DATA_NULL);
  108. switch (type)
  109. {
  110. break; case DATA_STRING: delete data.String;
  111. break; case DATA_VECTOR: delete data.Vector;
  112. break; case DATA_STRUCT: delete data.Struct;
  113. break; default:
  114. break;
  115. }
  116. //Set new node type
  117. type = Type;
  118. switch(type)
  119. {
  120. break; case DATA_NULL:
  121. break; case DATA_BOOL: data.Bool = false;
  122. break; case DATA_FLOAT: data.Float = 0;
  123. break; case DATA_STRING: data.String = new std::string;
  124. break; case DATA_VECTOR: data.Vector = new JsonVector;
  125. break; case DATA_STRUCT: data.Struct = new JsonMap;
  126. }
  127. }
  128. bool JsonNode::isNull() const
  129. {
  130. return type == DATA_NULL;
  131. }
  132. bool & JsonNode::Bool()
  133. {
  134. setType(DATA_BOOL);
  135. return data.Bool;
  136. }
  137. double & JsonNode::Float()
  138. {
  139. setType(DATA_FLOAT);
  140. return data.Float;
  141. }
  142. std::string & JsonNode::String()
  143. {
  144. setType(DATA_STRING);
  145. return *data.String;
  146. }
  147. JsonVector & JsonNode::Vector()
  148. {
  149. setType(DATA_VECTOR);
  150. return *data.Vector;
  151. }
  152. JsonMap & JsonNode::Struct()
  153. {
  154. setType(DATA_STRUCT);
  155. return *data.Struct;
  156. }
  157. const bool boolDefault = false;
  158. const bool & JsonNode::Bool() const
  159. {
  160. if (type == DATA_NULL)
  161. return boolDefault;
  162. assert(type == DATA_BOOL);
  163. return data.Bool;
  164. }
  165. const double floatDefault = 0;
  166. const double & JsonNode::Float() const
  167. {
  168. if (type == DATA_NULL)
  169. return floatDefault;
  170. assert(type == DATA_FLOAT);
  171. return data.Float;
  172. }
  173. const std::string stringDefault = std::string();
  174. const std::string & JsonNode::String() const
  175. {
  176. if (type == DATA_NULL)
  177. return stringDefault;
  178. assert(type == DATA_STRING);
  179. return *data.String;
  180. }
  181. const JsonVector vectorDefault = JsonVector();
  182. const JsonVector & JsonNode::Vector() const
  183. {
  184. if (type == DATA_NULL)
  185. return vectorDefault;
  186. assert(type == DATA_VECTOR);
  187. return *data.Vector;
  188. }
  189. const JsonMap mapDefault = JsonMap();
  190. const JsonMap & JsonNode::Struct() const
  191. {
  192. if (type == DATA_NULL)
  193. return mapDefault;
  194. assert(type == DATA_STRUCT);
  195. return *data.Struct;
  196. }
  197. JsonNode & JsonNode::operator[](std::string child)
  198. {
  199. return Struct()[child];
  200. }
  201. const JsonNode & JsonNode::operator[](std::string child) const
  202. {
  203. JsonMap::const_iterator it = Struct().find(child);
  204. if (it != Struct().end())
  205. return it->second;
  206. return nullNode;
  207. }
  208. ////////////////////////////////////////////////////////////////////////////////
  209. template<typename Iterator>
  210. void JsonWriter::writeContainer(Iterator begin, Iterator end)
  211. {
  212. if (begin == end)
  213. return;
  214. prefix += '\t';
  215. end--;
  216. while (begin != end)
  217. {
  218. writeEntry(begin++);
  219. out<<",\n";
  220. }
  221. writeEntry(begin);
  222. out<<"\n";
  223. prefix.resize(prefix.size()-1);
  224. }
  225. void JsonWriter::writeEntry(JsonMap::const_iterator entry)
  226. {
  227. out << prefix;
  228. writeString(entry->first);
  229. out << " : ";
  230. writeNode(entry->second);
  231. }
  232. void JsonWriter::writeEntry(JsonVector::const_iterator entry)
  233. {
  234. out << prefix;
  235. writeNode(*entry);
  236. }
  237. void JsonWriter::writeString(const std::string &string)
  238. {
  239. static const std::string escaped = "\"\\/\b\f\n\r\t";
  240. out <<'\"';
  241. size_t pos=0, start=0;
  242. for (; pos<string.size(); pos++)
  243. {
  244. size_t escapedChar = escaped.find(string[pos]);
  245. if (escapedChar != std::string::npos)
  246. {
  247. out.write(string.data()+start, pos - start);
  248. out << '\\' << escaped[escapedChar];
  249. start = pos;
  250. }
  251. }
  252. out.write(string.data()+start, pos - start);
  253. out <<'\"';
  254. }
  255. void JsonWriter::writeNode(const JsonNode &node)
  256. {
  257. switch(node.getType())
  258. {
  259. break; case JsonNode::DATA_NULL:
  260. out << "null";
  261. break; case JsonNode::DATA_BOOL:
  262. if (node.Bool())
  263. out << "true";
  264. else
  265. out << "false";
  266. break; case JsonNode::DATA_FLOAT:
  267. out << node.Float();
  268. break; case JsonNode::DATA_STRING:
  269. writeString(node.String());
  270. break; case JsonNode::DATA_VECTOR:
  271. out << "[" << "\n";
  272. writeContainer(node.Vector().begin(), node.Vector().end());
  273. out << prefix << "]";
  274. break; case JsonNode::DATA_STRUCT:
  275. out << "{" << "\n";
  276. writeContainer(node.Struct().begin(), node.Struct().end());
  277. out << prefix << "}";
  278. }
  279. }
  280. JsonWriter::JsonWriter(std::ostream &output, const JsonNode &node):
  281. out(output)
  282. {
  283. writeNode(node);
  284. }
  285. std::ostream & operator<<(std::ostream &out, const JsonNode &node)
  286. {
  287. JsonWriter(out, node);
  288. return out << "\n";
  289. }
  290. ////////////////////////////////////////////////////////////////////////////////
  291. JsonParser::JsonParser(const char * inputString, size_t stringSize, JsonNode &root):
  292. input(inputString, stringSize),
  293. lineCount(1),
  294. lineStart(0),
  295. pos(0)
  296. {
  297. extractValue(root);
  298. extractWhitespace(false);
  299. //Warn if there are any non-whitespace symbols left
  300. if (pos < input.size())
  301. error("Not all file was parsed!", true);
  302. //TODO: better way to show errors (like printing file name as well)
  303. tlog3<<errors;
  304. }
  305. bool JsonParser::extractSeparator()
  306. {
  307. if (!extractWhitespace())
  308. return false;
  309. if ( input[pos] !=':')
  310. return error("Separator expected");
  311. pos++;
  312. return true;
  313. }
  314. bool JsonParser::extractValue(JsonNode &node)
  315. {
  316. if (!extractWhitespace())
  317. return false;
  318. switch (input[pos])
  319. {
  320. case '\"': return extractString(node);
  321. case 'n' : return extractNull(node);
  322. case 't' : return extractTrue(node);
  323. case 'f' : return extractFalse(node);
  324. case '{' : return extractStruct(node);
  325. case '[' : return extractArray(node);
  326. case '-' : return extractFloat(node);
  327. default:
  328. {
  329. if (input[pos] >= '0' && input[pos] <= '9')
  330. return extractFloat(node);
  331. return error("Value expected!");
  332. }
  333. }
  334. }
  335. bool JsonParser::extractWhitespace(bool verbose)
  336. {
  337. while (true)
  338. {
  339. while (pos < input.size() && (ui8)input[pos] <= ' ')
  340. {
  341. if (input[pos] == '\n')
  342. {
  343. lineCount++;
  344. lineStart = pos+1;
  345. }
  346. pos++;
  347. }
  348. if (pos >= input.size() || input[pos] != '/')
  349. break;
  350. pos++;
  351. if (pos == input.size())
  352. break;
  353. if (input[pos] == '/')
  354. pos++;
  355. else
  356. error("Comments must consist from two slashes!", true);
  357. while (pos < input.size() && input[pos] != '\n')
  358. pos++;
  359. }
  360. if (pos >= input.size() && verbose)
  361. return error("Unexpected end of file!");
  362. return true;
  363. }
  364. bool JsonParser::extractEscaping(std::string &str)
  365. {
  366. switch(input[pos])
  367. {
  368. break; case '\"': str += '\"';
  369. break; case '\\': str += '\\';
  370. break; case '/': str += '/';
  371. break; case 'b': str += '\b';
  372. break; case 'f': str += '\f';
  373. break; case 'n': str += '\n';
  374. break; case 'r': str += '\r';
  375. break; case 't': str += '\t';
  376. break; default: return error("Unknown escape sequence!", true);
  377. };
  378. return true;
  379. }
  380. bool JsonParser::extractString(std::string &str)
  381. {
  382. if (input[pos] != '\"')
  383. return error("String expected!");
  384. pos++;
  385. size_t first = pos;
  386. while (pos != input.size())
  387. {
  388. if (input[pos] == '\"') // Correct end of string
  389. {
  390. str.append( &input[first], pos-first);
  391. pos++;
  392. return true;
  393. }
  394. if (input[pos] == '\\') // Escaping
  395. {
  396. str.append( &input[first], pos-first);
  397. first = pos++;
  398. if (pos == input.size())
  399. break;
  400. extractEscaping(str);
  401. }
  402. if (input[pos] == '\n') // end-of-line
  403. {
  404. str.append( &input[first], pos-first);
  405. return error("Closing quote not found!", true);
  406. }
  407. if ((unsigned char)(input[pos]) < ' ') // control character
  408. {
  409. str.append( &input[first], pos-first);
  410. first = pos+1;
  411. error("Illegal character in the string!", true);
  412. }
  413. pos++;
  414. }
  415. return error("Unterminated string!");
  416. }
  417. bool JsonParser::extractString(JsonNode &node)
  418. {
  419. std::string str;
  420. if (!extractString(str))
  421. return false;
  422. node.setType(JsonNode::DATA_STRING);
  423. node.String() = str;
  424. return true;
  425. }
  426. bool JsonParser::extractLiteral(const std::string &literal)
  427. {
  428. if (literal.compare(0, literal.size(), &input[pos], literal.size()) != 0)
  429. {
  430. while (pos < input.size() && ((input[pos]>'a' && input[pos]<'z')
  431. || (input[pos]>'A' && input[pos]<'Z')))
  432. pos++;
  433. return error("Unknown literal found", true);
  434. }
  435. pos += literal.size();
  436. return true;
  437. }
  438. bool JsonParser::extractNull(JsonNode &node)
  439. {
  440. if (!extractLiteral("null"))
  441. return false;
  442. node.setType(JsonNode::DATA_NULL);
  443. return true;
  444. }
  445. bool JsonParser::extractTrue(JsonNode &node)
  446. {
  447. if (!extractLiteral("true"))
  448. return false;
  449. node.Bool() = true;
  450. return true;
  451. }
  452. bool JsonParser::extractFalse(JsonNode &node)
  453. {
  454. if (!extractLiteral("false"))
  455. return false;
  456. node.Bool() = false;
  457. return true;
  458. }
  459. bool JsonParser::extractStruct(JsonNode &node)
  460. {
  461. node.setType(JsonNode::DATA_STRUCT);
  462. pos++;
  463. if (!extractWhitespace())
  464. return false;
  465. //Empty struct found
  466. if (input[pos] == '}')
  467. {
  468. pos++;
  469. return true;
  470. }
  471. while (true)
  472. {
  473. if (!extractWhitespace())
  474. return false;
  475. std::string key;
  476. if (!extractString(key))
  477. return false;
  478. if (node.Struct().find(key) != node.Struct().end())
  479. error("Dublicated element encountered!", true);
  480. if (!extractSeparator())
  481. return false;
  482. if (!extractElement(node.Struct()[key], '}'))
  483. return false;
  484. if (input[pos] == '}')
  485. {
  486. pos++;
  487. return true;
  488. }
  489. }
  490. }
  491. bool JsonParser::extractArray(JsonNode &node)
  492. {
  493. pos++;
  494. node.setType(JsonNode::DATA_VECTOR);
  495. if (!extractWhitespace())
  496. return false;
  497. //Empty array found
  498. if (input[pos] == ']')
  499. {
  500. pos++;
  501. return true;
  502. }
  503. while (true)
  504. {
  505. //NOTE: currently 50% of time is this vector resizing.
  506. //May be useful to use list during parsing and then swap() all items to vector
  507. node.Vector().resize(node.Vector().size()+1);
  508. if (!extractElement(node.Vector().back(), ']'))
  509. return false;
  510. if (input[pos] == ']')
  511. {
  512. pos++;
  513. return true;
  514. }
  515. }
  516. }
  517. bool JsonParser::extractElement(JsonNode &node, char terminator)
  518. {
  519. if (!extractValue(node))
  520. return false;
  521. if (!extractWhitespace())
  522. return false;
  523. bool comma = (input[pos] == ',');
  524. if (comma )
  525. {
  526. pos++;
  527. if (!extractWhitespace())
  528. return false;
  529. }
  530. if (input[pos] == terminator)
  531. return true;
  532. if (!comma)
  533. error("Comma expected!", true);
  534. return true;
  535. }
  536. bool JsonParser::extractFloat(JsonNode &node)
  537. {
  538. assert(input[pos] == '-' || (input[pos] >= '0' && input[pos] <= '9'));
  539. bool negative=false;
  540. double result=0;
  541. if (input[pos] == '-')
  542. {
  543. pos++;
  544. negative = true;
  545. }
  546. if (input[pos] < '0' || input[pos] > '9')
  547. return error("Number expected!");
  548. //Extract integer part
  549. while (input[pos] >= '0' && input[pos] <= '9')
  550. {
  551. result = result*10+(input[pos]-'0');
  552. pos++;
  553. }
  554. if (input[pos] == '.')
  555. {
  556. //extract fractional part
  557. pos++;
  558. double fractMult = 0.1;
  559. if (input[pos] < '0' || input[pos] > '9')
  560. return error("Decimal part expected!");
  561. while (input[pos] >= '0' && input[pos] <= '9')
  562. {
  563. result = result + fractMult*(input[pos]-'0');
  564. fractMult /= 10;
  565. pos++;
  566. }
  567. }
  568. //TODO: exponential part
  569. if (negative)
  570. result = -result;
  571. node.setType(JsonNode::DATA_FLOAT);
  572. node.Float() = result;
  573. return true;
  574. }
  575. bool JsonParser::error(const std::string &message, bool warning)
  576. {
  577. std::ostringstream stream;
  578. std::string type(warning?" warning: ":" error: ");
  579. stream << "At line " << lineCount << ", position "<<pos-lineStart
  580. << type << message <<"\n";
  581. errors += stream.str();
  582. return warning;
  583. }
  584. static const std::map<std::string, JsonNode::JsonType> stringToType =
  585. boost::assign::map_list_of
  586. ("null", JsonNode::DATA_NULL) ("bool", JsonNode::DATA_BOOL)
  587. ("number", JsonNode::DATA_FLOAT) ("string", JsonNode::DATA_STRING)
  588. ("array", JsonNode::DATA_VECTOR) ("object", JsonNode::DATA_STRUCT);
  589. //Check current schema entry for validness and converts "type" string to JsonType
  590. bool JsonValidator::validateSchema(JsonNode::JsonType &type, const JsonNode &schema)
  591. {
  592. if (schema.isNull())
  593. return addMessage("Missing schema for current entry!");
  594. const JsonNode &nodeType = schema["type"];
  595. if (nodeType.isNull())
  596. return addMessage("Entry type is not defined in schema!");
  597. if (nodeType.getType() != JsonNode::DATA_STRING)
  598. return addMessage("Entry type must be string!");
  599. std::map<std::string, JsonNode::JsonType>::const_iterator iter = stringToType.find(nodeType.String());
  600. if (iter == stringToType.end())
  601. return addMessage("Unknown entry type found!");
  602. type = iter->second;
  603. return true;
  604. }
  605. //Replaces node with default value if needed and calls type-specific validators
  606. bool JsonValidator::validateType(JsonNode &node, const JsonNode &schema, JsonNode::JsonType type)
  607. {
  608. if (node.isNull())
  609. {
  610. const JsonNode & defaultValue = schema["default"];
  611. if (defaultValue.isNull())
  612. return addMessage("Null entry without default entry!");
  613. else
  614. node = defaultValue;
  615. }
  616. if (minimize && node == schema["default"])
  617. {
  618. node.setType(JsonNode::DATA_NULL);
  619. return false;
  620. }
  621. if (type != node.getType())
  622. {
  623. node.setType(JsonNode::DATA_NULL);
  624. return addMessage("Type mismatch!");
  625. }
  626. if (type == JsonNode::DATA_VECTOR)
  627. return validateItems(node, schema["items"]);
  628. if (type == JsonNode::DATA_STRUCT)
  629. return validateProperties(node, schema["properties"]);
  630. return true;
  631. }
  632. // Basic checks common for any nodes
  633. bool JsonValidator::validateNode(JsonNode &node, const JsonNode &schema, const std::string &name)
  634. {
  635. currentPath.push_back(name);
  636. JsonNode::JsonType type = JsonNode::DATA_NULL;
  637. if (!validateSchema(type, schema)
  638. || !validateType(node, schema, type))
  639. {
  640. node.setType(JsonNode::DATA_NULL);
  641. currentPath.pop_back();
  642. return false;
  643. }
  644. currentPath.pop_back();
  645. return true;
  646. }
  647. //Checks "items" entry from schema (type-specific check for Vector)
  648. bool JsonValidator::validateItems(JsonNode &node, const JsonNode &schema)
  649. {
  650. JsonNode::JsonType type = JsonNode::DATA_NULL;
  651. if (!validateSchema(type, schema))
  652. return false;
  653. bool result = true;
  654. BOOST_FOREACH(JsonNode &entry, node.Vector())
  655. {
  656. if (!validateType(entry, schema, type))
  657. {
  658. result = false;
  659. entry.setType(JsonNode::DATA_NULL);
  660. }
  661. }
  662. return result;
  663. }
  664. //Checks "propertries" entry from schema (type-specific check for Struct)
  665. //Function is similar to merging of two sorted lists - check every entry that present in one of the input nodes
  666. bool JsonValidator::validateProperties(JsonNode &node, const JsonNode &schema)
  667. {
  668. if (schema.isNull())
  669. return addMessage("Properties entry is missing for struct in schema");
  670. JsonMap::iterator nodeIter = node.Struct().begin();
  671. JsonMap::const_iterator schemaIter = schema.Struct().begin();
  672. while (nodeIter != node.Struct().end() && schemaIter != schema.Struct().end())
  673. {
  674. if (nodeIter->first < schemaIter->first) //No schema for entry
  675. {
  676. validateNode(nodeIter->second, nullNode, nodeIter->first);
  677. JsonMap::iterator toRemove = nodeIter++;
  678. node.Struct().erase(toRemove);
  679. }
  680. else
  681. if (schemaIter->first < nodeIter->first) //No entry
  682. {
  683. if (!validateNode(node[schemaIter->first], schemaIter->second, schemaIter->first))
  684. node.Struct().erase(schemaIter->first);
  685. schemaIter++;
  686. }
  687. else //both entry and schema are present
  688. {
  689. JsonMap::iterator current = nodeIter++;
  690. if (!validateNode(current->second, schemaIter->second, current->first))
  691. node.Struct().erase(current);
  692. schemaIter++;
  693. }
  694. }
  695. while (nodeIter != node.Struct().end())
  696. {
  697. validateNode(nodeIter->second, nullNode, nodeIter->first);
  698. JsonMap::iterator toRemove = nodeIter++;
  699. node.Struct().erase(toRemove);
  700. }
  701. while (schemaIter != schema.Struct().end())
  702. {
  703. if (!validateNode(node[schemaIter->first], schemaIter->second, schemaIter->first))
  704. node.Struct().erase(schemaIter->first);
  705. schemaIter++;
  706. }
  707. return true;
  708. }
  709. bool JsonValidator::addMessage(const std::string &message)
  710. {
  711. std::ostringstream stream;
  712. stream << "At ";
  713. BOOST_FOREACH(const std::string &path, currentPath)
  714. stream << path<<"/";
  715. stream << "\t Error: " << message <<"\n";
  716. errors += stream.str();
  717. return false;
  718. }
  719. JsonValidator::JsonValidator(JsonNode &root, bool Minimize):
  720. minimize(Minimize)
  721. {
  722. if (root.getType() != JsonNode::DATA_STRUCT)
  723. return;
  724. JsonNode schema;
  725. schema.swap(root["schema"]);
  726. root.Struct().erase("schema");
  727. if (!schema.isNull())
  728. {
  729. validateProperties(root, schema);
  730. }
  731. //This message is quite annoying now - most files do not have schemas. May be re-enabled later
  732. //else
  733. // addMessage("Schema not found!", true);
  734. //TODO: better way to show errors (like printing file name as well)
  735. tlog3<<errors;
  736. }
  737. JsonValidator::JsonValidator(JsonNode &root, const JsonNode &schema, bool Minimize):
  738. minimize(Minimize)
  739. {
  740. validateProperties(root, schema);
  741. if (schema.isNull())
  742. addMessage("Schema not found!");
  743. tlog3<<errors;
  744. }
  745. Bonus * JsonUtils::parseBonus (const JsonVector &ability_vec) //TODO: merge with AddAbility, create universal parser for all bonus properties
  746. {
  747. Bonus * b = new Bonus();
  748. std::string type = ability_vec[0].String();
  749. auto it = bonusNameMap.find(type);
  750. if (it == bonusNameMap.end())
  751. {
  752. tlog1 << "Error: invalid ability type " << type << " in creatures.txt" << std::endl;
  753. return b;
  754. }
  755. b->type = it->second;
  756. b->val = ability_vec[1].Float();
  757. b->subtype = ability_vec[2].Float();
  758. b->additionalInfo = ability_vec[3].Float();
  759. b->duration = Bonus::PERMANENT; //TODO: handle flags (as integer)
  760. b->turnsRemain = 0;
  761. return b;
  762. }
  763. template <typename T>
  764. const T & parseByMap(const std::map<std::string, T> & map, const JsonNode * val, std::string err)
  765. {
  766. static T defaultValue;
  767. if (!val->isNull())
  768. {
  769. std::string type = val->String();
  770. auto it = map.find(type);
  771. if (it == map.end())
  772. {
  773. tlog1 << "Error: invalid " << err << type << std::endl;
  774. return defaultValue;
  775. }
  776. else
  777. {
  778. return it->second;
  779. }
  780. }
  781. else
  782. return defaultValue;
  783. };
  784. Bonus * JsonUtils::parseBonus (const JsonNode &ability)
  785. {
  786. Bonus * b = new Bonus();
  787. const JsonNode *value;
  788. std::string type = ability["type"].String();
  789. auto it = bonusNameMap.find(type);
  790. if (it == bonusNameMap.end())
  791. {
  792. tlog1 << "Error: invalid ability type " << type << std::endl;
  793. return b;
  794. }
  795. b->type = it->second;
  796. value = &ability["subtype"];
  797. if (!value->isNull())
  798. b->subtype = value->Float();
  799. value = &ability["val"];
  800. if (!value->isNull())
  801. b->val = value->Float();
  802. value = &ability["valueType"];
  803. if (!value->isNull())
  804. b->valType = parseByMap(bonusValueMap, value, "value type ");
  805. value = &ability["additionalInfo"];
  806. if (!value->isNull())
  807. b->additionalInfo = value->Float();
  808. value = &ability["turns"];
  809. if (!value->isNull())
  810. b->turnsRemain = value->Float();
  811. value = &ability["sourceID"];
  812. if (!value->isNull())
  813. b->sid = value->Float();
  814. value = &ability["description"];
  815. if (!value->isNull())
  816. b->description = value->String();
  817. value = &ability["effectRange"];
  818. if (!value->isNull())
  819. b->valType = parseByMap(bonusLimitEffect, value, "effect range ");
  820. value = &ability["duration"];
  821. if (!value->isNull())
  822. b->valType = parseByMap(bonusDurationMap, value, "duration type ");
  823. value = &ability["source"];
  824. if (!value->isNull())
  825. b->valType = parseByMap(bonusSourceMap, value, "source type ");
  826. value = &ability["limiter"];
  827. if (!value->isNull())
  828. b->limiter = parseByMap(bonusLimiterMap, value, "limiter type ");
  829. value = &ability["propagator"];
  830. if (!value->isNull())
  831. b->propagator = parseByMap(bonusPropagatorMap, value, "propagator type ");
  832. return b;
  833. }
  834. //returns first Key with value equal to given one
  835. template<class Key, class Val>
  836. Key reverseMapFirst(const Val & val, const std::map<Key, Val> map)
  837. {
  838. BOOST_FOREACH(auto it, map)
  839. {
  840. if(it.second == val)
  841. {
  842. return it.first;
  843. }
  844. }
  845. assert(0);
  846. return "";
  847. }
  848. void JsonUtils::unparseBonus( JsonNode &node, const Bonus * bonus )
  849. {
  850. node["type"].String() = reverseMapFirst<std::string, int>(bonus->type, bonusNameMap);
  851. node["subtype"].Float() = bonus->subtype;
  852. node["val"].Float() = bonus->val;
  853. node["valueType"].String() = reverseMapFirst<std::string, int>(bonus->valType, bonusValueMap);
  854. node["additionalInfo"].Float() = bonus->additionalInfo;
  855. node["turns"].Float() = bonus->turnsRemain;
  856. node["sourceID"].Float() = bonus->source;
  857. node["description"].String() = bonus->description;
  858. node["effectRange"].String() = reverseMapFirst<std::string, int>(bonus->effectRange, bonusLimitEffect);
  859. node["duration"].String() = reverseMapFirst<std::string, int>(bonus->duration, bonusDurationMap);
  860. node["source"].String() = reverseMapFirst<std::string, int>(bonus->source, bonusSourceMap);
  861. if(bonus->limiter)
  862. {
  863. node["limiter"].String() = reverseMapFirst<std::string, TLimiterPtr>(bonus->limiter, bonusLimiterMap);
  864. }
  865. if(bonus->propagator)
  866. {
  867. node["propagator"].String() = reverseMapFirst<std::string, TPropagatorPtr>(bonus->propagator, bonusPropagatorMap);
  868. }
  869. }
  870. void JsonUtils::minimize(JsonNode & node, const JsonNode& schema)
  871. {
  872. JsonValidator validator(node, schema, true);
  873. }
  874. void JsonUtils::validate(JsonNode & node, const JsonNode& schema)
  875. {
  876. JsonValidator validator(node, schema, false);
  877. }
  878. void JsonUtils::merge(JsonNode & dest, JsonNode & source)
  879. {
  880. if (dest.getType() == JsonNode::DATA_NULL)
  881. {
  882. std::swap(dest, source);
  883. return;
  884. }
  885. switch (source.getType())
  886. {
  887. break; case JsonNode::DATA_NULL: dest.setType(JsonNode::DATA_NULL);
  888. break; case JsonNode::DATA_BOOL: std::swap(dest.Bool(), source.Bool());
  889. break; case JsonNode::DATA_FLOAT: std::swap(dest.Float(), source.Float());
  890. break; case JsonNode::DATA_STRING: std::swap(dest.String(), source.String());
  891. break; case JsonNode::DATA_VECTOR:
  892. {
  893. size_t total = std::min(source.Vector().size(), dest.Vector().size());
  894. for (size_t i=0; i< total; i++)
  895. merge(dest.Vector()[i], source.Vector()[i]);
  896. if (source.Vector().size() < dest.Vector().size())
  897. {
  898. //reserve place and *move* data from source to dest
  899. source.Vector().reserve(source.Vector().size() + dest.Vector().size());
  900. std::move(source.Vector().begin(), source.Vector().end(),
  901. std::back_inserter(dest.Vector()));
  902. }
  903. }
  904. break; case JsonNode::DATA_STRUCT:
  905. {
  906. //recursively merge all entries from struct
  907. BOOST_FOREACH(auto & node, source.Struct())
  908. merge(dest[node.first], node.second);
  909. }
  910. }
  911. }
  912. void JsonUtils::mergeCopy(JsonNode & dest, JsonNode source)
  913. {
  914. // uses copy created in stack to safely merge two nodes
  915. merge(dest, source);
  916. }
  917. JsonNode JsonUtils::assembleFromFiles(std::vector<std::string> files)
  918. {
  919. JsonNode result;
  920. BOOST_FOREACH(std::string file, files)
  921. {
  922. JsonNode section(ResourceID(file, EResType::TEXT));
  923. merge(result, section);
  924. }
  925. return result;
  926. }