… | |
… | |
27 | #define F_SPACE_AFTER 0x00000040UL |
27 | #define F_SPACE_AFTER 0x00000040UL |
28 | #define F_ALLOW_NONREF 0x00000100UL |
28 | #define F_ALLOW_NONREF 0x00000100UL |
29 | #define F_SHRINK 0x00000200UL |
29 | #define F_SHRINK 0x00000200UL |
30 | #define F_ALLOW_BLESSED 0x00000400UL |
30 | #define F_ALLOW_BLESSED 0x00000400UL |
31 | #define F_CONV_BLESSED 0x00000800UL |
31 | #define F_CONV_BLESSED 0x00000800UL |
|
|
32 | #define F_RELAXED 0x00001000UL |
|
|
33 | |
32 | #define F_MAXDEPTH 0xf8000000UL |
34 | #define F_MAXDEPTH 0xf8000000UL |
33 | #define S_MAXDEPTH 27 |
35 | #define S_MAXDEPTH 27 |
34 | #define F_MAXSIZE 0x01f00000UL |
36 | #define F_MAXSIZE 0x01f00000UL |
35 | #define S_MAXSIZE 20 |
37 | #define S_MAXSIZE 20 |
36 | #define F_HOOK 0x00080000UL // some hooks exist, so slow-path processing |
38 | #define F_HOOK 0x00080000UL // some hooks exist, so slow-path processing |
… | |
… | |
48 | |
50 | |
49 | #define SB do { |
51 | #define SB do { |
50 | #define SE } while (0) |
52 | #define SE } while (0) |
51 | |
53 | |
52 | #if __GNUC__ >= 3 |
54 | #if __GNUC__ >= 3 |
53 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
55 | # define expect(expr,value) __builtin_expect ((expr), (value)) |
54 | # define inline inline |
56 | # define INLINE static inline |
55 | #else |
57 | #else |
56 | # define expect(expr,value) (expr) |
58 | # define expect(expr,value) (expr) |
57 | # define inline static |
59 | # define INLINE static |
58 | #endif |
60 | #endif |
59 | |
61 | |
60 | #define expect_false(expr) expect ((expr) != 0, 0) |
62 | #define expect_false(expr) expect ((expr) != 0, 0) |
61 | #define expect_true(expr) expect ((expr) != 0, 1) |
63 | #define expect_true(expr) expect ((expr) != 0, 1) |
62 | |
64 | |
… | |
… | |
78 | } JSON; |
80 | } JSON; |
79 | |
81 | |
80 | ///////////////////////////////////////////////////////////////////////////// |
82 | ///////////////////////////////////////////////////////////////////////////// |
81 | // utility functions |
83 | // utility functions |
82 | |
84 | |
83 | inline void |
85 | INLINE void |
84 | shrink (SV *sv) |
86 | shrink (SV *sv) |
85 | { |
87 | { |
86 | sv_utf8_downgrade (sv, 1); |
88 | sv_utf8_downgrade (sv, 1); |
87 | if (SvLEN (sv) > SvCUR (sv) + 1) |
89 | if (SvLEN (sv) > SvCUR (sv) + 1) |
88 | { |
90 | { |
… | |
… | |
97 | // decode an utf-8 character and return it, or (UV)-1 in |
99 | // decode an utf-8 character and return it, or (UV)-1 in |
98 | // case of an error. |
100 | // case of an error. |
99 | // we special-case "safe" characters from U+80 .. U+7FF, |
101 | // we special-case "safe" characters from U+80 .. U+7FF, |
100 | // but use the very good perl function to parse anything else. |
102 | // but use the very good perl function to parse anything else. |
101 | // note that we never call this function for a ascii codepoints |
103 | // note that we never call this function for a ascii codepoints |
102 | inline UV |
104 | INLINE UV |
103 | decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen) |
105 | decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen) |
104 | { |
106 | { |
105 | if (expect_false (s[0] > 0xdf || s[0] < 0xc2)) |
107 | if (expect_false (s[0] > 0xdf || s[0] < 0xc2)) |
106 | return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY); |
108 | return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY); |
107 | else if (len > 1 && s[1] >= 0x80 && s[1] <= 0xbf) |
109 | else if (len > 1 && s[1] >= 0x80 && s[1] <= 0xbf) |
… | |
… | |
126 | char *end; // SvEND (sv) |
128 | char *end; // SvEND (sv) |
127 | SV *sv; // result scalar |
129 | SV *sv; // result scalar |
128 | JSON json; |
130 | JSON json; |
129 | U32 indent; // indentation level |
131 | U32 indent; // indentation level |
130 | U32 maxdepth; // max. indentation/recursion level |
132 | U32 maxdepth; // max. indentation/recursion level |
|
|
133 | UV limit; // escape character values >= this value when encoding |
131 | } enc_t; |
134 | } enc_t; |
132 | |
135 | |
133 | inline void |
136 | INLINE void |
134 | need (enc_t *enc, STRLEN len) |
137 | need (enc_t *enc, STRLEN len) |
135 | { |
138 | { |
136 | if (expect_false (enc->cur + len >= enc->end)) |
139 | if (expect_false (enc->cur + len >= enc->end)) |
137 | { |
140 | { |
138 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
141 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
… | |
… | |
140 | enc->cur = SvPVX (enc->sv) + cur; |
143 | enc->cur = SvPVX (enc->sv) + cur; |
141 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
144 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
142 | } |
145 | } |
143 | } |
146 | } |
144 | |
147 | |
145 | inline void |
148 | INLINE void |
146 | encode_ch (enc_t *enc, char ch) |
149 | encode_ch (enc_t *enc, char ch) |
147 | { |
150 | { |
148 | need (enc, 1); |
151 | need (enc, 1); |
149 | *enc->cur++ = ch; |
152 | *enc->cur++ = ch; |
150 | } |
153 | } |
… | |
… | |
204 | { |
207 | { |
205 | uch = ch; |
208 | uch = ch; |
206 | clen = 1; |
209 | clen = 1; |
207 | } |
210 | } |
208 | |
211 | |
209 | if (uch > 0x10FFFFUL) |
212 | if (uch < 0x20 || uch >= enc->limit) |
210 | croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
|
|
211 | |
|
|
212 | if (uch < 0x80 || enc->json.flags & F_ASCII || (enc->json.flags & F_LATIN1 && uch > 0xFF)) |
|
|
213 | { |
213 | { |
214 | if (uch > 0xFFFFUL) |
214 | if (uch > 0xFFFFUL) |
215 | { |
215 | { |
|
|
216 | if (uch > 0x10FFFFUL) |
|
|
217 | croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
|
|
218 | |
216 | need (enc, len += 11); |
219 | need (enc, len += 11); |
217 | sprintf (enc->cur, "\\u%04x\\u%04x", |
220 | sprintf (enc->cur, "\\u%04x\\u%04x", |
218 | (int)((uch - 0x10000) / 0x400 + 0xD800), |
221 | (int)((uch - 0x10000) / 0x400 + 0xD800), |
219 | (int)((uch - 0x10000) % 0x400 + 0xDC00)); |
222 | (int)((uch - 0x10000) % 0x400 + 0xDC00)); |
220 | enc->cur += 12; |
223 | enc->cur += 12; |
… | |
… | |
259 | |
262 | |
260 | --len; |
263 | --len; |
261 | } |
264 | } |
262 | } |
265 | } |
263 | |
266 | |
264 | inline void |
267 | INLINE void |
265 | encode_indent (enc_t *enc) |
268 | encode_indent (enc_t *enc) |
266 | { |
269 | { |
267 | if (enc->json.flags & F_INDENT) |
270 | if (enc->json.flags & F_INDENT) |
268 | { |
271 | { |
269 | int spaces = enc->indent * INDENT_STEP; |
272 | int spaces = enc->indent * INDENT_STEP; |
… | |
… | |
272 | memset (enc->cur, ' ', spaces); |
275 | memset (enc->cur, ' ', spaces); |
273 | enc->cur += spaces; |
276 | enc->cur += spaces; |
274 | } |
277 | } |
275 | } |
278 | } |
276 | |
279 | |
277 | inline void |
280 | INLINE void |
278 | encode_space (enc_t *enc) |
281 | encode_space (enc_t *enc) |
279 | { |
282 | { |
280 | need (enc, 1); |
283 | need (enc, 1); |
281 | encode_ch (enc, ' '); |
284 | encode_ch (enc, ' '); |
282 | } |
285 | } |
283 | |
286 | |
284 | inline void |
287 | INLINE void |
285 | encode_nl (enc_t *enc) |
288 | encode_nl (enc_t *enc) |
286 | { |
289 | { |
287 | if (enc->json.flags & F_INDENT) |
290 | if (enc->json.flags & F_INDENT) |
288 | { |
291 | { |
289 | need (enc, 1); |
292 | need (enc, 1); |
290 | encode_ch (enc, '\n'); |
293 | encode_ch (enc, '\n'); |
291 | } |
294 | } |
292 | } |
295 | } |
293 | |
296 | |
294 | inline void |
297 | INLINE void |
295 | encode_comma (enc_t *enc) |
298 | encode_comma (enc_t *enc) |
296 | { |
299 | { |
297 | encode_ch (enc, ','); |
300 | encode_ch (enc, ','); |
298 | |
301 | |
299 | if (enc->json.flags & F_INDENT) |
302 | if (enc->json.flags & F_INDENT) |
… | |
… | |
310 | int i, len = av_len (av); |
313 | int i, len = av_len (av); |
311 | |
314 | |
312 | if (enc->indent >= enc->maxdepth) |
315 | if (enc->indent >= enc->maxdepth) |
313 | croak ("data structure too deep (hit recursion limit)"); |
316 | croak ("data structure too deep (hit recursion limit)"); |
314 | |
317 | |
315 | encode_ch (enc, '['); encode_nl (enc); |
318 | encode_ch (enc, '['); |
316 | ++enc->indent; |
319 | |
|
|
320 | if (len >= 0) |
|
|
321 | { |
|
|
322 | encode_nl (enc); ++enc->indent; |
317 | |
323 | |
318 | for (i = 0; i <= len; ++i) |
324 | for (i = 0; i <= len; ++i) |
319 | { |
325 | { |
320 | SV **svp = av_fetch (av, i, 0); |
326 | SV **svp = av_fetch (av, i, 0); |
321 | |
327 | |
322 | encode_indent (enc); |
328 | encode_indent (enc); |
323 | |
329 | |
324 | if (svp) |
330 | if (svp) |
325 | encode_sv (enc, *svp); |
331 | encode_sv (enc, *svp); |
326 | else |
332 | else |
327 | encode_str (enc, "null", 4, 0); |
333 | encode_str (enc, "null", 4, 0); |
328 | |
334 | |
329 | if (i < len) |
335 | if (i < len) |
330 | encode_comma (enc); |
336 | encode_comma (enc); |
331 | } |
337 | } |
332 | |
338 | |
|
|
339 | encode_nl (enc); --enc->indent; encode_indent (enc); |
|
|
340 | } |
|
|
341 | |
333 | encode_nl (enc); |
342 | encode_ch (enc, ']'); |
334 | |
|
|
335 | --enc->indent; |
|
|
336 | encode_indent (enc); encode_ch (enc, ']'); |
|
|
337 | } |
343 | } |
338 | |
344 | |
339 | static void |
345 | static void |
340 | encode_hk (enc_t *enc, HE *he) |
346 | encode_hk (enc_t *enc, HE *he) |
341 | { |
347 | { |
… | |
… | |
394 | int count; |
400 | int count; |
395 | |
401 | |
396 | if (enc->indent >= enc->maxdepth) |
402 | if (enc->indent >= enc->maxdepth) |
397 | croak ("data structure too deep (hit recursion limit)"); |
403 | croak ("data structure too deep (hit recursion limit)"); |
398 | |
404 | |
399 | encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; |
405 | encode_ch (enc, '{'); |
400 | |
406 | |
401 | // for canonical output we have to sort by keys first |
407 | // for canonical output we have to sort by keys first |
402 | // actually, this is mostly due to the stupid so-called |
408 | // actually, this is mostly due to the stupid so-called |
403 | // security workaround added somewhere in 5.8.x. |
409 | // security workaround added somewhere in 5.8.x. |
404 | // that randomises hash orderings |
410 | // that randomises hash orderings |
… | |
… | |
457 | |
463 | |
458 | FREETMPS; |
464 | FREETMPS; |
459 | LEAVE; |
465 | LEAVE; |
460 | } |
466 | } |
461 | |
467 | |
|
|
468 | encode_nl (enc); ++enc->indent; |
|
|
469 | |
462 | while (count--) |
470 | while (count--) |
463 | { |
471 | { |
464 | encode_indent (enc); |
472 | encode_indent (enc); |
465 | he = hes [count]; |
473 | he = hes [count]; |
466 | encode_hk (enc, he); |
474 | encode_hk (enc, he); |
467 | encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he)); |
475 | encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he)); |
468 | |
476 | |
469 | if (count) |
477 | if (count) |
470 | encode_comma (enc); |
478 | encode_comma (enc); |
471 | } |
479 | } |
|
|
480 | |
|
|
481 | encode_nl (enc); --enc->indent; encode_indent (enc); |
472 | } |
482 | } |
473 | } |
483 | } |
474 | else |
484 | else |
475 | { |
485 | { |
476 | if (hv_iterinit (hv) || SvMAGICAL (hv)) |
486 | if (hv_iterinit (hv) || SvMAGICAL (hv)) |
477 | if ((he = hv_iternext (hv))) |
487 | if ((he = hv_iternext (hv))) |
|
|
488 | { |
|
|
489 | encode_nl (enc); ++enc->indent; |
|
|
490 | |
478 | for (;;) |
491 | for (;;) |
479 | { |
492 | { |
480 | encode_indent (enc); |
493 | encode_indent (enc); |
481 | encode_hk (enc, he); |
494 | encode_hk (enc, he); |
482 | encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he)); |
495 | encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he)); |
483 | |
496 | |
484 | if (!(he = hv_iternext (hv))) |
497 | if (!(he = hv_iternext (hv))) |
485 | break; |
498 | break; |
486 | |
499 | |
487 | encode_comma (enc); |
500 | encode_comma (enc); |
488 | } |
501 | } |
489 | } |
|
|
490 | |
502 | |
|
|
503 | encode_nl (enc); --enc->indent; encode_indent (enc); |
|
|
504 | } |
|
|
505 | } |
|
|
506 | |
491 | encode_nl (enc); |
507 | encode_ch (enc, '}'); |
492 | |
|
|
493 | --enc->indent; encode_indent (enc); encode_ch (enc, '}'); |
|
|
494 | } |
508 | } |
495 | |
509 | |
496 | // encode objects, arrays and special \0=false and \1=true values. |
510 | // encode objects, arrays and special \0=false and \1=true values. |
497 | static void |
511 | static void |
498 | encode_rv (enc_t *enc, SV *sv) |
512 | encode_rv (enc_t *enc, SV *sv) |
… | |
… | |
667 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
681 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
668 | enc.cur = SvPVX (enc.sv); |
682 | enc.cur = SvPVX (enc.sv); |
669 | enc.end = SvEND (enc.sv); |
683 | enc.end = SvEND (enc.sv); |
670 | enc.indent = 0; |
684 | enc.indent = 0; |
671 | enc.maxdepth = DEC_DEPTH (enc.json.flags); |
685 | enc.maxdepth = DEC_DEPTH (enc.json.flags); |
|
|
686 | enc.limit = enc.json.flags & F_ASCII ? 0x000080UL |
|
|
687 | : enc.json.flags & F_LATIN1 ? 0x000100UL |
|
|
688 | : 0x10FFFFUL; |
672 | |
689 | |
673 | SvPOK_only (enc.sv); |
690 | SvPOK_only (enc.sv); |
674 | encode_sv (&enc, scalar); |
691 | encode_sv (&enc, scalar); |
675 | |
692 | |
676 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
693 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
… | |
… | |
697 | JSON json; |
714 | JSON json; |
698 | U32 depth; // recursion depth |
715 | U32 depth; // recursion depth |
699 | U32 maxdepth; // recursion depth limit |
716 | U32 maxdepth; // recursion depth limit |
700 | } dec_t; |
717 | } dec_t; |
701 | |
718 | |
702 | inline void |
719 | INLINE void |
|
|
720 | decode_comment (dec_t *dec) |
|
|
721 | { |
|
|
722 | // only '#'-style comments allowed a.t.m. |
|
|
723 | |
|
|
724 | while (*dec->cur && *dec->cur != 0x0a && *dec->cur != 0x0d) |
|
|
725 | ++dec->cur; |
|
|
726 | } |
|
|
727 | |
|
|
728 | INLINE void |
703 | decode_ws (dec_t *dec) |
729 | decode_ws (dec_t *dec) |
704 | { |
730 | { |
705 | for (;;) |
731 | for (;;) |
706 | { |
732 | { |
707 | char ch = *dec->cur; |
733 | char ch = *dec->cur; |
708 | |
734 | |
709 | if (ch > 0x20 |
735 | if (ch > 0x20) |
|
|
736 | { |
|
|
737 | if (expect_false (ch == '#')) |
|
|
738 | { |
|
|
739 | if (dec->json.flags & F_RELAXED) |
|
|
740 | decode_comment (dec); |
|
|
741 | else |
|
|
742 | break; |
|
|
743 | } |
|
|
744 | else |
|
|
745 | break; |
|
|
746 | } |
710 | || (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)) |
747 | else if (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09) |
711 | break; |
748 | break; // parse error, but let higher level handle it, gives better error messages |
712 | |
749 | |
713 | ++dec->cur; |
750 | ++dec->cur; |
714 | } |
751 | } |
715 | } |
752 | } |
716 | |
753 | |
… | |
… | |
967 | |
1004 | |
968 | if (!is_nv) |
1005 | if (!is_nv) |
969 | { |
1006 | { |
970 | int len = dec->cur - start; |
1007 | int len = dec->cur - start; |
971 | |
1008 | |
972 | // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so |
1009 | // special case the rather common 1..5-digit-int case |
973 | if (*start == '-') |
1010 | if (*start == '-') |
974 | switch (len) |
1011 | switch (len) |
975 | { |
1012 | { |
976 | case 2: return newSViv (-( start [1] - '0' * 1)); |
1013 | case 2: return newSViv (-( start [1] - '0' * 1)); |
977 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
1014 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
978 | case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); |
1015 | case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); |
979 | case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
1016 | case 5: return newSViv (-( start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
|
|
1017 | case 6: return newSViv (-(start [1] * 10000 + start [2] * 1000 + start [3] * 100 + start [4] * 10 + start [5] - '0' * 11111)); |
980 | } |
1018 | } |
981 | else |
1019 | else |
982 | switch (len) |
1020 | switch (len) |
983 | { |
1021 | { |
984 | case 1: return newSViv ( start [0] - '0' * 1); |
1022 | case 1: return newSViv ( start [0] - '0' * 1); |
985 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
1023 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
986 | case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); |
1024 | case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); |
987 | case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
1025 | case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
|
|
1026 | case 5: return newSViv ( start [0] * 10000 + start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 11111); |
988 | } |
1027 | } |
989 | |
1028 | |
990 | { |
1029 | { |
991 | UV uv; |
1030 | UV uv; |
992 | int numtype = grok_number (start, len, &uv); |
1031 | int numtype = grok_number (start, len, &uv); |
… | |
… | |
1053 | |
1092 | |
1054 | if (*dec->cur != ',') |
1093 | if (*dec->cur != ',') |
1055 | ERR (", or ] expected while parsing array"); |
1094 | ERR (", or ] expected while parsing array"); |
1056 | |
1095 | |
1057 | ++dec->cur; |
1096 | ++dec->cur; |
|
|
1097 | |
|
|
1098 | decode_ws (dec); |
|
|
1099 | |
|
|
1100 | if (*dec->cur == ']' && dec->json.flags & F_RELAXED) |
|
|
1101 | { |
|
|
1102 | ++dec->cur; |
|
|
1103 | break; |
|
|
1104 | } |
1058 | } |
1105 | } |
1059 | |
1106 | |
1060 | DEC_DEC_DEPTH; |
1107 | DEC_DEC_DEPTH; |
1061 | return newRV_noinc ((SV *)av); |
1108 | return newRV_noinc ((SV *)av); |
1062 | |
1109 | |
… | |
… | |
1078 | if (*dec->cur == '}') |
1125 | if (*dec->cur == '}') |
1079 | ++dec->cur; |
1126 | ++dec->cur; |
1080 | else |
1127 | else |
1081 | for (;;) |
1128 | for (;;) |
1082 | { |
1129 | { |
1083 | decode_ws (dec); EXPECT_CH ('"'); |
1130 | EXPECT_CH ('"'); |
1084 | |
1131 | |
1085 | // heuristic: assume that |
1132 | // heuristic: assume that |
1086 | // a) decode_str + hv_store_ent are abysmally slow. |
1133 | // a) decode_str + hv_store_ent are abysmally slow. |
1087 | // b) most hash keys are short, simple ascii text. |
1134 | // b) most hash keys are short, simple ascii text. |
1088 | // => try to "fast-match" such strings to avoid |
1135 | // => try to "fast-match" such strings to avoid |
… | |
… | |
1102 | if (!key) |
1149 | if (!key) |
1103 | goto fail; |
1150 | goto fail; |
1104 | |
1151 | |
1105 | decode_ws (dec); EXPECT_CH (':'); |
1152 | decode_ws (dec); EXPECT_CH (':'); |
1106 | |
1153 | |
|
|
1154 | decode_ws (dec); |
1107 | value = decode_sv (dec); |
1155 | value = decode_sv (dec); |
1108 | if (!value) |
1156 | if (!value) |
1109 | { |
1157 | { |
1110 | SvREFCNT_dec (key); |
1158 | SvREFCNT_dec (key); |
1111 | goto fail; |
1159 | goto fail; |
… | |
… | |
1123 | int len = p - key; |
1171 | int len = p - key; |
1124 | dec->cur = p + 1; |
1172 | dec->cur = p + 1; |
1125 | |
1173 | |
1126 | decode_ws (dec); EXPECT_CH (':'); |
1174 | decode_ws (dec); EXPECT_CH (':'); |
1127 | |
1175 | |
|
|
1176 | decode_ws (dec); |
1128 | value = decode_sv (dec); |
1177 | value = decode_sv (dec); |
1129 | if (!value) |
1178 | if (!value) |
1130 | goto fail; |
1179 | goto fail; |
1131 | |
1180 | |
1132 | hv_store (hv, key, len, value, 0); |
1181 | hv_store (hv, key, len, value, 0); |
… | |
… | |
1148 | |
1197 | |
1149 | if (*dec->cur != ',') |
1198 | if (*dec->cur != ',') |
1150 | ERR (", or } expected while parsing object/hash"); |
1199 | ERR (", or } expected while parsing object/hash"); |
1151 | |
1200 | |
1152 | ++dec->cur; |
1201 | ++dec->cur; |
|
|
1202 | |
|
|
1203 | decode_ws (dec); |
|
|
1204 | |
|
|
1205 | if (*dec->cur == '}' && dec->json.flags & F_RELAXED) |
|
|
1206 | { |
|
|
1207 | ++dec->cur; |
|
|
1208 | break; |
|
|
1209 | } |
1153 | } |
1210 | } |
1154 | |
1211 | |
1155 | DEC_DEC_DEPTH; |
1212 | DEC_DEC_DEPTH; |
1156 | sv = newRV_noinc ((SV *)hv); |
1213 | sv = newRV_noinc ((SV *)hv); |
1157 | |
1214 | |
… | |
… | |
1222 | } |
1279 | } |
1223 | |
1280 | |
1224 | static SV * |
1281 | static SV * |
1225 | decode_sv (dec_t *dec) |
1282 | decode_sv (dec_t *dec) |
1226 | { |
1283 | { |
1227 | decode_ws (dec); |
|
|
1228 | |
|
|
1229 | // the beauty of JSON: you need exactly one character lookahead |
1284 | // the beauty of JSON: you need exactly one character lookahead |
1230 | // to parse anything. |
1285 | // to parse anything. |
1231 | switch (*dec->cur) |
1286 | switch (*dec->cur) |
1232 | { |
1287 | { |
1233 | case '"': ++dec->cur; return decode_str (dec); |
1288 | case '"': ++dec->cur; return decode_str (dec); |
… | |
… | |
1317 | |
1372 | |
1318 | if (dec.json.cb_object || dec.json.cb_sk_object) |
1373 | if (dec.json.cb_object || dec.json.cb_sk_object) |
1319 | dec.json.flags |= F_HOOK; |
1374 | dec.json.flags |= F_HOOK; |
1320 | |
1375 | |
1321 | *dec.end = 0; // this should basically be a nop, too, but make sure it's there |
1376 | *dec.end = 0; // this should basically be a nop, too, but make sure it's there |
|
|
1377 | |
|
|
1378 | decode_ws (&dec); |
1322 | sv = decode_sv (&dec); |
1379 | sv = decode_sv (&dec); |
1323 | |
1380 | |
1324 | if (!(offset_return || !sv)) |
1381 | if (!(offset_return || !sv)) |
1325 | { |
1382 | { |
1326 | // check for trailing garbage |
1383 | // check for trailing garbage |
… | |
… | |
1406 | { |
1463 | { |
1407 | SV *pv = NEWSV (0, sizeof (JSON)); |
1464 | SV *pv = NEWSV (0, sizeof (JSON)); |
1408 | SvPOK_only (pv); |
1465 | SvPOK_only (pv); |
1409 | Zero (SvPVX (pv), 1, JSON); |
1466 | Zero (SvPVX (pv), 1, JSON); |
1410 | ((JSON *)SvPVX (pv))->flags = F_DEFAULT; |
1467 | ((JSON *)SvPVX (pv))->flags = F_DEFAULT; |
1411 | XPUSHs (sv_2mortal (sv_bless (newRV_noinc (pv), JSON_STASH))); |
1468 | XPUSHs (sv_2mortal (sv_bless ( |
|
|
1469 | newRV_noinc (pv), |
|
|
1470 | strEQ (klass, "JSON::XS") ? JSON_STASH : gv_stashpv (klass, 1) |
|
|
1471 | ))); |
1412 | } |
1472 | } |
1413 | |
1473 | |
1414 | void ascii (JSON *self, int enable = 1) |
1474 | void ascii (JSON *self, int enable = 1) |
1415 | ALIAS: |
1475 | ALIAS: |
1416 | ascii = F_ASCII |
1476 | ascii = F_ASCII |
… | |
… | |
1423 | pretty = F_PRETTY |
1483 | pretty = F_PRETTY |
1424 | allow_nonref = F_ALLOW_NONREF |
1484 | allow_nonref = F_ALLOW_NONREF |
1425 | shrink = F_SHRINK |
1485 | shrink = F_SHRINK |
1426 | allow_blessed = F_ALLOW_BLESSED |
1486 | allow_blessed = F_ALLOW_BLESSED |
1427 | convert_blessed = F_CONV_BLESSED |
1487 | convert_blessed = F_CONV_BLESSED |
|
|
1488 | relaxed = F_RELAXED |
1428 | PPCODE: |
1489 | PPCODE: |
1429 | { |
1490 | { |
1430 | if (enable) |
1491 | if (enable) |
1431 | self->flags |= ix; |
1492 | self->flags |= ix; |
1432 | else |
1493 | else |
1433 | self->flags &= ~ix; |
1494 | self->flags &= ~ix; |
1434 | |
1495 | |
1435 | XPUSHs (ST (0)); |
1496 | XPUSHs (ST (0)); |
1436 | } |
1497 | } |
1437 | |
1498 | |
|
|
1499 | void get_ascii (JSON *self) |
|
|
1500 | ALIAS: |
|
|
1501 | get_ascii = F_ASCII |
|
|
1502 | get_latin1 = F_LATIN1 |
|
|
1503 | get_utf8 = F_UTF8 |
|
|
1504 | get_indent = F_INDENT |
|
|
1505 | get_canonical = F_CANONICAL |
|
|
1506 | get_space_before = F_SPACE_BEFORE |
|
|
1507 | get_space_after = F_SPACE_AFTER |
|
|
1508 | get_allow_nonref = F_ALLOW_NONREF |
|
|
1509 | get_shrink = F_SHRINK |
|
|
1510 | get_allow_blessed = F_ALLOW_BLESSED |
|
|
1511 | get_convert_blessed = F_CONV_BLESSED |
|
|
1512 | get_relaxed = F_RELAXED |
|
|
1513 | PPCODE: |
|
|
1514 | XPUSHs (boolSV (self->flags & ix)); |
|
|
1515 | |
1438 | void max_depth (JSON *self, UV max_depth = 0x80000000UL) |
1516 | void max_depth (JSON *self, UV max_depth = 0x80000000UL) |
1439 | PPCODE: |
1517 | PPCODE: |
1440 | { |
1518 | { |
1441 | UV log2 = 0; |
1519 | UV log2 = 0; |
1442 | |
1520 | |
… | |
… | |
1448 | self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); |
1526 | self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); |
1449 | |
1527 | |
1450 | XPUSHs (ST (0)); |
1528 | XPUSHs (ST (0)); |
1451 | } |
1529 | } |
1452 | |
1530 | |
|
|
1531 | U32 get_max_depth (JSON *self) |
|
|
1532 | CODE: |
|
|
1533 | RETVAL = DEC_DEPTH (self->flags); |
|
|
1534 | OUTPUT: |
|
|
1535 | RETVAL |
|
|
1536 | |
1453 | void max_size (JSON *self, UV max_size = 0) |
1537 | void max_size (JSON *self, UV max_size = 0) |
1454 | PPCODE: |
1538 | PPCODE: |
1455 | { |
1539 | { |
1456 | UV log2 = 0; |
1540 | UV log2 = 0; |
1457 | |
1541 | |
… | |
… | |
1463 | |
1547 | |
1464 | self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE); |
1548 | self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE); |
1465 | |
1549 | |
1466 | XPUSHs (ST (0)); |
1550 | XPUSHs (ST (0)); |
1467 | } |
1551 | } |
|
|
1552 | |
|
|
1553 | int get_max_size (JSON *self) |
|
|
1554 | CODE: |
|
|
1555 | RETVAL = DEC_SIZE (self->flags); |
|
|
1556 | OUTPUT: |
|
|
1557 | RETVAL |
1468 | |
1558 | |
1469 | void filter_json_object (JSON *self, SV *cb = &PL_sv_undef) |
1559 | void filter_json_object (JSON *self, SV *cb = &PL_sv_undef) |
1470 | PPCODE: |
1560 | PPCODE: |
1471 | { |
1561 | { |
1472 | SvREFCNT_dec (self->cb_object); |
1562 | SvREFCNT_dec (self->cb_object); |
… | |
… | |
1519 | SvREFCNT_dec (self->cb_sk_object); |
1609 | SvREFCNT_dec (self->cb_sk_object); |
1520 | SvREFCNT_dec (self->cb_object); |
1610 | SvREFCNT_dec (self->cb_object); |
1521 | |
1611 | |
1522 | PROTOTYPES: ENABLE |
1612 | PROTOTYPES: ENABLE |
1523 | |
1613 | |
1524 | void to_json (SV *scalar) |
1614 | void encode_json (SV *scalar) |
1525 | PPCODE: |
1615 | PPCODE: |
1526 | { |
1616 | { |
1527 | JSON json = { F_DEFAULT | F_UTF8 }; |
1617 | JSON json = { F_DEFAULT | F_UTF8 }; |
1528 | XPUSHs (encode_json (scalar, &json)); |
1618 | XPUSHs (encode_json (scalar, &json)); |
1529 | } |
1619 | } |
1530 | |
1620 | |
1531 | void from_json (SV *jsonstr) |
1621 | void decode_json (SV *jsonstr) |
1532 | PPCODE: |
1622 | PPCODE: |
1533 | { |
1623 | { |
1534 | JSON json = { F_DEFAULT | F_UTF8 }; |
1624 | JSON json = { F_DEFAULT | F_UTF8 }; |
1535 | XPUSHs (decode_json (jsonstr, &json, 0)); |
1625 | XPUSHs (decode_json (jsonstr, &json, 0)); |
1536 | } |
1626 | } |