JSONParser.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include "JSONParser.h"
  24. #include "TempAllocator.h"
  25. #include "StringUtils.h"
  26. namespace crown
  27. {
  28. //-----------------------------------------------------------------------------
  29. static const char* next(const char* str, const char c = 0)
  30. {
  31. CE_ASSERT_NOT_NULL(str);
  32. if (c && c != (*str))
  33. {
  34. CE_ASSERT(false, "Expected '%c' got '%c'", c, (*str));
  35. }
  36. return str + 1;
  37. }
  38. //-----------------------------------------------------------------------------
  39. static const char* skip_whites(const char* s)
  40. {
  41. CE_ASSERT_NOT_NULL(s);
  42. const char* ch = s;
  43. while ((*ch) && (*ch) <= ' ') ch = next(ch);
  44. return ch;
  45. }
  46. //-----------------------------------------------------------------------------
  47. static const char* skip_string(const char* s)
  48. {
  49. CE_ASSERT_NOT_NULL(s);
  50. const char* ch = s;
  51. bool escaped = false;
  52. if ((*ch) == '"')
  53. {
  54. while ((*(ch = next(ch))) != 0)
  55. {
  56. if ((*ch) == '"' && !escaped)
  57. {
  58. ch = next(ch);
  59. return ch;
  60. }
  61. else if ((*ch) == '\\') escaped = true;
  62. else escaped = false;
  63. }
  64. }
  65. return ch;
  66. }
  67. //-----------------------------------------------------------------------------
  68. static const char* skip_number(const char* s)
  69. {
  70. CE_ASSERT_NOT_NULL(s);
  71. const char* ch = s;
  72. while ((*ch) && (((*ch) >= '0' && (*ch) <= '9') ||
  73. (*ch) == '-' || (*ch) == '.' || (*ch) == '+' ||
  74. (*ch) == 'e' || (*ch) == 'E'))
  75. {
  76. ch = next(ch);
  77. }
  78. return ch;
  79. }
  80. //-----------------------------------------------------------------------------
  81. static const char* skip_object(const char* s)
  82. {
  83. CE_ASSERT_NOT_NULL(s);
  84. const char* ch = s;
  85. uint32_t brackets = 1;
  86. if ((*ch) == '{')
  87. {
  88. brackets++;
  89. ch = next(ch, '{');
  90. while ((*ch) && brackets != 1)
  91. {
  92. if ((*ch) == '}') brackets--;
  93. else if ((*ch) == '{') brackets++;
  94. ch = next(ch);
  95. }
  96. }
  97. return ch;
  98. }
  99. //-----------------------------------------------------------------------------
  100. static const char* skip_array(const char* s)
  101. {
  102. CE_ASSERT_NOT_NULL(s);
  103. const char* ch = s;
  104. uint32_t brackets = 1;
  105. if ((*ch) == '[')
  106. {
  107. brackets++;
  108. ch = next(ch, '[');
  109. while ((*ch) && brackets != 1)
  110. {
  111. if ((*ch) == ']') brackets--;
  112. else if ((*ch) == '[') brackets++;
  113. ch = next(ch);
  114. }
  115. }
  116. return ch;
  117. }
  118. //-----------------------------------------------------------------------------
  119. static const char* skip_bool(const char* s)
  120. {
  121. CE_ASSERT_NOT_NULL(s);
  122. const char* ch = s;
  123. switch ((*ch))
  124. {
  125. case 't':
  126. {
  127. ch = next(ch, 't');
  128. ch = next(ch, 'r');
  129. ch = next(ch, 'u');
  130. ch = next(ch, 'e');
  131. break;
  132. }
  133. case 'f':
  134. {
  135. ch = next(ch, 'f');
  136. ch = next(ch, 'a');
  137. ch = next(ch, 'l');
  138. ch = next(ch, 's');
  139. ch = next(ch, 'e');
  140. break;
  141. }
  142. default:
  143. {
  144. break;
  145. }
  146. }
  147. return ch;
  148. }
  149. //-----------------------------------------------------------------------------
  150. static bool is_escapee(char c)
  151. {
  152. return c == '"' || c == '\\' || c == '/' || c == '\b' || c == '\f' || c == '\n' ||
  153. c == '\r' || c == '\t';
  154. }
  155. //--------------------------------------------------------------------------
  156. JSONElement::JSONElement() :
  157. m_parser(NULL),
  158. m_at(NULL)
  159. {
  160. }
  161. //--------------------------------------------------------------------------
  162. JSONElement::JSONElement(JSONParser& parser, const char* at) :
  163. m_parser(&parser),
  164. m_at(at)
  165. {
  166. }
  167. //--------------------------------------------------------------------------
  168. JSONElement& JSONElement::operator[](uint32_t i)
  169. {
  170. TempAllocator1024 alloc;
  171. List<const char*> array(alloc);
  172. JSONParser::parse_array(m_at, array);
  173. CE_ASSERT(i < array.size(), "Index out of bounds");
  174. m_at = array[i];
  175. return *this;
  176. }
  177. //--------------------------------------------------------------------------
  178. JSONElement& JSONElement::index(uint32_t i)
  179. {
  180. return this->operator[](i);
  181. }
  182. //--------------------------------------------------------------------------
  183. JSONElement& JSONElement::key(const char* k)
  184. {
  185. TempAllocator1024 alloc;
  186. List<JSONPair> object(alloc);
  187. JSONParser::parse_object(m_at, object);
  188. bool found = false;
  189. for (uint32_t i = 0; i < object.size(); i++)
  190. {
  191. TempAllocator256 key_alloc;
  192. List<char> key(key_alloc);
  193. JSONParser::parse_string(object[i].key, key);
  194. if (string::strcmp(k, key.begin()) == 0)
  195. {
  196. m_at = object[i].val;
  197. found = true;
  198. }
  199. }
  200. CE_ASSERT(found, "Key not found: '%s'", k);
  201. return *this;
  202. }
  203. //--------------------------------------------------------------------------
  204. bool JSONElement::has_key(const char* k) const
  205. {
  206. TempAllocator1024 alloc;
  207. List<JSONPair> object(alloc);
  208. JSONParser::parse_object(m_at, object);
  209. for (uint32_t i = 0; i < object.size(); i++)
  210. {
  211. if (string::strcmp(k, object[i].key) == 0)
  212. {
  213. return true;
  214. }
  215. }
  216. return false;
  217. }
  218. //--------------------------------------------------------------------------
  219. bool JSONElement::bool_value() const
  220. {
  221. return JSONParser::parse_bool(m_at);
  222. }
  223. //--------------------------------------------------------------------------
  224. int32_t JSONElement::int_value() const
  225. {
  226. return JSONParser::parse_int(m_at);
  227. }
  228. //--------------------------------------------------------------------------
  229. float JSONElement::float_value() const
  230. {
  231. return JSONParser::parse_float(m_at);
  232. }
  233. //--------------------------------------------------------------------------
  234. const char* JSONElement::string_value() const
  235. {
  236. static TempAllocator1024 alloc;
  237. static List<char> string(alloc);
  238. string.clear();
  239. JSONParser::parse_string(m_at, string);
  240. return string.begin();
  241. }
  242. //--------------------------------------------------------------------------
  243. bool JSONElement::is_nil() const
  244. {
  245. return JSONParser::type(m_at) == JT_NIL;
  246. }
  247. //--------------------------------------------------------------------------
  248. bool JSONElement::is_bool() const
  249. {
  250. return JSONParser::type(m_at) == JT_BOOL;
  251. }
  252. //--------------------------------------------------------------------------
  253. bool JSONElement::is_number() const
  254. {
  255. return JSONParser::type(m_at) == JT_NUMBER;
  256. }
  257. //--------------------------------------------------------------------------
  258. bool JSONElement::is_string() const
  259. {
  260. return JSONParser::type(m_at) == JT_STRING;
  261. }
  262. //--------------------------------------------------------------------------
  263. bool JSONElement::is_array() const
  264. {
  265. return JSONParser::type(m_at) == JT_ARRAY;
  266. }
  267. //--------------------------------------------------------------------------
  268. bool JSONElement::is_object() const
  269. {
  270. return JSONParser::type(m_at) == JT_OBJECT;
  271. }
  272. //--------------------------------------------------------------------------
  273. uint32_t JSONElement::size() const
  274. {
  275. switch(JSONParser::type(m_at))
  276. {
  277. case JT_NIL:
  278. {
  279. return 1;
  280. }
  281. case JT_OBJECT:
  282. {
  283. TempAllocator1024 alloc;
  284. List<JSONPair> object(alloc);
  285. JSONParser::parse_object(m_at, object);
  286. return object.size();
  287. }
  288. case JT_ARRAY:
  289. {
  290. TempAllocator1024 alloc;
  291. List<const char*> array(alloc);
  292. JSONParser::parse_array(m_at, array);
  293. return array.size();
  294. }
  295. case JT_STRING:
  296. {
  297. TempAllocator1024 alloc;
  298. List<char> string(alloc);
  299. JSONParser::parse_string(m_at, string);
  300. return string::strlen(string.begin());
  301. }
  302. case JT_NUMBER:
  303. {
  304. return 1;
  305. }
  306. case JT_BOOL:
  307. {
  308. return 1;
  309. }
  310. default:
  311. {
  312. return 0;
  313. }
  314. }
  315. }
  316. //--------------------------------------------------------------------------
  317. JSONParser::JSONParser(const char* s) :
  318. m_document(s)
  319. {
  320. CE_ASSERT_NOT_NULL(s);
  321. }
  322. //--------------------------------------------------------------------------
  323. JSONElement JSONParser::root()
  324. {
  325. return JSONElement(*this, skip_whites(m_document));
  326. }
  327. //-----------------------------------------------------------------------------
  328. JSONType JSONParser::type(const char* s)
  329. {
  330. CE_ASSERT_NOT_NULL(s);
  331. switch (s[0])
  332. {
  333. case '{': return JT_OBJECT;
  334. case '[': return JT_ARRAY;
  335. case '"': return JT_STRING;
  336. case '-': return JT_NUMBER;
  337. default: return s[0] >= '0' && s[0] <= '9' ? JT_NUMBER : (s[0] == 'n' ? JT_NIL : JT_BOOL);
  338. }
  339. }
  340. //-----------------------------------------------------------------------------
  341. void JSONParser::parse_string(const char* s, List<char>& str)
  342. {
  343. CE_ASSERT_NOT_NULL(s);
  344. const char* ch = s;
  345. if ((*ch) == '"')
  346. {
  347. while ((*(ch = next(ch))))
  348. {
  349. // Empty string
  350. if ((*ch) == '"')
  351. {
  352. ch = next(ch);
  353. str.push_back('\0');
  354. return;
  355. }
  356. else if ((*ch) == '\\')
  357. {
  358. ch = next(ch);
  359. if ((*ch) == 'u')
  360. {
  361. CE_ASSERT(false, "Not supported at the moment");
  362. }
  363. else if (is_escapee(*ch))
  364. {
  365. str.push_back('\\');
  366. str.push_back(*ch);
  367. }
  368. else
  369. {
  370. // Go to invalid string
  371. break;
  372. }
  373. }
  374. else
  375. {
  376. str.push_back(*ch);
  377. }
  378. }
  379. }
  380. CE_ASSERT(false, "Bad string");
  381. }
  382. //-----------------------------------------------------------------------------
  383. double JSONParser::parse_number(const char* s)
  384. {
  385. CE_ASSERT_NOT_NULL(s);
  386. const char* ch = s;
  387. TempAllocator1024 allocator;
  388. List<char> str(allocator);
  389. if ((*ch) == '-')
  390. {
  391. str.push_back('-');
  392. ch = next(ch, '-');
  393. }
  394. while ((*ch) >= '0' && (*ch) <= '9')
  395. {
  396. str.push_back((*ch));
  397. ch = next(ch);
  398. }
  399. if ((*ch) == '.')
  400. {
  401. str.push_back('.');
  402. while ((*(ch = next(ch))) && (*ch) >= '0' && (*ch) <= '9')
  403. {
  404. str.push_back(*ch);
  405. }
  406. }
  407. if ((*ch) == 'e' || (*ch) == 'E')
  408. {
  409. str.push_back(*ch);
  410. ch = next(ch);
  411. if ((*ch) == '-' || (*ch) == '+')
  412. {
  413. str.push_back(*ch);
  414. ch = next(ch);
  415. }
  416. while ((*ch) >= '0' && (*ch) <= '9')
  417. {
  418. str.push_back(*ch);
  419. ch = next(ch);
  420. }
  421. }
  422. // Ensure null terminated
  423. str.push_back('\0');
  424. return string::parse_double(str.begin());
  425. }
  426. //-----------------------------------------------------------------------------
  427. bool JSONParser::parse_bool(const char* s)
  428. {
  429. CE_ASSERT_NOT_NULL(s);
  430. const char* ch = s;
  431. switch(*ch)
  432. {
  433. case 't':
  434. {
  435. ch = next(ch, 't');
  436. ch = next(ch, 'r');
  437. ch = next(ch, 'u');
  438. ch = next(ch, 'e');
  439. return true;
  440. }
  441. case 'f':
  442. {
  443. ch = next(ch, 'f');
  444. ch = next(ch, 'a');
  445. ch = next(ch, 'l');
  446. ch = next(ch, 's');
  447. ch = next(ch, 'e');
  448. return false;
  449. }
  450. default: break;
  451. }
  452. CE_ASSERT(false, "Bad boolean");
  453. }
  454. //-----------------------------------------------------------------------------
  455. int32_t JSONParser::parse_int(const char* s)
  456. {
  457. CE_ASSERT_NOT_NULL(s);
  458. return (int32_t) parse_number(s);
  459. }
  460. //-----------------------------------------------------------------------------
  461. float JSONParser::parse_float(const char* s)
  462. {
  463. CE_ASSERT_NOT_NULL(s);
  464. return (float) parse_number(s);
  465. }
  466. //-----------------------------------------------------------------------------
  467. void JSONParser::parse_array(const char* s, List<const char*>& array)
  468. {
  469. CE_ASSERT_NOT_NULL(s);
  470. const char* ch = s;
  471. if ((*ch) == '[')
  472. {
  473. ch = next(ch, '[');
  474. // Skip whitespaces
  475. while ((*ch) && (*ch) <= ' ')
  476. {
  477. ch = next(ch);
  478. }
  479. if ((*ch) == ']')
  480. {
  481. ch = next(ch, ']');
  482. return;
  483. }
  484. while (*ch)
  485. {
  486. array.push_back(ch);
  487. ch = skip_array(ch);
  488. ch = skip_object(ch);
  489. ch = skip_number(ch);
  490. ch = skip_string(ch);
  491. ch = skip_bool(ch);
  492. ch = skip_whites(ch);
  493. // Closing bracket (top-most array)
  494. if ((*ch) == ']')
  495. {
  496. ch = next(ch, ']');
  497. return;
  498. }
  499. // Skip until next ','
  500. ch = next(ch, ',');
  501. // Skip whites, eventually
  502. ch = skip_whites(ch);
  503. }
  504. }
  505. CE_ASSERT(false, "Bad array");
  506. }
  507. //-----------------------------------------------------------------------------
  508. void JSONParser::parse_object(const char* s, List<JSONPair>& object)
  509. {
  510. CE_ASSERT_NOT_NULL(s);
  511. const char* ch = s;
  512. if ((*ch) == '{')
  513. {
  514. ch = next(ch, '{');
  515. ch = skip_whites(ch);
  516. if ((*ch) == '}')
  517. {
  518. next(ch, '}');
  519. return;
  520. }
  521. while (*ch)
  522. {
  523. JSONPair pair;
  524. pair.key = ch;
  525. // Skip any value
  526. ch = skip_array(ch);
  527. ch = skip_object(ch);
  528. ch = skip_number(ch);
  529. ch = skip_string(ch);
  530. ch = skip_bool(ch);
  531. ch = skip_whites(ch);
  532. ch = next(ch, ':');
  533. ch = skip_whites(ch);
  534. pair.val = ch;
  535. object.push_back(pair);
  536. // Skip any value
  537. ch = skip_array(ch);
  538. ch = skip_object(ch);
  539. ch = skip_number(ch);
  540. ch = skip_string(ch);
  541. ch = skip_bool(ch);
  542. ch = skip_whites(ch);
  543. if ((*ch) == '}')
  544. {
  545. next(ch, '}');
  546. return;
  547. }
  548. ch = next(ch, ',');
  549. ch = skip_whites(ch);
  550. }
  551. }
  552. CE_ASSERT(false, "Bad object");
  553. }
  554. } //namespace crown