JSON.cpp 7.7 KB

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