JsonNode.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. #define VCMI_DLL
  2. #include "JsonNode.h"
  3. #include <assert.h>
  4. #include <fstream>
  5. #include <sstream>
  6. #include <iostream>
  7. const JsonNode JsonNode::nullNode;
  8. JsonNode::JsonNode(JsonType Type):
  9. type(DATA_NULL)
  10. {
  11. setType(Type);
  12. }
  13. JsonNode::JsonNode(std::string input):
  14. type(DATA_NULL)
  15. {
  16. JsonParser parser(input, *this);
  17. }
  18. JsonNode::JsonNode(const char *filename):
  19. type(DATA_NULL)
  20. {
  21. std::ifstream file(filename);
  22. std::string str((std::istreambuf_iterator<char>(file)),
  23. std::istreambuf_iterator<char>());
  24. JsonParser parser(str, *this);
  25. }
  26. JsonNode::JsonNode(const JsonNode &copy):
  27. type(DATA_NULL)
  28. {
  29. *this = copy;
  30. }
  31. JsonNode::~JsonNode()
  32. {
  33. setType(DATA_NULL);
  34. }
  35. JsonNode & JsonNode::operator =(const JsonNode &node)
  36. {
  37. setType(node.getType());
  38. switch(type)
  39. {
  40. break; case DATA_NULL:
  41. break; case DATA_BOOL: Bool() = node.Bool();
  42. break; case DATA_FLOAT: Float() = node.Float();
  43. break; case DATA_STRING: String() = node.String();
  44. break; case DATA_VECTOR: Vector() = node.Vector();
  45. break; case DATA_STRUCT: Struct() = node.Struct();
  46. }
  47. return *this;
  48. }
  49. JsonNode::JsonType JsonNode::getType() const
  50. {
  51. return type;
  52. }
  53. void JsonNode::setType(JsonType Type)
  54. {
  55. if (type == Type)
  56. return;
  57. if (Type != DATA_NULL)
  58. setType(DATA_NULL);
  59. switch (type)
  60. {
  61. break; case DATA_STRING: delete data.String;
  62. break; case DATA_VECTOR : delete data.Vector;
  63. break; case DATA_STRUCT: delete data.Struct;
  64. break; default:
  65. break;
  66. }
  67. type = Type;
  68. switch(type)
  69. {
  70. break; case DATA_NULL:
  71. break; case DATA_BOOL: data.Bool = false;
  72. break; case DATA_FLOAT: data.Float = 0;
  73. break; case DATA_STRING: data.String = new std::string;
  74. break; case DATA_VECTOR: data.Vector = new JsonVector;
  75. break; case DATA_STRUCT: data.Struct = new JsonMap;
  76. }
  77. }
  78. bool JsonNode::isNull() const
  79. {
  80. return type == DATA_NULL;
  81. }
  82. bool & JsonNode::Bool()
  83. {
  84. setType(DATA_BOOL);
  85. return data.Bool;
  86. }
  87. float & JsonNode::Float()
  88. {
  89. setType(DATA_FLOAT);
  90. return data.Float;
  91. }
  92. std::string & JsonNode::String()
  93. {
  94. setType(DATA_STRING);
  95. return *data.String;
  96. }
  97. JsonVector & JsonNode::Vector()
  98. {
  99. setType(DATA_VECTOR);
  100. return *data.Vector;
  101. }
  102. JsonMap & JsonNode::Struct()
  103. {
  104. setType(DATA_STRUCT);
  105. return *data.Struct;
  106. }
  107. const bool & JsonNode::Bool() const
  108. {
  109. assert(type == DATA_BOOL);
  110. return data.Bool;
  111. }
  112. const float & JsonNode::Float() const
  113. {
  114. assert(type == DATA_FLOAT);
  115. return data.Float;
  116. }
  117. const std::string & JsonNode::String() const
  118. {
  119. assert(type == DATA_STRING);
  120. return *data.String;
  121. }
  122. const JsonVector & JsonNode::Vector() const
  123. {
  124. assert(type == DATA_VECTOR);
  125. return *data.Vector;
  126. }
  127. const JsonMap & JsonNode::Struct() const
  128. {
  129. assert(type == DATA_STRUCT);
  130. return *data.Struct;
  131. }
  132. JsonNode & JsonNode::operator[](std::string child)
  133. {
  134. return Struct()[child];
  135. }
  136. const JsonNode & JsonNode::operator[](std::string child) const
  137. {
  138. JsonMap::const_iterator it = Struct().find(child);
  139. if (it != Struct().end())
  140. return it->second;
  141. return nullNode;
  142. }
  143. ////////////////////////////////////////////////////////////////////////////////
  144. //Helper to write content of map/vector
  145. template<class iterator>
  146. void writeContainer(const iterator &begin, const iterator &end, std::ostream &out, std::string prefix)
  147. {
  148. if (begin == end)
  149. return;
  150. iterator last = end;
  151. last--;
  152. for (iterator it=begin; it != last; ++it)
  153. {
  154. writeNode(it, out, prefix);
  155. out<<",\n";
  156. }
  157. writeNode(last, out, prefix);
  158. out<<"\n";
  159. }
  160. void writeNode(JsonVector::const_iterator it, std::ostream &out, std::string prefix)
  161. {
  162. out << prefix;
  163. it->write(out, prefix);
  164. }
  165. void writeNode(JsonMap::const_iterator it, std::ostream &out, std::string prefix)
  166. {
  167. out << prefix << '\"' << it->first << '\"' << " : ";
  168. it->second.write(out, prefix);
  169. }
  170. void JsonNode::write(std::ostream &out, std::string prefix) const
  171. {
  172. switch(type)
  173. {
  174. break; case DATA_NULL:
  175. out << "null";
  176. break; case DATA_BOOL:
  177. if (Bool())
  178. out << "true";
  179. else
  180. out << "false";
  181. break; case DATA_FLOAT:
  182. out << Float();
  183. break; case DATA_STRING:
  184. out << "\"" << String() << "\"";
  185. break; case DATA_VECTOR:
  186. out << "[" << "\n";
  187. writeContainer(Vector().begin(), Vector().end(), out, prefix+'\t');
  188. out << prefix << "]";
  189. break; case DATA_STRUCT:
  190. out << "{" << "\n";
  191. writeContainer(Struct().begin(), Struct().end(), out, prefix+'\t');
  192. out << prefix << "}";
  193. }
  194. }
  195. std::ostream & operator<<(std::ostream &out, const JsonNode &node)
  196. {
  197. node.write(out);
  198. return out << "\n";
  199. }
  200. ////////////////////////////////////////////////////////////////////////////////
  201. JsonParser::JsonParser(const std::string inputString, JsonNode &root):
  202. input(inputString),
  203. lineCount(1),
  204. lineStart(0),
  205. pos(0)
  206. {
  207. extractValue(root);
  208. extractWhitespace(false);
  209. //Warn if there are any non-whitespace symbols left
  210. if (pos < input.size())
  211. error("Not all file was parsed!", true);
  212. //TODO: better way to show errors (like printing file name as well)
  213. tlog2<<errors;
  214. }
  215. bool JsonParser::extractSeparator()
  216. {
  217. if (!extractWhitespace())
  218. return false;
  219. if ( input[pos] !=':')
  220. return error("Separator expected");
  221. pos++;
  222. return true;
  223. }
  224. bool JsonParser::extractValue(JsonNode &node)
  225. {
  226. if (!extractWhitespace())
  227. return false;
  228. switch (input[pos])
  229. {
  230. case '\"': return extractString(node);
  231. case 'n' : return extractNull(node);
  232. case 't' : return extractTrue(node);
  233. case 'f' : return extractFalse(node);
  234. case '{' : return extractStruct(node);
  235. case '[' : return extractArray(node);
  236. case '-' : return extractFloat(node);
  237. default:
  238. {
  239. if (input[pos] >= '0' && input[pos] <= '9')
  240. return extractFloat(node);
  241. return error("Value expected!");
  242. }
  243. }
  244. }
  245. bool JsonParser::extractWhitespace(bool verbose)
  246. {
  247. while (true)
  248. {
  249. while (pos < input.size() && (unsigned char)input[pos] <= ' ')
  250. {
  251. if (input[pos] == '\n')
  252. {
  253. lineCount++;
  254. lineStart = pos+1;
  255. }
  256. pos++;
  257. }
  258. if (pos >= input.size() || input[pos] != '/')
  259. break;
  260. pos++;
  261. if (pos == input.size())
  262. break;
  263. if (input[pos] == '/')
  264. pos++;
  265. else
  266. error("Comments should have two slashes!", true);
  267. pos = input.find('\n', pos);
  268. }
  269. if (pos >= input.size() && verbose)
  270. return error("Unexpected end of file!");
  271. return true;
  272. }
  273. bool JsonParser::extractEscaping(std::string &str)
  274. {
  275. switch(input[pos++])
  276. {
  277. break; case '\"': str += '\"';
  278. break; case '\\': str += '\\';
  279. break; case '/': str += '/';
  280. break; case '\b': str += '\b';
  281. break; case '\f': str += '\f';
  282. break; case '\n': str += '\n';
  283. break; case '\r': str += '\r';
  284. break; case '\t': str += '\t';
  285. break; default: return error("Uknown escape sequence!", true);
  286. };
  287. return true;
  288. }
  289. bool JsonParser::extractString(std::string &str)
  290. {
  291. if (input[pos] != '\"')
  292. return error("String expected!");
  293. pos++;
  294. size_t first = pos;
  295. while (pos != input.size())
  296. {
  297. if (input[pos] == '\"') // Correct end of string
  298. {
  299. str += input.substr(first, pos-first);
  300. pos++;
  301. return true;
  302. }
  303. if (input[pos] == '\\') // Escaping
  304. {
  305. str += input.substr(first, pos-first);
  306. first = pos++;
  307. if (pos == input.size())
  308. break;
  309. extractEscaping(str);
  310. }
  311. if (input[pos] == '\n') // end-of-line
  312. {
  313. str += input.substr(first, pos-first);
  314. return error("Closing quote not found!", true);
  315. }
  316. if (input[pos] < ' ') // control character
  317. {
  318. str += input.substr(first, pos-first);
  319. first = pos+1;
  320. error("Illegal character in the string!", true);
  321. }
  322. pos++;
  323. }
  324. return error("Unterminated string!");
  325. }
  326. bool JsonParser::extractString(JsonNode &node)
  327. {
  328. std::string str;
  329. if (!extractString(str))
  330. return false;
  331. node.setType(JsonNode::DATA_STRING);
  332. node.String() = str;
  333. return true;
  334. }
  335. bool JsonParser::extractLiteral(const std::string &literal)
  336. {
  337. if (input.compare(pos, literal.size(), literal) != 0)
  338. {
  339. pos = input.find_first_of(" \n\r\t", pos);
  340. return error("Unknown literal found", true);
  341. }
  342. pos += literal.size();
  343. return true;
  344. }
  345. bool JsonParser::extractNull(JsonNode &node)
  346. {
  347. if (!extractLiteral("null"))
  348. return false;
  349. node.setType(JsonNode::DATA_NULL);
  350. return true;
  351. }
  352. bool JsonParser::extractTrue(JsonNode &node)
  353. {
  354. if (!extractLiteral("true"))
  355. return false;
  356. node.setType(JsonNode::DATA_BOOL);
  357. node.Bool() = true;
  358. return true;
  359. }
  360. bool JsonParser::extractFalse(JsonNode &node)
  361. {
  362. if (!extractLiteral("false"))
  363. return false;
  364. node.setType(JsonNode::DATA_BOOL);
  365. node.Bool() = false;
  366. return true;
  367. }
  368. bool JsonParser::extractStruct(JsonNode &node)
  369. {
  370. node.setType(JsonNode::DATA_STRUCT);
  371. pos++;
  372. if (!extractWhitespace())
  373. return false;
  374. //Empty struct found
  375. if (input[pos] == '}')
  376. {
  377. pos++;
  378. return true;
  379. }
  380. while (true)
  381. {
  382. if (!extractWhitespace())
  383. return false;
  384. std::string key;
  385. if (!extractString(key))
  386. return false;
  387. if (node.Struct().find(key) != node.Struct().end())
  388. error("Dublicated element encountered!", true);
  389. JsonNode &child = node.Struct()[key];
  390. if (!extractSeparator())
  391. return false;
  392. if (!extractValue(child))
  393. return false;
  394. if (!extractWhitespace())
  395. return false;
  396. bool comma = (input[pos] == ',');
  397. if (comma )
  398. {
  399. pos++;
  400. if (!extractWhitespace())
  401. return false;
  402. }
  403. if (input[pos] == '}')
  404. {
  405. pos++;
  406. return true;
  407. }
  408. if (!comma)
  409. error("Comma expected!", true);
  410. }
  411. }
  412. bool JsonParser::extractArray(JsonNode &node)
  413. {
  414. pos++;
  415. node.setType(JsonNode::DATA_VECTOR);
  416. if (!extractWhitespace())
  417. return false;
  418. //Empty array found
  419. if (input[pos] == ']')
  420. {
  421. pos++;
  422. return true;
  423. }
  424. while (true)
  425. {
  426. node.Vector().resize(node.Vector().size()+1);
  427. if (!extractValue(node.Vector().back()))
  428. return false;
  429. if (!extractWhitespace())
  430. return false;
  431. bool comma = (input[pos] == ',');
  432. if (comma )
  433. {
  434. pos++;
  435. if (!extractWhitespace())
  436. return false;
  437. }
  438. if (input[pos] == ']')
  439. {
  440. pos++;
  441. return true;
  442. }
  443. if (!comma)
  444. error("Comma expected!", true);
  445. }
  446. }
  447. bool JsonParser::extractFloat(JsonNode &node)
  448. {
  449. assert(input[pos] == '-' || (input[pos] >= '0' && input[pos] <= '9'));
  450. bool negative=false;
  451. float result=0;
  452. if (input[pos] == '-')
  453. {
  454. pos++;
  455. negative = true;
  456. }
  457. if (input[pos] < '0' || input[pos] > '9')
  458. return error("Number expected!");
  459. //Extract integer part
  460. while (input[pos] >= '0' && input[pos] <= '9')
  461. {
  462. result = result*10+(input[pos]-'0');
  463. pos++;
  464. }
  465. if (input[pos] == '.')
  466. {
  467. //extract fractional part
  468. pos++;
  469. float fractMult = 0.1;
  470. if (input[pos] < '0' || input[pos] > '9')
  471. return error("Decimal part expected!");
  472. while (input[pos] >= '0' && input[pos] <= '9')
  473. {
  474. result = result + fractMult*(input[pos]-'0');
  475. fractMult /= 10;
  476. pos++;
  477. }
  478. }
  479. //TODO: exponential part
  480. if (negative)
  481. result = -result;
  482. node.setType(JsonNode::DATA_FLOAT);
  483. node.Float() = result;
  484. return true;
  485. }
  486. bool JsonParser::error(const std::string &message, bool warning)
  487. {
  488. std::ostringstream stream;
  489. std::string type(warning?" warning: ":" error: ");
  490. stream << "At line " << lineCount << ", position "<<pos-lineStart
  491. << type << message <<"\n";
  492. errors += stream.str();
  493. return warning;
  494. }