ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/JSON-XS/XS.xs
(Generate patch)

Comparing JSON-XS/XS.xs (file contents):
Revision 1.62 by root, Sun Aug 26 22:27:32 2007 UTC vs.
Revision 1.74 by root, Wed Mar 19 15:17:53 2008 UTC

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)
64
65#define IN_RANGE_INC(type,val,beg,end) \
66 ((unsigned type)((unsigned type)(val) - (unsigned type)(beg)) \
67 <= (unsigned type)((unsigned type)(end) - (unsigned type)(beg)))
62 68
63#ifdef USE_ITHREADS 69#ifdef USE_ITHREADS
64# define JSON_SLOW 1 70# define JSON_SLOW 1
65# define JSON_STASH (json_stash ? json_stash : gv_stashpv ("JSON::XS", 1)) 71# define JSON_STASH (json_stash ? json_stash : gv_stashpv ("JSON::XS", 1))
66#else 72#else
78} JSON; 84} JSON;
79 85
80///////////////////////////////////////////////////////////////////////////// 86/////////////////////////////////////////////////////////////////////////////
81// utility functions 87// utility functions
82 88
83inline void 89INLINE void
84shrink (SV *sv) 90shrink (SV *sv)
85{ 91{
86 sv_utf8_downgrade (sv, 1); 92 sv_utf8_downgrade (sv, 1);
87 if (SvLEN (sv) > SvCUR (sv) + 1) 93 if (SvLEN (sv) > SvCUR (sv) + 1)
88 { 94 {
97// decode an utf-8 character and return it, or (UV)-1 in 103// decode an utf-8 character and return it, or (UV)-1 in
98// case of an error. 104// case of an error.
99// we special-case "safe" characters from U+80 .. U+7FF, 105// we special-case "safe" characters from U+80 .. U+7FF,
100// but use the very good perl function to parse anything else. 106// but use the very good perl function to parse anything else.
101// note that we never call this function for a ascii codepoints 107// note that we never call this function for a ascii codepoints
102inline UV 108INLINE UV
103decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen) 109decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen)
104{ 110{
105 if (expect_false (s[0] > 0xdf || s[0] < 0xc2)) 111 if (expect_true (len >= 2
106 return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY); 112 && IN_RANGE_INC (char, s[0], 0xc2, 0xdf)
107 else if (len > 1 && s[1] >= 0x80 && s[1] <= 0xbf) 113 && IN_RANGE_INC (char, s[1], 0x80, 0xbf)))
108 { 114 {
109 *clen = 2; 115 *clen = 2;
110 return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); 116 return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f);
111 } 117 }
112 else 118 else
113 { 119 return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY);
114 *clen = (STRLEN)-1; 120}
115 return (UV)-1; 121
116 } 122// likewise for encoding, also never called for ascii codepoints
123// this function takes advantage of this fact, although current gccs
124// seem to optimise the check for >= 0x80 away anyways
125INLINE unsigned char *
126encode_utf8 (unsigned char *s, UV ch)
127{
128 if (expect_false (ch < 0x000080))
129 *s++ = ch;
130 else if (expect_true (ch < 0x000800))
131 *s++ = 0xc0 | ( ch >> 6),
132 *s++ = 0x80 | ( ch & 0x3f);
133 else if ( ch < 0x010000)
134 *s++ = 0xe0 | ( ch >> 12),
135 *s++ = 0x80 | ((ch >> 6) & 0x3f),
136 *s++ = 0x80 | ( ch & 0x3f);
137 else if ( ch < 0x110000)
138 *s++ = 0xf0 | ( ch >> 18),
139 *s++ = 0x80 | ((ch >> 12) & 0x3f),
140 *s++ = 0x80 | ((ch >> 6) & 0x3f),
141 *s++ = 0x80 | ( ch & 0x3f);
142
143 return s;
117} 144}
118 145
119///////////////////////////////////////////////////////////////////////////// 146/////////////////////////////////////////////////////////////////////////////
120// encoder 147// encoder
121 148
126 char *end; // SvEND (sv) 153 char *end; // SvEND (sv)
127 SV *sv; // result scalar 154 SV *sv; // result scalar
128 JSON json; 155 JSON json;
129 U32 indent; // indentation level 156 U32 indent; // indentation level
130 U32 maxdepth; // max. indentation/recursion level 157 U32 maxdepth; // max. indentation/recursion level
158 UV limit; // escape character values >= this value when encoding
131} enc_t; 159} enc_t;
132 160
133inline void 161INLINE void
134need (enc_t *enc, STRLEN len) 162need (enc_t *enc, STRLEN len)
135{ 163{
136 if (expect_false (enc->cur + len >= enc->end)) 164 if (expect_false (enc->cur + len >= enc->end))
137 { 165 {
138 STRLEN cur = enc->cur - SvPVX (enc->sv); 166 STRLEN cur = enc->cur - SvPVX (enc->sv);
140 enc->cur = SvPVX (enc->sv) + cur; 168 enc->cur = SvPVX (enc->sv) + cur;
141 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; 169 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1;
142 } 170 }
143} 171}
144 172
145inline void 173INLINE void
146encode_ch (enc_t *enc, char ch) 174encode_ch (enc_t *enc, char ch)
147{ 175{
148 need (enc, 1); 176 need (enc, 1);
149 *enc->cur++ = ch; 177 *enc->cur++ = ch;
150} 178}
204 { 232 {
205 uch = ch; 233 uch = ch;
206 clen = 1; 234 clen = 1;
207 } 235 }
208 236
209 if (uch > 0x10FFFFUL) 237 if (uch < 0x80/*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 { 238 {
214 if (uch > 0xFFFFUL) 239 if (uch >= 0x10000UL)
215 { 240 {
241 if (uch >= 0x110000UL)
242 croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch);
243
216 need (enc, len += 11); 244 need (enc, len += 11);
217 sprintf (enc->cur, "\\u%04x\\u%04x", 245 sprintf (enc->cur, "\\u%04x\\u%04x",
218 (int)((uch - 0x10000) / 0x400 + 0xD800), 246 (int)((uch - 0x10000) / 0x400 + 0xD800),
219 (int)((uch - 0x10000) % 0x400 + 0xDC00)); 247 (int)((uch - 0x10000) % 0x400 + 0xDC00));
220 enc->cur += 12; 248 enc->cur += 12;
248 while (--clen); 276 while (--clen);
249 } 277 }
250 else 278 else
251 { 279 {
252 need (enc, len += UTF8_MAXBYTES - 1); // never more than 11 bytes needed 280 need (enc, len += UTF8_MAXBYTES - 1); // never more than 11 bytes needed
253 enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); 281 enc->cur = encode_utf8 (enc->cur, uch);
254 ++str; 282 ++str;
255 } 283 }
256 } 284 }
257 } 285 }
258 } 286 }
259 287
260 --len; 288 --len;
261 } 289 }
262} 290}
263 291
264inline void 292INLINE void
265encode_indent (enc_t *enc) 293encode_indent (enc_t *enc)
266{ 294{
267 if (enc->json.flags & F_INDENT) 295 if (enc->json.flags & F_INDENT)
268 { 296 {
269 int spaces = enc->indent * INDENT_STEP; 297 int spaces = enc->indent * INDENT_STEP;
272 memset (enc->cur, ' ', spaces); 300 memset (enc->cur, ' ', spaces);
273 enc->cur += spaces; 301 enc->cur += spaces;
274 } 302 }
275} 303}
276 304
277inline void 305INLINE void
278encode_space (enc_t *enc) 306encode_space (enc_t *enc)
279{ 307{
280 need (enc, 1); 308 need (enc, 1);
281 encode_ch (enc, ' '); 309 encode_ch (enc, ' ');
282} 310}
283 311
284inline void 312INLINE void
285encode_nl (enc_t *enc) 313encode_nl (enc_t *enc)
286{ 314{
287 if (enc->json.flags & F_INDENT) 315 if (enc->json.flags & F_INDENT)
288 { 316 {
289 need (enc, 1); 317 need (enc, 1);
290 encode_ch (enc, '\n'); 318 encode_ch (enc, '\n');
291 } 319 }
292} 320}
293 321
294inline void 322INLINE void
295encode_comma (enc_t *enc) 323encode_comma (enc_t *enc)
296{ 324{
297 encode_ch (enc, ','); 325 encode_ch (enc, ',');
298 326
299 if (enc->json.flags & F_INDENT) 327 if (enc->json.flags & F_INDENT)
310 int i, len = av_len (av); 338 int i, len = av_len (av);
311 339
312 if (enc->indent >= enc->maxdepth) 340 if (enc->indent >= enc->maxdepth)
313 croak ("data structure too deep (hit recursion limit)"); 341 croak ("data structure too deep (hit recursion limit)");
314 342
315 encode_ch (enc, '['); encode_nl (enc); 343 encode_ch (enc, '[');
316 ++enc->indent; 344
345 if (len >= 0)
346 {
347 encode_nl (enc); ++enc->indent;
317 348
318 for (i = 0; i <= len; ++i) 349 for (i = 0; i <= len; ++i)
319 { 350 {
320 SV **svp = av_fetch (av, i, 0); 351 SV **svp = av_fetch (av, i, 0);
321 352
322 encode_indent (enc); 353 encode_indent (enc);
323 354
324 if (svp) 355 if (svp)
325 encode_sv (enc, *svp); 356 encode_sv (enc, *svp);
326 else 357 else
327 encode_str (enc, "null", 4, 0); 358 encode_str (enc, "null", 4, 0);
328 359
329 if (i < len) 360 if (i < len)
330 encode_comma (enc); 361 encode_comma (enc);
331 } 362 }
332 363
364 encode_nl (enc); --enc->indent; encode_indent (enc);
365 }
366
333 encode_nl (enc); 367 encode_ch (enc, ']');
334
335 --enc->indent;
336 encode_indent (enc); encode_ch (enc, ']');
337} 368}
338 369
339static void 370static void
340encode_hk (enc_t *enc, HE *he) 371encode_hk (enc_t *enc, HE *he)
341{ 372{
394 int count; 425 int count;
395 426
396 if (enc->indent >= enc->maxdepth) 427 if (enc->indent >= enc->maxdepth)
397 croak ("data structure too deep (hit recursion limit)"); 428 croak ("data structure too deep (hit recursion limit)");
398 429
399 encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; 430 encode_ch (enc, '{');
400 431
401 // for canonical output we have to sort by keys first 432 // for canonical output we have to sort by keys first
402 // actually, this is mostly due to the stupid so-called 433 // actually, this is mostly due to the stupid so-called
403 // security workaround added somewhere in 5.8.x. 434 // security workaround added somewhere in 5.8.x.
404 // that randomises hash orderings 435 // that randomises hash orderings
457 488
458 FREETMPS; 489 FREETMPS;
459 LEAVE; 490 LEAVE;
460 } 491 }
461 492
493 encode_nl (enc); ++enc->indent;
494
462 while (count--) 495 while (count--)
463 { 496 {
464 encode_indent (enc); 497 encode_indent (enc);
465 he = hes [count]; 498 he = hes [count];
466 encode_hk (enc, he); 499 encode_hk (enc, he);
467 encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he)); 500 encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he));
468 501
469 if (count) 502 if (count)
470 encode_comma (enc); 503 encode_comma (enc);
471 } 504 }
505
506 encode_nl (enc); --enc->indent; encode_indent (enc);
472 } 507 }
473 } 508 }
474 else 509 else
475 { 510 {
476 if (hv_iterinit (hv) || SvMAGICAL (hv)) 511 if (hv_iterinit (hv) || SvMAGICAL (hv))
477 if ((he = hv_iternext (hv))) 512 if ((he = hv_iternext (hv)))
513 {
514 encode_nl (enc); ++enc->indent;
515
478 for (;;) 516 for (;;)
479 { 517 {
480 encode_indent (enc); 518 encode_indent (enc);
481 encode_hk (enc, he); 519 encode_hk (enc, he);
482 encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he)); 520 encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he));
483 521
484 if (!(he = hv_iternext (hv))) 522 if (!(he = hv_iternext (hv)))
485 break; 523 break;
486 524
487 encode_comma (enc); 525 encode_comma (enc);
488 } 526 }
489 }
490 527
528 encode_nl (enc); --enc->indent; encode_indent (enc);
529 }
530 }
531
491 encode_nl (enc); 532 encode_ch (enc, '}');
492
493 --enc->indent; encode_indent (enc); encode_ch (enc, '}');
494} 533}
495 534
496// encode objects, arrays and special \0=false and \1=true values. 535// encode objects, arrays and special \0=false and \1=true values.
497static void 536static void
498encode_rv (enc_t *enc, SV *sv) 537encode_rv (enc_t *enc, SV *sv)
606 Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur); 645 Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur);
607 enc->cur += strlen (enc->cur); 646 enc->cur += strlen (enc->cur);
608 } 647 }
609 else if (SvIOKp (sv)) 648 else if (SvIOKp (sv))
610 { 649 {
611 // we assume we can always read an IV as a UV 650 // we assume we can always read an IV as a UV and vice versa
612 if (SvUV (sv) & ~(UV)0x7fff) 651 // we assume two's complement
652 // we assume no aliasing issues in the union
653 if (SvIsUV (sv) ? SvUVX (sv) > 59000
654 : SvIVX (sv) > 59000 || SvIVX (sv) < -59000)
613 { 655 {
614 // large integer, use the (rather slow) snprintf way. 656 // large integer, use the (rather slow) snprintf way.
615 need (enc, sizeof (UV) * 3); 657 need (enc, sizeof (UV) * 5 / 2 + 1); // CHAR_BIT is at least 8
616 enc->cur += 658 enc->cur +=
617 SvIsUV(sv) 659 SvIsUV(sv)
618 ? snprintf (enc->cur, sizeof (UV) * 3, "%"UVuf, (UV)SvUVX (sv)) 660 ? snprintf (enc->cur, sizeof (UV) * 3, "%"UVuf, (UV)SvUVX (sv))
619 : snprintf (enc->cur, sizeof (UV) * 3, "%"IVdf, (IV)SvIVX (sv)); 661 : snprintf (enc->cur, sizeof (UV) * 3, "%"IVdf, (IV)SvIVX (sv));
620 } 662 }
621 else 663 else
622 { 664 {
623 // optimise the "small number case" 665 // optimise the "small number case"
624 // code will likely be branchless and use only a single multiplication 666 // code will likely be branchless and use only a single multiplication
667 // works for numbers up to 59074
625 I32 i = SvIV (sv); 668 I32 i = SvIVX (sv);
626 U32 u; 669 U32 u;
627 char digit, nz = 0; 670 char digit, nz = 0;
628 671
629 need (enc, 6); 672 need (enc, 6);
630 673
667 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); 710 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE));
668 enc.cur = SvPVX (enc.sv); 711 enc.cur = SvPVX (enc.sv);
669 enc.end = SvEND (enc.sv); 712 enc.end = SvEND (enc.sv);
670 enc.indent = 0; 713 enc.indent = 0;
671 enc.maxdepth = DEC_DEPTH (enc.json.flags); 714 enc.maxdepth = DEC_DEPTH (enc.json.flags);
715 enc.limit = enc.json.flags & F_ASCII ? 0x000080UL
716 : enc.json.flags & F_LATIN1 ? 0x000100UL
717 : 0x110000UL;
672 718
673 SvPOK_only (enc.sv); 719 SvPOK_only (enc.sv);
674 encode_sv (&enc, scalar); 720 encode_sv (&enc, scalar);
675 721
676 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); 722 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv));
697 JSON json; 743 JSON json;
698 U32 depth; // recursion depth 744 U32 depth; // recursion depth
699 U32 maxdepth; // recursion depth limit 745 U32 maxdepth; // recursion depth limit
700} dec_t; 746} dec_t;
701 747
702inline void 748INLINE void
749decode_comment (dec_t *dec)
750{
751 // only '#'-style comments allowed a.t.m.
752
753 while (*dec->cur && *dec->cur != 0x0a && *dec->cur != 0x0d)
754 ++dec->cur;
755}
756
757INLINE void
703decode_ws (dec_t *dec) 758decode_ws (dec_t *dec)
704{ 759{
705 for (;;) 760 for (;;)
706 { 761 {
707 char ch = *dec->cur; 762 char ch = *dec->cur;
708 763
709 if (ch > 0x20 764 if (ch > 0x20)
765 {
766 if (expect_false (ch == '#'))
767 {
768 if (dec->json.flags & F_RELAXED)
769 decode_comment (dec);
770 else
771 break;
772 }
773 else
774 break;
775 }
710 || (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)) 776 else if (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)
711 break; 777 break; // parse error, but let higher level handle it, gives better error messages
712 778
713 ++dec->cur; 779 ++dec->cur;
714 } 780 }
715} 781}
716 782
822 888
823 if (hi >= 0x80) 889 if (hi >= 0x80)
824 { 890 {
825 utf8 = 1; 891 utf8 = 1;
826 892
827 cur = (char *)uvuni_to_utf8_flags (cur, hi, 0); 893 cur = encode_utf8 (cur, hi);
828 } 894 }
829 else 895 else
830 *cur++ = hi; 896 *cur++ = hi;
831 } 897 }
832 break; 898 break;
834 default: 900 default:
835 --dec_cur; 901 --dec_cur;
836 ERR ("illegal backslash escape sequence in string"); 902 ERR ("illegal backslash escape sequence in string");
837 } 903 }
838 } 904 }
839 else if (expect_true (ch >= 0x20 && ch <= 0x7f)) 905 else if (expect_true (ch >= 0x20 && ch < 0x80))
840 *cur++ = ch; 906 *cur++ = ch;
841 else if (ch >= 0x80) 907 else if (ch >= 0x80)
842 { 908 {
843 STRLEN clen; 909 STRLEN clen;
844 UV uch; 910 UV uch;
967 1033
968 if (!is_nv) 1034 if (!is_nv)
969 { 1035 {
970 int len = dec->cur - start; 1036 int len = dec->cur - start;
971 1037
972 // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so 1038 // special case the rather common 1..5-digit-int case
973 if (*start == '-') 1039 if (*start == '-')
974 switch (len) 1040 switch (len)
975 { 1041 {
976 case 2: return newSViv (-( start [1] - '0' * 1)); 1042 case 2: return newSViv (-( start [1] - '0' * 1));
977 case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); 1043 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)); 1044 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)); 1045 case 5: return newSViv (-( start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111));
1046 case 6: return newSViv (-(start [1] * 10000 + start [2] * 1000 + start [3] * 100 + start [4] * 10 + start [5] - '0' * 11111));
980 } 1047 }
981 else 1048 else
982 switch (len) 1049 switch (len)
983 { 1050 {
984 case 1: return newSViv ( start [0] - '0' * 1); 1051 case 1: return newSViv ( start [0] - '0' * 1);
985 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); 1052 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); 1053 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); 1054 case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111);
1055 case 5: return newSViv ( start [0] * 10000 + start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 11111);
988 } 1056 }
989 1057
990 { 1058 {
991 UV uv; 1059 UV uv;
992 int numtype = grok_number (start, len, &uv); 1060 int numtype = grok_number (start, len, &uv);
1053 1121
1054 if (*dec->cur != ',') 1122 if (*dec->cur != ',')
1055 ERR (", or ] expected while parsing array"); 1123 ERR (", or ] expected while parsing array");
1056 1124
1057 ++dec->cur; 1125 ++dec->cur;
1126
1127 decode_ws (dec);
1128
1129 if (*dec->cur == ']' && dec->json.flags & F_RELAXED)
1130 {
1131 ++dec->cur;
1132 break;
1133 }
1058 } 1134 }
1059 1135
1060 DEC_DEC_DEPTH; 1136 DEC_DEC_DEPTH;
1061 return newRV_noinc ((SV *)av); 1137 return newRV_noinc ((SV *)av);
1062 1138
1078 if (*dec->cur == '}') 1154 if (*dec->cur == '}')
1079 ++dec->cur; 1155 ++dec->cur;
1080 else 1156 else
1081 for (;;) 1157 for (;;)
1082 { 1158 {
1083 decode_ws (dec); EXPECT_CH ('"'); 1159 EXPECT_CH ('"');
1084 1160
1085 // heuristic: assume that 1161 // heuristic: assume that
1086 // a) decode_str + hv_store_ent are abysmally slow. 1162 // a) decode_str + hv_store_ent are abysmally slow.
1087 // b) most hash keys are short, simple ascii text. 1163 // b) most hash keys are short, simple ascii text.
1088 // => try to "fast-match" such strings to avoid 1164 // => try to "fast-match" such strings to avoid
1102 if (!key) 1178 if (!key)
1103 goto fail; 1179 goto fail;
1104 1180
1105 decode_ws (dec); EXPECT_CH (':'); 1181 decode_ws (dec); EXPECT_CH (':');
1106 1182
1183 decode_ws (dec);
1107 value = decode_sv (dec); 1184 value = decode_sv (dec);
1108 if (!value) 1185 if (!value)
1109 { 1186 {
1110 SvREFCNT_dec (key); 1187 SvREFCNT_dec (key);
1111 goto fail; 1188 goto fail;
1123 int len = p - key; 1200 int len = p - key;
1124 dec->cur = p + 1; 1201 dec->cur = p + 1;
1125 1202
1126 decode_ws (dec); EXPECT_CH (':'); 1203 decode_ws (dec); EXPECT_CH (':');
1127 1204
1205 decode_ws (dec);
1128 value = decode_sv (dec); 1206 value = decode_sv (dec);
1129 if (!value) 1207 if (!value)
1130 goto fail; 1208 goto fail;
1131 1209
1132 hv_store (hv, key, len, value, 0); 1210 hv_store (hv, key, len, value, 0);
1148 1226
1149 if (*dec->cur != ',') 1227 if (*dec->cur != ',')
1150 ERR (", or } expected while parsing object/hash"); 1228 ERR (", or } expected while parsing object/hash");
1151 1229
1152 ++dec->cur; 1230 ++dec->cur;
1231
1232 decode_ws (dec);
1233
1234 if (*dec->cur == '}' && dec->json.flags & F_RELAXED)
1235 {
1236 ++dec->cur;
1237 break;
1238 }
1153 } 1239 }
1154 1240
1155 DEC_DEC_DEPTH; 1241 DEC_DEC_DEPTH;
1156 sv = newRV_noinc ((SV *)hv); 1242 sv = newRV_noinc ((SV *)hv);
1157 1243
1222} 1308}
1223 1309
1224static SV * 1310static SV *
1225decode_sv (dec_t *dec) 1311decode_sv (dec_t *dec)
1226{ 1312{
1227 decode_ws (dec);
1228
1229 // the beauty of JSON: you need exactly one character lookahead 1313 // the beauty of JSON: you need exactly one character lookahead
1230 // to parse anything. 1314 // to parse everything.
1231 switch (*dec->cur) 1315 switch (*dec->cur)
1232 { 1316 {
1233 case '"': ++dec->cur; return decode_str (dec); 1317 case '"': ++dec->cur; return decode_str (dec);
1234 case '[': ++dec->cur; return decode_av (dec); 1318 case '[': ++dec->cur; return decode_av (dec);
1235 case '{': ++dec->cur; return decode_hv (dec); 1319 case '{': ++dec->cur; return decode_hv (dec);
1236 1320
1237 case '-': 1321 case '-':
1238 case '0': case '1': case '2': case '3': case '4': 1322 case '0': case '1': case '2': case '3': case '4':
1239 case '5': case '6': case '7': case '8': case '9': 1323 case '5': case '6': case '7': case '8': case '9':
1240 return decode_num (dec); 1324 return decode_num (dec);
1317 1401
1318 if (dec.json.cb_object || dec.json.cb_sk_object) 1402 if (dec.json.cb_object || dec.json.cb_sk_object)
1319 dec.json.flags |= F_HOOK; 1403 dec.json.flags |= F_HOOK;
1320 1404
1321 *dec.end = 0; // this should basically be a nop, too, but make sure it's there 1405 *dec.end = 0; // this should basically be a nop, too, but make sure it's there
1406
1407 decode_ws (&dec);
1322 sv = decode_sv (&dec); 1408 sv = decode_sv (&dec);
1323 1409
1324 if (!(offset_return || !sv)) 1410 if (!(offset_return || !sv))
1325 { 1411 {
1326 // check for trailing garbage 1412 // check for trailing garbage
1406{ 1492{
1407 SV *pv = NEWSV (0, sizeof (JSON)); 1493 SV *pv = NEWSV (0, sizeof (JSON));
1408 SvPOK_only (pv); 1494 SvPOK_only (pv);
1409 Zero (SvPVX (pv), 1, JSON); 1495 Zero (SvPVX (pv), 1, JSON);
1410 ((JSON *)SvPVX (pv))->flags = F_DEFAULT; 1496 ((JSON *)SvPVX (pv))->flags = F_DEFAULT;
1411 XPUSHs (sv_2mortal (sv_bless (newRV_noinc (pv), JSON_STASH))); 1497 XPUSHs (sv_2mortal (sv_bless (
1498 newRV_noinc (pv),
1499 strEQ (klass, "JSON::XS") ? JSON_STASH : gv_stashpv (klass, 1)
1500 )));
1412} 1501}
1413 1502
1414void ascii (JSON *self, int enable = 1) 1503void ascii (JSON *self, int enable = 1)
1415 ALIAS: 1504 ALIAS:
1416 ascii = F_ASCII 1505 ascii = F_ASCII
1423 pretty = F_PRETTY 1512 pretty = F_PRETTY
1424 allow_nonref = F_ALLOW_NONREF 1513 allow_nonref = F_ALLOW_NONREF
1425 shrink = F_SHRINK 1514 shrink = F_SHRINK
1426 allow_blessed = F_ALLOW_BLESSED 1515 allow_blessed = F_ALLOW_BLESSED
1427 convert_blessed = F_CONV_BLESSED 1516 convert_blessed = F_CONV_BLESSED
1517 relaxed = F_RELAXED
1428 PPCODE: 1518 PPCODE:
1429{ 1519{
1430 if (enable) 1520 if (enable)
1431 self->flags |= ix; 1521 self->flags |= ix;
1432 else 1522 else
1433 self->flags &= ~ix; 1523 self->flags &= ~ix;
1434 1524
1435 XPUSHs (ST (0)); 1525 XPUSHs (ST (0));
1436} 1526}
1437 1527
1528void get_ascii (JSON *self)
1529 ALIAS:
1530 get_ascii = F_ASCII
1531 get_latin1 = F_LATIN1
1532 get_utf8 = F_UTF8
1533 get_indent = F_INDENT
1534 get_canonical = F_CANONICAL
1535 get_space_before = F_SPACE_BEFORE
1536 get_space_after = F_SPACE_AFTER
1537 get_allow_nonref = F_ALLOW_NONREF
1538 get_shrink = F_SHRINK
1539 get_allow_blessed = F_ALLOW_BLESSED
1540 get_convert_blessed = F_CONV_BLESSED
1541 get_relaxed = F_RELAXED
1542 PPCODE:
1543 XPUSHs (boolSV (self->flags & ix));
1544
1438void max_depth (JSON *self, UV max_depth = 0x80000000UL) 1545void max_depth (JSON *self, UV max_depth = 0x80000000UL)
1439 PPCODE: 1546 PPCODE:
1440{ 1547{
1441 UV log2 = 0; 1548 UV log2 = 0;
1442 1549
1448 self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); 1555 self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH);
1449 1556
1450 XPUSHs (ST (0)); 1557 XPUSHs (ST (0));
1451} 1558}
1452 1559
1560U32 get_max_depth (JSON *self)
1561 CODE:
1562 RETVAL = DEC_DEPTH (self->flags);
1563 OUTPUT:
1564 RETVAL
1565
1453void max_size (JSON *self, UV max_size = 0) 1566void max_size (JSON *self, UV max_size = 0)
1454 PPCODE: 1567 PPCODE:
1455{ 1568{
1456 UV log2 = 0; 1569 UV log2 = 0;
1457 1570
1463 1576
1464 self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE); 1577 self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE);
1465 1578
1466 XPUSHs (ST (0)); 1579 XPUSHs (ST (0));
1467} 1580}
1581
1582int get_max_size (JSON *self)
1583 CODE:
1584 RETVAL = DEC_SIZE (self->flags);
1585 OUTPUT:
1586 RETVAL
1468 1587
1469void filter_json_object (JSON *self, SV *cb = &PL_sv_undef) 1588void filter_json_object (JSON *self, SV *cb = &PL_sv_undef)
1470 PPCODE: 1589 PPCODE:
1471{ 1590{
1472 SvREFCNT_dec (self->cb_object); 1591 SvREFCNT_dec (self->cb_object);
1519 SvREFCNT_dec (self->cb_sk_object); 1638 SvREFCNT_dec (self->cb_sk_object);
1520 SvREFCNT_dec (self->cb_object); 1639 SvREFCNT_dec (self->cb_object);
1521 1640
1522PROTOTYPES: ENABLE 1641PROTOTYPES: ENABLE
1523 1642
1524void to_json (SV *scalar) 1643void encode_json (SV *scalar)
1525 PPCODE: 1644 PPCODE:
1526{ 1645{
1527 JSON json = { F_DEFAULT | F_UTF8 }; 1646 JSON json = { F_DEFAULT | F_UTF8 };
1528 XPUSHs (encode_json (scalar, &json)); 1647 XPUSHs (encode_json (scalar, &json));
1529} 1648}
1530 1649
1531void from_json (SV *jsonstr) 1650void decode_json (SV *jsonstr)
1532 PPCODE: 1651 PPCODE:
1533{ 1652{
1534 JSON json = { F_DEFAULT | F_UTF8 }; 1653 JSON json = { F_DEFAULT | F_UTF8 };
1535 XPUSHs (decode_json (jsonstr, &json, 0)); 1654 XPUSHs (decode_json (jsonstr, &json, 0));
1536} 1655}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines