JsonNode.cpp 24 KB

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