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.24 by root, Tue Apr 3 23:59:04 2007 UTC vs.
Revision 1.48 by root, Sun Jul 1 22:20:00 2007 UTC

3#include "XSUB.h" 3#include "XSUB.h"
4 4
5#include "assert.h" 5#include "assert.h"
6#include "string.h" 6#include "string.h"
7#include "stdlib.h" 7#include "stdlib.h"
8#include "stdio.h"
8 9
10#if defined(__BORLANDC__) || defined(_MSC_VER)
11# define snprintf _snprintf // C compilers have this in stdio.h
12#endif
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
9#define F_ASCII 0x00000001UL 20#define F_ASCII 0x00000001UL
21#define F_LATIN1 0x00000002UL
10#define F_UTF8 0x00000002UL 22#define F_UTF8 0x00000004UL
11#define F_INDENT 0x00000004UL 23#define F_INDENT 0x00000008UL
12#define F_CANONICAL 0x00000008UL 24#define F_CANONICAL 0x00000010UL
13#define F_SPACE_BEFORE 0x00000010UL 25#define F_SPACE_BEFORE 0x00000020UL
14#define F_SPACE_AFTER 0x00000020UL 26#define F_SPACE_AFTER 0x00000040UL
15#define F_ALLOW_NONREF 0x00000080UL 27#define F_ALLOW_NONREF 0x00000100UL
16#define F_SHRINK 0x00000100UL 28#define F_SHRINK 0x00000200UL
29#define F_ALLOW_BLESSED 0x00000400UL
30#define F_CONV_BLESSED 0x00000800UL // NYI
17#define F_MAXDEPTH 0xf8000000UL 31#define F_MAXDEPTH 0xf8000000UL
18#define S_MAXDEPTH 27 32#define S_MAXDEPTH 27
33#define F_MAXSIZE 0x01f00000UL
34#define S_MAXSIZE 20
19 35
20#define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) 36#define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH))
21 37#define DEC_SIZE(flags) (1UL << ((flags & F_MAXSIZE ) >> S_MAXSIZE ))
22// F_SELFCONVERT? <=> to_json/toJson
23// F_BLESSED? <=> { $__class__$ => }
24 38
25#define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER 39#define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER
26#define F_DEFAULT (9UL << S_MAXDEPTH) 40#define F_DEFAULT (9UL << S_MAXDEPTH)
27 41
28#define INIT_SIZE 32 // initial scalar size to be allocated 42#define INIT_SIZE 32 // initial scalar size to be allocated
29#define INDENT_STEP 3 // spaces per indentation level 43#define INDENT_STEP 3 // spaces per indentation level
30 44
31#define UTF8_MAX_LEN 11 // for perls UTF-X: max. number of octets per character
32#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
33 46
34#define SB do { 47#define SB do {
35#define SE } while (0) 48#define SE } while (0)
36 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
37static 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;
38 67
39///////////////////////////////////////////////////////////////////////////// 68/////////////////////////////////////////////////////////////////////////////
40// utility functions 69// utility functions
41 70
42static UV * 71inline void
43SvJSON (SV *sv)
44{
45 if (!(SvROK (sv) && SvOBJECT (SvRV (sv)) && SvSTASH (SvRV (sv)) == json_stash))
46 croak ("object is not of type JSON::XS");
47
48 return &SvUVX (SvRV (sv));
49}
50
51static void
52shrink (SV *sv) 72shrink (SV *sv)
53{ 73{
54 sv_utf8_downgrade (sv, 1); 74 sv_utf8_downgrade (sv, 1);
55 if (SvLEN (sv) > SvCUR (sv) + 1) 75 if (SvLEN (sv) > SvCUR (sv) + 1)
56 { 76 {
65// 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
66// case of an error. 86// case of an error.
67// we special-case "safe" characters from U+80 .. U+7FF, 87// we special-case "safe" characters from U+80 .. U+7FF,
68// but use the very good perl function to parse anything else. 88// but use the very good perl function to parse anything else.
69// note that we never call this function for a ascii codepoints 89// note that we never call this function for a ascii codepoints
70static UV 90inline UV
71decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen) 91decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen)
72{ 92{
73 if (s[0] > 0xdf || s[0] < 0xc2) 93 if (expect_false (s[0] > 0xdf || s[0] < 0xc2))
74 return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY); 94 return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY);
75 else if (len > 1 && s[1] >= 0x80 && s[1] <= 0xbf) 95 else if (len > 1 && s[1] >= 0x80 && s[1] <= 0xbf)
76 { 96 {
77 *clen = 2; 97 *clen = 2;
78 return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); 98 return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f);
91typedef struct 111typedef struct
92{ 112{
93 char *cur; // SvPVX (sv) + current output position 113 char *cur; // SvPVX (sv) + current output position
94 char *end; // SvEND (sv) 114 char *end; // SvEND (sv)
95 SV *sv; // result scalar 115 SV *sv; // result scalar
96 U32 flags; // F_* 116 JSON json;
97 U32 indent; // indentation level 117 U32 indent; // indentation level
98 U32 maxdepth; // max. indentation/recursion level 118 U32 maxdepth; // max. indentation/recursion level
99} enc_t; 119} enc_t;
100 120
101static void 121inline void
102need (enc_t *enc, STRLEN len) 122need (enc_t *enc, STRLEN len)
103{ 123{
104 if (enc->cur + len >= enc->end) 124 if (expect_false (enc->cur + len >= enc->end))
105 { 125 {
106 STRLEN cur = enc->cur - SvPVX (enc->sv); 126 STRLEN cur = enc->cur - SvPVX (enc->sv);
107 SvGROW (enc->sv, cur + len + 1); 127 SvGROW (enc->sv, cur + len + 1);
108 enc->cur = SvPVX (enc->sv) + cur; 128 enc->cur = SvPVX (enc->sv) + cur;
109 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv); 129 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1;
110 } 130 }
111} 131}
112 132
113static void 133inline void
114encode_ch (enc_t *enc, char ch) 134encode_ch (enc_t *enc, char ch)
115{ 135{
116 need (enc, 1); 136 need (enc, 1);
117 *enc->cur++ = ch; 137 *enc->cur++ = ch;
118} 138}
126 146
127 while (str < end) 147 while (str < end)
128 { 148 {
129 unsigned char ch = *(unsigned char *)str; 149 unsigned char ch = *(unsigned char *)str;
130 150
131 if (ch >= 0x20 && ch < 0x80) // most common case 151 if (expect_true (ch >= 0x20 && ch < 0x80)) // most common case
132 { 152 {
133 if (ch == '"') // but with slow exceptions 153 if (expect_false (ch == '"')) // but with slow exceptions
134 { 154 {
135 need (enc, len += 1); 155 need (enc, len += 1);
136 *enc->cur++ = '\\'; 156 *enc->cur++ = '\\';
137 *enc->cur++ = '"'; 157 *enc->cur++ = '"';
138 } 158 }
139 else if (ch == '\\') 159 else if (expect_false (ch == '\\'))
140 { 160 {
141 need (enc, len += 1); 161 need (enc, len += 1);
142 *enc->cur++ = '\\'; 162 *enc->cur++ = '\\';
143 *enc->cur++ = '\\'; 163 *enc->cur++ = '\\';
144 } 164 }
162 STRLEN clen; 182 STRLEN clen;
163 UV uch; 183 UV uch;
164 184
165 if (is_utf8) 185 if (is_utf8)
166 { 186 {
167 //uch = utf8n_to_uvuni (str, end - str, &clen, UTF8_CHECK_ONLY);
168 uch = decode_utf8 (str, end - str, &clen); 187 uch = decode_utf8 (str, end - str, &clen);
169 if (clen == (STRLEN)-1) 188 if (clen == (STRLEN)-1)
170 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);
171 } 190 }
172 else 191 else
176 } 195 }
177 196
178 if (uch > 0x10FFFFUL) 197 if (uch > 0x10FFFFUL)
179 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);
180 199
181 if (uch < 0x80 || enc->flags & F_ASCII) 200 if (uch < 0x80 || enc->json.flags & F_ASCII || (enc->json.flags & F_LATIN1 && uch > 0xFF))
182 { 201 {
183 if (uch > 0xFFFFUL) 202 if (uch > 0xFFFFUL)
184 { 203 {
185 need (enc, len += 11); 204 need (enc, len += 11);
186 sprintf (enc->cur, "\\u%04x\\u%04x", 205 sprintf (enc->cur, "\\u%04x\\u%04x",
200 *enc->cur++ = hexdigit [(uch >> 0) & 15]; 219 *enc->cur++ = hexdigit [(uch >> 0) & 15];
201 } 220 }
202 221
203 str += clen; 222 str += clen;
204 } 223 }
224 else if (enc->json.flags & F_LATIN1)
225 {
226 *enc->cur++ = uch;
227 str += clen;
228 }
205 else if (is_utf8) 229 else if (is_utf8)
206 { 230 {
207 need (enc, len += clen); 231 need (enc, len += clen);
208 do 232 do
209 { 233 {
211 } 235 }
212 while (--clen); 236 while (--clen);
213 } 237 }
214 else 238 else
215 { 239 {
216 need (enc, len += UTF8_MAX_LEN - 1); // never more than 11 bytes needed 240 need (enc, len += UTF8_MAXBYTES - 1); // never more than 11 bytes needed
217 enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); 241 enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0);
218 ++str; 242 ++str;
219 } 243 }
220 } 244 }
221 } 245 }
223 247
224 --len; 248 --len;
225 } 249 }
226} 250}
227 251
228static void 252inline void
229encode_indent (enc_t *enc) 253encode_indent (enc_t *enc)
230{ 254{
231 if (enc->flags & F_INDENT) 255 if (enc->json.flags & F_INDENT)
232 { 256 {
233 int spaces = enc->indent * INDENT_STEP; 257 int spaces = enc->indent * INDENT_STEP;
234 258
235 need (enc, spaces); 259 need (enc, spaces);
236 memset (enc->cur, ' ', spaces); 260 memset (enc->cur, ' ', spaces);
237 enc->cur += spaces; 261 enc->cur += spaces;
238 } 262 }
239} 263}
240 264
241static void 265inline void
242encode_space (enc_t *enc) 266encode_space (enc_t *enc)
243{ 267{
244 need (enc, 1); 268 need (enc, 1);
245 encode_ch (enc, ' '); 269 encode_ch (enc, ' ');
246} 270}
247 271
248static void 272inline void
249encode_nl (enc_t *enc) 273encode_nl (enc_t *enc)
250{ 274{
251 if (enc->flags & F_INDENT) 275 if (enc->json.flags & F_INDENT)
252 { 276 {
253 need (enc, 1); 277 need (enc, 1);
254 encode_ch (enc, '\n'); 278 encode_ch (enc, '\n');
255 } 279 }
256} 280}
257 281
258static void 282inline void
259encode_comma (enc_t *enc) 283encode_comma (enc_t *enc)
260{ 284{
261 encode_ch (enc, ','); 285 encode_ch (enc, ',');
262 286
263 if (enc->flags & F_INDENT) 287 if (enc->json.flags & F_INDENT)
264 encode_nl (enc); 288 encode_nl (enc);
265 else if (enc->flags & F_SPACE_AFTER) 289 else if (enc->json.flags & F_SPACE_AFTER)
266 encode_space (enc); 290 encode_space (enc);
267} 291}
268 292
269static void encode_sv (enc_t *enc, SV *sv); 293static void encode_sv (enc_t *enc, SV *sv);
270 294
313 else 337 else
314 encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); 338 encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he));
315 339
316 encode_ch (enc, '"'); 340 encode_ch (enc, '"');
317 341
318 if (enc->flags & F_SPACE_BEFORE) encode_space (enc); 342 if (enc->json.flags & F_SPACE_BEFORE) encode_space (enc);
319 encode_ch (enc, ':'); 343 encode_ch (enc, ':');
320 if (enc->flags & F_SPACE_AFTER ) encode_space (enc); 344 if (enc->json.flags & F_SPACE_AFTER ) encode_space (enc);
321 encode_sv (enc, HeVAL (he)); 345 encode_sv (enc, HeVAL (he));
322} 346}
323 347
324// compare hash entries, used when all keys are bytestrings 348// compare hash entries, used when all keys are bytestrings
325static int 349static int
360 { 384 {
361 // for canonical output we have to sort by keys first 385 // for canonical output we have to sort by keys first
362 // actually, this is mostly due to the stupid so-called 386 // actually, this is mostly due to the stupid so-called
363 // security workaround added somewhere in 5.8.x. 387 // security workaround added somewhere in 5.8.x.
364 // that randomises hash orderings 388 // that randomises hash orderings
365 if (enc->flags & F_CANONICAL) 389 if (enc->json.flags & F_CANONICAL)
366 { 390 {
367 HE *he, *hes [count]; // if your compiler dies here, you need to enable C99 mode
368 int fast = 1; 391 int fast = 1;
392 HE *he;
393#if defined(__BORLANDC__) || defined(_MSC_VER)
394 HE **hes = _alloca (count * sizeof (HE));
395#else
396 HE *hes [count]; // if your compiler dies here, you need to enable C99 mode
397#endif
369 398
370 i = 0; 399 i = 0;
371 while ((he = hv_iternext (hv))) 400 while ((he = hv_iternext (hv)))
372 { 401 {
373 hes [i++] = he; 402 hes [i++] = he;
408 437
409 encode_nl (enc); 438 encode_nl (enc);
410 } 439 }
411 else 440 else
412 { 441 {
413 SV *sv;
414 HE *he = hv_iternext (hv); 442 HE *he = hv_iternext (hv);
415 443
416 for (;;) 444 for (;;)
417 { 445 {
418 encode_indent (enc); 446 encode_indent (enc);
433 461
434// encode objects, arrays and special \0=false and \1=true values. 462// encode objects, arrays and special \0=false and \1=true values.
435static void 463static void
436encode_rv (enc_t *enc, SV *sv) 464encode_rv (enc_t *enc, SV *sv)
437{ 465{
466 svtype svt;
467
438 SvGETMAGIC (sv); 468 SvGETMAGIC (sv);
439
440 svtype svt = SvTYPE (sv); 469 svt = SvTYPE (sv);
441 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 }
442 if (svt == SVt_PVHV) 525 else if (svt == SVt_PVHV)
443 encode_hv (enc, (HV *)sv); 526 encode_hv (enc, (HV *)sv);
444 else if (svt == SVt_PVAV) 527 else if (svt == SVt_PVAV)
445 encode_av (enc, (AV *)sv); 528 encode_av (enc, (AV *)sv);
446 else if (svt < SVt_PVAV) 529 else if (svt < SVt_PVAV)
447 { 530 {
471 encode_str (enc, str, len, SvUTF8 (sv)); 554 encode_str (enc, str, len, SvUTF8 (sv));
472 encode_ch (enc, '"'); 555 encode_ch (enc, '"');
473 } 556 }
474 else if (SvNOKp (sv)) 557 else if (SvNOKp (sv))
475 { 558 {
559 // trust that perl will do the right thing w.r.t. JSON syntax.
476 need (enc, NV_DIG + 32); 560 need (enc, NV_DIG + 32);
477 Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur); 561 Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur);
478 enc->cur += strlen (enc->cur); 562 enc->cur += strlen (enc->cur);
479 } 563 }
480 else if (SvIOKp (sv)) 564 else if (SvIOKp (sv))
481 { 565 {
482 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);
483 enc->cur += 571 enc->cur +=
484 SvIsUV(sv) 572 SvIsUV(sv)
485 ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv)) 573 ? snprintf (enc->cur, sizeof (UV) * 3, "%"UVuf, (UV)SvUVX (sv))
486 : 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 }
487 } 603 }
488 else if (SvROK (sv)) 604 else if (SvROK (sv))
489 encode_rv (enc, SvRV (sv)); 605 encode_rv (enc, SvRV (sv));
490 else if (!SvOK (sv)) 606 else if (!SvOK (sv))
491 encode_str (enc, "null", 4, 0); 607 encode_str (enc, "null", 4, 0);
493 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",
494 SvPV_nolen (sv), SvFLAGS (sv)); 610 SvPV_nolen (sv), SvFLAGS (sv));
495} 611}
496 612
497static SV * 613static SV *
498encode_json (SV *scalar, U32 flags) 614encode_json (SV *scalar, JSON *json)
499{ 615{
616 enc_t enc;
617
500 if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) 618 if (!(json->flags & F_ALLOW_NONREF) && !SvROK (scalar))
501 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)");
502 620
503 enc_t enc; 621 enc.json = *json;
504 enc.flags = flags;
505 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); 622 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE));
506 enc.cur = SvPVX (enc.sv); 623 enc.cur = SvPVX (enc.sv);
507 enc.end = SvEND (enc.sv); 624 enc.end = SvEND (enc.sv);
508 enc.indent = 0; 625 enc.indent = 0;
509 enc.maxdepth = DEC_DEPTH (flags); 626 enc.maxdepth = DEC_DEPTH (enc.json.flags);
510 627
511 SvPOK_only (enc.sv); 628 SvPOK_only (enc.sv);
512 encode_sv (&enc, scalar); 629 encode_sv (&enc, scalar);
513 630
631 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv));
632 *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings
633
514 if (!(flags & (F_ASCII | F_UTF8))) 634 if (!(enc.json.flags & (F_ASCII | F_LATIN1 | F_UTF8)))
515 SvUTF8_on (enc.sv); 635 SvUTF8_on (enc.sv);
516 636
517 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv));
518
519 if (enc.flags & F_SHRINK) 637 if (enc.json.flags & F_SHRINK)
520 shrink (enc.sv); 638 shrink (enc.sv);
521 639
522 return enc.sv; 640 return enc.sv;
523} 641}
524 642
529typedef struct 647typedef struct
530{ 648{
531 char *cur; // current parser pointer 649 char *cur; // current parser pointer
532 char *end; // end of input string 650 char *end; // end of input string
533 const char *err; // parse error, if != 0 651 const char *err; // parse error, if != 0
534 U32 flags; // F_* 652 JSON json;
535 U32 depth; // recursion depth 653 U32 depth; // recursion depth
536 U32 maxdepth; // recursion depth limit 654 U32 maxdepth; // recursion depth limit
537} dec_t; 655} dec_t;
538 656
539static void 657inline void
540decode_ws (dec_t *dec) 658decode_ws (dec_t *dec)
541{ 659{
542 for (;;) 660 for (;;)
543 { 661 {
544 char ch = *dec->cur; 662 char ch = *dec->cur;
570decode_4hex (dec_t *dec) 688decode_4hex (dec_t *dec)
571{ 689{
572 signed char d1, d2, d3, d4; 690 signed char d1, d2, d3, d4;
573 unsigned char *cur = (unsigned char *)dec->cur; 691 unsigned char *cur = (unsigned char *)dec->cur;
574 692
575 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");
576 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");
577 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");
578 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");
579 697
580 dec->cur += 4; 698 dec->cur += 4;
581 699
582 return ((UV)d1) << 12 700 return ((UV)d1) << 12
583 | ((UV)d2) << 8 701 | ((UV)d2) << 8
591static SV * 709static SV *
592decode_str (dec_t *dec) 710decode_str (dec_t *dec)
593{ 711{
594 SV *sv = 0; 712 SV *sv = 0;
595 int utf8 = 0; 713 int utf8 = 0;
714 char *dec_cur = dec->cur;
596 715
597 do 716 do
598 { 717 {
599 char buf [SHORT_STRING_LEN + UTF8_MAX_LEN]; 718 char buf [SHORT_STRING_LEN + UTF8_MAXBYTES];
600 char *cur = buf; 719 char *cur = buf;
601 720
602 do 721 do
603 { 722 {
604 unsigned char ch = *(unsigned char *)dec->cur++; 723 unsigned char ch = *(unsigned char *)dec_cur++;
605 724
606 if (ch == '"') 725 if (expect_false (ch == '"'))
607 { 726 {
608 --dec->cur; 727 --dec_cur;
609 break; 728 break;
610 } 729 }
611 else if (ch == '\\') 730 else if (expect_false (ch == '\\'))
612 { 731 {
613 switch (*dec->cur) 732 switch (*dec_cur)
614 { 733 {
615 case '\\': 734 case '\\':
616 case '/': 735 case '/':
617 case '"': *cur++ = *dec->cur++; break; 736 case '"': *cur++ = *dec_cur++; break;
618 737
619 case 'b': ++dec->cur; *cur++ = '\010'; break; 738 case 'b': ++dec_cur; *cur++ = '\010'; break;
620 case 't': ++dec->cur; *cur++ = '\011'; break; 739 case 't': ++dec_cur; *cur++ = '\011'; break;
621 case 'n': ++dec->cur; *cur++ = '\012'; break; 740 case 'n': ++dec_cur; *cur++ = '\012'; break;
622 case 'f': ++dec->cur; *cur++ = '\014'; break; 741 case 'f': ++dec_cur; *cur++ = '\014'; break;
623 case 'r': ++dec->cur; *cur++ = '\015'; break; 742 case 'r': ++dec_cur; *cur++ = '\015'; break;
624 743
625 case 'u': 744 case 'u':
626 { 745 {
627 UV lo, hi; 746 UV lo, hi;
628 ++dec->cur; 747 ++dec_cur;
629 748
749 dec->cur = dec_cur;
630 hi = decode_4hex (dec); 750 hi = decode_4hex (dec);
751 dec_cur = dec->cur;
631 if (hi == (UV)-1) 752 if (hi == (UV)-1)
632 goto fail; 753 goto fail;
633 754
634 // possibly a surrogate pair 755 // possibly a surrogate pair
635 if (hi >= 0xd800) 756 if (hi >= 0xd800)
636 if (hi < 0xdc00) 757 if (hi < 0xdc00)
637 { 758 {
638 if (dec->cur [0] != '\\' || dec->cur [1] != 'u') 759 if (dec_cur [0] != '\\' || dec_cur [1] != 'u')
639 ERR ("missing low surrogate character in surrogate pair"); 760 ERR ("missing low surrogate character in surrogate pair");
640 761
641 dec->cur += 2; 762 dec_cur += 2;
642 763
764 dec->cur = dec_cur;
643 lo = decode_4hex (dec); 765 lo = decode_4hex (dec);
766 dec_cur = dec->cur;
644 if (lo == (UV)-1) 767 if (lo == (UV)-1)
645 goto fail; 768 goto fail;
646 769
647 if (lo < 0xdc00 || lo >= 0xe000) 770 if (lo < 0xdc00 || lo >= 0xe000)
648 ERR ("surrogate pair expected"); 771 ERR ("surrogate pair expected");
662 *cur++ = hi; 785 *cur++ = hi;
663 } 786 }
664 break; 787 break;
665 788
666 default: 789 default:
667 --dec->cur; 790 --dec_cur;
668 ERR ("illegal backslash escape sequence in string"); 791 ERR ("illegal backslash escape sequence in string");
669 } 792 }
670 } 793 }
671 else if (ch >= 0x20 && ch <= 0x7f) 794 else if (expect_true (ch >= 0x20 && ch <= 0x7f))
672 *cur++ = ch; 795 *cur++ = ch;
673 else if (ch >= 0x80) 796 else if (ch >= 0x80)
674 { 797 {
675 --dec->cur;
676
677 STRLEN clen; 798 STRLEN clen;
799 UV uch;
800
801 --dec_cur;
802
678 UV uch = decode_utf8 (dec->cur, dec->end - dec->cur, &clen); 803 uch = decode_utf8 (dec_cur, dec->end - dec_cur, &clen);
679 if (clen == (STRLEN)-1) 804 if (clen == (STRLEN)-1)
680 ERR ("malformed UTF-8 character in JSON string"); 805 ERR ("malformed UTF-8 character in JSON string");
681 806
682 do 807 do
683 *cur++ = *dec->cur++; 808 *cur++ = *dec_cur++;
684 while (--clen); 809 while (--clen);
685 810
686 utf8 = 1; 811 utf8 = 1;
687 } 812 }
688 else 813 else
689 { 814 {
690 --dec->cur; 815 --dec_cur;
691 816
692 if (!ch) 817 if (!ch)
693 ERR ("unexpected end of string while parsing JSON string"); 818 ERR ("unexpected end of string while parsing JSON string");
694 else 819 else
695 ERR ("invalid character encountered while parsing JSON string"); 820 ERR ("invalid character encountered while parsing JSON string");
696 } 821 }
697 } 822 }
698 while (cur < buf + SHORT_STRING_LEN); 823 while (cur < buf + SHORT_STRING_LEN);
699 824
825 {
700 STRLEN len = cur - buf; 826 STRLEN len = cur - buf;
701 827
702 if (sv) 828 if (sv)
703 { 829 {
704 SvGROW (sv, SvCUR (sv) + len + 1); 830 SvGROW (sv, SvCUR (sv) + len + 1);
705 memcpy (SvPVX (sv) + SvCUR (sv), buf, len); 831 memcpy (SvPVX (sv) + SvCUR (sv), buf, len);
706 SvCUR_set (sv, SvCUR (sv) + len); 832 SvCUR_set (sv, SvCUR (sv) + len);
707 } 833 }
708 else 834 else
709 sv = newSVpvn (buf, len); 835 sv = newSVpvn (buf, len);
710 } 836 }
837 }
711 while (*dec->cur != '"'); 838 while (*dec_cur != '"');
712 839
713 ++dec->cur; 840 ++dec_cur;
714 841
715 if (sv) 842 if (sv)
716 { 843 {
717 SvPOK_only (sv); 844 SvPOK_only (sv);
718 *SvEND (sv) = 0; 845 *SvEND (sv) = 0;
721 SvUTF8_on (sv); 848 SvUTF8_on (sv);
722 } 849 }
723 else 850 else
724 sv = newSVpvn ("", 0); 851 sv = newSVpvn ("", 0);
725 852
853 dec->cur = dec_cur;
726 return sv; 854 return sv;
727 855
728fail: 856fail:
857 dec->cur = dec_cur;
729 return 0; 858 return 0;
730} 859}
731 860
732static SV * 861static SV *
733decode_num (dec_t *dec) 862decode_num (dec_t *dec)
791 is_nv = 1; 920 is_nv = 1;
792 } 921 }
793 922
794 if (!is_nv) 923 if (!is_nv)
795 { 924 {
796 UV uv; 925 // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so
797 int numtype = grok_number (start, dec->cur - start, &uv); 926 if (*start == '-')
798 if (numtype & IS_NUMBER_IN_UV) 927 switch (dec->cur - start)
799 if (numtype & IS_NUMBER_NEG)
800 { 928 {
801 if (uv < (UV)IV_MIN) 929 case 2: return newSViv (-( start [1] - '0' * 1));
802 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));
803 } 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 }
804 else 952 else
805 return newSVuv (uv); 953 return newSVuv (uv);
954
955 // here would likely be the place for bigint support
806 } 956 }
957 }
807 958
959 // if we ever support bigint or bigfloat, this is the place for bigfloat
808 return newSVnv (Atof (start)); 960 return newSVnv (Atof (start));
809 961
810fail: 962fail:
811 return 0; 963 return 0;
812} 964}
866 if (*dec->cur == '}') 1018 if (*dec->cur == '}')
867 ++dec->cur; 1019 ++dec->cur;
868 else 1020 else
869 for (;;) 1021 for (;;)
870 { 1022 {
871 SV *key, *value;
872
873 decode_ws (dec); EXPECT_CH ('"'); 1023 decode_ws (dec); EXPECT_CH ('"');
874 1024
875 key = decode_str (dec); 1025 // heuristic: assume that
876 if (!key) 1026 // a) decode_str + hv_store_ent are abysmally slow.
877 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
878 1034
879 decode_ws (dec); EXPECT_CH (':'); 1035 for (;;)
880
881 value = decode_sv (dec);
882 if (!value)
883 { 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);
884 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)
885 goto fail; 1070 goto fail;
1071
1072 hv_store (hv, key, len, value, 0);
1073
1074 break;
1075 }
1076
1077 ++p;
886 } 1078 }
887 1079 }
888 hv_store_ent (hv, key, value, 0);
889 SvREFCNT_dec (key);
890 1080
891 decode_ws (dec); 1081 decode_ws (dec);
892 1082
893 if (*dec->cur == '}') 1083 if (*dec->cur == '}')
894 { 1084 {
913 1103
914static SV * 1104static SV *
915decode_sv (dec_t *dec) 1105decode_sv (dec_t *dec)
916{ 1106{
917 decode_ws (dec); 1107 decode_ws (dec);
1108
1109 // the beauty of JSON: you need exactly one character lookahead
1110 // to parse anything.
918 switch (*dec->cur) 1111 switch (*dec->cur)
919 { 1112 {
920 case '"': ++dec->cur; return decode_str (dec); 1113 case '"': ++dec->cur; return decode_str (dec);
921 case '[': ++dec->cur; return decode_av (dec); 1114 case '[': ++dec->cur; return decode_av (dec);
922 case '{': ++dec->cur; return decode_hv (dec); 1115 case '{': ++dec->cur; return decode_hv (dec);
928 1121
929 case 't': 1122 case 't':
930 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) 1123 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4))
931 { 1124 {
932 dec->cur += 4; 1125 dec->cur += 4;
933 return newSViv (1); 1126 return SvREFCNT_inc (json_true);
934 } 1127 }
935 else 1128 else
936 ERR ("'true' expected"); 1129 ERR ("'true' expected");
937 1130
938 break; 1131 break;
939 1132
940 case 'f': 1133 case 'f':
941 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) 1134 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5))
942 { 1135 {
943 dec->cur += 5; 1136 dec->cur += 5;
944 return newSViv (0); 1137 return SvREFCNT_inc (json_false);
945 } 1138 }
946 else 1139 else
947 ERR ("'false' expected"); 1140 ERR ("'false' expected");
948 1141
949 break; 1142 break;
967fail: 1160fail:
968 return 0; 1161 return 0;
969} 1162}
970 1163
971static SV * 1164static SV *
972decode_json (SV *string, U32 flags) 1165decode_json (SV *string, JSON *json, UV *offset_return)
973{ 1166{
1167 dec_t dec;
1168 UV offset;
974 SV *sv; 1169 SV *sv;
975 1170
1171 SvGETMAGIC (string);
976 SvUPGRADE (string, SVt_PV); 1172 SvUPGRADE (string, SVt_PV);
977 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
978 if (flags & F_UTF8) 1178 if (json->flags & F_UTF8)
979 sv_utf8_downgrade (string, 0); 1179 sv_utf8_downgrade (string, 0);
980 else 1180 else
981 sv_utf8_upgrade (string); 1181 sv_utf8_upgrade (string);
982 1182
983 SvGROW (string, SvCUR (string) + 1); // should basically be a NOP 1183 SvGROW (string, SvCUR (string) + 1); // should basically be a NOP
984 1184
985 dec_t dec; 1185 dec.json = *json;
986 dec.flags = flags;
987 dec.cur = SvPVX (string); 1186 dec.cur = SvPVX (string);
988 dec.end = SvEND (string); 1187 dec.end = SvEND (string);
989 dec.err = 0; 1188 dec.err = 0;
990 dec.depth = 0; 1189 dec.depth = 0;
991 dec.maxdepth = DEC_DEPTH (dec.flags); 1190 dec.maxdepth = DEC_DEPTH (dec.json.flags);
992 1191
993 *dec.end = 0; // this should basically be a nop, too, but make sure its there 1192 *dec.end = 0; // this should basically be a nop, too, but make sure it's there
994 sv = decode_sv (&dec); 1193 sv = decode_sv (&dec);
995 1194
1195 if (!(offset_return || !sv))
1196 {
1197 // check for trailing garbage
1198 decode_ws (&dec);
1199
1200 if (*dec.cur)
1201 {
1202 dec.err = "garbage after JSON object";
1203 SvREFCNT_dec (sv);
1204 sv = 0;
1205 }
1206 }
1207
1208 if (offset_return || !sv)
1209 {
1210 offset = dec.json.flags & F_UTF8
1211 ? dec.cur - SvPVX (string)
1212 : utf8_distance (dec.cur, SvPVX (string));
1213
1214 if (offset_return)
1215 *offset_return = offset;
1216 }
1217
996 if (!sv) 1218 if (!sv)
997 { 1219 {
998 IV offset = dec.flags & F_UTF8
999 ? dec.cur - SvPVX (string)
1000 : utf8_distance (dec.cur, SvPVX (string));
1001 SV *uni = sv_newmortal (); 1220 SV *uni = sv_newmortal ();
1002 1221
1003 // horrible hack to silence warning inside pv_uni_display 1222 // horrible hack to silence warning inside pv_uni_display
1004 COP cop = *PL_curcop; 1223 COP cop = *PL_curcop;
1005 cop.cop_warnings = pWARN_NONE; 1224 cop.cop_warnings = pWARN_NONE;
1015 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); 1234 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)");
1016 } 1235 }
1017 1236
1018 sv = sv_2mortal (sv); 1237 sv = sv_2mortal (sv);
1019 1238
1020 if (!(dec.flags & F_ALLOW_NONREF) && !SvROK (sv)) 1239 if (!(dec.json.flags & F_ALLOW_NONREF) && !SvROK (sv))
1021 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)");
1022 1241
1023 return sv; 1242 return sv;
1024} 1243}
1025 1244
1029MODULE = JSON::XS PACKAGE = JSON::XS 1248MODULE = JSON::XS PACKAGE = JSON::XS
1030 1249
1031BOOT: 1250BOOT:
1032{ 1251{
1033 int i; 1252 int i;
1034
1035 memset (decode_hexdigit, 0xff, 256);
1036 1253
1037 for (i = 0; i < 256; ++i) 1254 for (i = 0; i < 256; ++i)
1038 decode_hexdigit [i] = 1255 decode_hexdigit [i] =
1039 i >= '0' && i <= '9' ? i - '0' 1256 i >= '0' && i <= '9' ? i - '0'
1040 : i >= 'a' && i <= 'f' ? i - 'a' + 10 1257 : i >= 'a' && i <= 'f' ? i - 'a' + 10
1041 : i >= 'A' && i <= 'F' ? i - 'A' + 10 1258 : i >= 'A' && i <= 'F' ? i - 'A' + 10
1042 : -1; 1259 : -1;
1043 1260
1044 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);
1045} 1266}
1046 1267
1047PROTOTYPES: DISABLE 1268PROTOTYPES: DISABLE
1048 1269
1049SV *new (char *dummy) 1270void new (char *klass)
1050 CODE: 1271 PPCODE:
1051 RETVAL = sv_bless (newRV_noinc (newSVuv (F_DEFAULT)), json_stash); 1272{
1052 OUTPUT: 1273 SV *pv = NEWSV (0, sizeof (JSON));
1053 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}
1054 1279
1055SV *ascii (SV *self, int enable = 1) 1280void ascii (JSON *self, int enable = 1)
1056 ALIAS: 1281 ALIAS:
1057 ascii = F_ASCII 1282 ascii = F_ASCII
1283 latin1 = F_LATIN1
1058 utf8 = F_UTF8 1284 utf8 = F_UTF8
1059 indent = F_INDENT 1285 indent = F_INDENT
1060 canonical = F_CANONICAL 1286 canonical = F_CANONICAL
1061 space_before = F_SPACE_BEFORE 1287 space_before = F_SPACE_BEFORE
1062 space_after = F_SPACE_AFTER 1288 space_after = F_SPACE_AFTER
1063 pretty = F_PRETTY 1289 pretty = F_PRETTY
1064 allow_nonref = F_ALLOW_NONREF 1290 allow_nonref = F_ALLOW_NONREF
1065 shrink = F_SHRINK 1291 shrink = F_SHRINK
1292 allow_blessed = F_ALLOW_BLESSED
1293 convert_blessed = F_CONV_BLESSED
1066 CODE: 1294 PPCODE:
1067{ 1295{
1068 UV *uv = SvJSON (self);
1069 if (enable) 1296 if (enable)
1070 *uv |= ix; 1297 self->flags |= ix;
1071 else 1298 else
1072 *uv &= ~ix; 1299 self->flags &= ~ix;
1073 1300
1074 RETVAL = newSVsv (self); 1301 XPUSHs (ST (0));
1075} 1302}
1076 OUTPUT:
1077 RETVAL
1078 1303
1079SV *max_depth (SV *self, int max_depth = 0x80000000UL) 1304void max_depth (JSON *self, UV max_depth = 0x80000000UL)
1080 CODE: 1305 PPCODE:
1081{ 1306{
1082 UV *uv = SvJSON (self);
1083 UV log2 = 0; 1307 UV log2 = 0;
1084 1308
1085 if (max_depth > 0x80000000UL) max_depth = 0x80000000UL; 1309 if (max_depth > 0x80000000UL) max_depth = 0x80000000UL;
1086 1310
1087 while ((1UL << log2) < max_depth) 1311 while ((1UL << log2) < max_depth)
1088 ++log2; 1312 ++log2;
1089 1313
1090 *uv = *uv & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); 1314 self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH);
1091 1315
1092 RETVAL = newSVsv (self); 1316 XPUSHs (ST (0));
1093} 1317}
1094 OUTPUT:
1095 RETVAL
1096 1318
1097void encode (SV *self, SV *scalar) 1319void max_size (JSON *self, UV max_size = 0)
1098 PPCODE: 1320 PPCODE:
1099 XPUSHs (encode_json (scalar, *SvJSON (self))); 1321{
1322 UV log2 = 0;
1100 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
1101void decode (SV *self, SV *jsonstr) 1335void encode (JSON *self, SV *scalar)
1102 PPCODE: 1336 PPCODE:
1337 XPUSHs (encode_json (scalar, self));
1338
1339void decode (JSON *self, SV *jsonstr)
1340 PPCODE:
1103 XPUSHs (decode_json (jsonstr, *SvJSON (self))); 1341 XPUSHs (decode_json (jsonstr, self, 0));
1342
1343void decode_prefix (JSON *self, SV *jsonstr)
1344 PPCODE:
1345{
1346 UV offset;
1347 EXTEND (SP, 2);
1348 PUSHs (decode_json (jsonstr, self, &offset));
1349 PUSHs (sv_2mortal (newSVuv (offset)));
1350}
1104 1351
1105PROTOTYPES: ENABLE 1352PROTOTYPES: ENABLE
1106 1353
1107void to_json (SV *scalar) 1354void to_json (SV *scalar)
1108 ALIAS:
1109 objToJson = 0
1110 PPCODE: 1355 PPCODE:
1356{
1357 JSON json = { F_DEFAULT | F_UTF8 };
1111 XPUSHs (encode_json (scalar, F_DEFAULT | F_UTF8)); 1358 XPUSHs (encode_json (scalar, &json));
1359}
1112 1360
1113void from_json (SV *jsonstr) 1361void from_json (SV *jsonstr)
1114 ALIAS:
1115 jsonToObj = 0
1116 PPCODE: 1362 PPCODE:
1363{
1364 JSON json = { F_DEFAULT | F_UTF8 };
1117 XPUSHs (decode_json (jsonstr, F_DEFAULT | F_UTF8)); 1365 XPUSHs (decode_json (jsonstr, &json, 0));
1366}
1118 1367

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines