… | |
… | |
12 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
12 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
13 | # define snprintf _snprintf // C compilers have this in stdio.h |
13 | # define snprintf _snprintf // C compilers have this in stdio.h |
14 | #endif |
14 | #endif |
15 | |
15 | |
16 | // some old perls do not have this, try to make it work, no |
16 | // some old perls do not have this, try to make it work, no |
17 | // guarentees, though. if it breaks, you get to keep the pieces. |
17 | // guarantees, though. if it breaks, you get to keep the pieces. |
18 | #ifndef UTF8_MAXBYTES |
18 | #ifndef UTF8_MAXBYTES |
19 | # define UTF8_MAXBYTES 13 |
19 | # define UTF8_MAXBYTES 13 |
20 | #endif |
20 | #endif |
21 | |
21 | |
|
|
22 | // three extra for rounding, sign, and end of string |
22 | #define IVUV_MAXCHARS (sizeof (UV) * CHAR_BIT * 28 / 93 + 2) |
23 | #define IVUV_MAXCHARS (sizeof (UV) * CHAR_BIT * 28 / 93 + 3) |
23 | |
24 | |
24 | #define F_ASCII 0x00000001UL |
25 | #define F_ASCII 0x00000001UL |
25 | #define F_LATIN1 0x00000002UL |
26 | #define F_LATIN1 0x00000002UL |
26 | #define F_UTF8 0x00000004UL |
27 | #define F_UTF8 0x00000004UL |
27 | #define F_INDENT 0x00000008UL |
28 | #define F_INDENT 0x00000008UL |
… | |
… | |
41 | #define INIT_SIZE 32 // initial scalar size to be allocated |
42 | #define INIT_SIZE 32 // initial scalar size to be allocated |
42 | #define INDENT_STEP 3 // spaces per indentation level |
43 | #define INDENT_STEP 3 // spaces per indentation level |
43 | |
44 | |
44 | #define SHORT_STRING_LEN 16384 // special-case strings of up to this size |
45 | #define SHORT_STRING_LEN 16384 // special-case strings of up to this size |
45 | |
46 | |
|
|
47 | #define DECODE_WANTS_OCTETS(json) ((json)->flags & F_UTF8) |
|
|
48 | |
46 | #define SB do { |
49 | #define SB do { |
47 | #define SE } while (0) |
50 | #define SE } while (0) |
48 | |
51 | |
49 | #if __GNUC__ >= 3 |
52 | #if __GNUC__ >= 3 |
50 | # define expect(expr,value) __builtin_expect ((expr), (value)) |
53 | # define expect(expr,value) __builtin_expect ((expr), (value)) |
… | |
… | |
69 | #else |
72 | #else |
70 | # define JSON_SLOW 0 |
73 | # define JSON_SLOW 0 |
71 | # define JSON_STASH json_stash |
74 | # define JSON_STASH json_stash |
72 | #endif |
75 | #endif |
73 | |
76 | |
|
|
77 | // the amount of HEs to allocate on the stack, when sorting keys |
|
|
78 | #define STACK_HES 64 |
|
|
79 | |
74 | static HV *json_stash, *json_boolean_stash; // JSON::XS:: |
80 | static HV *json_stash, *json_boolean_stash; // JSON::XS:: |
75 | static SV *json_true, *json_false; |
81 | static SV *json_true, *json_false; |
76 | |
82 | |
77 | enum { |
83 | enum { |
78 | INCR_M_WS = 0, // initial whitespace skipping, must be 0 |
84 | INCR_M_WS = 0, // initial whitespace skipping, must be 0 |
79 | INCR_M_STR, // inside string |
85 | INCR_M_STR, // inside string |
80 | INCR_M_BS, // inside backslash |
86 | INCR_M_BS, // inside backslash |
|
|
87 | INCR_M_C0, // inside comment in initial whitespace sequence |
|
|
88 | INCR_M_C1, // inside comment in other places |
81 | INCR_M_JSON // outside anything, count nesting |
89 | INCR_M_JSON // outside anything, count nesting |
82 | }; |
90 | }; |
83 | |
91 | |
84 | #define INCR_DONE(json) ((json)->incr_nest <= 0 && (json)->incr_mode == INCR_M_JSON) |
92 | #define INCR_DONE(json) ((json)->incr_nest <= 0 && (json)->incr_mode == INCR_M_JSON) |
85 | |
93 | |
… | |
… | |
175 | *s++ = 0x80 | ( ch & 0x3f); |
183 | *s++ = 0x80 | ( ch & 0x3f); |
176 | |
184 | |
177 | return s; |
185 | return s; |
178 | } |
186 | } |
179 | |
187 | |
|
|
188 | // convert offset pointer to character index, sv must be string |
|
|
189 | static STRLEN |
|
|
190 | ptr_to_index (SV *sv, char *offset) |
|
|
191 | { |
|
|
192 | return SvUTF8 (sv) |
|
|
193 | ? utf8_distance (offset, SvPVX (sv)) |
|
|
194 | : offset - SvPVX (sv); |
|
|
195 | } |
|
|
196 | |
|
|
197 | ///////////////////////////////////////////////////////////////////////////// |
|
|
198 | // fp hell |
|
|
199 | |
|
|
200 | // scan a group of digits, and a trailing exponent |
|
|
201 | static void |
|
|
202 | json_atof_scan1 (const char *s, NV *accum, int *expo, int postdp, int maxdepth) |
|
|
203 | { |
|
|
204 | UV uaccum = 0; |
|
|
205 | int eaccum = 0; |
|
|
206 | |
|
|
207 | // if we recurse too deep, skip all remaining digits |
|
|
208 | // to avoid a stack overflow attack |
|
|
209 | if (expect_false (--maxdepth <= 0)) |
|
|
210 | while (((U8)*s - '0') < 10) |
|
|
211 | ++s; |
|
|
212 | |
|
|
213 | for (;;) |
|
|
214 | { |
|
|
215 | U8 dig = (U8)*s - '0'; |
|
|
216 | |
|
|
217 | if (expect_false (dig >= 10)) |
|
|
218 | { |
|
|
219 | if (dig == (U8)((U8)'.' - (U8)'0')) |
|
|
220 | { |
|
|
221 | ++s; |
|
|
222 | json_atof_scan1 (s, accum, expo, 1, maxdepth); |
|
|
223 | } |
|
|
224 | else if ((dig | ' ') == 'e' - '0') |
|
|
225 | { |
|
|
226 | int exp2 = 0; |
|
|
227 | int neg = 0; |
|
|
228 | |
|
|
229 | ++s; |
|
|
230 | |
|
|
231 | if (*s == '-') |
|
|
232 | { |
|
|
233 | ++s; |
|
|
234 | neg = 1; |
|
|
235 | } |
|
|
236 | else if (*s == '+') |
|
|
237 | ++s; |
|
|
238 | |
|
|
239 | while ((dig = (U8)*s - '0') < 10) |
|
|
240 | exp2 = exp2 * 10 + *s++ - '0'; |
|
|
241 | |
|
|
242 | *expo += neg ? -exp2 : exp2; |
|
|
243 | } |
|
|
244 | |
|
|
245 | break; |
|
|
246 | } |
|
|
247 | |
|
|
248 | ++s; |
|
|
249 | |
|
|
250 | uaccum = uaccum * 10 + dig; |
|
|
251 | ++eaccum; |
|
|
252 | |
|
|
253 | // if we have too many digits, then recurse for more |
|
|
254 | // we actually do this for rather few digits |
|
|
255 | if (uaccum >= (UV_MAX - 9) / 10) |
|
|
256 | { |
|
|
257 | if (postdp) *expo -= eaccum; |
|
|
258 | json_atof_scan1 (s, accum, expo, postdp, maxdepth); |
|
|
259 | if (postdp) *expo += eaccum; |
|
|
260 | |
|
|
261 | break; |
|
|
262 | } |
|
|
263 | } |
|
|
264 | |
|
|
265 | // this relies greatly on the quality of the pow () |
|
|
266 | // implementation of the platform, but a good |
|
|
267 | // implementation is hard to beat. |
|
|
268 | // (IEEE 754 conformant ones are required to be exact) |
|
|
269 | if (postdp) *expo -= eaccum; |
|
|
270 | *accum += uaccum * Perl_pow (10., *expo); |
|
|
271 | *expo += eaccum; |
|
|
272 | } |
|
|
273 | |
|
|
274 | static NV |
|
|
275 | json_atof (const char *s) |
|
|
276 | { |
|
|
277 | NV accum = 0.; |
|
|
278 | int expo = 0; |
|
|
279 | int neg = 0; |
|
|
280 | |
|
|
281 | if (*s == '-') |
|
|
282 | { |
|
|
283 | ++s; |
|
|
284 | neg = 1; |
|
|
285 | } |
|
|
286 | |
|
|
287 | // a recursion depth of ten gives us >>500 bits |
|
|
288 | json_atof_scan1 (s, &accum, &expo, 0, 10); |
|
|
289 | |
|
|
290 | return neg ? -accum : accum; |
|
|
291 | } |
180 | ///////////////////////////////////////////////////////////////////////////// |
292 | ///////////////////////////////////////////////////////////////////////////// |
181 | // encoder |
293 | // encoder |
182 | |
294 | |
183 | // structure used for encoding JSON |
295 | // structure used for encoding JSON |
184 | typedef struct |
296 | typedef struct |
… | |
… | |
194 | INLINE void |
306 | INLINE void |
195 | need (enc_t *enc, STRLEN len) |
307 | need (enc_t *enc, STRLEN len) |
196 | { |
308 | { |
197 | if (expect_false (enc->cur + len >= enc->end)) |
309 | if (expect_false (enc->cur + len >= enc->end)) |
198 | { |
310 | { |
199 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
311 | STRLEN cur = enc->cur - (char *)SvPVX (enc->sv); |
200 | SvGROW (enc->sv, cur + len + 1); |
312 | SvGROW (enc->sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1); |
201 | enc->cur = SvPVX (enc->sv) + cur; |
313 | enc->cur = SvPVX (enc->sv) + cur; |
202 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
314 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
203 | } |
315 | } |
204 | } |
316 | } |
205 | |
317 | |
… | |
… | |
371 | |
483 | |
372 | if (enc->indent >= enc->json.max_depth) |
484 | if (enc->indent >= enc->json.max_depth) |
373 | croak (ERR_NESTING_EXCEEDED); |
485 | croak (ERR_NESTING_EXCEEDED); |
374 | |
486 | |
375 | encode_ch (enc, '['); |
487 | encode_ch (enc, '['); |
376 | |
488 | |
377 | if (len >= 0) |
489 | if (len >= 0) |
378 | { |
490 | { |
379 | encode_nl (enc); ++enc->indent; |
491 | encode_nl (enc); ++enc->indent; |
380 | |
492 | |
381 | for (i = 0; i <= len; ++i) |
493 | for (i = 0; i <= len; ++i) |
… | |
… | |
393 | encode_comma (enc); |
505 | encode_comma (enc); |
394 | } |
506 | } |
395 | |
507 | |
396 | encode_nl (enc); --enc->indent; encode_indent (enc); |
508 | encode_nl (enc); --enc->indent; encode_indent (enc); |
397 | } |
509 | } |
398 | |
510 | |
399 | encode_ch (enc, ']'); |
511 | encode_ch (enc, ']'); |
400 | } |
512 | } |
401 | |
513 | |
402 | static void |
514 | static void |
403 | encode_hk (enc_t *enc, HE *he) |
515 | encode_hk (enc_t *enc, HE *he) |
… | |
… | |
407 | if (HeKLEN (he) == HEf_SVKEY) |
519 | if (HeKLEN (he) == HEf_SVKEY) |
408 | { |
520 | { |
409 | SV *sv = HeSVKEY (he); |
521 | SV *sv = HeSVKEY (he); |
410 | STRLEN len; |
522 | STRLEN len; |
411 | char *str; |
523 | char *str; |
412 | |
524 | |
413 | SvGETMAGIC (sv); |
525 | SvGETMAGIC (sv); |
414 | str = SvPV (sv, len); |
526 | str = SvPV (sv, len); |
415 | |
527 | |
416 | encode_str (enc, str, len, SvUTF8 (sv)); |
528 | encode_str (enc, str, len, SvUTF8 (sv)); |
417 | } |
529 | } |
… | |
… | |
462 | |
574 | |
463 | // for canonical output we have to sort by keys first |
575 | // for canonical output we have to sort by keys first |
464 | // actually, this is mostly due to the stupid so-called |
576 | // actually, this is mostly due to the stupid so-called |
465 | // security workaround added somewhere in 5.8.x |
577 | // security workaround added somewhere in 5.8.x |
466 | // that randomises hash orderings |
578 | // that randomises hash orderings |
467 | if (enc->json.flags & F_CANONICAL) |
579 | if (enc->json.flags & F_CANONICAL && !SvRMAGICAL (hv)) |
468 | { |
580 | { |
469 | int count = hv_iterinit (hv); |
581 | int count = hv_iterinit (hv); |
470 | |
582 | |
471 | if (SvMAGICAL (hv)) |
583 | if (SvMAGICAL (hv)) |
472 | { |
584 | { |
… | |
… | |
483 | } |
595 | } |
484 | |
596 | |
485 | if (count) |
597 | if (count) |
486 | { |
598 | { |
487 | int i, fast = 1; |
599 | int i, fast = 1; |
488 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
600 | HE *hes_stack [STACK_HES]; |
489 | HE **hes = _alloca (count * sizeof (HE)); |
601 | HE **hes = hes_stack; |
490 | #else |
602 | |
491 | HE *hes [count]; // if your compiler dies here, you need to enable C99 mode |
603 | // allocate larger arrays on the heap |
492 | #endif |
604 | if (count > STACK_HES) |
|
|
605 | { |
|
|
606 | SV *sv = sv_2mortal (NEWSV (0, count * sizeof (*hes))); |
|
|
607 | hes = (HE **)SvPVX (sv); |
|
|
608 | } |
493 | |
609 | |
494 | i = 0; |
610 | i = 0; |
495 | while ((he = hv_iternext (hv))) |
611 | while ((he = hv_iternext (hv))) |
496 | { |
612 | { |
497 | hes [i++] = he; |
613 | hes [i++] = he; |
… | |
… | |
716 | } |
832 | } |
717 | else |
833 | else |
718 | { |
834 | { |
719 | // large integer, use the (rather slow) snprintf way. |
835 | // large integer, use the (rather slow) snprintf way. |
720 | need (enc, IVUV_MAXCHARS); |
836 | need (enc, IVUV_MAXCHARS); |
721 | enc->cur += |
837 | enc->cur += |
722 | SvIsUV(sv) |
838 | SvIsUV(sv) |
723 | ? snprintf (enc->cur, IVUV_MAXCHARS, "%"UVuf, (UV)SvUVX (sv)) |
839 | ? snprintf (enc->cur, IVUV_MAXCHARS, "%"UVuf, (UV)SvUVX (sv)) |
724 | : snprintf (enc->cur, IVUV_MAXCHARS, "%"IVdf, (IV)SvIVX (sv)); |
840 | : snprintf (enc->cur, IVUV_MAXCHARS, "%"IVdf, (IV)SvIVX (sv)); |
725 | } |
841 | } |
726 | } |
842 | } |
727 | else if (SvROK (sv)) |
843 | else if (SvROK (sv)) |
728 | encode_rv (enc, SvRV (sv)); |
844 | encode_rv (enc, SvRV (sv)); |
729 | else if (!SvOK (sv) || enc->json.flags & F_ALLOW_UNKNOWN) |
845 | else if (!SvOK (sv) || enc->json.flags & F_ALLOW_UNKNOWN) |
730 | encode_str (enc, "null", 4, 0); |
846 | encode_str (enc, "null", 4, 0); |
731 | else |
847 | else |
732 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
848 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, check your input data", |
733 | SvPV_nolen (sv), SvFLAGS (sv)); |
849 | SvPV_nolen (sv), (unsigned int)SvFLAGS (sv)); |
734 | } |
850 | } |
735 | |
851 | |
736 | static SV * |
852 | static SV * |
737 | encode_json (SV *scalar, JSON *json) |
853 | encode_json (SV *scalar, JSON *json) |
738 | { |
854 | { |
… | |
… | |
750 | : enc.json.flags & F_LATIN1 ? 0x000100UL |
866 | : enc.json.flags & F_LATIN1 ? 0x000100UL |
751 | : 0x110000UL; |
867 | : 0x110000UL; |
752 | |
868 | |
753 | SvPOK_only (enc.sv); |
869 | SvPOK_only (enc.sv); |
754 | encode_sv (&enc, scalar); |
870 | encode_sv (&enc, scalar); |
|
|
871 | encode_nl (&enc); |
755 | |
872 | |
756 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
873 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
757 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
874 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
758 | |
875 | |
759 | if (!(enc.json.flags & (F_ASCII | F_LATIN1 | F_UTF8))) |
876 | if (!(enc.json.flags & (F_ASCII | F_LATIN1 | F_UTF8))) |
… | |
… | |
939 | else if (expect_true (ch >= 0x20 && ch < 0x80)) |
1056 | else if (expect_true (ch >= 0x20 && ch < 0x80)) |
940 | *cur++ = ch; |
1057 | *cur++ = ch; |
941 | else if (ch >= 0x80) |
1058 | else if (ch >= 0x80) |
942 | { |
1059 | { |
943 | STRLEN clen; |
1060 | STRLEN clen; |
944 | UV uch; |
|
|
945 | |
1061 | |
946 | --dec_cur; |
1062 | --dec_cur; |
947 | |
1063 | |
948 | uch = decode_utf8 (dec_cur, dec->end - dec_cur, &clen); |
1064 | decode_utf8 (dec_cur, dec->end - dec_cur, &clen); |
949 | if (clen == (STRLEN)-1) |
1065 | if (clen == (STRLEN)-1) |
950 | ERR ("malformed UTF-8 character in JSON string"); |
1066 | ERR ("malformed UTF-8 character in JSON string"); |
951 | |
1067 | |
952 | do |
1068 | do |
953 | *cur++ = *dec_cur++; |
1069 | *cur++ = *dec_cur++; |
… | |
… | |
970 | { |
1086 | { |
971 | STRLEN len = cur - buf; |
1087 | STRLEN len = cur - buf; |
972 | |
1088 | |
973 | if (sv) |
1089 | if (sv) |
974 | { |
1090 | { |
975 | SvGROW (sv, SvCUR (sv) + len + 1); |
1091 | STRLEN cur = SvCUR (sv); |
|
|
1092 | |
|
|
1093 | if (SvLEN (sv) <= cur + len) |
|
|
1094 | SvGROW (sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1); |
|
|
1095 | |
976 | memcpy (SvPVX (sv) + SvCUR (sv), buf, len); |
1096 | memcpy (SvPVX (sv) + SvCUR (sv), buf, len); |
977 | SvCUR_set (sv, SvCUR (sv) + len); |
1097 | SvCUR_set (sv, SvCUR (sv) + len); |
978 | } |
1098 | } |
979 | else |
1099 | else |
980 | sv = newSVpvn (buf, len); |
1100 | sv = newSVpvn (buf, len); |
… | |
… | |
1071 | |
1191 | |
1072 | // special case the rather common 1..5-digit-int case |
1192 | // special case the rather common 1..5-digit-int case |
1073 | if (*start == '-') |
1193 | if (*start == '-') |
1074 | switch (len) |
1194 | switch (len) |
1075 | { |
1195 | { |
1076 | case 2: return newSViv (-( start [1] - '0' * 1)); |
1196 | case 2: return newSViv (-(IV)( start [1] - '0' * 1)); |
1077 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
1197 | case 3: return newSViv (-(IV)( start [1] * 10 + start [2] - '0' * 11)); |
1078 | case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); |
1198 | case 4: return newSViv (-(IV)( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); |
1079 | case 5: return newSViv (-( start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
1199 | case 5: return newSViv (-(IV)( start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
1080 | case 6: return newSViv (-(start [1] * 10000 + start [2] * 1000 + start [3] * 100 + start [4] * 10 + start [5] - '0' * 11111)); |
1200 | case 6: return newSViv (-(IV)(start [1] * 10000 + start [2] * 1000 + start [3] * 100 + start [4] * 10 + start [5] - '0' * 11111)); |
1081 | } |
1201 | } |
1082 | else |
1202 | else |
1083 | switch (len) |
1203 | switch (len) |
1084 | { |
1204 | { |
1085 | case 1: return newSViv ( start [0] - '0' * 1); |
1205 | case 1: return newSViv ( start [0] - '0' * 1); |
1086 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
1206 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
1087 | case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); |
1207 | case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); |
1088 | case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
1208 | case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
1089 | case 5: return newSViv ( start [0] * 10000 + start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 11111); |
1209 | case 5: return newSViv ( start [0] * 10000 + start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 11111); |
1090 | } |
1210 | } |
1091 | |
1211 | |
1092 | { |
1212 | { |
1093 | UV uv; |
1213 | UV uv; |
1094 | int numtype = grok_number (start, len, &uv); |
1214 | int numtype = grok_number (start, len, &uv); |
… | |
… | |
1103 | } |
1223 | } |
1104 | |
1224 | |
1105 | len -= *start == '-' ? 1 : 0; |
1225 | len -= *start == '-' ? 1 : 0; |
1106 | |
1226 | |
1107 | // does not fit into IV or UV, try NV |
1227 | // does not fit into IV or UV, try NV |
1108 | if ((sizeof (NV) == sizeof (double) && DBL_DIG >= len) |
1228 | if (len <= NV_DIG) |
1109 | #if defined (LDBL_DIG) |
|
|
1110 | || (sizeof (NV) == sizeof (long double) && LDBL_DIG >= len) |
|
|
1111 | #endif |
|
|
1112 | ) |
|
|
1113 | // fits into NV without loss of precision |
1229 | // fits into NV without loss of precision |
1114 | return newSVnv (Atof (start)); |
1230 | return newSVnv (json_atof (start)); |
1115 | |
1231 | |
1116 | // everything else fails, convert it to a string |
1232 | // everything else fails, convert it to a string |
1117 | return newSVpvn (start, dec->cur - start); |
1233 | return newSVpvn (start, dec->cur - start); |
1118 | } |
1234 | } |
1119 | |
1235 | |
1120 | // loss of precision here |
1236 | // loss of precision here |
1121 | return newSVnv (Atof (start)); |
1237 | return newSVnv (json_atof (start)); |
1122 | |
1238 | |
1123 | fail: |
1239 | fail: |
1124 | return 0; |
1240 | return 0; |
1125 | } |
1241 | } |
1126 | |
1242 | |
… | |
… | |
1150 | if (*dec->cur == ']') |
1266 | if (*dec->cur == ']') |
1151 | { |
1267 | { |
1152 | ++dec->cur; |
1268 | ++dec->cur; |
1153 | break; |
1269 | break; |
1154 | } |
1270 | } |
1155 | |
1271 | |
1156 | if (*dec->cur != ',') |
1272 | if (*dec->cur != ',') |
1157 | ERR (", or ] expected while parsing array"); |
1273 | ERR (", or ] expected while parsing array"); |
1158 | |
1274 | |
1159 | ++dec->cur; |
1275 | ++dec->cur; |
1160 | |
1276 | |
… | |
… | |
1295 | dSP; |
1411 | dSP; |
1296 | int count; |
1412 | int count; |
1297 | |
1413 | |
1298 | ENTER; SAVETMPS; PUSHMARK (SP); |
1414 | ENTER; SAVETMPS; PUSHMARK (SP); |
1299 | XPUSHs (HeVAL (he)); |
1415 | XPUSHs (HeVAL (he)); |
|
|
1416 | sv_2mortal (sv); |
1300 | |
1417 | |
1301 | PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; |
1418 | PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; |
1302 | |
1419 | |
1303 | if (count == 1) |
1420 | if (count == 1) |
1304 | { |
1421 | { |
1305 | sv = newSVsv (POPs); |
1422 | sv = newSVsv (POPs); |
1306 | FREETMPS; LEAVE; |
1423 | FREETMPS; LEAVE; |
1307 | return sv; |
1424 | return sv; |
1308 | } |
1425 | } |
1309 | |
1426 | |
|
|
1427 | SvREFCNT_inc (sv); |
1310 | FREETMPS; LEAVE; |
1428 | FREETMPS; LEAVE; |
1311 | } |
1429 | } |
1312 | } |
1430 | } |
1313 | |
1431 | |
1314 | if (dec->json.cb_object) |
1432 | if (dec->json.cb_object) |
… | |
… | |
1346 | { |
1464 | { |
1347 | // the beauty of JSON: you need exactly one character lookahead |
1465 | // the beauty of JSON: you need exactly one character lookahead |
1348 | // to parse everything. |
1466 | // to parse everything. |
1349 | switch (*dec->cur) |
1467 | switch (*dec->cur) |
1350 | { |
1468 | { |
1351 | case '"': ++dec->cur; return decode_str (dec); |
1469 | case '"': ++dec->cur; return decode_str (dec); |
1352 | case '[': ++dec->cur; return decode_av (dec); |
1470 | case '[': ++dec->cur; return decode_av (dec); |
1353 | case '{': ++dec->cur; return decode_hv (dec); |
1471 | case '{': ++dec->cur; return decode_hv (dec); |
1354 | |
1472 | |
1355 | case '-': |
1473 | case '-': |
1356 | case '0': case '1': case '2': case '3': case '4': |
1474 | case '0': case '1': case '2': case '3': case '4': |
1357 | case '5': case '6': case '7': case '8': case '9': |
1475 | case '5': case '6': case '7': case '8': case '9': |
… | |
… | |
1404 | fail: |
1522 | fail: |
1405 | return 0; |
1523 | return 0; |
1406 | } |
1524 | } |
1407 | |
1525 | |
1408 | static SV * |
1526 | static SV * |
1409 | decode_json (SV *string, JSON *json, STRLEN *offset_return) |
1527 | decode_json (SV *string, JSON *json, char **offset_return) |
1410 | { |
1528 | { |
1411 | dec_t dec; |
1529 | dec_t dec; |
1412 | STRLEN offset; |
|
|
1413 | SV *sv; |
1530 | SV *sv; |
1414 | |
1531 | |
|
|
1532 | /* work around bugs in 5.10 where manipulating magic values |
|
|
1533 | * makes perl ignore the magic in subsequent accesses. |
|
|
1534 | * also make a copy of non-PV values, to get them into a clean |
|
|
1535 | * state (SvPV should do that, but it's buggy, see below). |
|
|
1536 | */ |
1415 | SvGETMAGIC (string); |
1537 | /*SvGETMAGIC (string);*/ |
|
|
1538 | if (SvMAGICAL (string) || !SvPOK (string)) |
|
|
1539 | string = sv_2mortal (newSVsv (string)); |
|
|
1540 | |
1416 | SvUPGRADE (string, SVt_PV); |
1541 | SvUPGRADE (string, SVt_PV); |
1417 | |
1542 | |
1418 | /* work around a bug in perl 5.10, which causes SvCUR to fail an |
1543 | /* work around a bug in perl 5.10, which causes SvCUR to fail an |
1419 | * assertion with -DDEBUGGING, although SvCUR is documented to |
1544 | * assertion with -DDEBUGGING, although SvCUR is documented to |
1420 | * return the xpv_cur field which certainly exists after upgrading. |
1545 | * return the xpv_cur field which certainly exists after upgrading. |
… | |
… | |
1423 | * and hope for the best. |
1548 | * and hope for the best. |
1424 | * Damnit, SvPV_nolen still trips over yet another assertion. This |
1549 | * Damnit, SvPV_nolen still trips over yet another assertion. This |
1425 | * assertion business is seriously broken, try yet another workaround |
1550 | * assertion business is seriously broken, try yet another workaround |
1426 | * for the broken -DDEBUGGING. |
1551 | * for the broken -DDEBUGGING. |
1427 | */ |
1552 | */ |
|
|
1553 | { |
1428 | #ifdef DEBUGGING |
1554 | #ifdef DEBUGGING |
1429 | offset = SvOK (string) ? sv_len (string) : 0; |
1555 | STRLEN offset = SvOK (string) ? sv_len (string) : 0; |
1430 | #else |
1556 | #else |
1431 | offset = SvCUR (string); |
1557 | STRLEN offset = SvCUR (string); |
1432 | #endif |
1558 | #endif |
1433 | |
1559 | |
1434 | if (offset > json->max_size && json->max_size) |
1560 | if (offset > json->max_size && json->max_size) |
1435 | croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", |
1561 | croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", |
1436 | (unsigned long)SvCUR (string), (unsigned long)json->max_size); |
1562 | (unsigned long)SvCUR (string), (unsigned long)json->max_size); |
|
|
1563 | } |
1437 | |
1564 | |
1438 | if (json->flags & F_UTF8) |
1565 | if (DECODE_WANTS_OCTETS (json)) |
1439 | sv_utf8_downgrade (string, 0); |
1566 | sv_utf8_downgrade (string, 0); |
1440 | else |
1567 | else |
1441 | sv_utf8_upgrade (string); |
1568 | sv_utf8_upgrade (string); |
1442 | |
1569 | |
1443 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
1570 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
… | |
… | |
1453 | |
1580 | |
1454 | *dec.end = 0; // this should basically be a nop, too, but make sure it's there |
1581 | *dec.end = 0; // this should basically be a nop, too, but make sure it's there |
1455 | |
1582 | |
1456 | decode_ws (&dec); |
1583 | decode_ws (&dec); |
1457 | sv = decode_sv (&dec); |
1584 | sv = decode_sv (&dec); |
|
|
1585 | |
|
|
1586 | if (offset_return) |
|
|
1587 | *offset_return = dec.cur; |
1458 | |
1588 | |
1459 | if (!(offset_return || !sv)) |
1589 | if (!(offset_return || !sv)) |
1460 | { |
1590 | { |
1461 | // check for trailing garbage |
1591 | // check for trailing garbage |
1462 | decode_ws (&dec); |
1592 | decode_ws (&dec); |
… | |
… | |
1465 | { |
1595 | { |
1466 | dec.err = "garbage after JSON object"; |
1596 | dec.err = "garbage after JSON object"; |
1467 | SvREFCNT_dec (sv); |
1597 | SvREFCNT_dec (sv); |
1468 | sv = 0; |
1598 | sv = 0; |
1469 | } |
1599 | } |
1470 | } |
|
|
1471 | |
|
|
1472 | if (offset_return || !sv) |
|
|
1473 | { |
|
|
1474 | offset = dec.json.flags & F_UTF8 |
|
|
1475 | ? dec.cur - SvPVX (string) |
|
|
1476 | : utf8_distance (dec.cur, SvPVX (string)); |
|
|
1477 | |
|
|
1478 | if (offset_return) |
|
|
1479 | *offset_return = offset; |
|
|
1480 | } |
1600 | } |
1481 | |
1601 | |
1482 | if (!sv) |
1602 | if (!sv) |
1483 | { |
1603 | { |
1484 | SV *uni = sv_newmortal (); |
1604 | SV *uni = sv_newmortal (); |
… | |
… | |
1490 | SAVEVPTR (PL_curcop); |
1610 | SAVEVPTR (PL_curcop); |
1491 | PL_curcop = &cop; |
1611 | PL_curcop = &cop; |
1492 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
1612 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
1493 | LEAVE; |
1613 | LEAVE; |
1494 | |
1614 | |
1495 | croak ("%s, at character offset %d [\"%s\"]", |
1615 | croak ("%s, at character offset %d (before \"%s\")", |
1496 | dec.err, |
1616 | dec.err, |
1497 | (int)offset, |
1617 | (int)ptr_to_index (string, dec.cur), |
1498 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1618 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1499 | } |
1619 | } |
1500 | |
1620 | |
1501 | sv = sv_2mortal (sv); |
1621 | sv = sv_2mortal (sv); |
1502 | |
1622 | |
… | |
… | |
1511 | |
1631 | |
1512 | static void |
1632 | static void |
1513 | incr_parse (JSON *self) |
1633 | incr_parse (JSON *self) |
1514 | { |
1634 | { |
1515 | const char *p = SvPVX (self->incr_text) + self->incr_pos; |
1635 | const char *p = SvPVX (self->incr_text) + self->incr_pos; |
|
|
1636 | |
|
|
1637 | // the state machine here is a bit convoluted and could be simplified a lot |
|
|
1638 | // but this would make it slower, so... |
1516 | |
1639 | |
1517 | for (;;) |
1640 | for (;;) |
1518 | { |
1641 | { |
1519 | //printf ("loop pod %d *p<%c><%s>, mode %d nest %d\n", p - SvPVX (self->incr_text), *p, p, self->incr_mode, self->incr_nest);//D |
1642 | //printf ("loop pod %d *p<%c><%s>, mode %d nest %d\n", p - SvPVX (self->incr_text), *p, p, self->incr_mode, self->incr_nest);//D |
1520 | switch (self->incr_mode) |
1643 | switch (self->incr_mode) |
1521 | { |
1644 | { |
1522 | // only used for intiial whitespace skipping |
1645 | // only used for initial whitespace skipping |
1523 | case INCR_M_WS: |
1646 | case INCR_M_WS: |
1524 | for (;;) |
1647 | for (;;) |
1525 | { |
1648 | { |
1526 | if (*p > 0x20) |
1649 | if (*p > 0x20) |
1527 | { |
1650 | { |
|
|
1651 | if (*p == '#') |
|
|
1652 | { |
|
|
1653 | self->incr_mode = INCR_M_C0; |
|
|
1654 | goto incr_m_c; |
|
|
1655 | } |
|
|
1656 | else |
|
|
1657 | { |
1528 | self->incr_mode = INCR_M_JSON; |
1658 | self->incr_mode = INCR_M_JSON; |
1529 | goto incr_m_json; |
1659 | goto incr_m_json; |
|
|
1660 | } |
1530 | } |
1661 | } |
1531 | else if (!*p) |
1662 | else if (!*p) |
1532 | goto interrupt; |
1663 | goto interrupt; |
1533 | |
1664 | |
1534 | ++p; |
1665 | ++p; |
… | |
… | |
1540 | goto interrupt; |
1671 | goto interrupt; |
1541 | |
1672 | |
1542 | ++p; |
1673 | ++p; |
1543 | self->incr_mode = INCR_M_STR; |
1674 | self->incr_mode = INCR_M_STR; |
1544 | goto incr_m_str; |
1675 | goto incr_m_str; |
|
|
1676 | |
|
|
1677 | // inside #-style comments |
|
|
1678 | case INCR_M_C0: |
|
|
1679 | case INCR_M_C1: |
|
|
1680 | incr_m_c: |
|
|
1681 | for (;;) |
|
|
1682 | { |
|
|
1683 | if (*p == '\n') |
|
|
1684 | { |
|
|
1685 | self->incr_mode = self->incr_mode == INCR_M_C0 ? INCR_M_WS : INCR_M_JSON; |
|
|
1686 | break; |
|
|
1687 | } |
|
|
1688 | else if (!*p) |
|
|
1689 | goto interrupt; |
|
|
1690 | |
|
|
1691 | ++p; |
|
|
1692 | } |
|
|
1693 | |
|
|
1694 | break; |
1545 | |
1695 | |
1546 | // inside a string |
1696 | // inside a string |
1547 | case INCR_M_STR: |
1697 | case INCR_M_STR: |
1548 | incr_m_str: |
1698 | incr_m_str: |
1549 | for (;;) |
1699 | for (;;) |
… | |
… | |
1608 | |
1758 | |
1609 | case ']': |
1759 | case ']': |
1610 | case '}': |
1760 | case '}': |
1611 | if (--self->incr_nest <= 0) |
1761 | if (--self->incr_nest <= 0) |
1612 | goto interrupt; |
1762 | goto interrupt; |
|
|
1763 | break; |
|
|
1764 | |
|
|
1765 | case '#': |
|
|
1766 | self->incr_mode = INCR_M_C1; |
|
|
1767 | goto incr_m_c; |
1613 | } |
1768 | } |
1614 | } |
1769 | } |
1615 | } |
1770 | } |
1616 | |
1771 | |
1617 | modechange: |
1772 | modechange: |
1618 | ; |
1773 | ; |
1619 | } |
1774 | } |
1620 | |
1775 | |
1621 | interrupt: |
1776 | interrupt: |
1622 | self->incr_pos = p - SvPVX (self->incr_text); |
1777 | self->incr_pos = p - SvPVX (self->incr_text); |
|
|
1778 | //printf ("interrupt<%.*s>\n", self->incr_pos, SvPVX(self->incr_text));//D |
1623 | //printf ("return pos %d mode %d nest %d\n", self->incr_pos, self->incr_mode, self->incr_nest);//D |
1779 | //printf ("return pos %d mode %d nest %d\n", self->incr_pos, self->incr_mode, self->incr_nest);//D |
1624 | } |
1780 | } |
1625 | |
1781 | |
1626 | ///////////////////////////////////////////////////////////////////////////// |
1782 | ///////////////////////////////////////////////////////////////////////////// |
1627 | // XS interface functions |
1783 | // XS interface functions |
… | |
… | |
1642 | json_stash = gv_stashpv ("JSON::XS" , 1); |
1798 | json_stash = gv_stashpv ("JSON::XS" , 1); |
1643 | json_boolean_stash = gv_stashpv ("JSON::XS::Boolean", 1); |
1799 | json_boolean_stash = gv_stashpv ("JSON::XS::Boolean", 1); |
1644 | |
1800 | |
1645 | json_true = get_bool ("JSON::XS::true"); |
1801 | json_true = get_bool ("JSON::XS::true"); |
1646 | json_false = get_bool ("JSON::XS::false"); |
1802 | json_false = get_bool ("JSON::XS::false"); |
|
|
1803 | |
|
|
1804 | CvNODEBUG_on (get_cv ("JSON::XS::incr_text", 0)); /* the debugger completely breaks lvalue subs */ |
1647 | } |
1805 | } |
1648 | |
1806 | |
1649 | PROTOTYPES: DISABLE |
1807 | PROTOTYPES: DISABLE |
1650 | |
1808 | |
1651 | void CLONE (...) |
1809 | void CLONE (...) |
… | |
… | |
1654 | json_boolean_stash = 0; |
1812 | json_boolean_stash = 0; |
1655 | |
1813 | |
1656 | void new (char *klass) |
1814 | void new (char *klass) |
1657 | PPCODE: |
1815 | PPCODE: |
1658 | { |
1816 | { |
1659 | SV *pv = NEWSV (0, sizeof (JSON)); |
1817 | SV *pv = NEWSV (0, sizeof (JSON)); |
1660 | SvPOK_only (pv); |
1818 | SvPOK_only (pv); |
1661 | json_init ((JSON *)SvPVX (pv)); |
1819 | json_init ((JSON *)SvPVX (pv)); |
1662 | XPUSHs (sv_2mortal (sv_bless ( |
1820 | XPUSHs (sv_2mortal (sv_bless ( |
1663 | newRV_noinc (pv), |
1821 | newRV_noinc (pv), |
1664 | strEQ (klass, "JSON::XS") ? JSON_STASH : gv_stashpv (klass, 1) |
1822 | strEQ (klass, "JSON::XS") ? JSON_STASH : gv_stashpv (klass, 1) |
… | |
… | |
1741 | } |
1899 | } |
1742 | |
1900 | |
1743 | void filter_json_single_key_object (JSON *self, SV *key, SV *cb = &PL_sv_undef) |
1901 | void filter_json_single_key_object (JSON *self, SV *key, SV *cb = &PL_sv_undef) |
1744 | PPCODE: |
1902 | PPCODE: |
1745 | { |
1903 | { |
1746 | if (!self->cb_sk_object) |
1904 | if (!self->cb_sk_object) |
1747 | self->cb_sk_object = newHV (); |
1905 | self->cb_sk_object = newHV (); |
1748 | |
1906 | |
1749 | if (SvOK (cb)) |
1907 | if (SvOK (cb)) |
1750 | hv_store_ent (self->cb_sk_object, key, newSVsv (cb), 0); |
1908 | hv_store_ent (self->cb_sk_object, key, newSVsv (cb), 0); |
1751 | else |
1909 | else |
… | |
… | |
1762 | XPUSHs (ST (0)); |
1920 | XPUSHs (ST (0)); |
1763 | } |
1921 | } |
1764 | |
1922 | |
1765 | void encode (JSON *self, SV *scalar) |
1923 | void encode (JSON *self, SV *scalar) |
1766 | PPCODE: |
1924 | PPCODE: |
1767 | XPUSHs (encode_json (scalar, self)); |
1925 | PUTBACK; scalar = encode_json (scalar, self); SPAGAIN; |
|
|
1926 | XPUSHs (scalar); |
1768 | |
1927 | |
1769 | void decode (JSON *self, SV *jsonstr) |
1928 | void decode (JSON *self, SV *jsonstr) |
1770 | PPCODE: |
1929 | PPCODE: |
1771 | XPUSHs (decode_json (jsonstr, self, 0)); |
1930 | PUTBACK; jsonstr = decode_json (jsonstr, self, 0); SPAGAIN; |
|
|
1931 | XPUSHs (jsonstr); |
1772 | |
1932 | |
1773 | void decode_prefix (JSON *self, SV *jsonstr) |
1933 | void decode_prefix (JSON *self, SV *jsonstr) |
1774 | PPCODE: |
1934 | PPCODE: |
1775 | { |
1935 | { |
1776 | STRLEN offset; |
1936 | SV *sv; |
|
|
1937 | char *offset; |
|
|
1938 | PUTBACK; sv = decode_json (jsonstr, self, &offset); SPAGAIN; |
1777 | EXTEND (SP, 2); |
1939 | EXTEND (SP, 2); |
1778 | PUSHs (decode_json (jsonstr, self, &offset)); |
1940 | PUSHs (sv); |
1779 | PUSHs (sv_2mortal (newSVuv (offset))); |
1941 | PUSHs (sv_2mortal (newSVuv (ptr_to_index (jsonstr, offset)))); |
1780 | } |
1942 | } |
1781 | |
1943 | |
1782 | void incr_parse (JSON *self, SV *jsonstr = 0) |
1944 | void incr_parse (JSON *self, SV *jsonstr = 0) |
1783 | PPCODE: |
1945 | PPCODE: |
1784 | { |
1946 | { |
1785 | if (!self->incr_text) |
1947 | if (!self->incr_text) |
1786 | self->incr_text = newSVpvn ("", 0); |
1948 | self->incr_text = newSVpvn ("", 0); |
|
|
1949 | |
|
|
1950 | /* if utf8-ness doesn't match the decoder, need to upgrade/downgrade */ |
|
|
1951 | if (!DECODE_WANTS_OCTETS (self) == !SvUTF8 (self->incr_text)) |
|
|
1952 | if (DECODE_WANTS_OCTETS (self)) |
|
|
1953 | { |
|
|
1954 | if (self->incr_pos) |
|
|
1955 | self->incr_pos = utf8_length ((U8 *)SvPVX (self->incr_text), |
|
|
1956 | (U8 *)SvPVX (self->incr_text) + self->incr_pos); |
|
|
1957 | |
|
|
1958 | sv_utf8_downgrade (self->incr_text, 0); |
|
|
1959 | } |
|
|
1960 | else |
|
|
1961 | { |
|
|
1962 | sv_utf8_upgrade (self->incr_text); |
|
|
1963 | |
|
|
1964 | if (self->incr_pos) |
|
|
1965 | self->incr_pos = utf8_hop ((U8 *)SvPVX (self->incr_text), self->incr_pos) |
|
|
1966 | - (U8 *)SvPVX (self->incr_text); |
|
|
1967 | } |
1787 | |
1968 | |
1788 | // append data, if any |
1969 | // append data, if any |
1789 | if (jsonstr) |
1970 | if (jsonstr) |
1790 | { |
1971 | { |
|
|
1972 | /* make sure both strings have same encoding */ |
1791 | if (SvUTF8 (jsonstr) && !SvUTF8 (self->incr_text)) |
1973 | if (SvUTF8 (jsonstr) != SvUTF8 (self->incr_text)) |
|
|
1974 | if (SvUTF8 (jsonstr)) |
|
|
1975 | sv_utf8_downgrade (jsonstr, 0); |
1792 | { |
1976 | else |
1793 | /* utf-8-ness differs, need to upgrade */ |
|
|
1794 | sv_utf8_upgrade (self->incr_text); |
1977 | sv_utf8_upgrade (jsonstr); |
1795 | |
1978 | |
1796 | if (self->incr_pos) |
1979 | /* and then just blindly append */ |
1797 | self->incr_pos = utf8_hop ((U8 *)SvPVX (self->incr_text), self->incr_pos) |
|
|
1798 | - (U8 *)SvPVX (self->incr_text); |
|
|
1799 | } |
|
|
1800 | |
|
|
1801 | { |
1980 | { |
1802 | STRLEN len; |
1981 | STRLEN len; |
1803 | const char *str = SvPV (jsonstr, len); |
1982 | const char *str = SvPV (jsonstr, len); |
1804 | SvGROW (self->incr_text, SvCUR (self->incr_text) + len + 1); |
1983 | STRLEN cur = SvCUR (self->incr_text); |
|
|
1984 | |
|
|
1985 | if (SvLEN (self->incr_text) <= cur + len) |
|
|
1986 | SvGROW (self->incr_text, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1); |
|
|
1987 | |
1805 | Move (str, SvEND (self->incr_text), len, char); |
1988 | Move (str, SvEND (self->incr_text), len, char); |
1806 | SvCUR_set (self->incr_text, SvCUR (self->incr_text) + len); |
1989 | SvCUR_set (self->incr_text, SvCUR (self->incr_text) + len); |
1807 | *SvEND (self->incr_text) = 0; // this should basically be a nop, too, but make sure it's there |
1990 | *SvEND (self->incr_text) = 0; // this should basically be a nop, too, but make sure it's there |
1808 | } |
1991 | } |
1809 | } |
1992 | } |
1810 | |
1993 | |
1811 | if (GIMME_V != G_VOID) |
1994 | if (GIMME_V != G_VOID) |
1812 | do |
1995 | do |
1813 | { |
1996 | { |
|
|
1997 | SV *sv; |
1814 | STRLEN offset; |
1998 | char *offset; |
1815 | |
1999 | |
1816 | if (!INCR_DONE (self)) |
2000 | if (!INCR_DONE (self)) |
1817 | { |
2001 | { |
1818 | incr_parse (self); |
2002 | incr_parse (self); |
1819 | |
2003 | |
1820 | if (self->incr_pos > self->max_size && self->max_size) |
2004 | if (self->incr_pos > self->max_size && self->max_size) |
1821 | croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", |
2005 | croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", |
1822 | (unsigned long)self->incr_pos, (unsigned long)self->max_size); |
2006 | (unsigned long)self->incr_pos, (unsigned long)self->max_size); |
1823 | |
2007 | |
1824 | if (!INCR_DONE (self)) |
2008 | if (!INCR_DONE (self)) |
|
|
2009 | { |
|
|
2010 | // as an optimisation, do not accumulate white space in the incr buffer |
|
|
2011 | if (self->incr_mode == INCR_M_WS && self->incr_pos) |
|
|
2012 | { |
|
|
2013 | self->incr_pos = 0; |
|
|
2014 | SvCUR_set (self->incr_text, 0); |
|
|
2015 | } |
|
|
2016 | |
1825 | break; |
2017 | break; |
|
|
2018 | } |
1826 | } |
2019 | } |
1827 | |
2020 | |
1828 | XPUSHs (decode_json (self->incr_text, self, &offset)); |
2021 | PUTBACK; sv = decode_json (self->incr_text, self, &offset); SPAGAIN; |
|
|
2022 | XPUSHs (sv); |
1829 | |
2023 | |
1830 | sv_chop (self->incr_text, SvPV_nolen (self->incr_text) + offset); |
|
|
1831 | self->incr_pos -= offset; |
2024 | self->incr_pos -= offset - SvPVX (self->incr_text); |
1832 | self->incr_nest = 0; |
2025 | self->incr_nest = 0; |
1833 | self->incr_mode = 0; |
2026 | self->incr_mode = 0; |
|
|
2027 | |
|
|
2028 | sv_chop (self->incr_text, offset); |
1834 | } |
2029 | } |
1835 | while (GIMME_V == G_ARRAY); |
2030 | while (GIMME_V == G_ARRAY); |
1836 | } |
2031 | } |
1837 | |
2032 | |
1838 | SV *incr_text (JSON *self) |
2033 | SV *incr_text (JSON *self) |
… | |
… | |
1876 | SvREFCNT_dec (self->incr_text); |
2071 | SvREFCNT_dec (self->incr_text); |
1877 | |
2072 | |
1878 | PROTOTYPES: ENABLE |
2073 | PROTOTYPES: ENABLE |
1879 | |
2074 | |
1880 | void encode_json (SV *scalar) |
2075 | void encode_json (SV *scalar) |
1881 | ALIAS: |
|
|
1882 | to_json_ = 0 |
|
|
1883 | encode_json = F_UTF8 |
|
|
1884 | PPCODE: |
2076 | PPCODE: |
1885 | { |
2077 | { |
1886 | JSON json; |
2078 | JSON json; |
1887 | json_init (&json); |
2079 | json_init (&json); |
1888 | json.flags |= ix; |
2080 | json.flags |= F_UTF8; |
1889 | XPUSHs (encode_json (scalar, &json)); |
2081 | PUTBACK; scalar = encode_json (scalar, &json); SPAGAIN; |
|
|
2082 | XPUSHs (scalar); |
1890 | } |
2083 | } |
1891 | |
2084 | |
1892 | void decode_json (SV *jsonstr) |
2085 | void decode_json (SV *jsonstr) |
1893 | ALIAS: |
|
|
1894 | from_json_ = 0 |
|
|
1895 | decode_json = F_UTF8 |
|
|
1896 | PPCODE: |
2086 | PPCODE: |
1897 | { |
2087 | { |
1898 | JSON json; |
2088 | JSON json; |
1899 | json_init (&json); |
2089 | json_init (&json); |
1900 | json.flags |= ix; |
2090 | json.flags |= F_UTF8; |
1901 | XPUSHs (decode_json (jsonstr, &json, 0)); |
2091 | PUTBACK; jsonstr = decode_json (jsonstr, &json, 0); SPAGAIN; |
|
|
2092 | XPUSHs (jsonstr); |
1902 | } |
2093 | } |
1903 | |
2094 | |
1904 | |
|
|