njson.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /*
  2. * Copyright (c) 2012-2015 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/taylor001/crown/blob/master/LICENSE
  4. */
  5. #include "njson.h"
  6. #include "string_utils.h"
  7. #include "temp_allocator.h"
  8. #include "map.h"
  9. namespace crown
  10. {
  11. namespace njson
  12. {
  13. static const char* next(const char* json, const char c = 0)
  14. {
  15. CE_ASSERT_NOT_NULL(json);
  16. if (c && c != *json)
  17. {
  18. CE_ASSERT(false, "Expected '%c' got '%c'", c, *json);
  19. }
  20. return ++json;
  21. }
  22. static const char* skip_string(const char* json)
  23. {
  24. CE_ASSERT_NOT_NULL(json);
  25. bool escaped = false;
  26. while ((*(json = next(json))) != 0)
  27. {
  28. if (*json == '"' && !escaped)
  29. {
  30. json = next(json);
  31. return json;
  32. }
  33. else if (*json == '\\') escaped = true;
  34. else escaped = false;
  35. }
  36. return json;
  37. }
  38. static const char* skip_value(const char* json)
  39. {
  40. CE_ASSERT_NOT_NULL(json);
  41. switch (*json)
  42. {
  43. case '"': json = skip_string(json); break;
  44. case '[': json = skip_block(json, '[', ']'); break;
  45. case '{': json = skip_block(json, '{', '}'); break;
  46. default: for (; *json != ',' && *json != '\n' && *json != ' ' && *json != '}' && *json != ']'; ++json) ; break;
  47. }
  48. return json;
  49. }
  50. static const char* skip_comments(const char* json)
  51. {
  52. CE_ASSERT_NOT_NULL(json);
  53. if (*json == '/')
  54. {
  55. json = next(json, '/');
  56. json = next(json, '/');
  57. while (*json && *json != '\n') json = next(json);
  58. }
  59. return json;
  60. }
  61. static const char* skip_spaces(const char* json)
  62. {
  63. CE_ASSERT_NOT_NULL(json);
  64. while (*json)
  65. {
  66. if (*json == '/') json = skip_comments(json);
  67. else if (isspace(*json) || *json == ',') ++json;
  68. else break;
  69. }
  70. return json;
  71. }
  72. NJSONValueType::Enum type(const char* json)
  73. {
  74. CE_ASSERT_NOT_NULL(json);
  75. switch (*json)
  76. {
  77. case '"': return NJSONValueType::STRING;
  78. case '{': return NJSONValueType::OBJECT;
  79. case '[': return NJSONValueType::ARRAY;
  80. case '-': return NJSONValueType::NUMBER;
  81. default: return (isdigit(*json)) ? NJSONValueType::NUMBER : (*json == 'n' ? NJSONValueType::NIL : NJSONValueType::BOOL);
  82. }
  83. }
  84. void parse_string(const char* json, DynamicString& string)
  85. {
  86. CE_ASSERT_NOT_NULL(json);
  87. if (*json == '"')
  88. {
  89. while (*(json = next(json)))
  90. {
  91. // Empty string
  92. if (*json == '"')
  93. {
  94. json = next(json);
  95. return;
  96. }
  97. else if (*json == '\\')
  98. {
  99. json = next(json);
  100. switch (*json)
  101. {
  102. case '"': string += '"'; break;
  103. case '\\': string += '\\'; break;
  104. case '/': string += '/'; break;
  105. case 'b': string += '\b'; break;
  106. case 'f': string += '\f'; break;
  107. case 'n': string += '\n'; break;
  108. case 'r': string += '\r'; break;
  109. case 't': string += '\t'; break;
  110. default:
  111. {
  112. CE_FATAL("Bad escape character");
  113. break;
  114. }
  115. }
  116. }
  117. else
  118. {
  119. string += *json;
  120. }
  121. }
  122. }
  123. CE_FATAL("Bad string");
  124. }
  125. static const char* parse_key(const char* json, DynamicString& key)
  126. {
  127. CE_ASSERT_NOT_NULL(json);
  128. if (*json == '"')
  129. {
  130. parse_string(json, key);
  131. return skip_string(json);
  132. }
  133. else if (isalpha(*json))
  134. {
  135. while (true)
  136. {
  137. if (isspace(*json) || *json == '=') return json;
  138. key += *json;
  139. ++json;
  140. }
  141. }
  142. CE_FATAL("Bad key");
  143. }
  144. double parse_number(const char* json)
  145. {
  146. CE_ASSERT_NOT_NULL(json);
  147. TempAllocator512 alloc;
  148. Array<char> number(alloc);
  149. if (*json == '-')
  150. {
  151. array::push_back(number, '-');
  152. json = next(json, '-');
  153. }
  154. while (isdigit(*json))
  155. {
  156. array::push_back(number, *json);
  157. json = next(json);
  158. }
  159. if (*json == '.')
  160. {
  161. array::push_back(number, '.');
  162. while ((*(json = next(json))) && isdigit(*json))
  163. {
  164. array::push_back(number, *json);
  165. }
  166. }
  167. if (*json == 'e' || *json == 'E')
  168. {
  169. array::push_back(number, *json);
  170. json = next(json);
  171. if (*json == '-' || *json == '+')
  172. {
  173. array::push_back(number, *json);
  174. json = next(json);
  175. }
  176. while (isdigit(*json))
  177. {
  178. array::push_back(number, *json);
  179. json = next(json);
  180. }
  181. }
  182. // Ensure null terminated
  183. array::push_back(number, '\0');
  184. return parse_double(array::begin(number));
  185. }
  186. bool parse_bool(const char* json)
  187. {
  188. CE_ASSERT_NOT_NULL(json);
  189. switch (*json)
  190. {
  191. case 't':
  192. {
  193. json = next(json, 't');
  194. json = next(json, 'r');
  195. json = next(json, 'u');
  196. json = next(json, 'e');
  197. return true;
  198. }
  199. case 'f':
  200. {
  201. json = next(json, 'f');
  202. json = next(json, 'a');
  203. json = next(json, 'l');
  204. json = next(json, 's');
  205. json = next(json, 'e');
  206. return false;
  207. }
  208. default:
  209. {
  210. CE_FATAL("Bad boolean");
  211. return false;
  212. }
  213. }
  214. }
  215. int32_t parse_int(const char* json)
  216. {
  217. return (int32_t) parse_number(json);
  218. }
  219. float parse_float(const char* json)
  220. {
  221. return (float) parse_number(json);
  222. }
  223. void parse_array(const char* json, Array<const char*>& array)
  224. {
  225. CE_ASSERT_NOT_NULL(json);
  226. if (*json == '[')
  227. {
  228. json = next(json, '[');
  229. json = skip_spaces(json);
  230. if (*json == ']')
  231. {
  232. json = next(json, ']');
  233. return;
  234. }
  235. while (*json)
  236. {
  237. array::push_back(array, json);
  238. json = skip_value(json);
  239. json = skip_spaces(json);
  240. if (*json == ']')
  241. {
  242. json = next(json, ']');
  243. return;
  244. }
  245. json = skip_spaces(json);
  246. }
  247. }
  248. CE_FATAL("Bad array");
  249. }
  250. void parse_object(const char* json, Map<DynamicString, const char*>& object)
  251. {
  252. CE_ASSERT_NOT_NULL(json);
  253. if (*json == '{')
  254. {
  255. json = next(json, '{');
  256. json = skip_spaces(json);
  257. if (*json == '}')
  258. {
  259. next(json, '}');
  260. return;
  261. }
  262. while (*json)
  263. {
  264. DynamicString key;
  265. json = parse_key(json, key);
  266. json = skip_spaces(json);
  267. json = next(json, '=');
  268. json = skip_spaces(json);
  269. map::set(object, key, json);
  270. json = skip_value(json);
  271. json = skip_spaces(json);
  272. if (*json == '}')
  273. {
  274. next(json, '}');
  275. return;
  276. }
  277. json = skip_spaces(json);
  278. }
  279. }
  280. CE_FATAL("Bad object");
  281. }
  282. } // namespace njson
  283. } // namespace crown