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.34 by root, Wed May 23 22:07:13 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
35#define INDENT_STEP 3 // spaces per indentation level 43#define INDENT_STEP 3 // spaces per indentation level
36 44
37#define SHORT_STRING_LEN 512 // special-case strings of up to this size 45#define SHORT_STRING_LEN 16384 // special-case strings of up to this size
38 46
39#define SB do { 47#define SB do {
40#define SE } while (0) 48#define SE } while (0)
41 49
50#if __GNUC__ >= 3
51# define expect(expr,value) __builtin_expect ((expr),(value))
52# define inline inline
53#else
54# define expect(expr,value) (expr)
55# define inline static
56#endif
57
58#define expect_false(expr) expect ((expr) != 0, 0)
59#define expect_true(expr) expect ((expr) != 0, 1)
60
42static 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;
43 67
44///////////////////////////////////////////////////////////////////////////// 68/////////////////////////////////////////////////////////////////////////////
45// utility functions 69// utility functions
46 70
47static UV * 71inline void
48SvJSON (SV *sv)
49{
50 if (!(SvROK (sv) && SvOBJECT (SvRV (sv)) && SvSTASH (SvRV (sv)) == json_stash))
51 croak ("object is not of type JSON::XS");
52
53 return &SvUVX (SvRV (sv));
54}
55
56static void
57shrink (SV *sv) 72shrink (SV *sv)
58{ 73{
59 sv_utf8_downgrade (sv, 1); 74 sv_utf8_downgrade (sv, 1);
60 if (SvLEN (sv) > SvCUR (sv) + 1) 75 if (SvLEN (sv) > SvCUR (sv) + 1)
61 { 76 {
70// decode an utf-8 character and return it, or (UV)-1 in 85// decode an utf-8 character and return it, or (UV)-1 in
71// case of an error. 86// case of an error.
72// we special-case "safe" characters from U+80 .. U+7FF, 87// we special-case "safe" characters from U+80 .. U+7FF,
73// but use the very good perl function to parse anything else. 88// but use the very good perl function to parse anything else.
74// note that we never call this function for a ascii codepoints 89// note that we never call this function for a ascii codepoints
75static UV 90inline UV
76decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen) 91decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen)
77{ 92{
78 if (s[0] > 0xdf || s[0] < 0xc2) 93 if (expect_false (s[0] > 0xdf || s[0] < 0xc2))
79 return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY); 94 return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY);
80 else if (len > 1 && s[1] >= 0x80 && s[1] <= 0xbf) 95 else if (len > 1 && s[1] >= 0x80 && s[1] <= 0xbf)
81 { 96 {
82 *clen = 2; 97 *clen = 2;
83 return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); 98 return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f);
96typedef struct 111typedef struct
97{ 112{
98 char *cur; // SvPVX (sv) + current output position 113 char *cur; // SvPVX (sv) + current output position
99 char *end; // SvEND (sv) 114 char *end; // SvEND (sv)
100 SV *sv; // result scalar 115 SV *sv; // result scalar
101 U32 flags; // F_* 116 JSON json;
102 U32 indent; // indentation level 117 U32 indent; // indentation level
103 U32 maxdepth; // max. indentation/recursion level 118 U32 maxdepth; // max. indentation/recursion level
104} enc_t; 119} enc_t;
105 120
106static void 121inline void
107need (enc_t *enc, STRLEN len) 122need (enc_t *enc, STRLEN len)
108{ 123{
109 if (enc->cur + len >= enc->end) 124 if (expect_false (enc->cur + len >= enc->end))
110 { 125 {
111 STRLEN cur = enc->cur - SvPVX (enc->sv); 126 STRLEN cur = enc->cur - SvPVX (enc->sv);
112 SvGROW (enc->sv, cur + len + 1); 127 SvGROW (enc->sv, cur + len + 1);
113 enc->cur = SvPVX (enc->sv) + cur; 128 enc->cur = SvPVX (enc->sv) + cur;
114 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; 129 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1;
115 } 130 }
116} 131}
117 132
118static void 133inline void
119encode_ch (enc_t *enc, char ch) 134encode_ch (enc_t *enc, char ch)
120{ 135{
121 need (enc, 1); 136 need (enc, 1);
122 *enc->cur++ = ch; 137 *enc->cur++ = ch;
123} 138}
131 146
132 while (str < end) 147 while (str < end)
133 { 148 {
134 unsigned char ch = *(unsigned char *)str; 149 unsigned char ch = *(unsigned char *)str;
135 150
136 if (ch >= 0x20 && ch < 0x80) // most common case 151 if (expect_true (ch >= 0x20 && ch < 0x80)) // most common case
137 { 152 {
138 if (ch == '"') // but with slow exceptions 153 if (expect_false (ch == '"')) // but with slow exceptions
139 { 154 {
140 need (enc, len += 1); 155 need (enc, len += 1);
141 *enc->cur++ = '\\'; 156 *enc->cur++ = '\\';
142 *enc->cur++ = '"'; 157 *enc->cur++ = '"';
143 } 158 }
144 else if (ch == '\\') 159 else if (expect_false (ch == '\\'))
145 { 160 {
146 need (enc, len += 1); 161 need (enc, len += 1);
147 *enc->cur++ = '\\'; 162 *enc->cur++ = '\\';
148 *enc->cur++ = '\\'; 163 *enc->cur++ = '\\';
149 } 164 }
167 STRLEN clen; 182 STRLEN clen;
168 UV uch; 183 UV uch;
169 184
170 if (is_utf8) 185 if (is_utf8)
171 { 186 {
172 //uch = utf8n_to_uvuni (str, end - str, &clen, UTF8_CHECK_ONLY);
173 uch = decode_utf8 (str, end - str, &clen); 187 uch = decode_utf8 (str, end - str, &clen);
174 if (clen == (STRLEN)-1) 188 if (clen == (STRLEN)-1)
175 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);
176 } 190 }
177 else 191 else
181 } 195 }
182 196
183 if (uch > 0x10FFFFUL) 197 if (uch > 0x10FFFFUL)
184 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);
185 199
186 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))
187 { 201 {
188 if (uch > 0xFFFFUL) 202 if (uch > 0xFFFFUL)
189 { 203 {
190 need (enc, len += 11); 204 need (enc, len += 11);
191 sprintf (enc->cur, "\\u%04x\\u%04x", 205 sprintf (enc->cur, "\\u%04x\\u%04x",
205 *enc->cur++ = hexdigit [(uch >> 0) & 15]; 219 *enc->cur++ = hexdigit [(uch >> 0) & 15];
206 } 220 }
207 221
208 str += clen; 222 str += clen;
209 } 223 }
210 else if (enc->flags & F_LATIN1) 224 else if (enc->json.flags & F_LATIN1)
211 { 225 {
212 *enc->cur++ = uch; 226 *enc->cur++ = uch;
213 str += clen; 227 str += clen;
214 } 228 }
215 else if (is_utf8) 229 else if (is_utf8)
233 247
234 --len; 248 --len;
235 } 249 }
236} 250}
237 251
238static void 252inline void
239encode_indent (enc_t *enc) 253encode_indent (enc_t *enc)
240{ 254{
241 if (enc->flags & F_INDENT) 255 if (enc->json.flags & F_INDENT)
242 { 256 {
243 int spaces = enc->indent * INDENT_STEP; 257 int spaces = enc->indent * INDENT_STEP;
244 258
245 need (enc, spaces); 259 need (enc, spaces);
246 memset (enc->cur, ' ', spaces); 260 memset (enc->cur, ' ', spaces);
247 enc->cur += spaces; 261 enc->cur += spaces;
248 } 262 }
249} 263}
250 264
251static void 265inline void
252encode_space (enc_t *enc) 266encode_space (enc_t *enc)
253{ 267{
254 need (enc, 1); 268 need (enc, 1);
255 encode_ch (enc, ' '); 269 encode_ch (enc, ' ');
256} 270}
257 271
258static void 272inline void
259encode_nl (enc_t *enc) 273encode_nl (enc_t *enc)
260{ 274{
261 if (enc->flags & F_INDENT) 275 if (enc->json.flags & F_INDENT)
262 { 276 {
263 need (enc, 1); 277 need (enc, 1);
264 encode_ch (enc, '\n'); 278 encode_ch (enc, '\n');
265 } 279 }
266} 280}
267 281
268static void 282inline void
269encode_comma (enc_t *enc) 283encode_comma (enc_t *enc)
270{ 284{
271 encode_ch (enc, ','); 285 encode_ch (enc, ',');
272 286
273 if (enc->flags & F_INDENT) 287 if (enc->json.flags & F_INDENT)
274 encode_nl (enc); 288 encode_nl (enc);
275 else if (enc->flags & F_SPACE_AFTER) 289 else if (enc->json.flags & F_SPACE_AFTER)
276 encode_space (enc); 290 encode_space (enc);
277} 291}
278 292
279static void encode_sv (enc_t *enc, SV *sv); 293static void encode_sv (enc_t *enc, SV *sv);
280 294
323 else 337 else
324 encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); 338 encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he));
325 339
326 encode_ch (enc, '"'); 340 encode_ch (enc, '"');
327 341
328 if (enc->flags & F_SPACE_BEFORE) encode_space (enc); 342 if (enc->json.flags & F_SPACE_BEFORE) encode_space (enc);
329 encode_ch (enc, ':'); 343 encode_ch (enc, ':');
330 if (enc->flags & F_SPACE_AFTER ) encode_space (enc); 344 if (enc->json.flags & F_SPACE_AFTER ) encode_space (enc);
331 encode_sv (enc, HeVAL (he)); 345 encode_sv (enc, HeVAL (he));
332} 346}
333 347
334// compare hash entries, used when all keys are bytestrings 348// compare hash entries, used when all keys are bytestrings
335static int 349static int
370 { 384 {
371 // for canonical output we have to sort by keys first 385 // for canonical output we have to sort by keys first
372 // actually, this is mostly due to the stupid so-called 386 // actually, this is mostly due to the stupid so-called
373 // security workaround added somewhere in 5.8.x. 387 // security workaround added somewhere in 5.8.x.
374 // that randomises hash orderings 388 // that randomises hash orderings
375 if (enc->flags & F_CANONICAL) 389 if (enc->json.flags & F_CANONICAL)
376 { 390 {
377 int fast = 1; 391 int fast = 1;
378 HE *he; 392 HE *he;
379#if defined(__BORLANDC__) || defined(_MSC_VER) 393#if defined(__BORLANDC__) || defined(_MSC_VER)
380 HE **hes = _alloca (count * sizeof (HE)); 394 HE **hes = _alloca (count * sizeof (HE));
452 svtype svt; 466 svtype svt;
453 467
454 SvGETMAGIC (sv); 468 SvGETMAGIC (sv);
455 svt = SvTYPE (sv); 469 svt = SvTYPE (sv);
456 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 }
457 if (svt == SVt_PVHV) 525 else if (svt == SVt_PVHV)
458 encode_hv (enc, (HV *)sv); 526 encode_hv (enc, (HV *)sv);
459 else if (svt == SVt_PVAV) 527 else if (svt == SVt_PVAV)
460 encode_av (enc, (AV *)sv); 528 encode_av (enc, (AV *)sv);
461 else if (svt < SVt_PVAV) 529 else if (svt < SVt_PVAV)
462 { 530 {
486 encode_str (enc, str, len, SvUTF8 (sv)); 554 encode_str (enc, str, len, SvUTF8 (sv));
487 encode_ch (enc, '"'); 555 encode_ch (enc, '"');
488 } 556 }
489 else if (SvNOKp (sv)) 557 else if (SvNOKp (sv))
490 { 558 {
559 // trust that perl will do the right thing w.r.t. JSON syntax.
491 need (enc, NV_DIG + 32); 560 need (enc, NV_DIG + 32);
492 Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur); 561 Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur);
493 enc->cur += strlen (enc->cur); 562 enc->cur += strlen (enc->cur);
494 } 563 }
495 else if (SvIOKp (sv)) 564 else if (SvIOKp (sv))
496 { 565 {
497 need (enc, 64); 566 // we assume we can always read an IV as a UV
567 if (SvUV (sv) & ~(UV)0x7fff)
568 {
569 // large integer, use the (rather slow) snprintf way.
570 need (enc, sizeof (UV) * 3);
498 enc->cur += 571 enc->cur +=
499 SvIsUV(sv) 572 SvIsUV(sv)
500 ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv)) 573 ? snprintf (enc->cur, sizeof (UV) * 3, "%"UVuf, (UV)SvUVX (sv))
501 : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv)); 574 : snprintf (enc->cur, sizeof (UV) * 3, "%"IVdf, (IV)SvIVX (sv));
575 }
576 else
577 {
578 // optimise the "small number case"
579 // code will likely be branchless and use only a single multiplication
580 I32 i = SvIV (sv);
581 U32 u;
582 char digit, nz = 0;
583
584 need (enc, 6);
585
586 *enc->cur = '-'; enc->cur += i < 0 ? 1 : 0;
587 u = i < 0 ? -i : i;
588
589 // convert to 4.28 fixed-point representation
590 u = u * ((0xfffffff + 10000) / 10000); // 10**5, 5 fractional digits
591
592 // now output digit by digit, each time masking out the integer part
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.
597 digit = u >> 28; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0xfffffff) * 5;
598 digit = u >> 27; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x7ffffff) * 5;
599 digit = u >> 26; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x3ffffff) * 5;
600 digit = u >> 25; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x1ffffff) * 5;
601 digit = u >> 24; *enc->cur = digit + '0'; enc->cur += 1; // correctly generate '0'
602 }
502 } 603 }
503 else if (SvROK (sv)) 604 else if (SvROK (sv))
504 encode_rv (enc, SvRV (sv)); 605 encode_rv (enc, SvRV (sv));
505 else if (!SvOK (sv)) 606 else if (!SvOK (sv))
506 encode_str (enc, "null", 4, 0); 607 encode_str (enc, "null", 4, 0);
508 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",
509 SvPV_nolen (sv), SvFLAGS (sv)); 610 SvPV_nolen (sv), SvFLAGS (sv));
510} 611}
511 612
512static SV * 613static SV *
513encode_json (SV *scalar, U32 flags) 614encode_json (SV *scalar, JSON *json)
514{ 615{
515 enc_t enc; 616 enc_t enc;
516 617
517 if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) 618 if (!(json->flags & F_ALLOW_NONREF) && !SvROK (scalar))
518 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)");
519 620
520 enc.flags = flags; 621 enc.json = *json;
521 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); 622 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE));
522 enc.cur = SvPVX (enc.sv); 623 enc.cur = SvPVX (enc.sv);
523 enc.end = SvEND (enc.sv); 624 enc.end = SvEND (enc.sv);
524 enc.indent = 0; 625 enc.indent = 0;
525 enc.maxdepth = DEC_DEPTH (flags); 626 enc.maxdepth = DEC_DEPTH (enc.json.flags);
526 627
527 SvPOK_only (enc.sv); 628 SvPOK_only (enc.sv);
528 encode_sv (&enc, scalar); 629 encode_sv (&enc, scalar);
529 630
530 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); 631 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv));
531 *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
532 633
533 if (!(flags & (F_ASCII | F_LATIN1 | F_UTF8))) 634 if (!(enc.json.flags & (F_ASCII | F_LATIN1 | F_UTF8)))
534 SvUTF8_on (enc.sv); 635 SvUTF8_on (enc.sv);
535 636
536 if (enc.flags & F_SHRINK) 637 if (enc.json.flags & F_SHRINK)
537 shrink (enc.sv); 638 shrink (enc.sv);
538 639
539 return enc.sv; 640 return enc.sv;
540} 641}
541 642
546typedef struct 647typedef struct
547{ 648{
548 char *cur; // current parser pointer 649 char *cur; // current parser pointer
549 char *end; // end of input string 650 char *end; // end of input string
550 const char *err; // parse error, if != 0 651 const char *err; // parse error, if != 0
551 U32 flags; // F_* 652 JSON json;
552 U32 depth; // recursion depth 653 U32 depth; // recursion depth
553 U32 maxdepth; // recursion depth limit 654 U32 maxdepth; // recursion depth limit
554} dec_t; 655} dec_t;
555 656
556static void 657inline void
557decode_ws (dec_t *dec) 658decode_ws (dec_t *dec)
558{ 659{
559 for (;;) 660 for (;;)
560 { 661 {
561 char ch = *dec->cur; 662 char ch = *dec->cur;
587decode_4hex (dec_t *dec) 688decode_4hex (dec_t *dec)
588{ 689{
589 signed char d1, d2, d3, d4; 690 signed char d1, d2, d3, d4;
590 unsigned char *cur = (unsigned char *)dec->cur; 691 unsigned char *cur = (unsigned char *)dec->cur;
591 692
592 d1 = decode_hexdigit [cur [0]]; if (d1 < 0) ERR ("four hexadecimal digits expected"); 693 d1 = decode_hexdigit [cur [0]]; if (expect_false (d1 < 0)) ERR ("exactly four hexadecimal digits expected");
593 d2 = decode_hexdigit [cur [1]]; if (d2 < 0) ERR ("four hexadecimal digits expected"); 694 d2 = decode_hexdigit [cur [1]]; if (expect_false (d2 < 0)) ERR ("exactly four hexadecimal digits expected");
594 d3 = decode_hexdigit [cur [2]]; if (d3 < 0) ERR ("four hexadecimal digits expected"); 695 d3 = decode_hexdigit [cur [2]]; if (expect_false (d3 < 0)) ERR ("exactly four hexadecimal digits expected");
595 d4 = decode_hexdigit [cur [3]]; if (d4 < 0) ERR ("four hexadecimal digits expected"); 696 d4 = decode_hexdigit [cur [3]]; if (expect_false (d4 < 0)) ERR ("exactly four hexadecimal digits expected");
596 697
597 dec->cur += 4; 698 dec->cur += 4;
598 699
599 return ((UV)d1) << 12 700 return ((UV)d1) << 12
600 | ((UV)d2) << 8 701 | ((UV)d2) << 8
608static SV * 709static SV *
609decode_str (dec_t *dec) 710decode_str (dec_t *dec)
610{ 711{
611 SV *sv = 0; 712 SV *sv = 0;
612 int utf8 = 0; 713 int utf8 = 0;
714 char *dec_cur = dec->cur;
613 715
614 do 716 do
615 { 717 {
616 char buf [SHORT_STRING_LEN + UTF8_MAXBYTES]; 718 char buf [SHORT_STRING_LEN + UTF8_MAXBYTES];
617 char *cur = buf; 719 char *cur = buf;
618 720
619 do 721 do
620 { 722 {
621 unsigned char ch = *(unsigned char *)dec->cur++; 723 unsigned char ch = *(unsigned char *)dec_cur++;
622 724
623 if (ch == '"') 725 if (expect_false (ch == '"'))
624 { 726 {
625 --dec->cur; 727 --dec_cur;
626 break; 728 break;
627 } 729 }
628 else if (ch == '\\') 730 else if (expect_false (ch == '\\'))
629 { 731 {
630 switch (*dec->cur) 732 switch (*dec_cur)
631 { 733 {
632 case '\\': 734 case '\\':
633 case '/': 735 case '/':
634 case '"': *cur++ = *dec->cur++; break; 736 case '"': *cur++ = *dec_cur++; break;
635 737
636 case 'b': ++dec->cur; *cur++ = '\010'; break; 738 case 'b': ++dec_cur; *cur++ = '\010'; break;
637 case 't': ++dec->cur; *cur++ = '\011'; break; 739 case 't': ++dec_cur; *cur++ = '\011'; break;
638 case 'n': ++dec->cur; *cur++ = '\012'; break; 740 case 'n': ++dec_cur; *cur++ = '\012'; break;
639 case 'f': ++dec->cur; *cur++ = '\014'; break; 741 case 'f': ++dec_cur; *cur++ = '\014'; break;
640 case 'r': ++dec->cur; *cur++ = '\015'; break; 742 case 'r': ++dec_cur; *cur++ = '\015'; break;
641 743
642 case 'u': 744 case 'u':
643 { 745 {
644 UV lo, hi; 746 UV lo, hi;
645 ++dec->cur; 747 ++dec_cur;
646 748
749 dec->cur = dec_cur;
647 hi = decode_4hex (dec); 750 hi = decode_4hex (dec);
751 dec_cur = dec->cur;
648 if (hi == (UV)-1) 752 if (hi == (UV)-1)
649 goto fail; 753 goto fail;
650 754
651 // possibly a surrogate pair 755 // possibly a surrogate pair
652 if (hi >= 0xd800) 756 if (hi >= 0xd800)
653 if (hi < 0xdc00) 757 if (hi < 0xdc00)
654 { 758 {
655 if (dec->cur [0] != '\\' || dec->cur [1] != 'u') 759 if (dec_cur [0] != '\\' || dec_cur [1] != 'u')
656 ERR ("missing low surrogate character in surrogate pair"); 760 ERR ("missing low surrogate character in surrogate pair");
657 761
658 dec->cur += 2; 762 dec_cur += 2;
659 763
764 dec->cur = dec_cur;
660 lo = decode_4hex (dec); 765 lo = decode_4hex (dec);
766 dec_cur = dec->cur;
661 if (lo == (UV)-1) 767 if (lo == (UV)-1)
662 goto fail; 768 goto fail;
663 769
664 if (lo < 0xdc00 || lo >= 0xe000) 770 if (lo < 0xdc00 || lo >= 0xe000)
665 ERR ("surrogate pair expected"); 771 ERR ("surrogate pair expected");
679 *cur++ = hi; 785 *cur++ = hi;
680 } 786 }
681 break; 787 break;
682 788
683 default: 789 default:
684 --dec->cur; 790 --dec_cur;
685 ERR ("illegal backslash escape sequence in string"); 791 ERR ("illegal backslash escape sequence in string");
686 } 792 }
687 } 793 }
688 else if (ch >= 0x20 && ch <= 0x7f) 794 else if (expect_true (ch >= 0x20 && ch <= 0x7f))
689 *cur++ = ch; 795 *cur++ = ch;
690 else if (ch >= 0x80) 796 else if (ch >= 0x80)
691 { 797 {
692 STRLEN clen; 798 STRLEN clen;
693 UV uch; 799 UV uch;
694 800
695 --dec->cur; 801 --dec_cur;
696 802
697 uch = decode_utf8 (dec->cur, dec->end - dec->cur, &clen); 803 uch = decode_utf8 (dec_cur, dec->end - dec_cur, &clen);
698 if (clen == (STRLEN)-1) 804 if (clen == (STRLEN)-1)
699 ERR ("malformed UTF-8 character in JSON string"); 805 ERR ("malformed UTF-8 character in JSON string");
700 806
701 do 807 do
702 *cur++ = *dec->cur++; 808 *cur++ = *dec_cur++;
703 while (--clen); 809 while (--clen);
704 810
705 utf8 = 1; 811 utf8 = 1;
706 } 812 }
707 else 813 else
708 { 814 {
709 --dec->cur; 815 --dec_cur;
710 816
711 if (!ch) 817 if (!ch)
712 ERR ("unexpected end of string while parsing JSON string"); 818 ERR ("unexpected end of string while parsing JSON string");
713 else 819 else
714 ERR ("invalid character encountered while parsing JSON string"); 820 ERR ("invalid character encountered while parsing JSON string");
727 } 833 }
728 else 834 else
729 sv = newSVpvn (buf, len); 835 sv = newSVpvn (buf, len);
730 } 836 }
731 } 837 }
732 while (*dec->cur != '"'); 838 while (*dec_cur != '"');
733 839
734 ++dec->cur; 840 ++dec_cur;
735 841
736 if (sv) 842 if (sv)
737 { 843 {
738 SvPOK_only (sv); 844 SvPOK_only (sv);
739 *SvEND (sv) = 0; 845 *SvEND (sv) = 0;
742 SvUTF8_on (sv); 848 SvUTF8_on (sv);
743 } 849 }
744 else 850 else
745 sv = newSVpvn ("", 0); 851 sv = newSVpvn ("", 0);
746 852
853 dec->cur = dec_cur;
747 return sv; 854 return sv;
748 855
749fail: 856fail:
857 dec->cur = dec_cur;
750 return 0; 858 return 0;
751} 859}
752 860
753static SV * 861static SV *
754decode_num (dec_t *dec) 862decode_num (dec_t *dec)
812 is_nv = 1; 920 is_nv = 1;
813 } 921 }
814 922
815 if (!is_nv) 923 if (!is_nv)
816 { 924 {
817 UV uv; 925 // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so
818 int numtype = grok_number (start, dec->cur - start, &uv); 926 if (*start == '-')
819 if (numtype & IS_NUMBER_IN_UV) 927 switch (dec->cur - start)
820 if (numtype & IS_NUMBER_NEG)
821 { 928 {
822 if (uv < (UV)IV_MIN) 929 case 2: return newSViv (-( start [1] - '0' * 1));
823 return newSViv (-(IV)uv); 930 case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11));
931 case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111));
932 case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111));
824 } 933 }
934 else
935 switch (dec->cur - start)
936 {
937 case 1: return newSViv ( start [0] - '0' * 1);
938 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11);
939 case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111);
940 case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111);
941 }
942
943 {
944 UV uv;
945 int numtype = grok_number (start, dec->cur - start, &uv);
946 if (numtype & IS_NUMBER_IN_UV)
947 if (numtype & IS_NUMBER_NEG)
948 {
949 if (uv < (UV)IV_MIN)
950 return newSViv (-(IV)uv);
951 }
825 else 952 else
826 return newSVuv (uv); 953 return newSVuv (uv);
954
955 // here would likely be the place for bigint support
827 } 956 }
957 }
828 958
959 // if we ever support bigint or bigfloat, this is the place for bigfloat
829 return newSVnv (Atof (start)); 960 return newSVnv (Atof (start));
830 961
831fail: 962fail:
832 return 0; 963 return 0;
833} 964}
887 if (*dec->cur == '}') 1018 if (*dec->cur == '}')
888 ++dec->cur; 1019 ++dec->cur;
889 else 1020 else
890 for (;;) 1021 for (;;)
891 { 1022 {
892 SV *key, *value;
893
894 decode_ws (dec); EXPECT_CH ('"'); 1023 decode_ws (dec); EXPECT_CH ('"');
895 1024
896 key = decode_str (dec); 1025 // heuristic: assume that
897 if (!key) 1026 // a) decode_str + hv_store_ent are abysmally slow.
898 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
899 1034
900 decode_ws (dec); EXPECT_CH (':'); 1035 for (;;)
901
902 value = decode_sv (dec);
903 if (!value)
904 { 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);
905 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)
906 goto fail; 1070 goto fail;
1071
1072 hv_store (hv, key, len, value, 0);
1073
1074 break;
1075 }
1076
1077 ++p;
907 } 1078 }
908 1079 }
909 hv_store_ent (hv, key, value, 0);
910 SvREFCNT_dec (key);
911 1080
912 decode_ws (dec); 1081 decode_ws (dec);
913 1082
914 if (*dec->cur == '}') 1083 if (*dec->cur == '}')
915 { 1084 {
934 1103
935static SV * 1104static SV *
936decode_sv (dec_t *dec) 1105decode_sv (dec_t *dec)
937{ 1106{
938 decode_ws (dec); 1107 decode_ws (dec);
1108
1109 // the beauty of JSON: you need exactly one character lookahead
1110 // to parse anything.
939 switch (*dec->cur) 1111 switch (*dec->cur)
940 { 1112 {
941 case '"': ++dec->cur; return decode_str (dec); 1113 case '"': ++dec->cur; return decode_str (dec);
942 case '[': ++dec->cur; return decode_av (dec); 1114 case '[': ++dec->cur; return decode_av (dec);
943 case '{': ++dec->cur; return decode_hv (dec); 1115 case '{': ++dec->cur; return decode_hv (dec);
949 1121
950 case 't': 1122 case 't':
951 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) 1123 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4))
952 { 1124 {
953 dec->cur += 4; 1125 dec->cur += 4;
954 return newSViv (1); 1126 return SvREFCNT_inc (json_true);
955 } 1127 }
956 else 1128 else
957 ERR ("'true' expected"); 1129 ERR ("'true' expected");
958 1130
959 break; 1131 break;
960 1132
961 case 'f': 1133 case 'f':
962 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) 1134 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5))
963 { 1135 {
964 dec->cur += 5; 1136 dec->cur += 5;
965 return newSViv (0); 1137 return SvREFCNT_inc (json_false);
966 } 1138 }
967 else 1139 else
968 ERR ("'false' expected"); 1140 ERR ("'false' expected");
969 1141
970 break; 1142 break;
988fail: 1160fail:
989 return 0; 1161 return 0;
990} 1162}
991 1163
992static SV * 1164static SV *
993decode_json (SV *string, U32 flags, UV *offset_return) 1165decode_json (SV *string, JSON *json, UV *offset_return)
994{ 1166{
995 dec_t dec; 1167 dec_t dec;
996 UV offset; 1168 UV offset;
997 SV *sv; 1169 SV *sv;
998 1170
999 SvGETMAGIC (string); 1171 SvGETMAGIC (string);
1000 SvUPGRADE (string, SVt_PV); 1172 SvUPGRADE (string, SVt_PV);
1001 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
1002 if (flags & F_UTF8) 1178 if (json->flags & F_UTF8)
1003 sv_utf8_downgrade (string, 0); 1179 sv_utf8_downgrade (string, 0);
1004 else 1180 else
1005 sv_utf8_upgrade (string); 1181 sv_utf8_upgrade (string);
1006 1182
1007 SvGROW (string, SvCUR (string) + 1); // should basically be a NOP 1183 SvGROW (string, SvCUR (string) + 1); // should basically be a NOP
1008 1184
1009 dec.flags = flags; 1185 dec.json = *json;
1010 dec.cur = SvPVX (string); 1186 dec.cur = SvPVX (string);
1011 dec.end = SvEND (string); 1187 dec.end = SvEND (string);
1012 dec.err = 0; 1188 dec.err = 0;
1013 dec.depth = 0; 1189 dec.depth = 0;
1014 dec.maxdepth = DEC_DEPTH (dec.flags); 1190 dec.maxdepth = DEC_DEPTH (dec.json.flags);
1015 1191
1016 *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
1017 sv = decode_sv (&dec); 1193 sv = decode_sv (&dec);
1018 1194
1019 if (!(offset_return || !sv)) 1195 if (!(offset_return || !sv))
1029 } 1205 }
1030 } 1206 }
1031 1207
1032 if (offset_return || !sv) 1208 if (offset_return || !sv)
1033 { 1209 {
1034 offset = dec.flags & F_UTF8 1210 offset = dec.json.flags & F_UTF8
1035 ? dec.cur - SvPVX (string) 1211 ? dec.cur - SvPVX (string)
1036 : utf8_distance (dec.cur, SvPVX (string)); 1212 : utf8_distance (dec.cur, SvPVX (string));
1037 1213
1038 if (offset_return) 1214 if (offset_return)
1039 *offset_return = offset; 1215 *offset_return = offset;
1058 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); 1234 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)");
1059 } 1235 }
1060 1236
1061 sv = sv_2mortal (sv); 1237 sv = sv_2mortal (sv);
1062 1238
1063 if (!(dec.flags & F_ALLOW_NONREF) && !SvROK (sv)) 1239 if (!(dec.json.flags & F_ALLOW_NONREF) && !SvROK (sv))
1064 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)");
1065 1241
1066 return sv; 1242 return sv;
1067} 1243}
1068 1244
1072MODULE = JSON::XS PACKAGE = JSON::XS 1248MODULE = JSON::XS PACKAGE = JSON::XS
1073 1249
1074BOOT: 1250BOOT:
1075{ 1251{
1076 int i; 1252 int i;
1077
1078 memset (decode_hexdigit, 0xff, 256);
1079 1253
1080 for (i = 0; i < 256; ++i) 1254 for (i = 0; i < 256; ++i)
1081 decode_hexdigit [i] = 1255 decode_hexdigit [i] =
1082 i >= '0' && i <= '9' ? i - '0' 1256 i >= '0' && i <= '9' ? i - '0'
1083 : i >= 'a' && i <= 'f' ? i - 'a' + 10 1257 : i >= 'a' && i <= 'f' ? i - 'a' + 10
1084 : i >= 'A' && i <= 'F' ? i - 'A' + 10 1258 : i >= 'A' && i <= 'F' ? i - 'A' + 10
1085 : -1; 1259 : -1;
1086 1260
1087 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);
1088} 1266}
1089 1267
1090PROTOTYPES: DISABLE 1268PROTOTYPES: DISABLE
1091 1269
1092SV *new (char *dummy) 1270void new (char *klass)
1093 CODE: 1271 PPCODE:
1094 RETVAL = sv_bless (newRV_noinc (newSVuv (F_DEFAULT)), json_stash); 1272{
1095 OUTPUT: 1273 SV *pv = NEWSV (0, sizeof (JSON));
1096 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}
1097 1279
1098SV *ascii (SV *self, int enable = 1) 1280void ascii (JSON *self, int enable = 1)
1099 ALIAS: 1281 ALIAS:
1100 ascii = F_ASCII 1282 ascii = F_ASCII
1101 latin1 = F_LATIN1 1283 latin1 = F_LATIN1
1102 utf8 = F_UTF8 1284 utf8 = F_UTF8
1103 indent = F_INDENT 1285 indent = F_INDENT
1104 canonical = F_CANONICAL 1286 canonical = F_CANONICAL
1105 space_before = F_SPACE_BEFORE 1287 space_before = F_SPACE_BEFORE
1106 space_after = F_SPACE_AFTER 1288 space_after = F_SPACE_AFTER
1107 pretty = F_PRETTY 1289 pretty = F_PRETTY
1108 allow_nonref = F_ALLOW_NONREF 1290 allow_nonref = F_ALLOW_NONREF
1109 shrink = F_SHRINK 1291 shrink = F_SHRINK
1292 allow_blessed = F_ALLOW_BLESSED
1293 convert_blessed = F_CONV_BLESSED
1110 CODE: 1294 PPCODE:
1111{ 1295{
1112 UV *uv = SvJSON (self);
1113 if (enable) 1296 if (enable)
1114 *uv |= ix; 1297 self->flags |= ix;
1115 else 1298 else
1116 *uv &= ~ix; 1299 self->flags &= ~ix;
1117 1300
1118 RETVAL = newSVsv (self); 1301 XPUSHs (ST (0));
1119} 1302}
1120 OUTPUT:
1121 RETVAL
1122 1303
1123SV *max_depth (SV *self, UV max_depth = 0x80000000UL) 1304void max_depth (JSON *self, UV max_depth = 0x80000000UL)
1124 CODE: 1305 PPCODE:
1125{ 1306{
1126 UV *uv = SvJSON (self);
1127 UV log2 = 0; 1307 UV log2 = 0;
1128 1308
1129 if (max_depth > 0x80000000UL) max_depth = 0x80000000UL; 1309 if (max_depth > 0x80000000UL) max_depth = 0x80000000UL;
1130 1310
1131 while ((1UL << log2) < max_depth) 1311 while ((1UL << log2) < max_depth)
1132 ++log2; 1312 ++log2;
1133 1313
1134 *uv = *uv & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); 1314 self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH);
1135 1315
1136 RETVAL = newSVsv (self); 1316 XPUSHs (ST (0));
1137} 1317}
1138 OUTPUT:
1139 RETVAL
1140 1318
1141void encode (SV *self, SV *scalar) 1319void max_size (JSON *self, UV max_size = 0)
1142 PPCODE: 1320 PPCODE:
1143 XPUSHs (encode_json (scalar, *SvJSON (self))); 1321{
1322 UV log2 = 0;
1144 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
1145void decode (SV *self, SV *jsonstr) 1335void encode (JSON *self, SV *scalar)
1146 PPCODE: 1336 PPCODE:
1337 XPUSHs (encode_json (scalar, self));
1338
1339void decode (JSON *self, SV *jsonstr)
1340 PPCODE:
1147 XPUSHs (decode_json (jsonstr, *SvJSON (self), 0)); 1341 XPUSHs (decode_json (jsonstr, self, 0));
1148 1342
1149void decode_prefix (SV *self, SV *jsonstr) 1343void decode_prefix (JSON *self, SV *jsonstr)
1150 PPCODE: 1344 PPCODE:
1151{ 1345{
1152 UV offset; 1346 UV offset;
1153 EXTEND (SP, 2); 1347 EXTEND (SP, 2);
1154 PUSHs (decode_json (jsonstr, *SvJSON (self), &offset)); 1348 PUSHs (decode_json (jsonstr, self, &offset));
1155 PUSHs (sv_2mortal (newSVuv (offset))); 1349 PUSHs (sv_2mortal (newSVuv (offset)));
1156} 1350}
1157 1351
1158PROTOTYPES: ENABLE 1352PROTOTYPES: ENABLE
1159 1353
1160void to_json (SV *scalar) 1354void to_json (SV *scalar)
1161 ALIAS:
1162 objToJson = 0
1163 PPCODE: 1355 PPCODE:
1356{
1357 JSON json = { F_DEFAULT | F_UTF8 };
1164 XPUSHs (encode_json (scalar, F_DEFAULT | F_UTF8)); 1358 XPUSHs (encode_json (scalar, &json));
1359}
1165 1360
1166void from_json (SV *jsonstr) 1361void from_json (SV *jsonstr)
1167 ALIAS:
1168 jsonToObj = 0
1169 PPCODE: 1362 PPCODE:
1363{
1364 JSON json = { F_DEFAULT | F_UTF8 };
1170 XPUSHs (decode_json (jsonstr, F_DEFAULT | F_UTF8, 0)); 1365 XPUSHs (decode_json (jsonstr, &json, 0));
1366}
1171 1367

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines