json.cpp 7.2 KB

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