Json.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. /*
  2. Copyright (c) 2009, Dave Gamble
  3. Copyright (c) 2013, Esoteric Software
  4. Permission is hereby granted, dispose of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. */
  20. /* Json */
  21. /* JSON parser in C. */
  22. #ifndef _DEFAULT_SOURCE
  23. /* Bring strings.h definitions into string.h, where appropriate */
  24. #define _DEFAULT_SOURCE
  25. #endif
  26. #ifndef _BSD_SOURCE
  27. /* Bring strings.h definitions into string.h, where appropriate */
  28. #define _BSD_SOURCE
  29. #endif
  30. #include "Json.h"
  31. #include <stdio.h>
  32. #include <ctype.h>
  33. #include <stdlib.h> /* strtod (C89), strtof (C99) */
  34. #include <string.h> /* strcasecmp (4.4BSD - compatibility), _stricmp (_WIN32) */
  35. #include <spine/extension.h>
  36. #ifndef SPINE_JSON_DEBUG
  37. /* Define this to do extra NULL and expected-character checking */
  38. #define SPINE_JSON_DEBUG 0
  39. #endif
  40. static const char* ep;
  41. const char* Json_getError (void) {
  42. return ep;
  43. }
  44. static int Json_strcasecmp (const char* s1, const char* s2) {
  45. /* TODO we may be able to elide these NULL checks if we can prove
  46. * the graph and input (only callsite is Json_getItem) should not have NULLs
  47. */
  48. if (s1 && s2) {
  49. #if defined(_WIN32)
  50. return _stricmp(s1, s2);
  51. #else
  52. return strcasecmp( s1, s2 );
  53. #endif
  54. } else {
  55. if (s1 < s2)
  56. return -1; /* s1 is null, s2 is not */
  57. else if (s1 == s2)
  58. return 0; /* both are null */
  59. else
  60. return 1; /* s2 is nul s1 is not */
  61. }
  62. }
  63. /* Internal constructor. */
  64. static Json *Json_new (void) {
  65. return (Json*)CALLOC(Json, 1);
  66. }
  67. /* Delete a Json structure. */
  68. void Json_dispose (Json *c) {
  69. Json *next;
  70. while (c) {
  71. next = c->next;
  72. if (c->child) Json_dispose(c->child);
  73. if (c->valueString) FREE(c->valueString);
  74. if (c->name) FREE(c->name);
  75. FREE(c);
  76. c = next;
  77. }
  78. }
  79. /* Parse the input text to generate a number, and populate the result into item. */
  80. static const char* parse_number (Json *item, const char* num) {
  81. char * endptr;
  82. float n;
  83. /* Using strtod and strtof is slightly more permissive than RFC4627,
  84. * accepting for example hex-encoded floating point, but either
  85. * is often leagues faster than any manual implementation.
  86. *
  87. * We also already know that this starts with [-0-9] from parse_value.
  88. */
  89. #if __STDC_VERSION__ >= 199901L
  90. n = strtof(num, &endptr);
  91. #else
  92. n = (float)strtod( num, &endptr );
  93. #endif
  94. /* ignore errno's ERANGE, which returns +/-HUGE_VAL */
  95. /* n is 0 on any other error */
  96. if (endptr != num) {
  97. /* Parse success, number found. */
  98. item->valueFloat = n;
  99. item->valueInt = (int)n;
  100. item->type = Json_Number;
  101. return endptr;
  102. } else {
  103. /* Parse failure, ep is set. */
  104. ep = num;
  105. return 0;
  106. }
  107. }
  108. /* Parse the input text into an unescaped cstring, and populate item. */
  109. static const unsigned char firstByteMark[7] = {0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC};
  110. static const char* parse_string (Json *item, const char* str) {
  111. const char* ptr = str + 1;
  112. char* ptr2;
  113. char* out;
  114. int len = 0;
  115. unsigned uc, uc2;
  116. if (*str != '\"') { /* TODO: don't need this check when called from parse_value, but do need from parse_object */
  117. ep = str;
  118. return 0;
  119. } /* not a string! */
  120. while (*ptr != '\"' && *ptr && ++len)
  121. if (*ptr++ == '\\') ptr++; /* Skip escaped quotes. */
  122. out = MALLOC(char, len + 1); /* The length needed for the string, roughly. */
  123. if (!out) return 0;
  124. ptr = str + 1;
  125. ptr2 = out;
  126. while (*ptr != '\"' && *ptr) {
  127. if (*ptr != '\\')
  128. *ptr2++ = *ptr++;
  129. else {
  130. ptr++;
  131. switch (*ptr) {
  132. case 'b':
  133. *ptr2++ = '\b';
  134. break;
  135. case 'f':
  136. *ptr2++ = '\f';
  137. break;
  138. case 'n':
  139. *ptr2++ = '\n';
  140. break;
  141. case 'r':
  142. *ptr2++ = '\r';
  143. break;
  144. case 't':
  145. *ptr2++ = '\t';
  146. break;
  147. case 'u': /* transcode utf16 to utf8. */
  148. sscanf(ptr + 1, "%4x", &uc);
  149. ptr += 4; /* get the unicode char. */
  150. if ((uc >= 0xDC00 && uc <= 0xDFFF) || uc == 0) break; /* check for invalid. */
  151. /* TODO provide an option to ignore surrogates, use unicode replacement character? */
  152. if (uc >= 0xD800 && uc <= 0xDBFF) /* UTF16 surrogate pairs. */
  153. {
  154. if (ptr[1] != '\\' || ptr[2] != 'u') break; /* missing second-half of surrogate. */
  155. sscanf(ptr + 3, "%4x", &uc2);
  156. ptr += 6;
  157. if (uc2 < 0xDC00 || uc2 > 0xDFFF) break; /* invalid second-half of surrogate. */
  158. uc = 0x10000 + (((uc & 0x3FF) << 10) | (uc2 & 0x3FF));
  159. }
  160. len = 4;
  161. if (uc < 0x80)
  162. len = 1;
  163. else if (uc < 0x800)
  164. len = 2;
  165. else if (uc < 0x10000) len = 3;
  166. ptr2 += len;
  167. switch (len) {
  168. case 4:
  169. *--ptr2 = ((uc | 0x80) & 0xBF);
  170. uc >>= 6;
  171. /* fallthrough */
  172. case 3:
  173. *--ptr2 = ((uc | 0x80) & 0xBF);
  174. uc >>= 6;
  175. /* fallthrough */
  176. case 2:
  177. *--ptr2 = ((uc | 0x80) & 0xBF);
  178. uc >>= 6;
  179. /* fallthrough */
  180. case 1:
  181. *--ptr2 = (uc | firstByteMark[len]);
  182. }
  183. ptr2 += len;
  184. break;
  185. default:
  186. *ptr2++ = *ptr;
  187. break;
  188. }
  189. ptr++;
  190. }
  191. }
  192. *ptr2 = 0;
  193. if (*ptr == '\"') ptr++; /* TODO error handling if not \" or \0 ? */
  194. item->valueString = out;
  195. item->type = Json_String;
  196. return ptr;
  197. }
  198. /* Predeclare these prototypes. */
  199. static const char* parse_value (Json *item, const char* value);
  200. static const char* parse_array (Json *item, const char* value);
  201. static const char* parse_object (Json *item, const char* value);
  202. /* Utility to jump whitespace and cr/lf */
  203. static const char* skip (const char* in) {
  204. if (!in) return 0; /* must propagate NULL since it's often called in skip(f(...)) form */
  205. while (*in && (unsigned char)*in <= 32)
  206. in++;
  207. return in;
  208. }
  209. /* Parse an object - create a new root, and populate. */
  210. Json *Json_create (const char* value) {
  211. Json *c;
  212. ep = 0;
  213. if (!value) return 0; /* only place we check for NULL other than skip() */
  214. c = Json_new();
  215. if (!c) return 0; /* memory fail */
  216. value = parse_value(c, skip(value));
  217. if (!value) {
  218. Json_dispose(c);
  219. return 0;
  220. } /* parse failure. ep is set. */
  221. return c;
  222. }
  223. /* Parser core - when encountering text, process appropriately. */
  224. static const char* parse_value (Json *item, const char* value) {
  225. /* Referenced by Json_create(), parse_array(), and parse_object(). */
  226. /* Always called with the result of skip(). */
  227. #if SPINE_JSON_DEBUG /* Checked at entry to graph, Json_create, and after every parse_ call. */
  228. if (!value) return 0; /* Fail on null. */
  229. #endif
  230. switch (*value) {
  231. case 'n': {
  232. if (!strncmp(value + 1, "ull", 3)) {
  233. item->type = Json_NULL;
  234. return value + 4;
  235. }
  236. break;
  237. }
  238. case 'f': {
  239. if (!strncmp(value + 1, "alse", 4)) {
  240. item->type = Json_False;
  241. /* calloc prevents us needing item->type = Json_False or valueInt = 0 here */
  242. return value + 5;
  243. }
  244. break;
  245. }
  246. case 't': {
  247. if (!strncmp(value + 1, "rue", 3)) {
  248. item->type = Json_True;
  249. item->valueInt = 1;
  250. return value + 4;
  251. }
  252. break;
  253. }
  254. case '\"':
  255. return parse_string(item, value);
  256. case '[':
  257. return parse_array(item, value);
  258. case '{':
  259. return parse_object(item, value);
  260. case '-': /* fallthrough */
  261. case '0': /* fallthrough */
  262. case '1': /* fallthrough */
  263. case '2': /* fallthrough */
  264. case '3': /* fallthrough */
  265. case '4': /* fallthrough */
  266. case '5': /* fallthrough */
  267. case '6': /* fallthrough */
  268. case '7': /* fallthrough */
  269. case '8': /* fallthrough */
  270. case '9':
  271. return parse_number(item, value);
  272. default:
  273. break;
  274. }
  275. ep = value;
  276. return 0; /* failure. */
  277. }
  278. /* Build an array from input text. */
  279. static const char* parse_array (Json *item, const char* value) {
  280. Json *child;
  281. #if SPINE_JSON_DEBUG /* unnecessary, only callsite (parse_value) verifies this */
  282. if (*value != '[') {
  283. ep = value;
  284. return 0;
  285. } /* not an array! */
  286. #endif
  287. item->type = Json_Array;
  288. value = skip(value + 1);
  289. if (*value == ']') return value + 1; /* empty array. */
  290. item->child = child = Json_new();
  291. if (!item->child) return 0; /* memory fail */
  292. value = skip(parse_value(child, skip(value))); /* skip any spacing, get the value. */
  293. if (!value) return 0;
  294. item->size = 1;
  295. while (*value == ',') {
  296. Json *new_item = Json_new();
  297. if (!new_item) return 0; /* memory fail */
  298. child->next = new_item;
  299. #if SPINE_JSON_HAVE_PREV
  300. new_item->prev = child;
  301. #endif
  302. child = new_item;
  303. value = skip(parse_value(child, skip(value + 1)));
  304. if (!value) return 0; /* parse fail */
  305. item->size++;
  306. }
  307. if (*value == ']') return value + 1; /* end of array */
  308. ep = value;
  309. return 0; /* malformed. */
  310. }
  311. /* Build an object from the text. */
  312. static const char* parse_object (Json *item, const char* value) {
  313. Json *child;
  314. #if SPINE_JSON_DEBUG /* unnecessary, only callsite (parse_value) verifies this */
  315. if (*value != '{') {
  316. ep = value;
  317. return 0;
  318. } /* not an object! */
  319. #endif
  320. item->type = Json_Object;
  321. value = skip(value + 1);
  322. if (*value == '}') return value + 1; /* empty array. */
  323. item->child = child = Json_new();
  324. if (!item->child) return 0;
  325. value = skip(parse_string(child, skip(value)));
  326. if (!value) return 0;
  327. child->name = child->valueString;
  328. child->valueString = 0;
  329. if (*value != ':') {
  330. ep = value;
  331. return 0;
  332. } /* fail! */
  333. value = skip(parse_value(child, skip(value + 1))); /* skip any spacing, get the value. */
  334. if (!value) return 0;
  335. item->size = 1;
  336. while (*value == ',') {
  337. Json *new_item = Json_new();
  338. if (!new_item) return 0; /* memory fail */
  339. child->next = new_item;
  340. #if SPINE_JSON_HAVE_PREV
  341. new_item->prev = child;
  342. #endif
  343. child = new_item;
  344. value = skip(parse_string(child, skip(value + 1)));
  345. if (!value) return 0;
  346. child->name = child->valueString;
  347. child->valueString = 0;
  348. if (*value != ':') {
  349. ep = value;
  350. return 0;
  351. } /* fail! */
  352. value = skip(parse_value(child, skip(value + 1))); /* skip any spacing, get the value. */
  353. if (!value) return 0;
  354. item->size++;
  355. }
  356. if (*value == '}') return value + 1; /* end of array */
  357. ep = value;
  358. return 0; /* malformed. */
  359. }
  360. Json *Json_getItem (Json *object, const char* string) {
  361. Json *c = object->child;
  362. while (c && Json_strcasecmp(c->name, string))
  363. c = c->next;
  364. return c;
  365. }
  366. const char* Json_getString (Json* object, const char* name, const char* defaultValue) {
  367. object = Json_getItem(object, name);
  368. if (object) return object->valueString;
  369. return defaultValue;
  370. }
  371. float Json_getFloat (Json* value, const char* name, float defaultValue) {
  372. value = Json_getItem(value, name);
  373. return value ? value->valueFloat : defaultValue;
  374. }
  375. int Json_getInt (Json* value, const char* name, int defaultValue) {
  376. value = Json_getItem(value, name);
  377. return value ? value->valueInt : defaultValue;
  378. }