JSON.cpp 8.6 KB

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