json.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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 "json.h"
  24. #include "container_types.h"
  25. #include "string_utils.h"
  26. #include "dynamic_string.h"
  27. #include "map.h"
  28. namespace crown
  29. {
  30. namespace json
  31. {
  32. static const char* next(const char* str, const char c = 0)
  33. {
  34. CE_ASSERT_NOT_NULL(str);
  35. if (c && c != (*str))
  36. {
  37. CE_ASSERT(false, "Expected '%c' got '%c'", c, (*str));
  38. }
  39. return str + 1;
  40. }
  41. static const char* skip_whites(const char* s)
  42. {
  43. CE_ASSERT_NOT_NULL(s);
  44. const char* ch = s;
  45. while ((*ch) && (*ch) <= ' ') ch = next(ch);
  46. return ch;
  47. }
  48. static const char* skip_string(const char* s)
  49. {
  50. CE_ASSERT_NOT_NULL(s);
  51. const char* ch = s;
  52. bool escaped = false;
  53. if ((*ch) == '"')
  54. {
  55. while ((*(ch = next(ch))) != 0)
  56. {
  57. if ((*ch) == '"' && !escaped)
  58. {
  59. ch = next(ch);
  60. return ch;
  61. }
  62. else if ((*ch) == '\\') escaped = true;
  63. else escaped = false;
  64. }
  65. }
  66. return ch;
  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. static const char* skip_object(const char* s)
  81. {
  82. CE_ASSERT_NOT_NULL(s);
  83. const char* ch = s;
  84. uint32_t brackets = 1;
  85. if ((*ch) == '{')
  86. {
  87. brackets++;
  88. ch = next(ch, '{');
  89. while ((*ch) && brackets != 1)
  90. {
  91. if ((*ch) == '}') brackets--;
  92. else if ((*ch) == '{') brackets++;
  93. ch = next(ch);
  94. }
  95. }
  96. return ch;
  97. }
  98. static const char* skip_array(const char* s)
  99. {
  100. CE_ASSERT_NOT_NULL(s);
  101. const char* ch = s;
  102. uint32_t brackets = 1;
  103. if ((*ch) == '[')
  104. {
  105. brackets++;
  106. ch = next(ch, '[');
  107. while ((*ch) && brackets != 1)
  108. {
  109. if ((*ch) == ']') brackets--;
  110. else if ((*ch) == '[') brackets++;
  111. ch = next(ch);
  112. }
  113. }
  114. return ch;
  115. }
  116. static const char* skip_bool(const char* s)
  117. {
  118. CE_ASSERT_NOT_NULL(s);
  119. const char* ch = s;
  120. switch ((*ch))
  121. {
  122. case 't':
  123. {
  124. ch = next(ch, 't');
  125. ch = next(ch, 'r');
  126. ch = next(ch, 'u');
  127. ch = next(ch, 'e');
  128. break;
  129. }
  130. case 'f':
  131. {
  132. ch = next(ch, 'f');
  133. ch = next(ch, 'a');
  134. ch = next(ch, 'l');
  135. ch = next(ch, 's');
  136. ch = next(ch, 'e');
  137. break;
  138. }
  139. default:
  140. {
  141. break;
  142. }
  143. }
  144. return ch;
  145. }
  146. static const char* skip_null(const char* s)
  147. {
  148. CE_ASSERT_NOT_NULL(s);
  149. const char* ch = s;
  150. if ((*ch) == 'n')
  151. {
  152. ch = next(ch, 'n');
  153. ch = next(ch, 'u');
  154. ch = next(ch, 'l');
  155. ch = next(ch, 'l');
  156. }
  157. return ch;
  158. }
  159. static const char* skip_value(const char* s)
  160. {
  161. CE_ASSERT_NOT_NULL(s);
  162. const char* ch = s;
  163. ch = skip_array(ch);
  164. ch = skip_object(ch);
  165. ch = skip_number(ch);
  166. ch = skip_string(ch);
  167. ch = skip_bool(ch);
  168. ch = skip_null(ch);
  169. return ch;
  170. }
  171. static bool is_escapee(char c)
  172. {
  173. return c == '"' || c == '\\' || c == '/' || c == '\b' || c == '\f' || c == '\n' ||
  174. c == '\r' || c == '\t';
  175. }
  176. JSONType::Enum type(const char* s)
  177. {
  178. CE_ASSERT_NOT_NULL(s);
  179. const char c = s[0];
  180. switch (c)
  181. {
  182. case '{': return JSONType::OBJECT;
  183. case '[': return JSONType::ARRAY;
  184. case '"': return JSONType::STRING;
  185. case '-': return JSONType::NUMBER;
  186. default: return (c >= '0' && c <= '9') ? JSONType::NUMBER : (c == 'n' ? JSONType::NIL : JSONType::BOOL);
  187. }
  188. }
  189. void parse_string(const char* s, DynamicString& str)
  190. {
  191. CE_ASSERT_NOT_NULL(s);
  192. const char* ch = s;
  193. if ((*ch) == '"')
  194. {
  195. while ((*(ch = next(ch))))
  196. {
  197. // Empty string
  198. if ((*ch) == '"')
  199. {
  200. ch = next(ch);
  201. return;
  202. }
  203. else if ((*ch) == '\\')
  204. {
  205. ch = next(ch);
  206. if ((*ch) == 'u')
  207. {
  208. CE_FATAL("Not supported at the moment");
  209. }
  210. else if (is_escapee(*ch))
  211. {
  212. str += (*ch);
  213. }
  214. else
  215. {
  216. // Go to invalid string
  217. break;
  218. }
  219. }
  220. else
  221. {
  222. str += (*ch);
  223. }
  224. }
  225. }
  226. CE_FATAL("Bad string");
  227. }
  228. double parse_number(const char* s)
  229. {
  230. CE_ASSERT_NOT_NULL(s);
  231. const char* ch = s;
  232. Array<char> str(default_allocator());
  233. if ((*ch) == '-')
  234. {
  235. array::push_back(str, '-');
  236. ch = next(ch, '-');
  237. }
  238. while ((*ch) >= '0' && (*ch) <= '9')
  239. {
  240. array::push_back(str, (*ch));
  241. ch = next(ch);
  242. }
  243. if ((*ch) == '.')
  244. {
  245. array::push_back(str, '.');
  246. while ((*(ch = next(ch))) && (*ch) >= '0' && (*ch) <= '9')
  247. {
  248. array::push_back(str, *ch);
  249. }
  250. }
  251. if ((*ch) == 'e' || (*ch) == 'E')
  252. {
  253. array::push_back(str, *ch);
  254. ch = next(ch);
  255. if ((*ch) == '-' || (*ch) == '+')
  256. {
  257. array::push_back(str, *ch);
  258. ch = next(ch);
  259. }
  260. while ((*ch) >= '0' && (*ch) <= '9')
  261. {
  262. array::push_back(str, *ch);
  263. ch = next(ch);
  264. }
  265. }
  266. // Ensure null terminated
  267. array::push_back(str, '\0');
  268. return string::parse_double(array::begin(str));
  269. }
  270. bool parse_bool(const char* s)
  271. {
  272. CE_ASSERT_NOT_NULL(s);
  273. const char* ch = s;
  274. switch(*ch)
  275. {
  276. case 't':
  277. {
  278. ch = next(ch, 't');
  279. ch = next(ch, 'r');
  280. ch = next(ch, 'u');
  281. ch = next(ch, 'e');
  282. return true;
  283. }
  284. case 'f':
  285. {
  286. ch = next(ch, 'f');
  287. ch = next(ch, 'a');
  288. ch = next(ch, 'l');
  289. ch = next(ch, 's');
  290. ch = next(ch, 'e');
  291. return false;
  292. }
  293. default:
  294. {
  295. CE_FATAL("Bad boolean");
  296. return false;
  297. }
  298. }
  299. }
  300. int32_t parse_int(const char* s)
  301. {
  302. CE_ASSERT_NOT_NULL(s);
  303. return (int32_t) parse_number(s);
  304. }
  305. float parse_float(const char* s)
  306. {
  307. CE_ASSERT_NOT_NULL(s);
  308. return (float) parse_number(s);
  309. }
  310. void parse_array(const char* s, Array<const char*>& array)
  311. {
  312. CE_ASSERT_NOT_NULL(s);
  313. const char* ch = s;
  314. if ((*ch) == '[')
  315. {
  316. ch = next(ch, '[');
  317. // Skip whitespaces
  318. while ((*ch) && (*ch) <= ' ')
  319. {
  320. ch = next(ch);
  321. }
  322. if ((*ch) == ']')
  323. {
  324. ch = next(ch, ']');
  325. return;
  326. }
  327. while (*ch)
  328. {
  329. array::push_back(array, ch);
  330. ch = skip_value(ch);
  331. ch = skip_whites(ch);
  332. // Closing bracket (top-most array)
  333. if ((*ch) == ']')
  334. {
  335. ch = next(ch, ']');
  336. return;
  337. }
  338. // Skip until next ','
  339. ch = next(ch, ',');
  340. // Skip whites, eventually
  341. ch = skip_whites(ch);
  342. }
  343. }
  344. CE_FATAL("Bad array");
  345. }
  346. void parse_object(const char* s, Map<DynamicString, const char*>& object)
  347. {
  348. CE_ASSERT_NOT_NULL(s);
  349. const char* ch = s;
  350. if ((*ch) == '{')
  351. {
  352. ch = next(ch, '{');
  353. ch = skip_whites(ch);
  354. if ((*ch) == '}')
  355. {
  356. next(ch, '}');
  357. return;
  358. }
  359. while (*ch)
  360. {
  361. DynamicString key;
  362. parse_string(ch, key);
  363. ch = skip_string(ch);
  364. ch = skip_whites(ch);
  365. ch = next(ch, ':');
  366. ch = skip_whites(ch);
  367. map::set(object, key, ch);
  368. ch = skip_value(ch);
  369. ch = skip_whites(ch);
  370. if ((*ch) == '}')
  371. {
  372. next(ch, '}');
  373. return;
  374. }
  375. ch = next(ch, ',');
  376. ch = skip_whites(ch);
  377. }
  378. }
  379. CE_FATAL("Bad object");
  380. }
  381. } // namespace json
  382. } // namespace crown