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.57 by root, Mon Aug 13 16:05:42 2007 UTC vs.
Revision 1.71 by root, Wed Mar 19 03:17:38 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)
62 64
63#ifdef USE_ITHREADS 65#ifdef USE_ITHREADS
64# define JSON_SLOW 1 66# define JSON_SLOW 1
67# define JSON_STASH (json_stash ? json_stash : gv_stashpv ("JSON::XS", 1))
65#else 68#else
66# define JSON_SLOW 1 69# define JSON_SLOW 0
70# define JSON_STASH json_stash
67#endif 71#endif
68 72
69static HV *json_stash, *json_boolean_stash; // JSON::XS:: 73static HV *json_stash, *json_boolean_stash; // JSON::XS::
70static SV *json_true, *json_false; 74static SV *json_true, *json_false;
71 75
76} JSON; 80} JSON;
77 81
78///////////////////////////////////////////////////////////////////////////// 82/////////////////////////////////////////////////////////////////////////////
79// utility functions 83// utility functions
80 84
81inline void 85INLINE void
82shrink (SV *sv) 86shrink (SV *sv)
83{ 87{
84 sv_utf8_downgrade (sv, 1); 88 sv_utf8_downgrade (sv, 1);
85 if (SvLEN (sv) > SvCUR (sv) + 1) 89 if (SvLEN (sv) > SvCUR (sv) + 1)
86 { 90 {
95// 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
96// case of an error. 100// case of an error.
97// we special-case "safe" characters from U+80 .. U+7FF, 101// we special-case "safe" characters from U+80 .. U+7FF,
98// but use the very good perl function to parse anything else. 102// but use the very good perl function to parse anything else.
99// note that we never call this function for a ascii codepoints 103// note that we never call this function for a ascii codepoints
100inline UV 104INLINE UV
101decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen) 105decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen)
102{ 106{
103 if (expect_false (s[0] > 0xdf || s[0] < 0xc2)) 107 if (expect_false (s[0] > 0xdf || s[0] < 0xc2))
104 return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY); 108 return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY);
105 else if (len > 1 && s[1] >= 0x80 && s[1] <= 0xbf) 109 else if (len > 1 && s[1] >= 0x80 && s[1] <= 0xbf)
124 char *end; // SvEND (sv) 128 char *end; // SvEND (sv)
125 SV *sv; // result scalar 129 SV *sv; // result scalar
126 JSON json; 130 JSON json;
127 U32 indent; // indentation level 131 U32 indent; // indentation level
128 U32 maxdepth; // max. indentation/recursion level 132 U32 maxdepth; // max. indentation/recursion level
133 UV limit; // escape character values >= this value when encoding
129} enc_t; 134} enc_t;
130 135
131inline void 136INLINE void
132need (enc_t *enc, STRLEN len) 137need (enc_t *enc, STRLEN len)
133{ 138{
134 if (expect_false (enc->cur + len >= enc->end)) 139 if (expect_false (enc->cur + len >= enc->end))
135 { 140 {
136 STRLEN cur = enc->cur - SvPVX (enc->sv); 141 STRLEN cur = enc->cur - SvPVX (enc->sv);
138 enc->cur = SvPVX (enc->sv) + cur; 143 enc->cur = SvPVX (enc->sv) + cur;
139 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; 144 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1;
140 } 145 }
141} 146}
142 147
143inline void 148INLINE void
144encode_ch (enc_t *enc, char ch) 149encode_ch (enc_t *enc, char ch)
145{ 150{
146 need (enc, 1); 151 need (enc, 1);
147 *enc->cur++ = ch; 152 *enc->cur++ = ch;
148} 153}
202 { 207 {
203 uch = ch; 208 uch = ch;
204 clen = 1; 209 clen = 1;
205 } 210 }
206 211
207 if (uch > 0x10FFFFUL) 212 if (uch < 0x20 || uch >= enc->limit)
208 croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch);
209
210 if (uch < 0x80 || enc->json.flags & F_ASCII || (enc->json.flags & F_LATIN1 && uch > 0xFF))
211 { 213 {
212 if (uch > 0xFFFFUL) 214 if (uch > 0xFFFFUL)
213 { 215 {
216 if (uch > 0x10FFFFUL)
217 croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch);
218
214 need (enc, len += 11); 219 need (enc, len += 11);
215 sprintf (enc->cur, "\\u%04x\\u%04x", 220 sprintf (enc->cur, "\\u%04x\\u%04x",
216 (int)((uch - 0x10000) / 0x400 + 0xD800), 221 (int)((uch - 0x10000) / 0x400 + 0xD800),
217 (int)((uch - 0x10000) % 0x400 + 0xDC00)); 222 (int)((uch - 0x10000) % 0x400 + 0xDC00));
218 enc->cur += 12; 223 enc->cur += 12;
257 262
258 --len; 263 --len;
259 } 264 }
260} 265}
261 266
262inline void 267INLINE void
263encode_indent (enc_t *enc) 268encode_indent (enc_t *enc)
264{ 269{
265 if (enc->json.flags & F_INDENT) 270 if (enc->json.flags & F_INDENT)
266 { 271 {
267 int spaces = enc->indent * INDENT_STEP; 272 int spaces = enc->indent * INDENT_STEP;
270 memset (enc->cur, ' ', spaces); 275 memset (enc->cur, ' ', spaces);
271 enc->cur += spaces; 276 enc->cur += spaces;
272 } 277 }
273} 278}
274 279
275inline void 280INLINE void
276encode_space (enc_t *enc) 281encode_space (enc_t *enc)
277{ 282{
278 need (enc, 1); 283 need (enc, 1);
279 encode_ch (enc, ' '); 284 encode_ch (enc, ' ');
280} 285}
281 286
282inline void 287INLINE void
283encode_nl (enc_t *enc) 288encode_nl (enc_t *enc)
284{ 289{
285 if (enc->json.flags & F_INDENT) 290 if (enc->json.flags & F_INDENT)
286 { 291 {
287 need (enc, 1); 292 need (enc, 1);
288 encode_ch (enc, '\n'); 293 encode_ch (enc, '\n');
289 } 294 }
290} 295}
291 296
292inline void 297INLINE void
293encode_comma (enc_t *enc) 298encode_comma (enc_t *enc)
294{ 299{
295 encode_ch (enc, ','); 300 encode_ch (enc, ',');
296 301
297 if (enc->json.flags & F_INDENT) 302 if (enc->json.flags & F_INDENT)
308 int i, len = av_len (av); 313 int i, len = av_len (av);
309 314
310 if (enc->indent >= enc->maxdepth) 315 if (enc->indent >= enc->maxdepth)
311 croak ("data structure too deep (hit recursion limit)"); 316 croak ("data structure too deep (hit recursion limit)");
312 317
313 encode_ch (enc, '['); encode_nl (enc); 318 encode_ch (enc, '[');
314 ++enc->indent; 319
320 if (len >= 0)
321 {
322 encode_nl (enc); ++enc->indent;
315 323
316 for (i = 0; i <= len; ++i) 324 for (i = 0; i <= len; ++i)
317 { 325 {
318 SV **svp = av_fetch (av, i, 0); 326 SV **svp = av_fetch (av, i, 0);
319 327
320 encode_indent (enc); 328 encode_indent (enc);
321 329
322 if (svp) 330 if (svp)
323 encode_sv (enc, *svp); 331 encode_sv (enc, *svp);
324 else 332 else
325 encode_str (enc, "null", 4, 0); 333 encode_str (enc, "null", 4, 0);
326 334
327 if (i < len) 335 if (i < len)
328 encode_comma (enc); 336 encode_comma (enc);
329 } 337 }
330 338
339 encode_nl (enc); --enc->indent; encode_indent (enc);
340 }
341
331 encode_nl (enc); 342 encode_ch (enc, ']');
332
333 --enc->indent;
334 encode_indent (enc); encode_ch (enc, ']');
335} 343}
336 344
337static void 345static void
338encode_he (enc_t *enc, HE *he) 346encode_hk (enc_t *enc, HE *he)
339{ 347{
340 encode_ch (enc, '"'); 348 encode_ch (enc, '"');
341 349
342 if (HeKLEN (he) == HEf_SVKEY) 350 if (HeKLEN (he) == HEf_SVKEY)
343 { 351 {
356 encode_ch (enc, '"'); 364 encode_ch (enc, '"');
357 365
358 if (enc->json.flags & F_SPACE_BEFORE) encode_space (enc); 366 if (enc->json.flags & F_SPACE_BEFORE) encode_space (enc);
359 encode_ch (enc, ':'); 367 encode_ch (enc, ':');
360 if (enc->json.flags & F_SPACE_AFTER ) encode_space (enc); 368 if (enc->json.flags & F_SPACE_AFTER ) encode_space (enc);
361 encode_sv (enc, HeVAL (he));
362} 369}
363 370
364// compare hash entries, used when all keys are bytestrings 371// compare hash entries, used when all keys are bytestrings
365static int 372static int
366he_cmp_fast (const void *a_, const void *b_) 373he_cmp_fast (const void *a_, const void *b_)
371 HE *b = *(HE **)b_; 378 HE *b = *(HE **)b_;
372 379
373 STRLEN la = HeKLEN (a); 380 STRLEN la = HeKLEN (a);
374 STRLEN lb = HeKLEN (b); 381 STRLEN lb = HeKLEN (b);
375 382
376 if (!(cmp = memcmp (HeKEY (a), HeKEY (b), la < lb ? la : lb))) 383 if (!(cmp = memcmp (HeKEY (b), HeKEY (a), lb < la ? lb : la)))
377 cmp = la - lb; 384 cmp = lb - la;
378 385
379 return cmp; 386 return cmp;
380} 387}
381 388
382// compare hash entries, used when some keys are sv's or utf-x 389// compare hash entries, used when some keys are sv's or utf-x
383static int 390static int
384he_cmp_slow (const void *a, const void *b) 391he_cmp_slow (const void *a, const void *b)
385{ 392{
386 return sv_cmp (HeSVKEY_force (*(HE **)a), HeSVKEY_force (*(HE **)b)); 393 return sv_cmp (HeSVKEY_force (*(HE **)b), HeSVKEY_force (*(HE **)a));
387} 394}
388 395
389static void 396static void
390encode_hv (enc_t *enc, HV *hv) 397encode_hv (enc_t *enc, HV *hv)
391{ 398{
399 HE *he;
392 int count, i; 400 int count;
393 401
394 if (enc->indent >= enc->maxdepth) 402 if (enc->indent >= enc->maxdepth)
395 croak ("data structure too deep (hit recursion limit)"); 403 croak ("data structure too deep (hit recursion limit)");
396 404
397 encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; 405 encode_ch (enc, '{');
398 406
399 if ((count = hv_iterinit (hv)))
400 {
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
405 if (enc->json.flags & F_CANONICAL) 411 if (enc->json.flags & F_CANONICAL)
412 {
413 int count = hv_iterinit (hv);
414
415 if (SvMAGICAL (hv))
406 { 416 {
417 // need to count by iterating. could improve by dynamically building the vector below
418 // but I don't care for the speed of this special case.
419 // note also that we will run into undefined behaviour when the two iterations
420 // do not result in the same count, something I might care for in some later release.
421
422 count = 0;
423 while (hv_iternext (hv))
424 ++count;
425
426 hv_iterinit (hv);
427 }
428
429 if (count)
430 {
407 int fast = 1; 431 int i, fast = 1;
408 HE *he;
409#if defined(__BORLANDC__) || defined(_MSC_VER) 432#if defined(__BORLANDC__) || defined(_MSC_VER)
410 HE **hes = _alloca (count * sizeof (HE)); 433 HE **hes = _alloca (count * sizeof (HE));
411#else 434#else
412 HE *hes [count]; // if your compiler dies here, you need to enable C99 mode 435 HE *hes [count]; // if your compiler dies here, you need to enable C99 mode
413#endif 436#endif
440 463
441 FREETMPS; 464 FREETMPS;
442 LEAVE; 465 LEAVE;
443 } 466 }
444 467
445 for (i = 0; i < count; ++i) 468 encode_nl (enc); ++enc->indent;
469
470 while (count--)
446 { 471 {
447 encode_indent (enc); 472 encode_indent (enc);
473 he = hes [count];
448 encode_he (enc, hes [i]); 474 encode_hk (enc, he);
475 encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he));
449 476
450 if (i < count - 1) 477 if (count)
451 encode_comma (enc); 478 encode_comma (enc);
452 } 479 }
453 480
454 encode_nl (enc); 481 encode_nl (enc); --enc->indent; encode_indent (enc);
455 } 482 }
483 }
456 else 484 else
485 {
486 if (hv_iterinit (hv) || SvMAGICAL (hv))
487 if ((he = hv_iternext (hv)))
457 { 488 {
458 HE *he = hv_iternext (hv); 489 encode_nl (enc); ++enc->indent;
459 490
460 for (;;) 491 for (;;)
461 { 492 {
462 encode_indent (enc); 493 encode_indent (enc);
463 encode_he (enc, he); 494 encode_hk (enc, he);
495 encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he));
464 496
465 if (!(he = hv_iternext (hv))) 497 if (!(he = hv_iternext (hv)))
466 break; 498 break;
467 499
468 encode_comma (enc); 500 encode_comma (enc);
469 } 501 }
470 502
471 encode_nl (enc); 503 encode_nl (enc); --enc->indent; encode_indent (enc);
472 } 504 }
473 } 505 }
474 506
475 --enc->indent; encode_indent (enc); encode_ch (enc, '}'); 507 encode_ch (enc, '}');
476} 508}
477 509
478// encode objects, arrays and special \0=false and \1=true values. 510// encode objects, arrays and special \0=false and \1=true values.
479static void 511static void
480encode_rv (enc_t *enc, SV *sv) 512encode_rv (enc_t *enc, SV *sv)
510 // we re-bless the reference to get overload and other niceties right 542 // we re-bless the reference to get overload and other niceties right
511 GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 0); 543 GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 0);
512 544
513 if (to_json) 545 if (to_json)
514 { 546 {
515 int count;
516 dSP; 547 dSP;
517 548
518 ENTER; SAVETMPS; PUSHMARK (SP); 549 ENTER; SAVETMPS; PUSHMARK (SP);
519 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); 550 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv)));
520 551
650 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); 681 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE));
651 enc.cur = SvPVX (enc.sv); 682 enc.cur = SvPVX (enc.sv);
652 enc.end = SvEND (enc.sv); 683 enc.end = SvEND (enc.sv);
653 enc.indent = 0; 684 enc.indent = 0;
654 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;
655 689
656 SvPOK_only (enc.sv); 690 SvPOK_only (enc.sv);
657 encode_sv (&enc, scalar); 691 encode_sv (&enc, scalar);
658 692
659 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); 693 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv));
680 JSON json; 714 JSON json;
681 U32 depth; // recursion depth 715 U32 depth; // recursion depth
682 U32 maxdepth; // recursion depth limit 716 U32 maxdepth; // recursion depth limit
683} dec_t; 717} dec_t;
684 718
685inline void 719INLINE void
720decode_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
728INLINE void
686decode_ws (dec_t *dec) 729decode_ws (dec_t *dec)
687{ 730{
688 for (;;) 731 for (;;)
689 { 732 {
690 char ch = *dec->cur; 733 char ch = *dec->cur;
691 734
692 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 }
693 || (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)) 747 else if (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)
694 break; 748 break; // parse error, but let higher level handle it, gives better error messages
695 749
696 ++dec->cur; 750 ++dec->cur;
697 } 751 }
698} 752}
699 753
950 1004
951 if (!is_nv) 1005 if (!is_nv)
952 { 1006 {
953 int len = dec->cur - start; 1007 int len = dec->cur - start;
954 1008
955 // 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
956 if (*start == '-') 1010 if (*start == '-')
957 switch (len) 1011 switch (len)
958 { 1012 {
959 case 2: return newSViv (-( start [1] - '0' * 1)); 1013 case 2: return newSViv (-( start [1] - '0' * 1));
960 case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); 1014 case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11));
961 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));
962 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));
963 } 1018 }
964 else 1019 else
965 switch (len) 1020 switch (len)
966 { 1021 {
967 case 1: return newSViv ( start [0] - '0' * 1); 1022 case 1: return newSViv ( start [0] - '0' * 1);
968 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); 1023 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11);
969 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);
970 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);
971 } 1027 }
972 1028
973 { 1029 {
974 UV uv; 1030 UV uv;
975 int numtype = grok_number (start, len, &uv); 1031 int numtype = grok_number (start, len, &uv);
1036 1092
1037 if (*dec->cur != ',') 1093 if (*dec->cur != ',')
1038 ERR (", or ] expected while parsing array"); 1094 ERR (", or ] expected while parsing array");
1039 1095
1040 ++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 }
1041 } 1105 }
1042 1106
1043 DEC_DEC_DEPTH; 1107 DEC_DEC_DEPTH;
1044 return newRV_noinc ((SV *)av); 1108 return newRV_noinc ((SV *)av);
1045 1109
1061 if (*dec->cur == '}') 1125 if (*dec->cur == '}')
1062 ++dec->cur; 1126 ++dec->cur;
1063 else 1127 else
1064 for (;;) 1128 for (;;)
1065 { 1129 {
1066 decode_ws (dec); EXPECT_CH ('"'); 1130 EXPECT_CH ('"');
1067 1131
1068 // heuristic: assume that 1132 // heuristic: assume that
1069 // a) decode_str + hv_store_ent are abysmally slow. 1133 // a) decode_str + hv_store_ent are abysmally slow.
1070 // b) most hash keys are short, simple ascii text. 1134 // b) most hash keys are short, simple ascii text.
1071 // => try to "fast-match" such strings to avoid 1135 // => try to "fast-match" such strings to avoid
1085 if (!key) 1149 if (!key)
1086 goto fail; 1150 goto fail;
1087 1151
1088 decode_ws (dec); EXPECT_CH (':'); 1152 decode_ws (dec); EXPECT_CH (':');
1089 1153
1154 decode_ws (dec);
1090 value = decode_sv (dec); 1155 value = decode_sv (dec);
1091 if (!value) 1156 if (!value)
1092 { 1157 {
1093 SvREFCNT_dec (key); 1158 SvREFCNT_dec (key);
1094 goto fail; 1159 goto fail;
1106 int len = p - key; 1171 int len = p - key;
1107 dec->cur = p + 1; 1172 dec->cur = p + 1;
1108 1173
1109 decode_ws (dec); EXPECT_CH (':'); 1174 decode_ws (dec); EXPECT_CH (':');
1110 1175
1176 decode_ws (dec);
1111 value = decode_sv (dec); 1177 value = decode_sv (dec);
1112 if (!value) 1178 if (!value)
1113 goto fail; 1179 goto fail;
1114 1180
1115 hv_store (hv, key, len, value, 0); 1181 hv_store (hv, key, len, value, 0);
1131 1197
1132 if (*dec->cur != ',') 1198 if (*dec->cur != ',')
1133 ERR (", or } expected while parsing object/hash"); 1199 ERR (", or } expected while parsing object/hash");
1134 1200
1135 ++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 }
1136 } 1210 }
1137 1211
1138 DEC_DEC_DEPTH; 1212 DEC_DEC_DEPTH;
1139 sv = newRV_noinc ((SV *)hv); 1213 sv = newRV_noinc ((SV *)hv);
1140 1214
1205} 1279}
1206 1280
1207static SV * 1281static SV *
1208decode_sv (dec_t *dec) 1282decode_sv (dec_t *dec)
1209{ 1283{
1210 decode_ws (dec);
1211
1212 // the beauty of JSON: you need exactly one character lookahead 1284 // the beauty of JSON: you need exactly one character lookahead
1213 // to parse anything. 1285 // to parse anything.
1214 switch (*dec->cur) 1286 switch (*dec->cur)
1215 { 1287 {
1216 case '"': ++dec->cur; return decode_str (dec); 1288 case '"': ++dec->cur; return decode_str (dec);
1224 1296
1225 case 't': 1297 case 't':
1226 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) 1298 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4))
1227 { 1299 {
1228 dec->cur += 4; 1300 dec->cur += 4;
1301#if JSON_SLOW
1302 json_true = get_sv ("JSON::XS::true", 1); SvREADONLY_on (json_true);
1303#endif
1229 return SvREFCNT_inc (json_true); 1304 return SvREFCNT_inc (json_true);
1230 } 1305 }
1231 else 1306 else
1232 ERR ("'true' expected"); 1307 ERR ("'true' expected");
1233 1308
1235 1310
1236 case 'f': 1311 case 'f':
1237 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) 1312 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5))
1238 { 1313 {
1239 dec->cur += 5; 1314 dec->cur += 5;
1315#if JSON_SLOW
1316 json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false);
1317#endif
1240 return SvREFCNT_inc (json_false); 1318 return SvREFCNT_inc (json_false);
1241 } 1319 }
1242 else 1320 else
1243 ERR ("'false' expected"); 1321 ERR ("'false' expected");
1244 1322
1294 1372
1295 if (dec.json.cb_object || dec.json.cb_sk_object) 1373 if (dec.json.cb_object || dec.json.cb_sk_object)
1296 dec.json.flags |= F_HOOK; 1374 dec.json.flags |= F_HOOK;
1297 1375
1298 *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);
1299 sv = decode_sv (&dec); 1379 sv = decode_sv (&dec);
1300 1380
1301 if (!(offset_return || !sv)) 1381 if (!(offset_return || !sv))
1302 { 1382 {
1303 // check for trailing garbage 1383 // check for trailing garbage
1379 json_boolean_stash = 0; 1459 json_boolean_stash = 0;
1380 1460
1381void new (char *klass) 1461void new (char *klass)
1382 PPCODE: 1462 PPCODE:
1383{ 1463{
1384 HV *stash = !JSON_SLOW || json_stash
1385 ? json_stash
1386 : gv_stashpv ("JSON::XS", 1);
1387 SV *pv = NEWSV (0, sizeof (JSON)); 1464 SV *pv = NEWSV (0, sizeof (JSON));
1388 SvPOK_only (pv); 1465 SvPOK_only (pv);
1389 Zero (SvPVX (pv), 1, JSON); 1466 Zero (SvPVX (pv), 1, JSON);
1390 ((JSON *)SvPVX (pv))->flags = F_DEFAULT; 1467 ((JSON *)SvPVX (pv))->flags = F_DEFAULT;
1391 XPUSHs (sv_2mortal (sv_bless (newRV_noinc (pv), stash))); 1468 XPUSHs (sv_2mortal (sv_bless (
1469 newRV_noinc (pv),
1470 strEQ (klass, "JSON::XS") ? JSON_STASH : gv_stashpv (klass, 1)
1471 )));
1392} 1472}
1393 1473
1394void ascii (JSON *self, int enable = 1) 1474void ascii (JSON *self, int enable = 1)
1395 ALIAS: 1475 ALIAS:
1396 ascii = F_ASCII 1476 ascii = F_ASCII
1403 pretty = F_PRETTY 1483 pretty = F_PRETTY
1404 allow_nonref = F_ALLOW_NONREF 1484 allow_nonref = F_ALLOW_NONREF
1405 shrink = F_SHRINK 1485 shrink = F_SHRINK
1406 allow_blessed = F_ALLOW_BLESSED 1486 allow_blessed = F_ALLOW_BLESSED
1407 convert_blessed = F_CONV_BLESSED 1487 convert_blessed = F_CONV_BLESSED
1488 relaxed = F_RELAXED
1408 PPCODE: 1489 PPCODE:
1409{ 1490{
1410 if (enable) 1491 if (enable)
1411 self->flags |= ix; 1492 self->flags |= ix;
1412 else 1493 else
1413 self->flags &= ~ix; 1494 self->flags &= ~ix;
1414 1495
1415 XPUSHs (ST (0)); 1496 XPUSHs (ST (0));
1416} 1497}
1417 1498
1499void 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
1418void max_depth (JSON *self, UV max_depth = 0x80000000UL) 1516void max_depth (JSON *self, UV max_depth = 0x80000000UL)
1419 PPCODE: 1517 PPCODE:
1420{ 1518{
1421 UV log2 = 0; 1519 UV log2 = 0;
1422 1520
1428 self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); 1526 self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH);
1429 1527
1430 XPUSHs (ST (0)); 1528 XPUSHs (ST (0));
1431} 1529}
1432 1530
1531U32 get_max_depth (JSON *self)
1532 CODE:
1533 RETVAL = DEC_DEPTH (self->flags);
1534 OUTPUT:
1535 RETVAL
1536
1433void max_size (JSON *self, UV max_size = 0) 1537void max_size (JSON *self, UV max_size = 0)
1434 PPCODE: 1538 PPCODE:
1435{ 1539{
1436 UV log2 = 0; 1540 UV log2 = 0;
1437 1541
1443 1547
1444 self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE); 1548 self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE);
1445 1549
1446 XPUSHs (ST (0)); 1550 XPUSHs (ST (0));
1447} 1551}
1552
1553int get_max_size (JSON *self)
1554 CODE:
1555 RETVAL = DEC_SIZE (self->flags);
1556 OUTPUT:
1557 RETVAL
1448 1558
1449void filter_json_object (JSON *self, SV *cb = &PL_sv_undef) 1559void filter_json_object (JSON *self, SV *cb = &PL_sv_undef)
1450 PPCODE: 1560 PPCODE:
1451{ 1561{
1452 SvREFCNT_dec (self->cb_object); 1562 SvREFCNT_dec (self->cb_object);
1499 SvREFCNT_dec (self->cb_sk_object); 1609 SvREFCNT_dec (self->cb_sk_object);
1500 SvREFCNT_dec (self->cb_object); 1610 SvREFCNT_dec (self->cb_object);
1501 1611
1502PROTOTYPES: ENABLE 1612PROTOTYPES: ENABLE
1503 1613
1504void to_json (SV *scalar) 1614void encode_json (SV *scalar)
1505 PPCODE: 1615 PPCODE:
1506{ 1616{
1507 JSON json = { F_DEFAULT | F_UTF8 }; 1617 JSON json = { F_DEFAULT | F_UTF8 };
1508 XPUSHs (encode_json (scalar, &json)); 1618 XPUSHs (encode_json (scalar, &json));
1509} 1619}
1510 1620
1511void from_json (SV *jsonstr) 1621void decode_json (SV *jsonstr)
1512 PPCODE: 1622 PPCODE:
1513{ 1623{
1514 JSON json = { F_DEFAULT | F_UTF8 }; 1624 JSON json = { F_DEFAULT | F_UTF8 };
1515 XPUSHs (decode_json (jsonstr, &json, 0)); 1625 XPUSHs (decode_json (jsonstr, &json, 0));
1516} 1626}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines