JsonNode.cpp 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090
  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. pos++;
  398. if (pos == input.size())
  399. break;
  400. extractEscaping(str);
  401. first = pos + 1;
  402. }
  403. if (input[pos] == '\n') // end-of-line
  404. {
  405. str.append( &input[first], pos-first);
  406. return error("Closing quote not found!", true);
  407. }
  408. if ((unsigned char)(input[pos]) < ' ') // control character
  409. {
  410. str.append( &input[first], pos-first);
  411. first = pos+1;
  412. error("Illegal character in the string!", true);
  413. }
  414. pos++;
  415. }
  416. return error("Unterminated string!");
  417. }
  418. bool JsonParser::extractString(JsonNode &node)
  419. {
  420. std::string str;
  421. if (!extractString(str))
  422. return false;
  423. node.setType(JsonNode::DATA_STRING);
  424. node.String() = str;
  425. return true;
  426. }
  427. bool JsonParser::extractLiteral(const std::string &literal)
  428. {
  429. if (literal.compare(0, literal.size(), &input[pos], literal.size()) != 0)
  430. {
  431. while (pos < input.size() && ((input[pos]>'a' && input[pos]<'z')
  432. || (input[pos]>'A' && input[pos]<'Z')))
  433. pos++;
  434. return error("Unknown literal found", true);
  435. }
  436. pos += literal.size();
  437. return true;
  438. }
  439. bool JsonParser::extractNull(JsonNode &node)
  440. {
  441. if (!extractLiteral("null"))
  442. return false;
  443. node.setType(JsonNode::DATA_NULL);
  444. return true;
  445. }
  446. bool JsonParser::extractTrue(JsonNode &node)
  447. {
  448. if (!extractLiteral("true"))
  449. return false;
  450. node.Bool() = true;
  451. return true;
  452. }
  453. bool JsonParser::extractFalse(JsonNode &node)
  454. {
  455. if (!extractLiteral("false"))
  456. return false;
  457. node.Bool() = false;
  458. return true;
  459. }
  460. bool JsonParser::extractStruct(JsonNode &node)
  461. {
  462. node.setType(JsonNode::DATA_STRUCT);
  463. pos++;
  464. if (!extractWhitespace())
  465. return false;
  466. //Empty struct found
  467. if (input[pos] == '}')
  468. {
  469. pos++;
  470. return true;
  471. }
  472. while (true)
  473. {
  474. if (!extractWhitespace())
  475. return false;
  476. std::string key;
  477. if (!extractString(key))
  478. return false;
  479. if (node.Struct().find(key) != node.Struct().end())
  480. error("Dublicated element encountered!", true);
  481. if (!extractSeparator())
  482. return false;
  483. if (!extractElement(node.Struct()[key], '}'))
  484. return false;
  485. if (input[pos] == '}')
  486. {
  487. pos++;
  488. return true;
  489. }
  490. }
  491. }
  492. bool JsonParser::extractArray(JsonNode &node)
  493. {
  494. pos++;
  495. node.setType(JsonNode::DATA_VECTOR);
  496. if (!extractWhitespace())
  497. return false;
  498. //Empty array found
  499. if (input[pos] == ']')
  500. {
  501. pos++;
  502. return true;
  503. }
  504. while (true)
  505. {
  506. //NOTE: currently 50% of time is this vector resizing.
  507. //May be useful to use list during parsing and then swap() all items to vector
  508. node.Vector().resize(node.Vector().size()+1);
  509. if (!extractElement(node.Vector().back(), ']'))
  510. return false;
  511. if (input[pos] == ']')
  512. {
  513. pos++;
  514. return true;
  515. }
  516. }
  517. }
  518. bool JsonParser::extractElement(JsonNode &node, char terminator)
  519. {
  520. if (!extractValue(node))
  521. return false;
  522. if (!extractWhitespace())
  523. return false;
  524. bool comma = (input[pos] == ',');
  525. if (comma )
  526. {
  527. pos++;
  528. if (!extractWhitespace())
  529. return false;
  530. }
  531. if (input[pos] == terminator)
  532. return true;
  533. if (!comma)
  534. error("Comma expected!", true);
  535. return true;
  536. }
  537. bool JsonParser::extractFloat(JsonNode &node)
  538. {
  539. assert(input[pos] == '-' || (input[pos] >= '0' && input[pos] <= '9'));
  540. bool negative=false;
  541. double result=0;
  542. if (input[pos] == '-')
  543. {
  544. pos++;
  545. negative = true;
  546. }
  547. if (input[pos] < '0' || input[pos] > '9')
  548. return error("Number expected!");
  549. //Extract integer part
  550. while (input[pos] >= '0' && input[pos] <= '9')
  551. {
  552. result = result*10+(input[pos]-'0');
  553. pos++;
  554. }
  555. if (input[pos] == '.')
  556. {
  557. //extract fractional part
  558. pos++;
  559. double fractMult = 0.1;
  560. if (input[pos] < '0' || input[pos] > '9')
  561. return error("Decimal part expected!");
  562. while (input[pos] >= '0' && input[pos] <= '9')
  563. {
  564. result = result + fractMult*(input[pos]-'0');
  565. fractMult /= 10;
  566. pos++;
  567. }
  568. }
  569. //TODO: exponential part
  570. if (negative)
  571. result = -result;
  572. node.setType(JsonNode::DATA_FLOAT);
  573. node.Float() = result;
  574. return true;
  575. }
  576. bool JsonParser::error(const std::string &message, bool warning)
  577. {
  578. std::ostringstream stream;
  579. std::string type(warning?" warning: ":" error: ");
  580. stream << "At line " << lineCount << ", position "<<pos-lineStart
  581. << type << message <<"\n";
  582. errors += stream.str();
  583. return warning;
  584. }
  585. static const std::map<std::string, JsonNode::JsonType> stringToType =
  586. boost::assign::map_list_of
  587. ("null", JsonNode::DATA_NULL) ("bool", JsonNode::DATA_BOOL)
  588. ("number", JsonNode::DATA_FLOAT) ("string", JsonNode::DATA_STRING)
  589. ("array", JsonNode::DATA_VECTOR) ("object", JsonNode::DATA_STRUCT);
  590. //Check current schema entry for validness and converts "type" string to JsonType
  591. bool JsonValidator::validateSchema(JsonNode::JsonType &type, const JsonNode &schema)
  592. {
  593. if (schema.isNull())
  594. return addMessage("Missing schema for current entry!");
  595. const JsonNode &nodeType = schema["type"];
  596. if (nodeType.isNull())
  597. return addMessage("Entry type is not defined in schema!");
  598. if (nodeType.getType() != JsonNode::DATA_STRING)
  599. return addMessage("Entry type must be string!");
  600. std::map<std::string, JsonNode::JsonType>::const_iterator iter = stringToType.find(nodeType.String());
  601. if (iter == stringToType.end())
  602. return addMessage("Unknown entry type found!");
  603. type = iter->second;
  604. return true;
  605. }
  606. //Replaces node with default value if needed and calls type-specific validators
  607. bool JsonValidator::validateType(JsonNode &node, const JsonNode &schema, JsonNode::JsonType type)
  608. {
  609. if (node.isNull())
  610. {
  611. const JsonNode & defaultValue = schema["default"];
  612. if (defaultValue.isNull())
  613. return addMessage("Null entry without default entry!");
  614. else
  615. node = defaultValue;
  616. }
  617. if (minimize && node == schema["default"])
  618. {
  619. node.setType(JsonNode::DATA_NULL);
  620. return false;
  621. }
  622. if (type != node.getType())
  623. {
  624. node.setType(JsonNode::DATA_NULL);
  625. return addMessage("Type mismatch!");
  626. }
  627. if (type == JsonNode::DATA_VECTOR)
  628. return validateItems(node, schema["items"]);
  629. if (type == JsonNode::DATA_STRUCT)
  630. return validateProperties(node, schema["properties"]);
  631. return true;
  632. }
  633. // Basic checks common for any nodes
  634. bool JsonValidator::validateNode(JsonNode &node, const JsonNode &schema, const std::string &name)
  635. {
  636. currentPath.push_back(name);
  637. JsonNode::JsonType type = JsonNode::DATA_NULL;
  638. if (!validateSchema(type, schema)
  639. || !validateType(node, schema, type))
  640. {
  641. node.setType(JsonNode::DATA_NULL);
  642. currentPath.pop_back();
  643. return false;
  644. }
  645. currentPath.pop_back();
  646. return true;
  647. }
  648. //Checks "items" entry from schema (type-specific check for Vector)
  649. bool JsonValidator::validateItems(JsonNode &node, const JsonNode &schema)
  650. {
  651. JsonNode::JsonType type = JsonNode::DATA_NULL;
  652. if (!validateSchema(type, schema))
  653. return false;
  654. bool result = true;
  655. BOOST_FOREACH(JsonNode &entry, node.Vector())
  656. {
  657. if (!validateType(entry, schema, type))
  658. {
  659. result = false;
  660. entry.setType(JsonNode::DATA_NULL);
  661. }
  662. }
  663. return result;
  664. }
  665. //Checks "propertries" entry from schema (type-specific check for Struct)
  666. //Function is similar to merging of two sorted lists - check every entry that present in one of the input nodes
  667. bool JsonValidator::validateProperties(JsonNode &node, const JsonNode &schema)
  668. {
  669. if (schema.isNull())
  670. return addMessage("Properties entry is missing for struct in schema");
  671. JsonMap::iterator nodeIter = node.Struct().begin();
  672. JsonMap::const_iterator schemaIter = schema.Struct().begin();
  673. while (nodeIter != node.Struct().end() && schemaIter != schema.Struct().end())
  674. {
  675. if (nodeIter->first < schemaIter->first) //No schema for entry
  676. {
  677. validateNode(nodeIter->second, nullNode, nodeIter->first);
  678. JsonMap::iterator toRemove = nodeIter++;
  679. node.Struct().erase(toRemove);
  680. }
  681. else
  682. if (schemaIter->first < nodeIter->first) //No entry
  683. {
  684. if (!validateNode(node[schemaIter->first], schemaIter->second, schemaIter->first))
  685. node.Struct().erase(schemaIter->first);
  686. schemaIter++;
  687. }
  688. else //both entry and schema are present
  689. {
  690. JsonMap::iterator current = nodeIter++;
  691. if (!validateNode(current->second, schemaIter->second, current->first))
  692. node.Struct().erase(current);
  693. schemaIter++;
  694. }
  695. }
  696. while (nodeIter != node.Struct().end())
  697. {
  698. validateNode(nodeIter->second, nullNode, nodeIter->first);
  699. JsonMap::iterator toRemove = nodeIter++;
  700. node.Struct().erase(toRemove);
  701. }
  702. while (schemaIter != schema.Struct().end())
  703. {
  704. if (!validateNode(node[schemaIter->first], schemaIter->second, schemaIter->first))
  705. node.Struct().erase(schemaIter->first);
  706. schemaIter++;
  707. }
  708. return true;
  709. }
  710. bool JsonValidator::addMessage(const std::string &message)
  711. {
  712. std::ostringstream stream;
  713. stream << "At ";
  714. BOOST_FOREACH(const std::string &path, currentPath)
  715. stream << path<<"/";
  716. stream << "\t Error: " << message <<"\n";
  717. errors += stream.str();
  718. return false;
  719. }
  720. JsonValidator::JsonValidator(JsonNode &root, bool Minimize):
  721. minimize(Minimize)
  722. {
  723. if (root.getType() != JsonNode::DATA_STRUCT)
  724. return;
  725. JsonNode schema;
  726. schema.swap(root["schema"]);
  727. root.Struct().erase("schema");
  728. if (!schema.isNull())
  729. {
  730. validateProperties(root, schema);
  731. }
  732. //This message is quite annoying now - most files do not have schemas. May be re-enabled later
  733. //else
  734. // addMessage("Schema not found!", true);
  735. //TODO: better way to show errors (like printing file name as well)
  736. tlog3<<errors;
  737. }
  738. JsonValidator::JsonValidator(JsonNode &root, const JsonNode &schema, bool Minimize):
  739. minimize(Minimize)
  740. {
  741. validateProperties(root, schema);
  742. if (schema.isNull())
  743. addMessage("Schema not found!");
  744. tlog3<<errors;
  745. }
  746. Bonus * JsonUtils::parseBonus (const JsonVector &ability_vec) //TODO: merge with AddAbility, create universal parser for all bonus properties
  747. {
  748. Bonus * b = new Bonus();
  749. std::string type = ability_vec[0].String();
  750. auto it = bonusNameMap.find(type);
  751. if (it == bonusNameMap.end())
  752. {
  753. tlog1 << "Error: invalid ability type " << type << " in creatures.txt" << std::endl;
  754. return b;
  755. }
  756. b->type = it->second;
  757. b->val = ability_vec[1].Float();
  758. b->subtype = ability_vec[2].Float();
  759. b->additionalInfo = ability_vec[3].Float();
  760. b->duration = Bonus::PERMANENT; //TODO: handle flags (as integer)
  761. b->turnsRemain = 0;
  762. return b;
  763. }
  764. template <typename T>
  765. const T & parseByMap(const std::map<std::string, T> & map, const JsonNode * val, std::string err)
  766. {
  767. static T defaultValue;
  768. if (!val->isNull())
  769. {
  770. std::string type = val->String();
  771. auto it = map.find(type);
  772. if (it == map.end())
  773. {
  774. tlog1 << "Error: invalid " << err << type << std::endl;
  775. return defaultValue;
  776. }
  777. else
  778. {
  779. return it->second;
  780. }
  781. }
  782. else
  783. return defaultValue;
  784. };
  785. Bonus * JsonUtils::parseBonus (const JsonNode &ability)
  786. {
  787. Bonus * b = new Bonus();
  788. const JsonNode *value;
  789. std::string type = ability["type"].String();
  790. auto it = bonusNameMap.find(type);
  791. if (it == bonusNameMap.end())
  792. {
  793. tlog1 << "Error: invalid ability type " << type << std::endl;
  794. return b;
  795. }
  796. b->type = it->second;
  797. value = &ability["subtype"];
  798. if (!value->isNull())
  799. b->subtype = value->Float();
  800. value = &ability["val"];
  801. if (!value->isNull())
  802. b->val = value->Float();
  803. value = &ability["valueType"];
  804. if (!value->isNull())
  805. b->valType = parseByMap(bonusValueMap, value, "value type ");
  806. value = &ability["additionalInfo"];
  807. if (!value->isNull())
  808. b->additionalInfo = value->Float();
  809. value = &ability["turns"];
  810. if (!value->isNull())
  811. b->turnsRemain = value->Float();
  812. value = &ability["sourceID"];
  813. if (!value->isNull())
  814. b->sid = value->Float();
  815. value = &ability["description"];
  816. if (!value->isNull())
  817. b->description = value->String();
  818. value = &ability["effectRange"];
  819. if (!value->isNull())
  820. b->valType = parseByMap(bonusLimitEffect, value, "effect range ");
  821. value = &ability["duration"];
  822. if (!value->isNull())
  823. b->valType = parseByMap(bonusDurationMap, value, "duration type ");
  824. value = &ability["source"];
  825. if (!value->isNull())
  826. b->valType = parseByMap(bonusSourceMap, value, "source type ");
  827. value = &ability["limiter"];
  828. if (!value->isNull())
  829. b->limiter = parseByMap(bonusLimiterMap, value, "limiter type ");
  830. value = &ability["propagator"];
  831. if (!value->isNull())
  832. b->propagator = parseByMap(bonusPropagatorMap, value, "propagator type ");
  833. return b;
  834. }
  835. //returns first Key with value equal to given one
  836. template<class Key, class Val>
  837. Key reverseMapFirst(const Val & val, const std::map<Key, Val> map)
  838. {
  839. BOOST_FOREACH(auto it, map)
  840. {
  841. if(it.second == val)
  842. {
  843. return it.first;
  844. }
  845. }
  846. assert(0);
  847. return "";
  848. }
  849. void JsonUtils::unparseBonus( JsonNode &node, const Bonus * bonus )
  850. {
  851. node["type"].String() = reverseMapFirst<std::string, int>(bonus->type, bonusNameMap);
  852. node["subtype"].Float() = bonus->subtype;
  853. node["val"].Float() = bonus->val;
  854. node["valueType"].String() = reverseMapFirst<std::string, int>(bonus->valType, bonusValueMap);
  855. node["additionalInfo"].Float() = bonus->additionalInfo;
  856. node["turns"].Float() = bonus->turnsRemain;
  857. node["sourceID"].Float() = bonus->source;
  858. node["description"].String() = bonus->description;
  859. node["effectRange"].String() = reverseMapFirst<std::string, int>(bonus->effectRange, bonusLimitEffect);
  860. node["duration"].String() = reverseMapFirst<std::string, int>(bonus->duration, bonusDurationMap);
  861. node["source"].String() = reverseMapFirst<std::string, int>(bonus->source, bonusSourceMap);
  862. if(bonus->limiter)
  863. {
  864. node["limiter"].String() = reverseMapFirst<std::string, TLimiterPtr>(bonus->limiter, bonusLimiterMap);
  865. }
  866. if(bonus->propagator)
  867. {
  868. node["propagator"].String() = reverseMapFirst<std::string, TPropagatorPtr>(bonus->propagator, bonusPropagatorMap);
  869. }
  870. }
  871. void JsonUtils::minimize(JsonNode & node, const JsonNode& schema)
  872. {
  873. JsonValidator validator(node, schema, true);
  874. }
  875. void JsonUtils::validate(JsonNode & node, const JsonNode& schema)
  876. {
  877. JsonValidator validator(node, schema, false);
  878. }
  879. void JsonUtils::merge(JsonNode & dest, JsonNode & source)
  880. {
  881. if (dest.getType() == JsonNode::DATA_NULL)
  882. {
  883. std::swap(dest, source);
  884. return;
  885. }
  886. switch (source.getType())
  887. {
  888. break; case JsonNode::DATA_NULL: dest.setType(JsonNode::DATA_NULL);
  889. break; case JsonNode::DATA_BOOL: std::swap(dest.Bool(), source.Bool());
  890. break; case JsonNode::DATA_FLOAT: std::swap(dest.Float(), source.Float());
  891. break; case JsonNode::DATA_STRING: std::swap(dest.String(), source.String());
  892. break; case JsonNode::DATA_VECTOR:
  893. {
  894. size_t total = std::min(source.Vector().size(), dest.Vector().size());
  895. for (size_t i=0; i< total; i++)
  896. merge(dest.Vector()[i], source.Vector()[i]);
  897. if (source.Vector().size() < dest.Vector().size())
  898. {
  899. //reserve place and *move* data from source to dest
  900. source.Vector().reserve(source.Vector().size() + dest.Vector().size());
  901. std::move(source.Vector().begin(), source.Vector().end(),
  902. std::back_inserter(dest.Vector()));
  903. }
  904. }
  905. break; case JsonNode::DATA_STRUCT:
  906. {
  907. //recursively merge all entries from struct
  908. BOOST_FOREACH(auto & node, source.Struct())
  909. merge(dest[node.first], node.second);
  910. }
  911. }
  912. }
  913. void JsonUtils::mergeCopy(JsonNode & dest, JsonNode source)
  914. {
  915. // uses copy created in stack to safely merge two nodes
  916. merge(dest, source);
  917. }
  918. JsonNode JsonUtils::assembleFromFiles(std::vector<std::string> files)
  919. {
  920. JsonNode result;
  921. BOOST_FOREACH(std::string file, files)
  922. {
  923. JsonNode section(ResourceID(file, EResType::TEXT));
  924. merge(result, section);
  925. }
  926. return result;
  927. }