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.38 by root, Mon Jun 11 03:18:07 2007 UTC vs.
Revision 1.48 by root, Sun Jul 1 22:20:00 2007 UTC

9 9
10#if defined(__BORLANDC__) || defined(_MSC_VER) 10#if defined(__BORLANDC__) || defined(_MSC_VER)
11# define snprintf _snprintf // C compilers have this in stdio.h 11# define snprintf _snprintf // C compilers have this in stdio.h
12#endif 12#endif
13 13
14// some old perls do not have this, try to make it work, no
15// guarentees, though. if it breaks, you get to keep the pieces.
16#ifndef UTF8_MAXBYTES
17# define UTF8_MAXBYTES 13
18#endif
19
14#define F_ASCII 0x00000001UL 20#define F_ASCII 0x00000001UL
15#define F_LATIN1 0x00000002UL 21#define F_LATIN1 0x00000002UL
16#define F_UTF8 0x00000004UL 22#define F_UTF8 0x00000004UL
17#define F_INDENT 0x00000008UL 23#define F_INDENT 0x00000008UL
18#define F_CANONICAL 0x00000010UL 24#define F_CANONICAL 0x00000010UL
19#define F_SPACE_BEFORE 0x00000020UL 25#define F_SPACE_BEFORE 0x00000020UL
20#define F_SPACE_AFTER 0x00000040UL 26#define F_SPACE_AFTER 0x00000040UL
21#define F_ALLOW_NONREF 0x00000100UL 27#define F_ALLOW_NONREF 0x00000100UL
22#define F_SHRINK 0x00000200UL 28#define F_SHRINK 0x00000200UL
29#define F_ALLOW_BLESSED 0x00000400UL
30#define F_CONV_BLESSED 0x00000800UL // NYI
23#define F_MAXDEPTH 0xf8000000UL 31#define F_MAXDEPTH 0xf8000000UL
24#define S_MAXDEPTH 27 32#define S_MAXDEPTH 27
33#define F_MAXSIZE 0x01f00000UL
34#define S_MAXSIZE 20
25 35
26#define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) 36#define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH))
27 37#define DEC_SIZE(flags) (1UL << ((flags & F_MAXSIZE ) >> S_MAXSIZE ))
28// F_SELFCONVERT? <=> to_json/toJson
29// F_BLESSED? <=> { $__class__$ => }
30 38
31#define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER 39#define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER
32#define F_DEFAULT (9UL << S_MAXDEPTH) 40#define F_DEFAULT (9UL << S_MAXDEPTH)
33 41
34#define INIT_SIZE 32 // initial scalar size to be allocated 42#define INIT_SIZE 32 // initial scalar size to be allocated
48#endif 56#endif
49 57
50#define expect_false(expr) expect ((expr) != 0, 0) 58#define expect_false(expr) expect ((expr) != 0, 0)
51#define expect_true(expr) expect ((expr) != 0, 1) 59#define expect_true(expr) expect ((expr) != 0, 1)
52 60
53static HV *json_stash; // JSON::XS:: 61static HV *json_stash, *json_boolean_stash; // JSON::XS::
62static SV *json_true, *json_false;
63
64typedef struct {
65 U32 flags;
66} JSON;
54 67
55///////////////////////////////////////////////////////////////////////////// 68/////////////////////////////////////////////////////////////////////////////
56// utility functions 69// utility functions
57 70
58static UV * 71inline void
59SvJSON (SV *sv)
60{
61 if (!(SvROK (sv) && SvOBJECT (SvRV (sv)) && SvSTASH (SvRV (sv)) == json_stash))
62 croak ("object is not of type JSON::XS");
63
64 return &SvUVX (SvRV (sv));
65}
66
67static void
68shrink (SV *sv) 72shrink (SV *sv)
69{ 73{
70 sv_utf8_downgrade (sv, 1); 74 sv_utf8_downgrade (sv, 1);
71 if (SvLEN (sv) > SvCUR (sv) + 1) 75 if (SvLEN (sv) > SvCUR (sv) + 1)
72 { 76 {
107typedef struct 111typedef struct
108{ 112{
109 char *cur; // SvPVX (sv) + current output position 113 char *cur; // SvPVX (sv) + current output position
110 char *end; // SvEND (sv) 114 char *end; // SvEND (sv)
111 SV *sv; // result scalar 115 SV *sv; // result scalar
112 U32 flags; // F_* 116 JSON json;
113 U32 indent; // indentation level 117 U32 indent; // indentation level
114 U32 maxdepth; // max. indentation/recursion level 118 U32 maxdepth; // max. indentation/recursion level
115} enc_t; 119} enc_t;
116 120
117inline void 121inline void
178 STRLEN clen; 182 STRLEN clen;
179 UV uch; 183 UV uch;
180 184
181 if (is_utf8) 185 if (is_utf8)
182 { 186 {
183 //uch = utf8n_to_uvuni (str, end - str, &clen, UTF8_CHECK_ONLY);
184 uch = decode_utf8 (str, end - str, &clen); 187 uch = decode_utf8 (str, end - str, &clen);
185 if (clen == (STRLEN)-1) 188 if (clen == (STRLEN)-1)
186 croak ("malformed or illegal unicode character in string [%.11s], cannot convert to JSON", str); 189 croak ("malformed or illegal unicode character in string [%.11s], cannot convert to JSON", str);
187 } 190 }
188 else 191 else
192 } 195 }
193 196
194 if (uch > 0x10FFFFUL) 197 if (uch > 0x10FFFFUL)
195 croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); 198 croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch);
196 199
197 if (uch < 0x80 || enc->flags & F_ASCII || (enc->flags & F_LATIN1 && uch > 0xFF)) 200 if (uch < 0x80 || enc->json.flags & F_ASCII || (enc->json.flags & F_LATIN1 && uch > 0xFF))
198 { 201 {
199 if (uch > 0xFFFFUL) 202 if (uch > 0xFFFFUL)
200 { 203 {
201 need (enc, len += 11); 204 need (enc, len += 11);
202 sprintf (enc->cur, "\\u%04x\\u%04x", 205 sprintf (enc->cur, "\\u%04x\\u%04x",
216 *enc->cur++ = hexdigit [(uch >> 0) & 15]; 219 *enc->cur++ = hexdigit [(uch >> 0) & 15];
217 } 220 }
218 221
219 str += clen; 222 str += clen;
220 } 223 }
221 else if (enc->flags & F_LATIN1) 224 else if (enc->json.flags & F_LATIN1)
222 { 225 {
223 *enc->cur++ = uch; 226 *enc->cur++ = uch;
224 str += clen; 227 str += clen;
225 } 228 }
226 else if (is_utf8) 229 else if (is_utf8)
247} 250}
248 251
249inline void 252inline void
250encode_indent (enc_t *enc) 253encode_indent (enc_t *enc)
251{ 254{
252 if (enc->flags & F_INDENT) 255 if (enc->json.flags & F_INDENT)
253 { 256 {
254 int spaces = enc->indent * INDENT_STEP; 257 int spaces = enc->indent * INDENT_STEP;
255 258
256 need (enc, spaces); 259 need (enc, spaces);
257 memset (enc->cur, ' ', spaces); 260 memset (enc->cur, ' ', spaces);
267} 270}
268 271
269inline void 272inline void
270encode_nl (enc_t *enc) 273encode_nl (enc_t *enc)
271{ 274{
272 if (enc->flags & F_INDENT) 275 if (enc->json.flags & F_INDENT)
273 { 276 {
274 need (enc, 1); 277 need (enc, 1);
275 encode_ch (enc, '\n'); 278 encode_ch (enc, '\n');
276 } 279 }
277} 280}
279inline void 282inline void
280encode_comma (enc_t *enc) 283encode_comma (enc_t *enc)
281{ 284{
282 encode_ch (enc, ','); 285 encode_ch (enc, ',');
283 286
284 if (enc->flags & F_INDENT) 287 if (enc->json.flags & F_INDENT)
285 encode_nl (enc); 288 encode_nl (enc);
286 else if (enc->flags & F_SPACE_AFTER) 289 else if (enc->json.flags & F_SPACE_AFTER)
287 encode_space (enc); 290 encode_space (enc);
288} 291}
289 292
290static void encode_sv (enc_t *enc, SV *sv); 293static void encode_sv (enc_t *enc, SV *sv);
291 294
334 else 337 else
335 encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); 338 encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he));
336 339
337 encode_ch (enc, '"'); 340 encode_ch (enc, '"');
338 341
339 if (enc->flags & F_SPACE_BEFORE) encode_space (enc); 342 if (enc->json.flags & F_SPACE_BEFORE) encode_space (enc);
340 encode_ch (enc, ':'); 343 encode_ch (enc, ':');
341 if (enc->flags & F_SPACE_AFTER ) encode_space (enc); 344 if (enc->json.flags & F_SPACE_AFTER ) encode_space (enc);
342 encode_sv (enc, HeVAL (he)); 345 encode_sv (enc, HeVAL (he));
343} 346}
344 347
345// compare hash entries, used when all keys are bytestrings 348// compare hash entries, used when all keys are bytestrings
346static int 349static int
381 { 384 {
382 // for canonical output we have to sort by keys first 385 // for canonical output we have to sort by keys first
383 // actually, this is mostly due to the stupid so-called 386 // actually, this is mostly due to the stupid so-called
384 // security workaround added somewhere in 5.8.x. 387 // security workaround added somewhere in 5.8.x.
385 // that randomises hash orderings 388 // that randomises hash orderings
386 if (enc->flags & F_CANONICAL) 389 if (enc->json.flags & F_CANONICAL)
387 { 390 {
388 int fast = 1; 391 int fast = 1;
389 HE *he; 392 HE *he;
390#if defined(__BORLANDC__) || defined(_MSC_VER) 393#if defined(__BORLANDC__) || defined(_MSC_VER)
391 HE **hes = _alloca (count * sizeof (HE)); 394 HE **hes = _alloca (count * sizeof (HE));
463 svtype svt; 466 svtype svt;
464 467
465 SvGETMAGIC (sv); 468 SvGETMAGIC (sv);
466 svt = SvTYPE (sv); 469 svt = SvTYPE (sv);
467 470
471 if (expect_false (SvOBJECT (sv)))
472 {
473 if (SvSTASH (sv) == json_boolean_stash)
474 {
475 if (SvIV (sv) == 0)
476 encode_str (enc, "false", 5, 0);
477 else
478 encode_str (enc, "true", 4, 0);
479 }
480 else
481 {
482#if 0
483 if (0 && sv_derived_from (rv, "JSON::Literal"))
484 {
485 // not yet
486 }
487#endif
488 if (enc->json.flags & F_CONV_BLESSED)
489 {
490 // we re-bless the reference to get overload and other niceties right
491 GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 1);
492
493 if (to_json)
494 {
495 dSP;
496 ENTER;
497 SAVETMPS;
498 PUSHMARK (SP);
499 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv)));
500
501 // calling with G_SCALAR ensures that we always get a 1 reutrn value
502 // check anyways.
503 PUTBACK;
504 assert (1 == call_sv ((SV *)GvCV (to_json), G_SCALAR));
505 SPAGAIN;
506
507 encode_sv (enc, POPs);
508
509 FREETMPS;
510 LEAVE;
511 }
512 else if (enc->json.flags & F_ALLOW_BLESSED)
513 encode_str (enc, "null", 4, 0);
514 else
515 croak ("encountered object '%s', but neither allow_blessed enabled nor TO_JSON method available on it",
516 SvPV_nolen (sv_2mortal (newRV_inc (sv))));
517 }
518 else if (enc->json.flags & F_ALLOW_BLESSED)
519 encode_str (enc, "null", 4, 0);
520 else
521 croak ("encountered object '%s', but neither allow_blessed nor convert_blessed settings are enabled",
522 SvPV_nolen (sv_2mortal (newRV_inc (sv))));
523 }
524 }
468 if (svt == SVt_PVHV) 525 else if (svt == SVt_PVHV)
469 encode_hv (enc, (HV *)sv); 526 encode_hv (enc, (HV *)sv);
470 else if (svt == SVt_PVAV) 527 else if (svt == SVt_PVAV)
471 encode_av (enc, (AV *)sv); 528 encode_av (enc, (AV *)sv);
472 else if (svt < SVt_PVAV) 529 else if (svt < SVt_PVAV)
473 { 530 {
497 encode_str (enc, str, len, SvUTF8 (sv)); 554 encode_str (enc, str, len, SvUTF8 (sv));
498 encode_ch (enc, '"'); 555 encode_ch (enc, '"');
499 } 556 }
500 else if (SvNOKp (sv)) 557 else if (SvNOKp (sv))
501 { 558 {
559 // trust that perl will do the right thing w.r.t. JSON syntax.
502 need (enc, NV_DIG + 32); 560 need (enc, NV_DIG + 32);
503 Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur); 561 Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur);
504 enc->cur += strlen (enc->cur); 562 enc->cur += strlen (enc->cur);
505 } 563 }
506 else if (SvIOKp (sv)) 564 else if (SvIOKp (sv))
507 { 565 {
508 // we assume we can always read an IV as a UV 566 // we assume we can always read an IV as a UV
509 if (SvUV (sv) & ~(UV)0x7fff) 567 if (SvUV (sv) & ~(UV)0x7fff)
510 { 568 {
569 // large integer, use the (rather slow) snprintf way.
511 need (enc, sizeof (UV) * 3); 570 need (enc, sizeof (UV) * 3);
512 enc->cur += 571 enc->cur +=
513 SvIsUV(sv) 572 SvIsUV(sv)
514 ? snprintf (enc->cur, sizeof (UV) * 3, "%"UVuf, (UV)SvUVX (sv)) 573 ? snprintf (enc->cur, sizeof (UV) * 3, "%"UVuf, (UV)SvUVX (sv))
515 : snprintf (enc->cur, sizeof (UV) * 3, "%"IVdf, (IV)SvIVX (sv)); 574 : snprintf (enc->cur, sizeof (UV) * 3, "%"IVdf, (IV)SvIVX (sv));
518 { 577 {
519 // optimise the "small number case" 578 // optimise the "small number case"
520 // code will likely be branchless and use only a single multiplication 579 // code will likely be branchless and use only a single multiplication
521 I32 i = SvIV (sv); 580 I32 i = SvIV (sv);
522 U32 u; 581 U32 u;
582 char digit, nz = 0;
523 583
524 need (enc, 6); 584 need (enc, 6);
525 585
526 *enc->cur = '-'; enc->cur += i < 0 ? 1 : 0; 586 *enc->cur = '-'; enc->cur += i < 0 ? 1 : 0;
527 u = i < 0 ? -i : i; 587 u = i < 0 ? -i : i;
528 588
529 // convert to 4.28 fixed-point representation 589 // convert to 4.28 fixed-point representation
530 u = u * ((0xfffffff + 10000) / 10000); // 10**5, 5 fractional digits 590 u = u * ((0xfffffff + 10000) / 10000); // 10**5, 5 fractional digits
531 591
532 char digit, nz = 0; 592 // now output digit by digit, each time masking out the integer part
533 593 // and multiplying by 5 while moving the decimal point one to the right,
594 // resulting in a net multiplication by 10.
595 // we always write the digit to memory but conditionally increment
596 // the pointer, to ease the usage of conditional move instructions.
534 digit = u >> 28; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0xfffffff) * 5; 597 digit = u >> 28; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0xfffffff) * 5;
535 digit = u >> 27; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x7ffffff) * 5; 598 digit = u >> 27; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x7ffffff) * 5;
536 digit = u >> 26; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x3ffffff) * 5; 599 digit = u >> 26; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x3ffffff) * 5;
537 digit = u >> 25; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x1ffffff) * 5; 600 digit = u >> 25; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x1ffffff) * 5;
538 digit = u >> 24; *enc->cur = digit + '0'; enc->cur += 1; 601 digit = u >> 24; *enc->cur = digit + '0'; enc->cur += 1; // correctly generate '0'
539 } 602 }
540 } 603 }
541 else if (SvROK (sv)) 604 else if (SvROK (sv))
542 encode_rv (enc, SvRV (sv)); 605 encode_rv (enc, SvRV (sv));
543 else if (!SvOK (sv)) 606 else if (!SvOK (sv))
546 croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", 609 croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this",
547 SvPV_nolen (sv), SvFLAGS (sv)); 610 SvPV_nolen (sv), SvFLAGS (sv));
548} 611}
549 612
550static SV * 613static SV *
551encode_json (SV *scalar, U32 flags) 614encode_json (SV *scalar, JSON *json)
552{ 615{
553 enc_t enc; 616 enc_t enc;
554 617
555 if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) 618 if (!(json->flags & F_ALLOW_NONREF) && !SvROK (scalar))
556 croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); 619 croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)");
557 620
558 enc.flags = flags; 621 enc.json = *json;
559 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); 622 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE));
560 enc.cur = SvPVX (enc.sv); 623 enc.cur = SvPVX (enc.sv);
561 enc.end = SvEND (enc.sv); 624 enc.end = SvEND (enc.sv);
562 enc.indent = 0; 625 enc.indent = 0;
563 enc.maxdepth = DEC_DEPTH (flags); 626 enc.maxdepth = DEC_DEPTH (enc.json.flags);
564 627
565 SvPOK_only (enc.sv); 628 SvPOK_only (enc.sv);
566 encode_sv (&enc, scalar); 629 encode_sv (&enc, scalar);
567 630
568 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); 631 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv));
569 *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings 632 *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings
570 633
571 if (!(flags & (F_ASCII | F_LATIN1 | F_UTF8))) 634 if (!(enc.json.flags & (F_ASCII | F_LATIN1 | F_UTF8)))
572 SvUTF8_on (enc.sv); 635 SvUTF8_on (enc.sv);
573 636
574 if (enc.flags & F_SHRINK) 637 if (enc.json.flags & F_SHRINK)
575 shrink (enc.sv); 638 shrink (enc.sv);
576 639
577 return enc.sv; 640 return enc.sv;
578} 641}
579 642
584typedef struct 647typedef struct
585{ 648{
586 char *cur; // current parser pointer 649 char *cur; // current parser pointer
587 char *end; // end of input string 650 char *end; // end of input string
588 const char *err; // parse error, if != 0 651 const char *err; // parse error, if != 0
589 U32 flags; // F_* 652 JSON json;
590 U32 depth; // recursion depth 653 U32 depth; // recursion depth
591 U32 maxdepth; // recursion depth limit 654 U32 maxdepth; // recursion depth limit
592} dec_t; 655} dec_t;
593 656
594inline void 657inline void
625decode_4hex (dec_t *dec) 688decode_4hex (dec_t *dec)
626{ 689{
627 signed char d1, d2, d3, d4; 690 signed char d1, d2, d3, d4;
628 unsigned char *cur = (unsigned char *)dec->cur; 691 unsigned char *cur = (unsigned char *)dec->cur;
629 692
630 d1 = decode_hexdigit [cur [0]]; if (expect_false (d1 < 0)) ERR ("four hexadecimal digits expected"); 693 d1 = decode_hexdigit [cur [0]]; if (expect_false (d1 < 0)) ERR ("exactly four hexadecimal digits expected");
631 d2 = decode_hexdigit [cur [1]]; if (expect_false (d2 < 0)) ERR ("four hexadecimal digits expected"); 694 d2 = decode_hexdigit [cur [1]]; if (expect_false (d2 < 0)) ERR ("exactly four hexadecimal digits expected");
632 d3 = decode_hexdigit [cur [2]]; if (expect_false (d3 < 0)) ERR ("four hexadecimal digits expected"); 695 d3 = decode_hexdigit [cur [2]]; if (expect_false (d3 < 0)) ERR ("exactly four hexadecimal digits expected");
633 d4 = decode_hexdigit [cur [3]]; if (expect_false (d4 < 0)) ERR ("four hexadecimal digits expected"); 696 d4 = decode_hexdigit [cur [3]]; if (expect_false (d4 < 0)) ERR ("exactly four hexadecimal digits expected");
634 697
635 dec->cur += 4; 698 dec->cur += 4;
636 699
637 return ((UV)d1) << 12 700 return ((UV)d1) << 12
638 | ((UV)d2) << 8 701 | ((UV)d2) << 8
861 { 924 {
862 // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so 925 // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so
863 if (*start == '-') 926 if (*start == '-')
864 switch (dec->cur - start) 927 switch (dec->cur - start)
865 { 928 {
866 case 2: return newSViv (-( start [1] - '0' )); 929 case 2: return newSViv (-( start [1] - '0' * 1));
867 case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); 930 case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11));
868 case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); 931 case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111));
869 case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); 932 case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111));
870 } 933 }
871 else 934 else
872 switch (dec->cur - start) 935 switch (dec->cur - start)
873 { 936 {
874 case 1: return newSViv ( start [0] - '0' ); 937 case 1: return newSViv ( start [0] - '0' * 1);
875 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); 938 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11);
876 case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); 939 case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111);
877 case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); 940 case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111);
878 } 941 }
879 942
886 if (uv < (UV)IV_MIN) 949 if (uv < (UV)IV_MIN)
887 return newSViv (-(IV)uv); 950 return newSViv (-(IV)uv);
888 } 951 }
889 else 952 else
890 return newSVuv (uv); 953 return newSVuv (uv);
954
955 // here would likely be the place for bigint support
891 } 956 }
892 } 957 }
893 958
959 // if we ever support bigint or bigfloat, this is the place for bigfloat
894 return newSVnv (Atof (start)); 960 return newSVnv (Atof (start));
895 961
896fail: 962fail:
897 return 0; 963 return 0;
898} 964}
952 if (*dec->cur == '}') 1018 if (*dec->cur == '}')
953 ++dec->cur; 1019 ++dec->cur;
954 else 1020 else
955 for (;;) 1021 for (;;)
956 { 1022 {
957 SV *key, *value;
958
959 decode_ws (dec); EXPECT_CH ('"'); 1023 decode_ws (dec); EXPECT_CH ('"');
960 1024
961 key = decode_str (dec); 1025 // heuristic: assume that
962 if (!key) 1026 // a) decode_str + hv_store_ent are abysmally slow.
963 goto fail; 1027 // b) most hash keys are short, simple ascii text.
1028 // => try to "fast-match" such strings to avoid
1029 // the overhead of decode_str + hv_store_ent.
1030 {
1031 SV *value;
1032 char *p = dec->cur;
1033 char *e = p + 24; // only try up to 24 bytes
964 1034
965 decode_ws (dec); EXPECT_CH (':'); 1035 for (;;)
966
967 value = decode_sv (dec);
968 if (!value)
969 { 1036 {
1037 // the >= 0x80 is true on most architectures
1038 if (p == e || *p < 0x20 || *p >= 0x80 || *p == '\\')
1039 {
1040 // slow path, back up and use decode_str
1041 SV *key = decode_str (dec);
1042 if (!key)
1043 goto fail;
1044
1045 decode_ws (dec); EXPECT_CH (':');
1046
1047 value = decode_sv (dec);
1048 if (!value)
1049 {
1050 SvREFCNT_dec (key);
1051 goto fail;
1052 }
1053
1054 hv_store_ent (hv, key, value, 0);
970 SvREFCNT_dec (key); 1055 SvREFCNT_dec (key);
1056
1057 break;
1058 }
1059 else if (*p == '"')
1060 {
1061 // fast path, got a simple key
1062 char *key = dec->cur;
1063 int len = p - key;
1064 dec->cur = p + 1;
1065
1066 decode_ws (dec); EXPECT_CH (':');
1067
1068 value = decode_sv (dec);
1069 if (!value)
971 goto fail; 1070 goto fail;
1071
1072 hv_store (hv, key, len, value, 0);
1073
1074 break;
1075 }
1076
1077 ++p;
972 } 1078 }
973 1079 }
974 hv_store_ent (hv, key, value, 0);
975 SvREFCNT_dec (key);
976 1080
977 decode_ws (dec); 1081 decode_ws (dec);
978 1082
979 if (*dec->cur == '}') 1083 if (*dec->cur == '}')
980 { 1084 {
999 1103
1000static SV * 1104static SV *
1001decode_sv (dec_t *dec) 1105decode_sv (dec_t *dec)
1002{ 1106{
1003 decode_ws (dec); 1107 decode_ws (dec);
1108
1109 // the beauty of JSON: you need exactly one character lookahead
1110 // to parse anything.
1004 switch (*dec->cur) 1111 switch (*dec->cur)
1005 { 1112 {
1006 case '"': ++dec->cur; return decode_str (dec); 1113 case '"': ++dec->cur; return decode_str (dec);
1007 case '[': ++dec->cur; return decode_av (dec); 1114 case '[': ++dec->cur; return decode_av (dec);
1008 case '{': ++dec->cur; return decode_hv (dec); 1115 case '{': ++dec->cur; return decode_hv (dec);
1014 1121
1015 case 't': 1122 case 't':
1016 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) 1123 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4))
1017 { 1124 {
1018 dec->cur += 4; 1125 dec->cur += 4;
1019 return newSViv (1); 1126 return SvREFCNT_inc (json_true);
1020 } 1127 }
1021 else 1128 else
1022 ERR ("'true' expected"); 1129 ERR ("'true' expected");
1023 1130
1024 break; 1131 break;
1025 1132
1026 case 'f': 1133 case 'f':
1027 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) 1134 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5))
1028 { 1135 {
1029 dec->cur += 5; 1136 dec->cur += 5;
1030 return newSViv (0); 1137 return SvREFCNT_inc (json_false);
1031 } 1138 }
1032 else 1139 else
1033 ERR ("'false' expected"); 1140 ERR ("'false' expected");
1034 1141
1035 break; 1142 break;
1053fail: 1160fail:
1054 return 0; 1161 return 0;
1055} 1162}
1056 1163
1057static SV * 1164static SV *
1058decode_json (SV *string, U32 flags, UV *offset_return) 1165decode_json (SV *string, JSON *json, UV *offset_return)
1059{ 1166{
1060 dec_t dec; 1167 dec_t dec;
1061 UV offset; 1168 UV offset;
1062 SV *sv; 1169 SV *sv;
1063 1170
1064 SvGETMAGIC (string); 1171 SvGETMAGIC (string);
1065 SvUPGRADE (string, SVt_PV); 1172 SvUPGRADE (string, SVt_PV);
1066 1173
1174 if (json->flags & F_MAXSIZE && SvCUR (string) > DEC_SIZE (json->flags))
1175 croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu",
1176 (unsigned long)SvCUR (string), (unsigned long)DEC_SIZE (json->flags));
1177
1067 if (flags & F_UTF8) 1178 if (json->flags & F_UTF8)
1068 sv_utf8_downgrade (string, 0); 1179 sv_utf8_downgrade (string, 0);
1069 else 1180 else
1070 sv_utf8_upgrade (string); 1181 sv_utf8_upgrade (string);
1071 1182
1072 SvGROW (string, SvCUR (string) + 1); // should basically be a NOP 1183 SvGROW (string, SvCUR (string) + 1); // should basically be a NOP
1073 1184
1074 dec.flags = flags; 1185 dec.json = *json;
1075 dec.cur = SvPVX (string); 1186 dec.cur = SvPVX (string);
1076 dec.end = SvEND (string); 1187 dec.end = SvEND (string);
1077 dec.err = 0; 1188 dec.err = 0;
1078 dec.depth = 0; 1189 dec.depth = 0;
1079 dec.maxdepth = DEC_DEPTH (dec.flags); 1190 dec.maxdepth = DEC_DEPTH (dec.json.flags);
1080 1191
1081 *dec.end = 0; // this should basically be a nop, too, but make sure it's there 1192 *dec.end = 0; // this should basically be a nop, too, but make sure it's there
1082 sv = decode_sv (&dec); 1193 sv = decode_sv (&dec);
1083 1194
1084 if (!(offset_return || !sv)) 1195 if (!(offset_return || !sv))
1094 } 1205 }
1095 } 1206 }
1096 1207
1097 if (offset_return || !sv) 1208 if (offset_return || !sv)
1098 { 1209 {
1099 offset = dec.flags & F_UTF8 1210 offset = dec.json.flags & F_UTF8
1100 ? dec.cur - SvPVX (string) 1211 ? dec.cur - SvPVX (string)
1101 : utf8_distance (dec.cur, SvPVX (string)); 1212 : utf8_distance (dec.cur, SvPVX (string));
1102 1213
1103 if (offset_return) 1214 if (offset_return)
1104 *offset_return = offset; 1215 *offset_return = offset;
1123 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); 1234 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)");
1124 } 1235 }
1125 1236
1126 sv = sv_2mortal (sv); 1237 sv = sv_2mortal (sv);
1127 1238
1128 if (!(dec.flags & F_ALLOW_NONREF) && !SvROK (sv)) 1239 if (!(dec.json.flags & F_ALLOW_NONREF) && !SvROK (sv))
1129 croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)"); 1240 croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)");
1130 1241
1131 return sv; 1242 return sv;
1132} 1243}
1133 1244
1137MODULE = JSON::XS PACKAGE = JSON::XS 1248MODULE = JSON::XS PACKAGE = JSON::XS
1138 1249
1139BOOT: 1250BOOT:
1140{ 1251{
1141 int i; 1252 int i;
1142
1143 memset (decode_hexdigit, 0xff, 256);
1144 1253
1145 for (i = 0; i < 256; ++i) 1254 for (i = 0; i < 256; ++i)
1146 decode_hexdigit [i] = 1255 decode_hexdigit [i] =
1147 i >= '0' && i <= '9' ? i - '0' 1256 i >= '0' && i <= '9' ? i - '0'
1148 : i >= 'a' && i <= 'f' ? i - 'a' + 10 1257 : i >= 'a' && i <= 'f' ? i - 'a' + 10
1149 : i >= 'A' && i <= 'F' ? i - 'A' + 10 1258 : i >= 'A' && i <= 'F' ? i - 'A' + 10
1150 : -1; 1259 : -1;
1151 1260
1152 json_stash = gv_stashpv ("JSON::XS", 1); 1261 json_stash = gv_stashpv ("JSON::XS" , 1);
1262 json_boolean_stash = gv_stashpv ("JSON::XS::Boolean", 1);
1263
1264 json_true = get_sv ("JSON::XS::true" , 1); SvREADONLY_on (json_true );
1265 json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false);
1153} 1266}
1154 1267
1155PROTOTYPES: DISABLE 1268PROTOTYPES: DISABLE
1156 1269
1157SV *new (char *dummy) 1270void new (char *klass)
1158 CODE: 1271 PPCODE:
1159 RETVAL = sv_bless (newRV_noinc (newSVuv (F_DEFAULT)), json_stash); 1272{
1160 OUTPUT: 1273 SV *pv = NEWSV (0, sizeof (JSON));
1161 RETVAL 1274 SvPOK_only (pv);
1275 Zero (SvPVX (pv), 1, sizeof (JSON));
1276 ((JSON *)SvPVX (pv))->flags = F_DEFAULT;
1277 XPUSHs (sv_2mortal (sv_bless (newRV_noinc (pv), json_stash)));
1278}
1162 1279
1163SV *ascii (SV *self, int enable = 1) 1280void ascii (JSON *self, int enable = 1)
1164 ALIAS: 1281 ALIAS:
1165 ascii = F_ASCII 1282 ascii = F_ASCII
1166 latin1 = F_LATIN1 1283 latin1 = F_LATIN1
1167 utf8 = F_UTF8 1284 utf8 = F_UTF8
1168 indent = F_INDENT 1285 indent = F_INDENT
1169 canonical = F_CANONICAL 1286 canonical = F_CANONICAL
1170 space_before = F_SPACE_BEFORE 1287 space_before = F_SPACE_BEFORE
1171 space_after = F_SPACE_AFTER 1288 space_after = F_SPACE_AFTER
1172 pretty = F_PRETTY 1289 pretty = F_PRETTY
1173 allow_nonref = F_ALLOW_NONREF 1290 allow_nonref = F_ALLOW_NONREF
1174 shrink = F_SHRINK 1291 shrink = F_SHRINK
1292 allow_blessed = F_ALLOW_BLESSED
1293 convert_blessed = F_CONV_BLESSED
1175 CODE: 1294 PPCODE:
1176{ 1295{
1177 UV *uv = SvJSON (self);
1178 if (enable) 1296 if (enable)
1179 *uv |= ix; 1297 self->flags |= ix;
1180 else 1298 else
1181 *uv &= ~ix; 1299 self->flags &= ~ix;
1182 1300
1183 RETVAL = newSVsv (self); 1301 XPUSHs (ST (0));
1184} 1302}
1185 OUTPUT:
1186 RETVAL
1187 1303
1188SV *max_depth (SV *self, UV max_depth = 0x80000000UL) 1304void max_depth (JSON *self, UV max_depth = 0x80000000UL)
1189 CODE: 1305 PPCODE:
1190{ 1306{
1191 UV *uv = SvJSON (self);
1192 UV log2 = 0; 1307 UV log2 = 0;
1193 1308
1194 if (max_depth > 0x80000000UL) max_depth = 0x80000000UL; 1309 if (max_depth > 0x80000000UL) max_depth = 0x80000000UL;
1195 1310
1196 while ((1UL << log2) < max_depth) 1311 while ((1UL << log2) < max_depth)
1197 ++log2; 1312 ++log2;
1198 1313
1199 *uv = *uv & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); 1314 self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH);
1200 1315
1201 RETVAL = newSVsv (self); 1316 XPUSHs (ST (0));
1202} 1317}
1203 OUTPUT:
1204 RETVAL
1205 1318
1206void encode (SV *self, SV *scalar) 1319void max_size (JSON *self, UV max_size = 0)
1207 PPCODE: 1320 PPCODE:
1208 XPUSHs (encode_json (scalar, *SvJSON (self))); 1321{
1322 UV log2 = 0;
1209 1323
1324 if (max_size > 0x80000000UL) max_size = 0x80000000UL;
1325 if (max_size == 1) max_size = 2;
1326
1327 while ((1UL << log2) < max_size)
1328 ++log2;
1329
1330 self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE);
1331
1332 XPUSHs (ST (0));
1333}
1334
1210void decode (SV *self, SV *jsonstr) 1335void encode (JSON *self, SV *scalar)
1211 PPCODE: 1336 PPCODE:
1337 XPUSHs (encode_json (scalar, self));
1338
1339void decode (JSON *self, SV *jsonstr)
1340 PPCODE:
1212 XPUSHs (decode_json (jsonstr, *SvJSON (self), 0)); 1341 XPUSHs (decode_json (jsonstr, self, 0));
1213 1342
1214void decode_prefix (SV *self, SV *jsonstr) 1343void decode_prefix (JSON *self, SV *jsonstr)
1215 PPCODE: 1344 PPCODE:
1216{ 1345{
1217 UV offset; 1346 UV offset;
1218 EXTEND (SP, 2); 1347 EXTEND (SP, 2);
1219 PUSHs (decode_json (jsonstr, *SvJSON (self), &offset)); 1348 PUSHs (decode_json (jsonstr, self, &offset));
1220 PUSHs (sv_2mortal (newSVuv (offset))); 1349 PUSHs (sv_2mortal (newSVuv (offset)));
1221} 1350}
1222 1351
1223PROTOTYPES: ENABLE 1352PROTOTYPES: ENABLE
1224 1353
1225void to_json (SV *scalar) 1354void to_json (SV *scalar)
1226 ALIAS:
1227 objToJson = 0
1228 PPCODE: 1355 PPCODE:
1356{
1357 JSON json = { F_DEFAULT | F_UTF8 };
1229 XPUSHs (encode_json (scalar, F_DEFAULT | F_UTF8)); 1358 XPUSHs (encode_json (scalar, &json));
1359}
1230 1360
1231void from_json (SV *jsonstr) 1361void from_json (SV *jsonstr)
1232 ALIAS:
1233 jsonToObj = 0
1234 PPCODE: 1362 PPCODE:
1363{
1364 JSON json = { F_DEFAULT | F_UTF8 };
1235 XPUSHs (decode_json (jsonstr, F_DEFAULT | F_UTF8, 0)); 1365 XPUSHs (decode_json (jsonstr, &json, 0));
1366}
1236 1367

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines