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.60 by root, Mon Aug 13 16:19:13 2007 UTC vs.
Revision 1.73 by root, Wed Mar 19 13:44:43 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
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 (ch <= 0x7FF)
116 } 129 {
130 *s++ = (ch >> 6) | 0xc0;
131 *s++ = (ch & 0x3f) | 0x80;
132 }
133 else
134 s = uvuni_to_utf8_flags (s, ch, 0);
135
136 return s;
117} 137}
118 138
119///////////////////////////////////////////////////////////////////////////// 139/////////////////////////////////////////////////////////////////////////////
120// encoder 140// encoder
121 141
126 char *end; // SvEND (sv) 146 char *end; // SvEND (sv)
127 SV *sv; // result scalar 147 SV *sv; // result scalar
128 JSON json; 148 JSON json;
129 U32 indent; // indentation level 149 U32 indent; // indentation level
130 U32 maxdepth; // max. indentation/recursion level 150 U32 maxdepth; // max. indentation/recursion level
151 UV limit; // escape character values >= this value when encoding
131} enc_t; 152} enc_t;
132 153
133inline void 154INLINE void
134need (enc_t *enc, STRLEN len) 155need (enc_t *enc, STRLEN len)
135{ 156{
136 if (expect_false (enc->cur + len >= enc->end)) 157 if (expect_false (enc->cur + len >= enc->end))
137 { 158 {
138 STRLEN cur = enc->cur - SvPVX (enc->sv); 159 STRLEN cur = enc->cur - SvPVX (enc->sv);
140 enc->cur = SvPVX (enc->sv) + cur; 161 enc->cur = SvPVX (enc->sv) + cur;
141 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; 162 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1;
142 } 163 }
143} 164}
144 165
145inline void 166INLINE void
146encode_ch (enc_t *enc, char ch) 167encode_ch (enc_t *enc, char ch)
147{ 168{
148 need (enc, 1); 169 need (enc, 1);
149 *enc->cur++ = ch; 170 *enc->cur++ = ch;
150} 171}
204 { 225 {
205 uch = ch; 226 uch = ch;
206 clen = 1; 227 clen = 1;
207 } 228 }
208 229
209 if (uch > 0x10FFFFUL) 230 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 { 231 {
214 if (uch > 0xFFFFUL) 232 if (uch > 0xFFFFUL)
215 { 233 {
234 if (uch > 0x10FFFFUL)
235 croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch);
236
216 need (enc, len += 11); 237 need (enc, len += 11);
217 sprintf (enc->cur, "\\u%04x\\u%04x", 238 sprintf (enc->cur, "\\u%04x\\u%04x",
218 (int)((uch - 0x10000) / 0x400 + 0xD800), 239 (int)((uch - 0x10000) / 0x400 + 0xD800),
219 (int)((uch - 0x10000) % 0x400 + 0xDC00)); 240 (int)((uch - 0x10000) % 0x400 + 0xDC00));
220 enc->cur += 12; 241 enc->cur += 12;
248 while (--clen); 269 while (--clen);
249 } 270 }
250 else 271 else
251 { 272 {
252 need (enc, len += UTF8_MAXBYTES - 1); // never more than 11 bytes needed 273 need (enc, len += UTF8_MAXBYTES - 1); // never more than 11 bytes needed
253 enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); 274 enc->cur = encode_utf8 (enc->cur, uch);
254 ++str; 275 ++str;
255 } 276 }
256 } 277 }
257 } 278 }
258 } 279 }
259 280
260 --len; 281 --len;
261 } 282 }
262} 283}
263 284
264inline void 285INLINE void
265encode_indent (enc_t *enc) 286encode_indent (enc_t *enc)
266{ 287{
267 if (enc->json.flags & F_INDENT) 288 if (enc->json.flags & F_INDENT)
268 { 289 {
269 int spaces = enc->indent * INDENT_STEP; 290 int spaces = enc->indent * INDENT_STEP;
272 memset (enc->cur, ' ', spaces); 293 memset (enc->cur, ' ', spaces);
273 enc->cur += spaces; 294 enc->cur += spaces;
274 } 295 }
275} 296}
276 297
277inline void 298INLINE void
278encode_space (enc_t *enc) 299encode_space (enc_t *enc)
279{ 300{
280 need (enc, 1); 301 need (enc, 1);
281 encode_ch (enc, ' '); 302 encode_ch (enc, ' ');
282} 303}
283 304
284inline void 305INLINE void
285encode_nl (enc_t *enc) 306encode_nl (enc_t *enc)
286{ 307{
287 if (enc->json.flags & F_INDENT) 308 if (enc->json.flags & F_INDENT)
288 { 309 {
289 need (enc, 1); 310 need (enc, 1);
290 encode_ch (enc, '\n'); 311 encode_ch (enc, '\n');
291 } 312 }
292} 313}
293 314
294inline void 315INLINE void
295encode_comma (enc_t *enc) 316encode_comma (enc_t *enc)
296{ 317{
297 encode_ch (enc, ','); 318 encode_ch (enc, ',');
298 319
299 if (enc->json.flags & F_INDENT) 320 if (enc->json.flags & F_INDENT)
310 int i, len = av_len (av); 331 int i, len = av_len (av);
311 332
312 if (enc->indent >= enc->maxdepth) 333 if (enc->indent >= enc->maxdepth)
313 croak ("data structure too deep (hit recursion limit)"); 334 croak ("data structure too deep (hit recursion limit)");
314 335
315 encode_ch (enc, '['); encode_nl (enc); 336 encode_ch (enc, '[');
316 ++enc->indent; 337
338 if (len >= 0)
339 {
340 encode_nl (enc); ++enc->indent;
317 341
318 for (i = 0; i <= len; ++i) 342 for (i = 0; i <= len; ++i)
319 { 343 {
320 SV **svp = av_fetch (av, i, 0); 344 SV **svp = av_fetch (av, i, 0);
321 345
322 encode_indent (enc); 346 encode_indent (enc);
323 347
324 if (svp) 348 if (svp)
325 encode_sv (enc, *svp); 349 encode_sv (enc, *svp);
326 else 350 else
327 encode_str (enc, "null", 4, 0); 351 encode_str (enc, "null", 4, 0);
328 352
329 if (i < len) 353 if (i < len)
330 encode_comma (enc); 354 encode_comma (enc);
331 } 355 }
332 356
357 encode_nl (enc); --enc->indent; encode_indent (enc);
358 }
359
333 encode_nl (enc); 360 encode_ch (enc, ']');
334
335 --enc->indent;
336 encode_indent (enc); encode_ch (enc, ']');
337} 361}
338 362
339static void 363static void
340encode_he (enc_t *enc, HE *he) 364encode_hk (enc_t *enc, HE *he)
341{ 365{
342 encode_ch (enc, '"'); 366 encode_ch (enc, '"');
343 367
344 if (HeKLEN (he) == HEf_SVKEY) 368 if (HeKLEN (he) == HEf_SVKEY)
345 { 369 {
358 encode_ch (enc, '"'); 382 encode_ch (enc, '"');
359 383
360 if (enc->json.flags & F_SPACE_BEFORE) encode_space (enc); 384 if (enc->json.flags & F_SPACE_BEFORE) encode_space (enc);
361 encode_ch (enc, ':'); 385 encode_ch (enc, ':');
362 if (enc->json.flags & F_SPACE_AFTER ) encode_space (enc); 386 if (enc->json.flags & F_SPACE_AFTER ) encode_space (enc);
363 encode_sv (enc, HeVAL (he));
364} 387}
365 388
366// compare hash entries, used when all keys are bytestrings 389// compare hash entries, used when all keys are bytestrings
367static int 390static int
368he_cmp_fast (const void *a_, const void *b_) 391he_cmp_fast (const void *a_, const void *b_)
373 HE *b = *(HE **)b_; 396 HE *b = *(HE **)b_;
374 397
375 STRLEN la = HeKLEN (a); 398 STRLEN la = HeKLEN (a);
376 STRLEN lb = HeKLEN (b); 399 STRLEN lb = HeKLEN (b);
377 400
378 if (!(cmp = memcmp (HeKEY (a), HeKEY (b), la < lb ? la : lb))) 401 if (!(cmp = memcmp (HeKEY (b), HeKEY (a), lb < la ? lb : la)))
379 cmp = la - lb; 402 cmp = lb - la;
380 403
381 return cmp; 404 return cmp;
382} 405}
383 406
384// compare hash entries, used when some keys are sv's or utf-x 407// compare hash entries, used when some keys are sv's or utf-x
385static int 408static int
386he_cmp_slow (const void *a, const void *b) 409he_cmp_slow (const void *a, const void *b)
387{ 410{
388 return sv_cmp (HeSVKEY_force (*(HE **)a), HeSVKEY_force (*(HE **)b)); 411 return sv_cmp (HeSVKEY_force (*(HE **)b), HeSVKEY_force (*(HE **)a));
389} 412}
390 413
391static void 414static void
392encode_hv (enc_t *enc, HV *hv) 415encode_hv (enc_t *enc, HV *hv)
393{ 416{
417 HE *he;
394 int count, i; 418 int count;
395 419
396 if (enc->indent >= enc->maxdepth) 420 if (enc->indent >= enc->maxdepth)
397 croak ("data structure too deep (hit recursion limit)"); 421 croak ("data structure too deep (hit recursion limit)");
398 422
399 encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; 423 encode_ch (enc, '{');
400 424
401 if ((count = hv_iterinit (hv)))
402 {
403 // for canonical output we have to sort by keys first 425 // for canonical output we have to sort by keys first
404 // actually, this is mostly due to the stupid so-called 426 // actually, this is mostly due to the stupid so-called
405 // security workaround added somewhere in 5.8.x. 427 // security workaround added somewhere in 5.8.x.
406 // that randomises hash orderings 428 // that randomises hash orderings
407 if (enc->json.flags & F_CANONICAL) 429 if (enc->json.flags & F_CANONICAL)
430 {
431 int count = hv_iterinit (hv);
432
433 if (SvMAGICAL (hv))
408 { 434 {
435 // need to count by iterating. could improve by dynamically building the vector below
436 // but I don't care for the speed of this special case.
437 // note also that we will run into undefined behaviour when the two iterations
438 // do not result in the same count, something I might care for in some later release.
439
440 count = 0;
441 while (hv_iternext (hv))
442 ++count;
443
444 hv_iterinit (hv);
445 }
446
447 if (count)
448 {
409 int fast = 1; 449 int i, fast = 1;
410 HE *he;
411#if defined(__BORLANDC__) || defined(_MSC_VER) 450#if defined(__BORLANDC__) || defined(_MSC_VER)
412 HE **hes = _alloca (count * sizeof (HE)); 451 HE **hes = _alloca (count * sizeof (HE));
413#else 452#else
414 HE *hes [count]; // if your compiler dies here, you need to enable C99 mode 453 HE *hes [count]; // if your compiler dies here, you need to enable C99 mode
415#endif 454#endif
442 481
443 FREETMPS; 482 FREETMPS;
444 LEAVE; 483 LEAVE;
445 } 484 }
446 485
447 for (i = 0; i < count; ++i) 486 encode_nl (enc); ++enc->indent;
487
488 while (count--)
448 { 489 {
449 encode_indent (enc); 490 encode_indent (enc);
491 he = hes [count];
450 encode_he (enc, hes [i]); 492 encode_hk (enc, he);
493 encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he));
451 494
452 if (i < count - 1) 495 if (count)
453 encode_comma (enc); 496 encode_comma (enc);
454 } 497 }
455 498
456 encode_nl (enc); 499 encode_nl (enc); --enc->indent; encode_indent (enc);
457 } 500 }
501 }
458 else 502 else
503 {
504 if (hv_iterinit (hv) || SvMAGICAL (hv))
505 if ((he = hv_iternext (hv)))
459 { 506 {
460 HE *he = hv_iternext (hv); 507 encode_nl (enc); ++enc->indent;
461 508
462 for (;;) 509 for (;;)
463 { 510 {
464 encode_indent (enc); 511 encode_indent (enc);
465 encode_he (enc, he); 512 encode_hk (enc, he);
513 encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he));
466 514
467 if (!(he = hv_iternext (hv))) 515 if (!(he = hv_iternext (hv)))
468 break; 516 break;
469 517
470 encode_comma (enc); 518 encode_comma (enc);
471 } 519 }
472 520
473 encode_nl (enc); 521 encode_nl (enc); --enc->indent; encode_indent (enc);
474 } 522 }
475 } 523 }
476 524
477 --enc->indent; encode_indent (enc); encode_ch (enc, '}'); 525 encode_ch (enc, '}');
478} 526}
479 527
480// encode objects, arrays and special \0=false and \1=true values. 528// encode objects, arrays and special \0=false and \1=true values.
481static void 529static void
482encode_rv (enc_t *enc, SV *sv) 530encode_rv (enc_t *enc, SV *sv)
651 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); 699 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE));
652 enc.cur = SvPVX (enc.sv); 700 enc.cur = SvPVX (enc.sv);
653 enc.end = SvEND (enc.sv); 701 enc.end = SvEND (enc.sv);
654 enc.indent = 0; 702 enc.indent = 0;
655 enc.maxdepth = DEC_DEPTH (enc.json.flags); 703 enc.maxdepth = DEC_DEPTH (enc.json.flags);
704 enc.limit = enc.json.flags & F_ASCII ? 0x000080UL
705 : enc.json.flags & F_LATIN1 ? 0x000100UL
706 : 0x10FFFFUL;
656 707
657 SvPOK_only (enc.sv); 708 SvPOK_only (enc.sv);
658 encode_sv (&enc, scalar); 709 encode_sv (&enc, scalar);
659 710
660 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); 711 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv));
681 JSON json; 732 JSON json;
682 U32 depth; // recursion depth 733 U32 depth; // recursion depth
683 U32 maxdepth; // recursion depth limit 734 U32 maxdepth; // recursion depth limit
684} dec_t; 735} dec_t;
685 736
686inline void 737INLINE void
738decode_comment (dec_t *dec)
739{
740 // only '#'-style comments allowed a.t.m.
741
742 while (*dec->cur && *dec->cur != 0x0a && *dec->cur != 0x0d)
743 ++dec->cur;
744}
745
746INLINE void
687decode_ws (dec_t *dec) 747decode_ws (dec_t *dec)
688{ 748{
689 for (;;) 749 for (;;)
690 { 750 {
691 char ch = *dec->cur; 751 char ch = *dec->cur;
692 752
693 if (ch > 0x20 753 if (ch > 0x20)
754 {
755 if (expect_false (ch == '#'))
756 {
757 if (dec->json.flags & F_RELAXED)
758 decode_comment (dec);
759 else
760 break;
761 }
762 else
763 break;
764 }
694 || (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)) 765 else if (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)
695 break; 766 break; // parse error, but let higher level handle it, gives better error messages
696 767
697 ++dec->cur; 768 ++dec->cur;
698 } 769 }
699} 770}
700 771
806 877
807 if (hi >= 0x80) 878 if (hi >= 0x80)
808 { 879 {
809 utf8 = 1; 880 utf8 = 1;
810 881
811 cur = (char *)uvuni_to_utf8_flags (cur, hi, 0); 882 cur = encode_utf8 (cur, hi);
812 } 883 }
813 else 884 else
814 *cur++ = hi; 885 *cur++ = hi;
815 } 886 }
816 break; 887 break;
951 1022
952 if (!is_nv) 1023 if (!is_nv)
953 { 1024 {
954 int len = dec->cur - start; 1025 int len = dec->cur - start;
955 1026
956 // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so 1027 // special case the rather common 1..5-digit-int case
957 if (*start == '-') 1028 if (*start == '-')
958 switch (len) 1029 switch (len)
959 { 1030 {
960 case 2: return newSViv (-( start [1] - '0' * 1)); 1031 case 2: return newSViv (-( start [1] - '0' * 1));
961 case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); 1032 case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11));
962 case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); 1033 case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111));
963 case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); 1034 case 5: return newSViv (-( start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111));
1035 case 6: return newSViv (-(start [1] * 10000 + start [2] * 1000 + start [3] * 100 + start [4] * 10 + start [5] - '0' * 11111));
964 } 1036 }
965 else 1037 else
966 switch (len) 1038 switch (len)
967 { 1039 {
968 case 1: return newSViv ( start [0] - '0' * 1); 1040 case 1: return newSViv ( start [0] - '0' * 1);
969 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); 1041 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11);
970 case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); 1042 case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111);
971 case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); 1043 case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111);
1044 case 5: return newSViv ( start [0] * 10000 + start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 11111);
972 } 1045 }
973 1046
974 { 1047 {
975 UV uv; 1048 UV uv;
976 int numtype = grok_number (start, len, &uv); 1049 int numtype = grok_number (start, len, &uv);
1037 1110
1038 if (*dec->cur != ',') 1111 if (*dec->cur != ',')
1039 ERR (", or ] expected while parsing array"); 1112 ERR (", or ] expected while parsing array");
1040 1113
1041 ++dec->cur; 1114 ++dec->cur;
1115
1116 decode_ws (dec);
1117
1118 if (*dec->cur == ']' && dec->json.flags & F_RELAXED)
1119 {
1120 ++dec->cur;
1121 break;
1122 }
1042 } 1123 }
1043 1124
1044 DEC_DEC_DEPTH; 1125 DEC_DEC_DEPTH;
1045 return newRV_noinc ((SV *)av); 1126 return newRV_noinc ((SV *)av);
1046 1127
1062 if (*dec->cur == '}') 1143 if (*dec->cur == '}')
1063 ++dec->cur; 1144 ++dec->cur;
1064 else 1145 else
1065 for (;;) 1146 for (;;)
1066 { 1147 {
1067 decode_ws (dec); EXPECT_CH ('"'); 1148 EXPECT_CH ('"');
1068 1149
1069 // heuristic: assume that 1150 // heuristic: assume that
1070 // a) decode_str + hv_store_ent are abysmally slow. 1151 // a) decode_str + hv_store_ent are abysmally slow.
1071 // b) most hash keys are short, simple ascii text. 1152 // b) most hash keys are short, simple ascii text.
1072 // => try to "fast-match" such strings to avoid 1153 // => try to "fast-match" such strings to avoid
1086 if (!key) 1167 if (!key)
1087 goto fail; 1168 goto fail;
1088 1169
1089 decode_ws (dec); EXPECT_CH (':'); 1170 decode_ws (dec); EXPECT_CH (':');
1090 1171
1172 decode_ws (dec);
1091 value = decode_sv (dec); 1173 value = decode_sv (dec);
1092 if (!value) 1174 if (!value)
1093 { 1175 {
1094 SvREFCNT_dec (key); 1176 SvREFCNT_dec (key);
1095 goto fail; 1177 goto fail;
1107 int len = p - key; 1189 int len = p - key;
1108 dec->cur = p + 1; 1190 dec->cur = p + 1;
1109 1191
1110 decode_ws (dec); EXPECT_CH (':'); 1192 decode_ws (dec); EXPECT_CH (':');
1111 1193
1194 decode_ws (dec);
1112 value = decode_sv (dec); 1195 value = decode_sv (dec);
1113 if (!value) 1196 if (!value)
1114 goto fail; 1197 goto fail;
1115 1198
1116 hv_store (hv, key, len, value, 0); 1199 hv_store (hv, key, len, value, 0);
1132 1215
1133 if (*dec->cur != ',') 1216 if (*dec->cur != ',')
1134 ERR (", or } expected while parsing object/hash"); 1217 ERR (", or } expected while parsing object/hash");
1135 1218
1136 ++dec->cur; 1219 ++dec->cur;
1220
1221 decode_ws (dec);
1222
1223 if (*dec->cur == '}' && dec->json.flags & F_RELAXED)
1224 {
1225 ++dec->cur;
1226 break;
1227 }
1137 } 1228 }
1138 1229
1139 DEC_DEC_DEPTH; 1230 DEC_DEC_DEPTH;
1140 sv = newRV_noinc ((SV *)hv); 1231 sv = newRV_noinc ((SV *)hv);
1141 1232
1206} 1297}
1207 1298
1208static SV * 1299static SV *
1209decode_sv (dec_t *dec) 1300decode_sv (dec_t *dec)
1210{ 1301{
1211 decode_ws (dec);
1212
1213 // the beauty of JSON: you need exactly one character lookahead 1302 // the beauty of JSON: you need exactly one character lookahead
1214 // to parse anything. 1303 // to parse everything.
1215 switch (*dec->cur) 1304 switch (*dec->cur)
1216 { 1305 {
1217 case '"': ++dec->cur; return decode_str (dec); 1306 case '"': ++dec->cur; return decode_str (dec);
1218 case '[': ++dec->cur; return decode_av (dec); 1307 case '[': ++dec->cur; return decode_av (dec);
1219 case '{': ++dec->cur; return decode_hv (dec); 1308 case '{': ++dec->cur; return decode_hv (dec);
1220 1309
1221 case '-': 1310 case '-':
1222 case '0': case '1': case '2': case '3': case '4': 1311 case '0': case '1': case '2': case '3': case '4':
1223 case '5': case '6': case '7': case '8': case '9': 1312 case '5': case '6': case '7': case '8': case '9':
1224 return decode_num (dec); 1313 return decode_num (dec);
1301 1390
1302 if (dec.json.cb_object || dec.json.cb_sk_object) 1391 if (dec.json.cb_object || dec.json.cb_sk_object)
1303 dec.json.flags |= F_HOOK; 1392 dec.json.flags |= F_HOOK;
1304 1393
1305 *dec.end = 0; // this should basically be a nop, too, but make sure it's there 1394 *dec.end = 0; // this should basically be a nop, too, but make sure it's there
1395
1396 decode_ws (&dec);
1306 sv = decode_sv (&dec); 1397 sv = decode_sv (&dec);
1307 1398
1308 if (!(offset_return || !sv)) 1399 if (!(offset_return || !sv))
1309 { 1400 {
1310 // check for trailing garbage 1401 // check for trailing garbage
1390{ 1481{
1391 SV *pv = NEWSV (0, sizeof (JSON)); 1482 SV *pv = NEWSV (0, sizeof (JSON));
1392 SvPOK_only (pv); 1483 SvPOK_only (pv);
1393 Zero (SvPVX (pv), 1, JSON); 1484 Zero (SvPVX (pv), 1, JSON);
1394 ((JSON *)SvPVX (pv))->flags = F_DEFAULT; 1485 ((JSON *)SvPVX (pv))->flags = F_DEFAULT;
1395 XPUSHs (sv_2mortal (sv_bless (newRV_noinc (pv), JSON_STASH))); 1486 XPUSHs (sv_2mortal (sv_bless (
1487 newRV_noinc (pv),
1488 strEQ (klass, "JSON::XS") ? JSON_STASH : gv_stashpv (klass, 1)
1489 )));
1396} 1490}
1397 1491
1398void ascii (JSON *self, int enable = 1) 1492void ascii (JSON *self, int enable = 1)
1399 ALIAS: 1493 ALIAS:
1400 ascii = F_ASCII 1494 ascii = F_ASCII
1407 pretty = F_PRETTY 1501 pretty = F_PRETTY
1408 allow_nonref = F_ALLOW_NONREF 1502 allow_nonref = F_ALLOW_NONREF
1409 shrink = F_SHRINK 1503 shrink = F_SHRINK
1410 allow_blessed = F_ALLOW_BLESSED 1504 allow_blessed = F_ALLOW_BLESSED
1411 convert_blessed = F_CONV_BLESSED 1505 convert_blessed = F_CONV_BLESSED
1506 relaxed = F_RELAXED
1412 PPCODE: 1507 PPCODE:
1413{ 1508{
1414 if (enable) 1509 if (enable)
1415 self->flags |= ix; 1510 self->flags |= ix;
1416 else 1511 else
1417 self->flags &= ~ix; 1512 self->flags &= ~ix;
1418 1513
1419 XPUSHs (ST (0)); 1514 XPUSHs (ST (0));
1420} 1515}
1421 1516
1517void get_ascii (JSON *self)
1518 ALIAS:
1519 get_ascii = F_ASCII
1520 get_latin1 = F_LATIN1
1521 get_utf8 = F_UTF8
1522 get_indent = F_INDENT
1523 get_canonical = F_CANONICAL
1524 get_space_before = F_SPACE_BEFORE
1525 get_space_after = F_SPACE_AFTER
1526 get_allow_nonref = F_ALLOW_NONREF
1527 get_shrink = F_SHRINK
1528 get_allow_blessed = F_ALLOW_BLESSED
1529 get_convert_blessed = F_CONV_BLESSED
1530 get_relaxed = F_RELAXED
1531 PPCODE:
1532 XPUSHs (boolSV (self->flags & ix));
1533
1422void max_depth (JSON *self, UV max_depth = 0x80000000UL) 1534void max_depth (JSON *self, UV max_depth = 0x80000000UL)
1423 PPCODE: 1535 PPCODE:
1424{ 1536{
1425 UV log2 = 0; 1537 UV log2 = 0;
1426 1538
1432 self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); 1544 self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH);
1433 1545
1434 XPUSHs (ST (0)); 1546 XPUSHs (ST (0));
1435} 1547}
1436 1548
1549U32 get_max_depth (JSON *self)
1550 CODE:
1551 RETVAL = DEC_DEPTH (self->flags);
1552 OUTPUT:
1553 RETVAL
1554
1437void max_size (JSON *self, UV max_size = 0) 1555void max_size (JSON *self, UV max_size = 0)
1438 PPCODE: 1556 PPCODE:
1439{ 1557{
1440 UV log2 = 0; 1558 UV log2 = 0;
1441 1559
1447 1565
1448 self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE); 1566 self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE);
1449 1567
1450 XPUSHs (ST (0)); 1568 XPUSHs (ST (0));
1451} 1569}
1570
1571int get_max_size (JSON *self)
1572 CODE:
1573 RETVAL = DEC_SIZE (self->flags);
1574 OUTPUT:
1575 RETVAL
1452 1576
1453void filter_json_object (JSON *self, SV *cb = &PL_sv_undef) 1577void filter_json_object (JSON *self, SV *cb = &PL_sv_undef)
1454 PPCODE: 1578 PPCODE:
1455{ 1579{
1456 SvREFCNT_dec (self->cb_object); 1580 SvREFCNT_dec (self->cb_object);
1503 SvREFCNT_dec (self->cb_sk_object); 1627 SvREFCNT_dec (self->cb_sk_object);
1504 SvREFCNT_dec (self->cb_object); 1628 SvREFCNT_dec (self->cb_object);
1505 1629
1506PROTOTYPES: ENABLE 1630PROTOTYPES: ENABLE
1507 1631
1508void to_json (SV *scalar) 1632void encode_json (SV *scalar)
1509 PPCODE: 1633 PPCODE:
1510{ 1634{
1511 JSON json = { F_DEFAULT | F_UTF8 }; 1635 JSON json = { F_DEFAULT | F_UTF8 };
1512 XPUSHs (encode_json (scalar, &json)); 1636 XPUSHs (encode_json (scalar, &json));
1513} 1637}
1514 1638
1515void from_json (SV *jsonstr) 1639void decode_json (SV *jsonstr)
1516 PPCODE: 1640 PPCODE:
1517{ 1641{
1518 JSON json = { F_DEFAULT | F_UTF8 }; 1642 JSON json = { F_DEFAULT | F_UTF8 };
1519 XPUSHs (decode_json (jsonstr, &json, 0)); 1643 XPUSHs (decode_json (jsonstr, &json, 0));
1520} 1644}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines