Json.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  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. #ifdef SPINE_UE4
  21. #include "SpinePluginPrivatePCH.h"
  22. #endif
  23. /* Json */
  24. /* JSON parser in CPP, from json.c in the spine-c runtime */
  25. #ifndef _DEFAULT_SOURCE
  26. /* Bring strings.h definitions into string.h, where appropriate */
  27. #define _DEFAULT_SOURCE
  28. #endif
  29. #ifndef _BSD_SOURCE
  30. /* Bring strings.h definitions into string.h, where appropriate */
  31. #define _BSD_SOURCE
  32. #endif
  33. #include <spine/Json.h>
  34. #include <spine/extension.h>
  35. #include <spine/SpineString.h>
  36. #include <assert.h>
  37. #include <math.h>
  38. using namespace spine;
  39. const int Json::JSON_FALSE = 0;
  40. const int Json::JSON_TRUE = 1;
  41. const int Json::JSON_NULL = 2;
  42. const int Json::JSON_NUMBER = 3;
  43. const int Json::JSON_STRING = 4;
  44. const int Json::JSON_ARRAY = 5;
  45. const int Json::JSON_OBJECT = 6;
  46. const char *Json::_error = NULL;
  47. Json *Json::getItem(Json *object, const char *string) {
  48. Json *c = object->_child;
  49. while (c && json_strcasecmp(c->_name, string)) {
  50. c = c->_next;
  51. }
  52. return c;
  53. }
  54. const char *Json::getString(Json *object, const char *name, const char *defaultValue) {
  55. object = getItem(object, name);
  56. if (object) {
  57. return object->_valueString;
  58. }
  59. return defaultValue;
  60. }
  61. float Json::getFloat(Json *value, const char *name, float defaultValue) {
  62. value = getItem(value, name);
  63. return value ? value->_valueFloat : defaultValue;
  64. }
  65. int Json::getInt(Json *value, const char *name, int defaultValue) {
  66. value = getItem(value, name);
  67. return value ? value->_valueInt : defaultValue;
  68. }
  69. bool Json::getBoolean(spine::Json *value, const char *name, bool defaultValue) {
  70. value = getItem(value, name);
  71. if (value) {
  72. if (value->_valueString) return strcmp(value->_valueString, "true") == 0;
  73. if (value->_type == JSON_NULL) return false;
  74. if (value->_type == JSON_NUMBER) return value->_valueFloat != 0;
  75. return defaultValue;
  76. } else {
  77. return defaultValue;
  78. }
  79. }
  80. const char *Json::getError() {
  81. return _error;
  82. }
  83. Json::Json(const char *value) :
  84. _next(NULL),
  85. #if SPINE_JSON_HAVE_PREV
  86. _prev(NULL),
  87. #endif
  88. _child(NULL),
  89. _type(0),
  90. _size(0),
  91. _valueString(NULL),
  92. _valueInt(0),
  93. _valueFloat(0),
  94. _name(NULL) {
  95. if (value) {
  96. value = parseValue(this, skip(value));
  97. assert(value);
  98. }
  99. }
  100. Json::~Json() {
  101. spine::Json* curr = nullptr;
  102. spine::Json* next = _child;
  103. do {
  104. curr = next;
  105. if (curr) {
  106. next = curr->_next;
  107. }
  108. delete curr;
  109. } while(next);
  110. if (_valueString) {
  111. SpineExtension::free(_valueString, __FILE__, __LINE__);
  112. }
  113. if (_name) {
  114. SpineExtension::free(_name, __FILE__, __LINE__);
  115. }
  116. }
  117. const char *Json::skip(const char *inValue) {
  118. if (!inValue) {
  119. /* must propagate NULL since it's often called in skip(f(...)) form */
  120. return NULL;
  121. }
  122. while (*inValue && (unsigned char) *inValue <= 32) {
  123. inValue++;
  124. }
  125. return inValue;
  126. }
  127. const char *Json::parseValue(Json *item, const char *value) {
  128. /* Referenced by constructor, parseArray(), and parseObject(). */
  129. /* Always called with the result of skip(). */
  130. #ifdef SPINE_JSON_DEBUG /* Checked at entry to graph, constructor, and after every parse call. */
  131. if (!value) {
  132. /* Fail on null. */
  133. return NULL;
  134. }
  135. #endif
  136. switch (*value) {
  137. case 'n': {
  138. if (!strncmp(value + 1, "ull", 3)) {
  139. item->_type = JSON_NULL;
  140. return value + 4;
  141. }
  142. break;
  143. }
  144. case 'f': {
  145. if (!strncmp(value + 1, "alse", 4)) {
  146. item->_type = JSON_FALSE;
  147. /* calloc prevents us needing item->_type = JSON_FALSE or valueInt = 0 here */
  148. return value + 5;
  149. }
  150. break;
  151. }
  152. case 't': {
  153. if (!strncmp(value + 1, "rue", 3)) {
  154. item->_type = JSON_TRUE;
  155. item->_valueInt = 1;
  156. return value + 4;
  157. }
  158. break;
  159. }
  160. case '\"':
  161. return parseString(item, value);
  162. case '[':
  163. return parseArray(item, value);
  164. case '{':
  165. return parseObject(item, value);
  166. case '-': /* fallthrough */
  167. case '0': /* fallthrough */
  168. case '1': /* fallthrough */
  169. case '2': /* fallthrough */
  170. case '3': /* fallthrough */
  171. case '4': /* fallthrough */
  172. case '5': /* fallthrough */
  173. case '6': /* fallthrough */
  174. case '7': /* fallthrough */
  175. case '8': /* fallthrough */
  176. case '9':
  177. return parseNumber(item, value);
  178. default:
  179. break;
  180. }
  181. _error = value;
  182. return NULL; /* failure. */
  183. }
  184. static const unsigned char firstByteMark[7] = {0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC};
  185. const char *Json::parseString(Json *item, const char *str) {
  186. const char *ptr = str + 1;
  187. char *ptr2;
  188. char *out;
  189. int len = 0;
  190. unsigned uc, uc2;
  191. if (*str != '\"') {
  192. /* TODO: don't need this check when called from parseValue, but do need from parseObject */
  193. _error = str;
  194. return 0;
  195. } /* not a string! */
  196. while (*ptr != '\"' && *ptr && ++len) {
  197. if (*ptr++ == '\\') {
  198. ptr++; /* Skip escaped quotes. */
  199. }
  200. }
  201. out = SpineExtension::alloc<char>(len + 1, __FILE__, __LINE__); /* The length needed for the string, roughly. */
  202. if (!out) {
  203. return 0;
  204. }
  205. ptr = str + 1;
  206. ptr2 = out;
  207. while (*ptr != '\"' && *ptr) {
  208. if (*ptr != '\\') {
  209. *ptr2++ = *ptr++;
  210. } else {
  211. ptr++;
  212. switch (*ptr) {
  213. case 'b':
  214. *ptr2++ = '\b';
  215. break;
  216. case 'f':
  217. *ptr2++ = '\f';
  218. break;
  219. case 'n':
  220. *ptr2++ = '\n';
  221. break;
  222. case 'r':
  223. *ptr2++ = '\r';
  224. break;
  225. case 't':
  226. *ptr2++ = '\t';
  227. break;
  228. case 'u': {
  229. /* transcode utf16 to utf8. */
  230. sscanf(ptr + 1, "%4x", &uc);
  231. ptr += 4; /* get the unicode char. */
  232. if ((uc >= 0xDC00 && uc <= 0xDFFF) || uc == 0) {
  233. break; /* check for invalid. */
  234. }
  235. /* TODO provide an option to ignore surrogates, use unicode replacement character? */
  236. if (uc >= 0xD800 && uc <= 0xDBFF) /* UTF16 surrogate pairs. */ {
  237. if (ptr[1] != '\\' || ptr[2] != 'u') {
  238. break; /* missing second-half of surrogate. */
  239. }
  240. sscanf(ptr + 3, "%4x", &uc2);
  241. ptr += 6;
  242. if (uc2 < 0xDC00 || uc2 > 0xDFFF) {
  243. break; /* invalid second-half of surrogate. */
  244. }
  245. uc = 0x10000 + (((uc & 0x3FF) << 10) | (uc2 & 0x3FF));
  246. }
  247. len = 4;
  248. if (uc < 0x80) {
  249. len = 1;
  250. } else if (uc < 0x800) {
  251. len = 2;
  252. } else if (uc < 0x10000) {
  253. len = 3;
  254. }
  255. ptr2 += len;
  256. switch (len) {
  257. case 4:
  258. *--ptr2 = ((uc | 0x80) & 0xBF);
  259. uc >>= 6;
  260. /* fallthrough */
  261. case 3:
  262. *--ptr2 = ((uc | 0x80) & 0xBF);
  263. uc >>= 6;
  264. /* fallthrough */
  265. case 2:
  266. *--ptr2 = ((uc | 0x80) & 0xBF);
  267. uc >>= 6;
  268. /* fallthrough */
  269. case 1:
  270. *--ptr2 = (uc | firstByteMark[len]);
  271. }
  272. ptr2 += len;
  273. break;
  274. }
  275. default:
  276. *ptr2++ = *ptr;
  277. break;
  278. }
  279. ptr++;
  280. }
  281. }
  282. *ptr2 = 0;
  283. if (*ptr == '\"') {
  284. ptr++; /* TODO error handling if not \" or \0 ? */
  285. }
  286. item->_valueString = out;
  287. item->_type = JSON_STRING;
  288. return ptr;
  289. }
  290. const char *Json::parseNumber(Json *item, const char *num) {
  291. double result = 0.0;
  292. int negative = 0;
  293. char *ptr = (char *) num;
  294. if (*ptr == '-') {
  295. negative = -1;
  296. ++ptr;
  297. }
  298. while (*ptr >= '0' && *ptr <= '9') {
  299. result = result * 10.0 + (*ptr - '0');
  300. ++ptr;
  301. }
  302. if (*ptr == '.') {
  303. double fraction = 0.0;
  304. int n = 0;
  305. ++ptr;
  306. while (*ptr >= '0' && *ptr <= '9') {
  307. fraction = (fraction * 10.0) + (*ptr - '0');
  308. ++ptr;
  309. ++n;
  310. }
  311. result += fraction / pow(10.0, n);
  312. }
  313. if (negative) {
  314. result = -result;
  315. }
  316. if (*ptr == 'e' || *ptr == 'E') {
  317. double exponent = 0;
  318. int expNegative = 0;
  319. int n = 0;
  320. ++ptr;
  321. if (*ptr == '-') {
  322. expNegative = -1;
  323. ++ptr;
  324. } else if (*ptr == '+') {
  325. ++ptr;
  326. }
  327. while (*ptr >= '0' && *ptr <= '9') {
  328. exponent = (exponent * 10.0) + (*ptr - '0');
  329. ++ptr;
  330. ++n;
  331. }
  332. if (expNegative) {
  333. result = result / pow(10, exponent);
  334. } else {
  335. result = result * pow(10, exponent);
  336. }
  337. }
  338. if (ptr != num) {
  339. /* Parse success, number found. */
  340. item->_valueFloat = (float)result;
  341. item->_valueInt = (int)result;
  342. item->_type = JSON_NUMBER;
  343. return ptr;
  344. } else {
  345. /* Parse failure, _error is set. */
  346. _error = num;
  347. return NULL;
  348. }
  349. }
  350. const char *Json::parseArray(Json *item, const char *value) {
  351. Json *child;
  352. #ifdef SPINE_JSON_DEBUG /* unnecessary, only callsite (parse_value) verifies this */
  353. if (*value != '[') {
  354. ep = value;
  355. return 0;
  356. } /* not an array! */
  357. #endif
  358. item->_type = JSON_ARRAY;
  359. value = skip(value + 1);
  360. if (*value == ']') {
  361. return value + 1; /* empty array. */
  362. }
  363. item->_child = child = new(__FILE__, __LINE__) Json(NULL);
  364. if (!item->_child) {
  365. return NULL; /* memory fail */
  366. }
  367. value = skip(parseValue(child, skip(value))); /* skip any spacing, get the value. */
  368. if (!value) {
  369. return NULL;
  370. }
  371. item->_size = 1;
  372. while (*value == ',') {
  373. Json *new_item = new(__FILE__, __LINE__) Json(NULL);
  374. if (!new_item) {
  375. return NULL; /* memory fail */
  376. }
  377. child->_next = new_item;
  378. #if SPINE_JSON_HAVE_PREV
  379. new_item->prev = child;
  380. #endif
  381. child = new_item;
  382. value = skip(parseValue(child, skip(value + 1)));
  383. if (!value) {
  384. return NULL; /* parse fail */
  385. }
  386. item->_size++;
  387. }
  388. if (*value == ']') {
  389. return value + 1; /* end of array */
  390. }
  391. _error = value;
  392. return NULL; /* malformed. */
  393. }
  394. /* Build an object from the text. */
  395. const char *Json::parseObject(Json *item, const char *value) {
  396. Json *child;
  397. #ifdef SPINE_JSON_DEBUG /* unnecessary, only callsite (parse_value) verifies this */
  398. if (*value != '{') {
  399. ep = value;
  400. return 0;
  401. } /* not an object! */
  402. #endif
  403. item->_type = JSON_OBJECT;
  404. value = skip(value + 1);
  405. if (*value == '}') {
  406. return value + 1; /* empty array. */
  407. }
  408. item->_child = child = new(__FILE__, __LINE__) Json(NULL);
  409. if (!item->_child) {
  410. return NULL;
  411. }
  412. value = skip(parseString(child, skip(value)));
  413. if (!value) {
  414. return NULL;
  415. }
  416. child->_name = child->_valueString;
  417. child->_valueString = 0;
  418. if (*value != ':') {
  419. _error = value;
  420. return NULL;
  421. } /* fail! */
  422. value = skip(parseValue(child, skip(value + 1))); /* skip any spacing, get the value. */
  423. if (!value) {
  424. return NULL;
  425. }
  426. item->_size = 1;
  427. while (*value == ',') {
  428. Json *new_item = new(__FILE__, __LINE__) Json(NULL);
  429. if (!new_item) {
  430. return NULL; /* memory fail */
  431. }
  432. child->_next = new_item;
  433. #if SPINE_JSON_HAVE_PREV
  434. new_item->prev = child;
  435. #endif
  436. child = new_item;
  437. value = skip(parseString(child, skip(value + 1)));
  438. if (!value) {
  439. return NULL;
  440. }
  441. child->_name = child->_valueString;
  442. child->_valueString = 0;
  443. if (*value != ':') {
  444. _error = value;
  445. return NULL;
  446. } /* fail! */
  447. value = skip(parseValue(child, skip(value + 1))); /* skip any spacing, get the value. */
  448. if (!value) {
  449. return NULL;
  450. }
  451. item->_size++;
  452. }
  453. if (*value == '}') {
  454. return value + 1; /* end of array */
  455. }
  456. _error = value;
  457. return NULL; /* malformed. */
  458. }
  459. int Json::json_strcasecmp(const char *s1, const char *s2) {
  460. /* TODO we may be able to elide these NULL checks if we can prove
  461. * the graph and input (only callsite is Json_getItem) should not have NULLs
  462. */
  463. if (s1 && s2) {
  464. #if defined(_WIN32)
  465. return _stricmp(s1, s2);
  466. #else
  467. return strcasecmp(s1, s2);
  468. #endif
  469. } else {
  470. if (s1 < s2) {
  471. return -1; /* s1 is null, s2 is not */
  472. } else if (s1 == s2) {
  473. return 0; /* both are null */
  474. } else {
  475. return 1; /* s2 is nul s1 is not */
  476. }
  477. }
  478. }