2
0

JsonParser.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. /*
  2. * JsonParser.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 "JsonParser.h"
  12. #include "JsonFormatException.h"
  13. #include "../TextOperations.h"
  14. VCMI_LIB_NAMESPACE_BEGIN
  15. JsonParser::JsonParser(const char * inputString, size_t stringSize, const JsonParsingSettings & settings):
  16. input(inputString, stringSize),
  17. settings(settings),
  18. currentDepth(0),
  19. lineCount(1),
  20. lineStart(0),
  21. pos(0)
  22. {
  23. }
  24. JsonNode JsonParser::parse(const std::string & fileName)
  25. {
  26. JsonNode root;
  27. if (input.empty())
  28. {
  29. error("File is empty", false);
  30. }
  31. else
  32. {
  33. if (!TextOperations::isValidUnicodeString(input.data(), input.size()))
  34. error("Not a valid UTF-8 file", false);
  35. extractValue(root);
  36. extractWhitespace(false);
  37. //Warn if there are any non-whitespace symbols left
  38. if (pos < input.size())
  39. error("Not all file was parsed!", true);
  40. }
  41. if (!errors.empty())
  42. {
  43. logMod->warn("File %s is not a valid JSON file!", fileName);
  44. logMod->warn(errors);
  45. }
  46. return root;
  47. }
  48. bool JsonParser::isValid()
  49. {
  50. return errors.empty();
  51. }
  52. bool JsonParser::extractSeparator()
  53. {
  54. if (!extractWhitespace())
  55. return false;
  56. if ( input[pos] !=':')
  57. return error("Separator expected");
  58. pos++;
  59. return true;
  60. }
  61. bool JsonParser::extractValue(JsonNode &node)
  62. {
  63. if (!extractWhitespace())
  64. return false;
  65. switch (input[pos])
  66. {
  67. case '\"': return extractString(node);
  68. case 'n' : return extractNull(node);
  69. case 't' : return extractTrue(node);
  70. case 'f' : return extractFalse(node);
  71. case '{' : return extractStruct(node);
  72. case '[' : return extractArray(node);
  73. case '-' : return extractFloat(node);
  74. default:
  75. {
  76. if (input[pos] >= '0' && input[pos] <= '9')
  77. return extractFloat(node);
  78. return error("Value expected!");
  79. }
  80. }
  81. }
  82. bool JsonParser::extractWhitespace(bool verbose)
  83. {
  84. //TODO: JSON5 - C-style multi-line comments
  85. //TODO: JSON5 - Additional white space characters are allowed
  86. while (true)
  87. {
  88. while(pos < input.size() && static_cast<ui8>(input[pos]) <= ' ')
  89. {
  90. if (input[pos] == '\n')
  91. {
  92. lineCount++;
  93. lineStart = pos+1;
  94. }
  95. pos++;
  96. }
  97. if (pos >= input.size() || input[pos] != '/')
  98. break;
  99. if (settings.mode == JsonParsingSettings::JsonFormatMode::JSON)
  100. error("Comments are not permitted in json!", true);
  101. pos++;
  102. if (pos == input.size())
  103. break;
  104. if (input[pos] == '/')
  105. pos++;
  106. else
  107. error("Comments must consist of two slashes!", true);
  108. while (pos < input.size() && input[pos] != '\n')
  109. pos++;
  110. }
  111. if (pos >= input.size() && verbose)
  112. return error("Unexpected end of file!");
  113. return true;
  114. }
  115. bool JsonParser::extractEscaping(std::string &str)
  116. {
  117. // TODO: support unicode escaping:
  118. // \u1234
  119. switch(input[pos])
  120. {
  121. break; case '\"': str += '\"';
  122. break; case '\\': str += '\\';
  123. break; case 'b': str += '\b';
  124. break; case 'f': str += '\f';
  125. break; case 'n': str += '\n';
  126. break; case 'r': str += '\r';
  127. break; case 't': str += '\t';
  128. break; case '/': str += '/';
  129. break; default: return error("Unknown escape sequence!", true);
  130. }
  131. return true;
  132. }
  133. bool JsonParser::extractString(std::string &str)
  134. {
  135. //TODO: JSON5 - line breaks escaping
  136. if (settings.mode < JsonParsingSettings::JsonFormatMode::JSON5)
  137. {
  138. if (input[pos] != '\"')
  139. return error("String expected!");
  140. }
  141. else
  142. {
  143. if (input[pos] != '\"' && input[pos] != '\'')
  144. return error("String expected!");
  145. }
  146. char lineTerminator = input[pos];
  147. pos++;
  148. size_t first = pos;
  149. while (pos != input.size())
  150. {
  151. if (input[pos] == lineTerminator) // Correct end of string
  152. {
  153. str.append( &input[first], pos-first);
  154. pos++;
  155. return true;
  156. }
  157. if (input[pos] == '\\') // Escaping
  158. {
  159. str.append( &input[first], pos-first);
  160. pos++;
  161. if (pos == input.size())
  162. break;
  163. extractEscaping(str);
  164. first = pos + 1;
  165. }
  166. if (input[pos] == '\n') // end-of-line
  167. {
  168. str.append( &input[first], pos-first);
  169. return error("Closing quote not found!", true);
  170. }
  171. if(static_cast<unsigned char>(input[pos]) < ' ') // control character
  172. {
  173. str.append( &input[first], pos-first);
  174. first = pos+1;
  175. error("Illegal character in the string!", true);
  176. }
  177. pos++;
  178. }
  179. return error("Unterminated string!");
  180. }
  181. bool JsonParser::extractString(JsonNode &node)
  182. {
  183. std::string str;
  184. if (!extractString(str))
  185. return false;
  186. node.setType(JsonNode::JsonType::DATA_STRING);
  187. node.String() = str;
  188. return true;
  189. }
  190. bool JsonParser::extractLiteral(std::string & literal)
  191. {
  192. while (pos < input.size() )
  193. {
  194. bool isUpperCase = input[pos]>='A' && input[pos]<='Z';
  195. bool isLowerCase = input[pos]>='a' && input[pos]<='z';
  196. bool isNumber = input[pos]>='0' && input[pos]<='9';
  197. if (!isUpperCase && !isLowerCase && !isNumber)
  198. break;
  199. literal += input[pos];
  200. pos++;
  201. }
  202. pos += literal.size();
  203. return true;
  204. }
  205. bool JsonParser::extractAndCompareLiteral(const std::string &expectedLiteral)
  206. {
  207. std::string literal;
  208. if (!extractLiteral(literal))
  209. return false;
  210. if (literal != expectedLiteral)
  211. {
  212. return error("Expected " + expectedLiteral + ", but unknown literal found", true);
  213. return false;
  214. }
  215. return true;
  216. }
  217. bool JsonParser::extractNull(JsonNode &node)
  218. {
  219. if (!extractAndCompareLiteral("null"))
  220. return false;
  221. node.clear();
  222. return true;
  223. }
  224. bool JsonParser::extractTrue(JsonNode &node)
  225. {
  226. if (!extractAndCompareLiteral("true"))
  227. return false;
  228. node.Bool() = true;
  229. return true;
  230. }
  231. bool JsonParser::extractFalse(JsonNode &node)
  232. {
  233. if (!extractAndCompareLiteral("false"))
  234. return false;
  235. node.Bool() = false;
  236. return true;
  237. }
  238. bool JsonParser::extractStruct(JsonNode &node)
  239. {
  240. node.setType(JsonNode::JsonType::DATA_STRUCT);
  241. if (currentDepth > settings.maxDepth)
  242. error("Macimum allowed depth of json structure has been reached", true);
  243. currentDepth++;
  244. pos++;
  245. if (!extractWhitespace())
  246. return false;
  247. //Empty struct found
  248. if (input[pos] == '}')
  249. {
  250. pos++;
  251. return true;
  252. }
  253. while (true)
  254. {
  255. if (!extractWhitespace())
  256. return false;
  257. bool overrideFlag = false;
  258. std::string key;
  259. if (settings.mode < JsonParsingSettings::JsonFormatMode::JSON5)
  260. {
  261. if (!extractString(key))
  262. return false;
  263. }
  264. else
  265. {
  266. if (input[pos] == '\'' || input[pos] == '\"')
  267. {
  268. if (!extractString(key))
  269. return false;
  270. }
  271. else
  272. {
  273. if (!extractLiteral(key))
  274. return false;
  275. }
  276. }
  277. if (key.find('#') != std::string::npos)
  278. {
  279. // split key string into actual key and meta-flags
  280. std::vector<std::string> keyAndFlags;
  281. boost::split(keyAndFlags, key, boost::is_any_of("#"));
  282. key = keyAndFlags[0];
  283. for(int i = 1; i < keyAndFlags.size(); i++)
  284. {
  285. if (keyAndFlags[i] == "override")
  286. overrideFlag = true;
  287. else
  288. error("Encountered unknown flag #" + keyAndFlags[i], true);
  289. }
  290. }
  291. if (node.Struct().find(key) != node.Struct().end())
  292. error("Duplicate element encountered!", true);
  293. if (!extractSeparator())
  294. return false;
  295. if (!extractElement(node.Struct()[key], '}'))
  296. return false;
  297. node.Struct()[key].setOverrideFlag(overrideFlag);
  298. if (input[pos] == '}')
  299. {
  300. pos++;
  301. return true;
  302. }
  303. }
  304. }
  305. bool JsonParser::extractArray(JsonNode &node)
  306. {
  307. if (currentDepth > settings.maxDepth)
  308. error("Macimum allowed depth of json structure has been reached", true);
  309. currentDepth++;
  310. pos++;
  311. node.setType(JsonNode::JsonType::DATA_VECTOR);
  312. if (!extractWhitespace())
  313. return false;
  314. //Empty array found
  315. if (input[pos] == ']')
  316. {
  317. pos++;
  318. return true;
  319. }
  320. while (true)
  321. {
  322. //NOTE: currently 50% of time is this vector resizing.
  323. //May be useful to use list during parsing and then swap() all items to vector
  324. node.Vector().resize(node.Vector().size()+1);
  325. if (!extractElement(node.Vector().back(), ']'))
  326. return false;
  327. if (input[pos] == ']')
  328. {
  329. pos++;
  330. return true;
  331. }
  332. }
  333. }
  334. bool JsonParser::extractElement(JsonNode &node, char terminator)
  335. {
  336. if (!extractValue(node))
  337. return false;
  338. if (!extractWhitespace())
  339. return false;
  340. bool comma = (input[pos] == ',');
  341. if (comma )
  342. {
  343. pos++;
  344. if (!extractWhitespace())
  345. return false;
  346. }
  347. if (input[pos] == terminator)
  348. {
  349. if (comma)
  350. {
  351. if (settings.mode < JsonParsingSettings::JsonFormatMode::JSON5)
  352. error("Extra comma found!", true);
  353. }
  354. return true;
  355. }
  356. if (!comma)
  357. error("Comma expected!", true);
  358. return true;
  359. }
  360. bool JsonParser::extractFloat(JsonNode &node)
  361. {
  362. //TODO: JSON5 - hexacedimal support
  363. //TODO: JSON5 - Numbers may be IEEE 754 positive infinity, negative infinity, and NaN (why?)
  364. assert(input[pos] == '-' || (input[pos] >= '0' && input[pos] <= '9'));
  365. bool negative=false;
  366. double result=0;
  367. si64 integerPart = 0;
  368. bool isFloat = false;
  369. if (input[pos] == '+')
  370. {
  371. if (settings.mode < JsonParsingSettings::JsonFormatMode::JSON5)
  372. error("Positive numbers should not have plus sign!", true);
  373. pos++;
  374. }
  375. else if (input[pos] == '-')
  376. {
  377. pos++;
  378. negative = true;
  379. }
  380. if (input[pos] < '0' || input[pos] > '9')
  381. {
  382. if (input[pos] != '.' && settings.mode < JsonParsingSettings::JsonFormatMode::JSON5)
  383. return error("Number expected!");
  384. }
  385. //Extract integer part
  386. while (input[pos] >= '0' && input[pos] <= '9')
  387. {
  388. integerPart = integerPart*10+(input[pos]-'0');
  389. pos++;
  390. }
  391. result = static_cast<double>(integerPart);
  392. if (input[pos] == '.')
  393. {
  394. //extract fractional part
  395. isFloat = true;
  396. pos++;
  397. double fractMult = 0.1;
  398. if (settings.mode < JsonParsingSettings::JsonFormatMode::JSON5)
  399. {
  400. if (input[pos] < '0' || input[pos] > '9')
  401. return error("Decimal part expected!");
  402. }
  403. while (input[pos] >= '0' && input[pos] <= '9')
  404. {
  405. result = result + fractMult*(input[pos]-'0');
  406. fractMult /= 10;
  407. pos++;
  408. }
  409. }
  410. if(input[pos] == 'e')
  411. {
  412. //extract exponential part
  413. pos++;
  414. isFloat = true;
  415. bool powerNegative = false;
  416. double power = 0;
  417. if(input[pos] == '-')
  418. {
  419. pos++;
  420. powerNegative = true;
  421. }
  422. else if(input[pos] == '+')
  423. {
  424. pos++;
  425. }
  426. if (input[pos] < '0' || input[pos] > '9')
  427. return error("Exponential part expected!");
  428. while (input[pos] >= '0' && input[pos] <= '9')
  429. {
  430. power = power*10 + (input[pos]-'0');
  431. pos++;
  432. }
  433. if(powerNegative)
  434. power = -power;
  435. result *= std::pow(10, power);
  436. }
  437. if(isFloat)
  438. {
  439. if(negative)
  440. result = -result;
  441. node.setType(JsonNode::JsonType::DATA_FLOAT);
  442. node.Float() = result;
  443. }
  444. else
  445. {
  446. if(negative)
  447. integerPart = -integerPart;
  448. node.setType(JsonNode::JsonType::DATA_INTEGER);
  449. node.Integer() = integerPart;
  450. }
  451. return true;
  452. }
  453. bool JsonParser::error(const std::string &message, bool warning)
  454. {
  455. if (settings.strict)
  456. throw JsonFormatException(message);
  457. std::ostringstream stream;
  458. std::string type(warning?" warning: ":" error: ");
  459. stream << "At line " << lineCount << ", position "<<pos-lineStart
  460. << type << message <<"\n";
  461. errors += stream.str();
  462. return warning;
  463. }
  464. VCMI_LIB_NAMESPACE_END