ultrajsonenc.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830
  1. /*
  2. Copyright (c) 2011, Jonas Tarnstrom and ESN Social Software AB
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are met:
  6. 1. Redistributions of source code must retain the above copyright
  7. notice, this list of conditions and the following disclaimer.
  8. 2. Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the distribution.
  11. 3. All advertising materials mentioning features or use of this software
  12. must display the following acknowledgement:
  13. This product includes software developed by ESN Social Software AB (www.esn.me).
  14. 4. Neither the name of the ESN Social Software AB nor the
  15. names of its contributors may be used to endorse or promote products
  16. derived from this software without specific prior written permission.
  17. THIS SOFTWARE IS PROVIDED BY ESN SOCIAL SOFTWARE AB ''AS IS'' AND ANY
  18. EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. DISCLAIMED. IN NO EVENT SHALL ESN SOCIAL SOFTWARE AB BE LIABLE FOR ANY
  21. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  24. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  26. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. Portions of code from:
  28. MODP_ASCII - Ascii transformations (upper/lower, etc)
  29. http://code.google.com/p/stringencoders/
  30. Copyright (c) 2007 Nick Galbreath -- nickg [at] modp [dot] com. All rights reserved.
  31. */
  32. #include "ultrajson.h"
  33. #include <stdio.h>
  34. #include <assert.h>
  35. #include <string.h>
  36. #include <stdlib.h>
  37. #include <math.h>
  38. #include <float.h>
  39. #ifndef TRUE
  40. #define TRUE 1
  41. #endif
  42. #ifndef FALSE
  43. #define FALSE 0
  44. #endif
  45. static const double g_pow10[] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000};
  46. static const char g_hexChars[] = "0123456789abcdef";
  47. static const char g_escapeChars[] = "0123456789\\b\\t\\n\\f\\r\\\"\\\\\\/";
  48. /*
  49. FIXME: While this is fine dandy and working it's a magic value mess which probably only the author understands.
  50. Needs a cleanup and more documentation */
  51. /*
  52. Table for pure ascii output escaping all characters above 127 to \uXXXX */
  53. static const JSUINT8 g_asciiOutputTable[256] =
  54. {
  55. /* 0x00 */ 0, 30, 30, 30, 30, 30, 30, 30, 10, 12, 14, 30, 16, 18, 30, 30,
  56. /* 0x10 */ 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30,
  57. /* 0x20 */ 1, 1, 20, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1/*24*/,
  58. /* 0x30 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  59. /* 0x40 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  60. /* 0x50 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 22, 1, 1, 1,
  61. /* 0x60 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  62. /* 0x70 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  63. /* 0x80 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  64. /* 0x90 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  65. /* 0xa0 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  66. /* 0xb0 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  67. /* 0xc0 */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  68. /* 0xd0 */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  69. /* 0xe0 */ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  70. /* 0xf0 */ 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 1, 1
  71. };
  72. static void SetError (JSOBJ obj, JSONObjectEncoder *enc, const char *message)
  73. {
  74. enc->errorMsg = message;
  75. enc->errorObj = obj;
  76. }
  77. /*
  78. FIXME: Keep track of how big these get across several encoder calls and try to make an estimate
  79. That way we won't run our head into the wall each call */
  80. void Buffer_Realloc (JSONObjectEncoder *enc, size_t cbNeeded)
  81. {
  82. size_t curSize = enc->end - enc->start;
  83. size_t newSize = curSize * 2;
  84. size_t offset = enc->offset - enc->start;
  85. while (newSize < curSize + cbNeeded)
  86. {
  87. newSize *= 2;
  88. }
  89. if (enc->heap)
  90. {
  91. enc->start = (char *) enc->realloc (enc->start, newSize);
  92. }
  93. else
  94. {
  95. char *oldStart = enc->start;
  96. enc->heap = 1;
  97. enc->start = (char *) enc->malloc (newSize);
  98. memcpy (enc->start, oldStart, offset);
  99. }
  100. enc->offset = enc->start + offset;
  101. enc->end = enc->start + newSize;
  102. }
  103. FASTCALL_ATTR INLINE_PREFIX void FASTCALL_MSVC Buffer_AppendShortHexUnchecked (char *outputOffset, unsigned short value)
  104. {
  105. *(outputOffset++) = g_hexChars[(value & 0xf000) >> 12];
  106. *(outputOffset++) = g_hexChars[(value & 0x0f00) >> 8];
  107. *(outputOffset++) = g_hexChars[(value & 0x00f0) >> 4];
  108. *(outputOffset++) = g_hexChars[(value & 0x000f) >> 0];
  109. }
  110. int Buffer_EscapeStringUnvalidated (JSOBJ obj, JSONObjectEncoder *enc, const char *io, const char *end)
  111. {
  112. char *of = (char *) enc->offset;
  113. while (1)
  114. {
  115. switch (*io)
  116. {
  117. case 0x00:
  118. enc->offset += (of - enc->offset);
  119. return TRUE;
  120. case '\"': (*of++) = '\\'; (*of++) = '\"'; break;
  121. case '\\': (*of++) = '\\'; (*of++) = '\\'; break;
  122. //case '/': (*of++) = '\\'; (*of++) = '/'; break;
  123. case '\b': (*of++) = '\\'; (*of++) = 'b'; break;
  124. case '\f': (*of++) = '\\'; (*of++) = 'f'; break;
  125. case '\n': (*of++) = '\\'; (*of++) = 'n'; break;
  126. case '\r': (*of++) = '\\'; (*of++) = 'r'; break;
  127. case '\t': (*of++) = '\\'; (*of++) = 't'; break;
  128. case 0x01:
  129. case 0x02:
  130. case 0x03:
  131. case 0x04:
  132. case 0x05:
  133. case 0x06:
  134. case 0x07:
  135. case 0x0b:
  136. case 0x0e:
  137. case 0x0f:
  138. case 0x10:
  139. case 0x11:
  140. case 0x12:
  141. case 0x13:
  142. case 0x14:
  143. case 0x15:
  144. case 0x16:
  145. case 0x17:
  146. case 0x18:
  147. case 0x19:
  148. case 0x1a:
  149. case 0x1b:
  150. case 0x1c:
  151. case 0x1d:
  152. case 0x1e:
  153. case 0x1f:
  154. *(of++) = '\\';
  155. *(of++) = 'u';
  156. *(of++) = '0';
  157. *(of++) = '0';
  158. *(of++) = g_hexChars[ (unsigned char) (((*io) & 0xf0) >> 4)];
  159. *(of++) = g_hexChars[ (unsigned char) ((*io) & 0x0f)];
  160. break;
  161. default: (*of++) = (*io); break;
  162. }
  163. *io++;
  164. }
  165. return FALSE;
  166. }
  167. /*
  168. FIXME:
  169. This code only works with Little and Big Endian
  170. FIXME: The JSON spec says escape "/" but non of the others do and we don't
  171. want to be left alone doing it so we don't :)
  172. */
  173. int Buffer_EscapeStringValidated (JSOBJ obj, JSONObjectEncoder *enc, const char *io, const char *end)
  174. {
  175. JSUTF32 ucs;
  176. char *of = (char *) enc->offset;
  177. while (1)
  178. {
  179. //JSUINT8 chr = (unsigned char) *io;
  180. JSUINT8 utflen = g_asciiOutputTable[(unsigned char) *io];
  181. switch (utflen)
  182. {
  183. case 0:
  184. {
  185. enc->offset += (of - enc->offset);
  186. return TRUE;
  187. }
  188. case 1:
  189. {
  190. *(of++)= (*io++);
  191. continue;
  192. }
  193. case 2:
  194. {
  195. JSUTF32 in;
  196. if (io + 1 > end)
  197. {
  198. enc->offset += (of - enc->offset);
  199. SetError (obj, enc, "Unterminated UTF-8 sequence when encoding string");
  200. return FALSE;
  201. }
  202. in = *((JSUTF16 *) io);
  203. #ifdef __LITTLE_ENDIAN__
  204. ucs = ((in & 0x1f) << 6) | ((in >> 8) & 0x3f);
  205. #else
  206. ucs = ((in & 0x1f00) >> 2) | (in & 0x3f);
  207. #endif
  208. if (ucs < 0x80)
  209. {
  210. enc->offset += (of - enc->offset);
  211. SetError (obj, enc, "Overlong 2 byte UTF-8 sequence detected when encoding string");
  212. return FALSE;
  213. }
  214. io += 2;
  215. break;
  216. }
  217. case 3:
  218. {
  219. JSUTF32 in;
  220. if (io + 2 > end)
  221. {
  222. enc->offset += (of - enc->offset);
  223. SetError (obj, enc, "Unterminated UTF-8 sequence when encoding string");
  224. return FALSE;
  225. }
  226. #ifdef __LITTLE_ENDIAN__
  227. in = *((JSUTF16 *) io);
  228. in |= *((JSUINT8 *) io + 2) << 16;
  229. ucs = ((in & 0x0f) << 12) | ((in & 0x3f00) >> 2) | ((in & 0x3f0000) >> 16);
  230. #else
  231. in = *((JSUTF16 *) io) << 8;
  232. in |= *((JSUINT8 *) io + 2);
  233. ucs = ((in & 0x0f0000) >> 4) | ((in & 0x3f00) >> 2) | (in & 0x3f);
  234. #endif
  235. if (ucs < 0x800)
  236. {
  237. enc->offset += (of - enc->offset);
  238. SetError (obj, enc, "Overlong 3 byte UTF-8 sequence detected when encoding string");
  239. return FALSE;
  240. }
  241. io += 3;
  242. break;
  243. }
  244. case 4:
  245. {
  246. JSUTF32 in;
  247. if (io + 3 > end)
  248. {
  249. enc->offset += (of - enc->offset);
  250. SetError (obj, enc, "Unterminated UTF-8 sequence when encoding string");
  251. return FALSE;
  252. }
  253. #ifdef __LITTLE_ENDIAN__
  254. in = *((JSUTF32 *) io);
  255. ucs = ((in & 0x07) << 18) | ((in & 0x3f00) << 4) | ((in & 0x3f0000) >> 10) | ((in & 0x3f000000) >> 24);
  256. #else
  257. in = *((JSUTF32 *) io);
  258. ucs = ((in & 0x07000000) >> 6) | ((in & 0x3f0000) >> 4) | ((in & 0x3f00) >> 2) | (in & 0x3f);
  259. #endif
  260. if (ucs < 0x10000)
  261. {
  262. enc->offset += (of - enc->offset);
  263. SetError (obj, enc, "Overlong 4 byte UTF-8 sequence detected when encoding string");
  264. return FALSE;
  265. }
  266. io += 4;
  267. break;
  268. }
  269. case 5:
  270. case 6:
  271. enc->offset += (of - enc->offset);
  272. SetError (obj, enc, "Unsupported UTF-8 sequence length when encoding string");
  273. return FALSE;
  274. case 30:
  275. // \uXXXX encode
  276. *(of++) = '\\';
  277. *(of++) = 'u';
  278. *(of++) = '0';
  279. *(of++) = '0';
  280. *(of++) = g_hexChars[ (unsigned char) (((*io) & 0xf0) >> 4)];
  281. *(of++) = g_hexChars[ (unsigned char) ((*io) & 0x0f)];
  282. io ++;
  283. continue;
  284. case 10:
  285. case 12:
  286. case 14:
  287. case 16:
  288. case 18:
  289. case 20:
  290. case 22:
  291. //case 24: (enable for / escaping)
  292. *(of++) = *( (char *) (g_escapeChars + utflen + 0));
  293. *(of++) = *( (char *) (g_escapeChars + utflen + 1));
  294. io ++;
  295. continue;
  296. }
  297. /*
  298. If the character is a UTF8 sequence of length > 1 we end up here */
  299. if (ucs >= 0x10000)
  300. {
  301. ucs -= 0x10000;
  302. *(of++) = '\\';
  303. *(of++) = 'u';
  304. Buffer_AppendShortHexUnchecked(of, (ucs >> 10) + 0xd800);
  305. of += 4;
  306. *(of++) = '\\';
  307. *(of++) = 'u';
  308. Buffer_AppendShortHexUnchecked(of, (ucs & 0x3ff) + 0xdc00);
  309. of += 4;
  310. }
  311. else
  312. {
  313. *(of++) = '\\';
  314. *(of++) = 'u';
  315. Buffer_AppendShortHexUnchecked(of, ucs);
  316. of += 4;
  317. }
  318. }
  319. return FALSE;
  320. }
  321. #define Buffer_Reserve(__enc, __len) \
  322. if ((__enc)->offset + (__len) > (__enc)->end) \
  323. { \
  324. Buffer_Realloc((__enc), (__len));\
  325. } \
  326. #define Buffer_AppendCharUnchecked(__enc, __chr) \
  327. *((__enc)->offset++) = __chr; \
  328. FASTCALL_ATTR INLINE_PREFIX void FASTCALL_MSVC strreverse(char* begin, char* end)
  329. {
  330. char aux;
  331. while (end > begin)
  332. aux = *end, *end-- = *begin, *begin++ = aux;
  333. }
  334. void Buffer_AppendIntUnchecked(JSONObjectEncoder *enc, JSINT32 value)
  335. {
  336. char* wstr;
  337. JSUINT32 uvalue = (value < 0) ? -value : value;
  338. wstr = enc->offset;
  339. // Conversion. Number is reversed.
  340. do *wstr++ = (char)(48 + (uvalue % 10)); while(uvalue /= 10);
  341. if (value < 0) *wstr++ = '-';
  342. // Reverse string
  343. strreverse(enc->offset,wstr - 1);
  344. enc->offset += (wstr - (enc->offset));
  345. }
  346. void Buffer_AppendLongUnchecked(JSONObjectEncoder *enc, JSINT64 value)
  347. {
  348. char* wstr;
  349. JSUINT64 uvalue = (value < 0) ? -value : value;
  350. wstr = enc->offset;
  351. // Conversion. Number is reversed.
  352. do *wstr++ = (char)(48 + (uvalue % 10ULL)); while(uvalue /= 10ULL);
  353. if (value < 0) *wstr++ = '-';
  354. // Reverse string
  355. strreverse(enc->offset,wstr - 1);
  356. enc->offset += (wstr - (enc->offset));
  357. }
  358. int Buffer_AppendDoubleUnchecked(JSOBJ obj, JSONObjectEncoder *enc, double value)
  359. {
  360. /* if input is larger than thres_max, revert to exponential */
  361. const double thres_max = (double)(0x7FFFFFFF);
  362. int count;
  363. double diff = 0.0;
  364. char* str = enc->offset;
  365. char* wstr = str;
  366. int whole;
  367. double tmp;
  368. uint32_t frac;
  369. int neg;
  370. double pow10;
  371. if (value == HUGE_VAL || value == -HUGE_VAL)
  372. {
  373. SetError (obj, enc, "Invalid Inf value when encoding double");
  374. return FALSE;
  375. }
  376. if (! (value == value))
  377. {
  378. SetError (obj, enc, "Invalid Nan value when encoding double");
  379. return FALSE;
  380. }
  381. /* we'll work in positive values and deal with the
  382. negative sign issue later */
  383. neg = 0;
  384. if (value < 0)
  385. {
  386. neg = 1;
  387. value = -value;
  388. }
  389. pow10 = g_pow10[enc->doublePrecision];
  390. whole = (int) value;
  391. tmp = (value - whole) * pow10;
  392. frac = (uint32_t)(tmp);
  393. diff = tmp - frac;
  394. if (diff > 0.5)
  395. {
  396. ++frac;
  397. /* handle rollover, e.g. case 0.99 with prec 1 is 1.0 */
  398. if (frac >= pow10)
  399. {
  400. frac = 0;
  401. ++whole;
  402. }
  403. }
  404. else
  405. if (diff == 0.5 && ((frac == 0) || (frac & 1)))
  406. {
  407. /* if halfway, round up if odd, OR
  408. if last digit is 0. That last part is strange */
  409. ++frac;
  410. }
  411. /* for very large numbers switch back to native sprintf for exponentials.
  412. anyone want to write code to replace this? */
  413. /*
  414. normal printf behavior is to print EVERY whole number digit
  415. which can be 100s of characters overflowing your buffers == bad
  416. */
  417. if (value > thres_max)
  418. {
  419. enc->offset += sprintf(str, "%e", neg ? -value : value);
  420. return TRUE;
  421. }
  422. if (enc->doublePrecision == 0)
  423. {
  424. diff = value - whole;
  425. if (diff > 0.5)
  426. {
  427. /* greater than 0.5, round up, e.g. 1.6 -> 2 */
  428. ++whole;
  429. }
  430. else
  431. if (diff == 0.5 && (whole & 1))
  432. {
  433. /* exactly 0.5 and ODD, then round up */
  434. /* 1.5 -> 2, but 2.5 -> 2 */
  435. ++whole;
  436. }
  437. //vvvvvvvvvvvvvvvvvvv Diff from modp_dto2
  438. }
  439. else
  440. if (frac)
  441. {
  442. count = enc->doublePrecision;
  443. // now do fractional part, as an unsigned number
  444. // we know it is not 0 but we can have leading zeros, these
  445. // should be removed
  446. while (!(frac % 10))
  447. {
  448. --count;
  449. frac /= 10;
  450. }
  451. //^^^^^^^^^^^^^^^^^^^ Diff from modp_dto2
  452. // now do fractional part, as an unsigned number
  453. do
  454. {
  455. --count;
  456. *wstr++ = (char)(48 + (frac % 10));
  457. } while (frac /= 10);
  458. // add extra 0s
  459. while (count-- > 0)
  460. {
  461. *wstr++ = '0';
  462. }
  463. // add decimal
  464. *wstr++ = '.';
  465. }
  466. else
  467. {
  468. *wstr++ = '0';
  469. *wstr++ = '.';
  470. }
  471. // do whole part
  472. // Take care of sign
  473. // Conversion. Number is reversed.
  474. do *wstr++ = (char)(48 + (whole % 10)); while (whole /= 10);
  475. if (neg)
  476. {
  477. *wstr++ = '-';
  478. }
  479. strreverse(str, wstr-1);
  480. enc->offset += (wstr - (enc->offset));
  481. return TRUE;
  482. }
  483. /*
  484. FIXME:
  485. Handle integration functions returning NULL here */
  486. /*
  487. FIXME:
  488. Perhaps implement recursion detection */
  489. void encode(JSOBJ obj, JSONObjectEncoder *enc, const char *name, size_t cbName)
  490. {
  491. JSONTypeContext tc;
  492. size_t szlen;
  493. if (enc->level > enc->recursionMax)
  494. {
  495. SetError (obj, enc, "Maximum recursion level reached");
  496. return;
  497. }
  498. /*
  499. This reservation must hold
  500. length of _name as encoded worst case +
  501. maxLength of double to string OR maxLength of JSLONG to string
  502. Since input is assumed to be UTF-8 the worst character length is:
  503. 4 bytes (of UTF-8) => "\uXXXX\uXXXX" (12 bytes)
  504. */
  505. Buffer_Reserve(enc, 256 + (((cbName / 4) + 1) * 12));
  506. if (name)
  507. {
  508. Buffer_AppendCharUnchecked(enc, '\"');
  509. if (enc->forceASCII)
  510. {
  511. if (!Buffer_EscapeStringValidated(obj, enc, name, name + cbName))
  512. {
  513. return;
  514. }
  515. }
  516. else
  517. {
  518. if (!Buffer_EscapeStringUnvalidated(obj, enc, name, name + cbName))
  519. {
  520. return;
  521. }
  522. }
  523. Buffer_AppendCharUnchecked(enc, '\"');
  524. Buffer_AppendCharUnchecked (enc, ':');
  525. #ifndef JSON_NO_EXTRA_WHITESPACE
  526. Buffer_AppendCharUnchecked (enc, ' ');
  527. #endif
  528. }
  529. enc->beginTypeContext(obj, &tc);
  530. switch (tc.type)
  531. {
  532. case JT_INVALID:
  533. return;
  534. case JT_ARRAY:
  535. {
  536. int count = 0;
  537. JSOBJ iterObj;
  538. enc->iterBegin(obj, &tc);
  539. Buffer_AppendCharUnchecked (enc, '[');
  540. while (enc->iterNext(obj, &tc))
  541. {
  542. if (count > 0)
  543. {
  544. Buffer_AppendCharUnchecked (enc, ',');
  545. #ifndef JSON_NO_EXTRA_WHITESPACE
  546. Buffer_AppendCharUnchecked (buffer, ' ');
  547. #endif
  548. }
  549. iterObj = enc->iterGetValue(obj, &tc);
  550. enc->level ++;
  551. encode (iterObj, enc, NULL, 0);
  552. count ++;
  553. }
  554. enc->iterEnd(obj, &tc);
  555. Buffer_AppendCharUnchecked (enc, ']');
  556. break;
  557. }
  558. case JT_OBJECT:
  559. {
  560. int count = 0;
  561. JSOBJ iterObj;
  562. char *objName;
  563. enc->iterBegin(obj, &tc);
  564. Buffer_AppendCharUnchecked (enc, '{');
  565. while (enc->iterNext(obj, &tc))
  566. {
  567. if (count > 0)
  568. {
  569. Buffer_AppendCharUnchecked (enc, ',');
  570. #ifndef JSON_NO_EXTRA_WHITESPACE
  571. Buffer_AppendCharUnchecked (enc, ' ');
  572. #endif
  573. }
  574. iterObj = enc->iterGetValue(obj, &tc);
  575. objName = enc->iterGetName(obj, &tc, &szlen);
  576. enc->level ++;
  577. encode (iterObj, enc, objName, szlen);
  578. count ++;
  579. }
  580. enc->iterEnd(obj, &tc);
  581. Buffer_AppendCharUnchecked (enc, '}');
  582. break;
  583. }
  584. case JT_LONG:
  585. {
  586. Buffer_AppendLongUnchecked (enc, enc->getLongValue(obj, &tc));
  587. break;
  588. }
  589. case JT_INT:
  590. {
  591. Buffer_AppendIntUnchecked (enc, enc->getIntValue(obj, &tc));
  592. break;
  593. }
  594. case JT_TRUE:
  595. {
  596. Buffer_AppendCharUnchecked (enc, 't');
  597. Buffer_AppendCharUnchecked (enc, 'r');
  598. Buffer_AppendCharUnchecked (enc, 'u');
  599. Buffer_AppendCharUnchecked (enc, 'e');
  600. break;
  601. }
  602. case JT_FALSE:
  603. {
  604. Buffer_AppendCharUnchecked (enc, 'f');
  605. Buffer_AppendCharUnchecked (enc, 'a');
  606. Buffer_AppendCharUnchecked (enc, 'l');
  607. Buffer_AppendCharUnchecked (enc, 's');
  608. Buffer_AppendCharUnchecked (enc, 'e');
  609. break;
  610. }
  611. case JT_NULL:
  612. {
  613. Buffer_AppendCharUnchecked (enc, 'n');
  614. Buffer_AppendCharUnchecked (enc, 'u');
  615. Buffer_AppendCharUnchecked (enc, 'l');
  616. Buffer_AppendCharUnchecked (enc, 'l');
  617. break;
  618. }
  619. case JT_DOUBLE:
  620. {
  621. if (!Buffer_AppendDoubleUnchecked (obj, enc, enc->getDoubleValue(obj, &tc)))
  622. {
  623. enc->endTypeContext(obj, &tc);
  624. enc->level --;
  625. return;
  626. }
  627. break;
  628. }
  629. case JT_UTF8:
  630. {
  631. const char *value = enc->getStringValue(obj, &tc, &szlen);
  632. Buffer_Reserve(enc, ((szlen / 4) + 1) * 12);
  633. Buffer_AppendCharUnchecked (enc, '\"');
  634. if (enc->forceASCII)
  635. {
  636. if (!Buffer_EscapeStringValidated(obj, enc, value, value + szlen))
  637. {
  638. enc->endTypeContext(obj, &tc);
  639. enc->level --;
  640. return;
  641. }
  642. }
  643. else
  644. {
  645. if (!Buffer_EscapeStringUnvalidated(obj, enc, value, value + szlen))
  646. {
  647. enc->endTypeContext(obj, &tc);
  648. enc->level --;
  649. return;
  650. }
  651. }
  652. Buffer_AppendCharUnchecked (enc, '\"');
  653. break;
  654. }
  655. }
  656. enc->endTypeContext(obj, &tc);
  657. enc->level --;
  658. }
  659. char *JSON_EncodeObject(JSOBJ obj, JSONObjectEncoder *enc, char *_buffer, size_t _cbBuffer)
  660. {
  661. enc->malloc = enc->malloc ? enc->malloc : malloc;
  662. enc->free = enc->free ? enc->free : free;
  663. enc->realloc = enc->realloc ? enc->realloc : realloc;
  664. enc->errorMsg = NULL;
  665. enc->errorObj = NULL;
  666. enc->level = 0;
  667. if (enc->recursionMax < 1)
  668. {
  669. enc->recursionMax = JSON_MAX_RECURSION_DEPTH;
  670. }
  671. if (enc->doublePrecision < 0 ||
  672. enc->doublePrecision > JSON_DOUBLE_MAX_DECIMALS)
  673. {
  674. enc->doublePrecision = JSON_DOUBLE_MAX_DECIMALS;
  675. }
  676. if (_buffer == NULL)
  677. {
  678. _cbBuffer = 32768;
  679. enc->start = (char *) enc->malloc (_cbBuffer);
  680. enc->heap = 1;
  681. }
  682. else
  683. {
  684. enc->start = _buffer;
  685. enc->heap = 0;
  686. }
  687. enc->end = enc->start + _cbBuffer;
  688. enc->offset = enc->start;
  689. encode (obj, enc, NULL, 0);
  690. Buffer_Reserve(enc, 1);
  691. Buffer_AppendCharUnchecked(enc, '\0');
  692. return enc->start;
  693. }