… | |
… | |
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 | |
42 | #if __GNUC__ >= 3 |
50 | #if __GNUC__ >= 3 |
… | |
… | |
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 | |
53 | static HV *json_stash; // JSON::XS:: |
61 | static HV *json_stash, *json_boolean_stash; // JSON::XS:: |
|
|
62 | static SV *json_true, *json_false; |
|
|
63 | |
|
|
64 | typedef struct json { |
|
|
65 | U32 flags; |
|
|
66 | } JSON__XS; |
54 | |
67 | |
55 | ///////////////////////////////////////////////////////////////////////////// |
68 | ///////////////////////////////////////////////////////////////////////////// |
56 | // utility functions |
69 | // utility functions |
57 | |
70 | |
58 | static UV * |
71 | static UV * |
… | |
… | |
107 | typedef struct |
120 | typedef struct |
108 | { |
121 | { |
109 | char *cur; // SvPVX (sv) + current output position |
122 | char *cur; // SvPVX (sv) + current output position |
110 | char *end; // SvEND (sv) |
123 | char *end; // SvEND (sv) |
111 | SV *sv; // result scalar |
124 | SV *sv; // result scalar |
112 | U32 flags; // F_* |
125 | struct json json; |
113 | U32 indent; // indentation level |
126 | U32 indent; // indentation level |
114 | U32 maxdepth; // max. indentation/recursion level |
127 | U32 maxdepth; // max. indentation/recursion level |
115 | } enc_t; |
128 | } enc_t; |
116 | |
129 | |
117 | inline void |
130 | inline void |
… | |
… | |
178 | STRLEN clen; |
191 | STRLEN clen; |
179 | UV uch; |
192 | UV uch; |
180 | |
193 | |
181 | if (is_utf8) |
194 | if (is_utf8) |
182 | { |
195 | { |
183 | //uch = utf8n_to_uvuni (str, end - str, &clen, UTF8_CHECK_ONLY); |
|
|
184 | uch = decode_utf8 (str, end - str, &clen); |
196 | uch = decode_utf8 (str, end - str, &clen); |
185 | if (clen == (STRLEN)-1) |
197 | if (clen == (STRLEN)-1) |
186 | croak ("malformed or illegal unicode character in string [%.11s], cannot convert to JSON", str); |
198 | croak ("malformed or illegal unicode character in string [%.11s], cannot convert to JSON", str); |
187 | } |
199 | } |
188 | else |
200 | else |
… | |
… | |
192 | } |
204 | } |
193 | |
205 | |
194 | if (uch > 0x10FFFFUL) |
206 | if (uch > 0x10FFFFUL) |
195 | croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
207 | croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
196 | |
208 | |
197 | if (uch < 0x80 || enc->flags & F_ASCII || (enc->flags & F_LATIN1 && uch > 0xFF)) |
209 | if (uch < 0x80 || enc->json.flags & F_ASCII || (enc->json.flags & F_LATIN1 && uch > 0xFF)) |
198 | { |
210 | { |
199 | if (uch > 0xFFFFUL) |
211 | if (uch > 0xFFFFUL) |
200 | { |
212 | { |
201 | need (enc, len += 11); |
213 | need (enc, len += 11); |
202 | sprintf (enc->cur, "\\u%04x\\u%04x", |
214 | sprintf (enc->cur, "\\u%04x\\u%04x", |
… | |
… | |
216 | *enc->cur++ = hexdigit [(uch >> 0) & 15]; |
228 | *enc->cur++ = hexdigit [(uch >> 0) & 15]; |
217 | } |
229 | } |
218 | |
230 | |
219 | str += clen; |
231 | str += clen; |
220 | } |
232 | } |
221 | else if (enc->flags & F_LATIN1) |
233 | else if (enc->json.flags & F_LATIN1) |
222 | { |
234 | { |
223 | *enc->cur++ = uch; |
235 | *enc->cur++ = uch; |
224 | str += clen; |
236 | str += clen; |
225 | } |
237 | } |
226 | else if (is_utf8) |
238 | else if (is_utf8) |
… | |
… | |
247 | } |
259 | } |
248 | |
260 | |
249 | inline void |
261 | inline void |
250 | encode_indent (enc_t *enc) |
262 | encode_indent (enc_t *enc) |
251 | { |
263 | { |
252 | if (enc->flags & F_INDENT) |
264 | if (enc->json.flags & F_INDENT) |
253 | { |
265 | { |
254 | int spaces = enc->indent * INDENT_STEP; |
266 | int spaces = enc->indent * INDENT_STEP; |
255 | |
267 | |
256 | need (enc, spaces); |
268 | need (enc, spaces); |
257 | memset (enc->cur, ' ', spaces); |
269 | memset (enc->cur, ' ', spaces); |
… | |
… | |
267 | } |
279 | } |
268 | |
280 | |
269 | inline void |
281 | inline void |
270 | encode_nl (enc_t *enc) |
282 | encode_nl (enc_t *enc) |
271 | { |
283 | { |
272 | if (enc->flags & F_INDENT) |
284 | if (enc->json.flags & F_INDENT) |
273 | { |
285 | { |
274 | need (enc, 1); |
286 | need (enc, 1); |
275 | encode_ch (enc, '\n'); |
287 | encode_ch (enc, '\n'); |
276 | } |
288 | } |
277 | } |
289 | } |
… | |
… | |
279 | inline void |
291 | inline void |
280 | encode_comma (enc_t *enc) |
292 | encode_comma (enc_t *enc) |
281 | { |
293 | { |
282 | encode_ch (enc, ','); |
294 | encode_ch (enc, ','); |
283 | |
295 | |
284 | if (enc->flags & F_INDENT) |
296 | if (enc->json.flags & F_INDENT) |
285 | encode_nl (enc); |
297 | encode_nl (enc); |
286 | else if (enc->flags & F_SPACE_AFTER) |
298 | else if (enc->json.flags & F_SPACE_AFTER) |
287 | encode_space (enc); |
299 | encode_space (enc); |
288 | } |
300 | } |
289 | |
301 | |
290 | static void encode_sv (enc_t *enc, SV *sv); |
302 | static void encode_sv (enc_t *enc, SV *sv); |
291 | |
303 | |
… | |
… | |
334 | else |
346 | else |
335 | encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); |
347 | encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); |
336 | |
348 | |
337 | encode_ch (enc, '"'); |
349 | encode_ch (enc, '"'); |
338 | |
350 | |
339 | if (enc->flags & F_SPACE_BEFORE) encode_space (enc); |
351 | if (enc->json.flags & F_SPACE_BEFORE) encode_space (enc); |
340 | encode_ch (enc, ':'); |
352 | encode_ch (enc, ':'); |
341 | if (enc->flags & F_SPACE_AFTER ) encode_space (enc); |
353 | if (enc->json.flags & F_SPACE_AFTER ) encode_space (enc); |
342 | encode_sv (enc, HeVAL (he)); |
354 | encode_sv (enc, HeVAL (he)); |
343 | } |
355 | } |
344 | |
356 | |
345 | // compare hash entries, used when all keys are bytestrings |
357 | // compare hash entries, used when all keys are bytestrings |
346 | static int |
358 | static int |
… | |
… | |
381 | { |
393 | { |
382 | // for canonical output we have to sort by keys first |
394 | // for canonical output we have to sort by keys first |
383 | // actually, this is mostly due to the stupid so-called |
395 | // actually, this is mostly due to the stupid so-called |
384 | // security workaround added somewhere in 5.8.x. |
396 | // security workaround added somewhere in 5.8.x. |
385 | // that randomises hash orderings |
397 | // that randomises hash orderings |
386 | if (enc->flags & F_CANONICAL) |
398 | if (enc->json.flags & F_CANONICAL) |
387 | { |
399 | { |
388 | int fast = 1; |
400 | int fast = 1; |
389 | HE *he; |
401 | HE *he; |
390 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
402 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
391 | HE **hes = _alloca (count * sizeof (HE)); |
403 | HE **hes = _alloca (count * sizeof (HE)); |
… | |
… | |
463 | svtype svt; |
475 | svtype svt; |
464 | |
476 | |
465 | SvGETMAGIC (sv); |
477 | SvGETMAGIC (sv); |
466 | svt = SvTYPE (sv); |
478 | svt = SvTYPE (sv); |
467 | |
479 | |
|
|
480 | if (expect_false (SvOBJECT (sv))) |
|
|
481 | { |
|
|
482 | if (SvSTASH (sv) == json_boolean_stash) |
|
|
483 | { |
|
|
484 | if (SvIV (sv) == 0) |
|
|
485 | encode_str (enc, "false", 5, 0); |
|
|
486 | else |
|
|
487 | encode_str (enc, "true", 4, 0); |
|
|
488 | } |
|
|
489 | else |
|
|
490 | { |
|
|
491 | #if 0 |
|
|
492 | if (0 && sv_derived_from (rv, "JSON::Literal")) |
|
|
493 | { |
|
|
494 | // not yet |
|
|
495 | } |
|
|
496 | #endif |
|
|
497 | if (enc->json.flags & F_CONV_BLESSED) |
|
|
498 | { |
|
|
499 | // we re-bless the reference to get overload and other niceties right |
|
|
500 | GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 1); |
|
|
501 | |
|
|
502 | if (to_json) |
|
|
503 | { |
|
|
504 | dSP; |
|
|
505 | ENTER; |
|
|
506 | SAVETMPS; |
|
|
507 | PUSHMARK (SP); |
|
|
508 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); |
|
|
509 | |
|
|
510 | // calling with G_SCALAR ensures that we always get a 1 reutrn value |
|
|
511 | // check anyways. |
|
|
512 | PUTBACK; |
|
|
513 | assert (1 == call_sv ((SV *)GvCV (to_json), G_SCALAR)); |
|
|
514 | SPAGAIN; |
|
|
515 | |
|
|
516 | encode_sv (enc, POPs); |
|
|
517 | |
|
|
518 | FREETMPS; |
|
|
519 | LEAVE; |
|
|
520 | } |
|
|
521 | else if (enc->json.flags & F_ALLOW_BLESSED) |
|
|
522 | encode_str (enc, "null", 4, 0); |
|
|
523 | else |
|
|
524 | croak ("encountered object '%s', but neither allow_blessed enabled nor TO_JSON method available on it", |
|
|
525 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
|
|
526 | } |
|
|
527 | else if (enc->json.flags & F_ALLOW_BLESSED) |
|
|
528 | encode_str (enc, "null", 4, 0); |
|
|
529 | else |
|
|
530 | croak ("encountered object '%s', but neither allow_blessed nor convert_blessed settings are enabled", |
|
|
531 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
|
|
532 | } |
|
|
533 | } |
468 | if (svt == SVt_PVHV) |
534 | else if (svt == SVt_PVHV) |
469 | encode_hv (enc, (HV *)sv); |
535 | encode_hv (enc, (HV *)sv); |
470 | else if (svt == SVt_PVAV) |
536 | else if (svt == SVt_PVAV) |
471 | encode_av (enc, (AV *)sv); |
537 | encode_av (enc, (AV *)sv); |
472 | else if (svt < SVt_PVAV) |
538 | else if (svt < SVt_PVAV) |
473 | { |
539 | { |
… | |
… | |
497 | encode_str (enc, str, len, SvUTF8 (sv)); |
563 | encode_str (enc, str, len, SvUTF8 (sv)); |
498 | encode_ch (enc, '"'); |
564 | encode_ch (enc, '"'); |
499 | } |
565 | } |
500 | else if (SvNOKp (sv)) |
566 | else if (SvNOKp (sv)) |
501 | { |
567 | { |
|
|
568 | // trust that perl will do the right thing w.r.t. JSON syntax. |
502 | need (enc, NV_DIG + 32); |
569 | need (enc, NV_DIG + 32); |
503 | Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur); |
570 | Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur); |
504 | enc->cur += strlen (enc->cur); |
571 | enc->cur += strlen (enc->cur); |
505 | } |
572 | } |
506 | else if (SvIOKp (sv)) |
573 | else if (SvIOKp (sv)) |
507 | { |
574 | { |
508 | // we assume we can always read an IV as a UV |
575 | // we assume we can always read an IV as a UV |
509 | if (SvUV (sv) & ~(UV)0x7fff) |
576 | if (SvUV (sv) & ~(UV)0x7fff) |
510 | { |
577 | { |
|
|
578 | // large integer, use the (rather slow) snprintf way. |
511 | need (enc, 32); |
579 | need (enc, sizeof (UV) * 3); |
512 | enc->cur += |
580 | enc->cur += |
513 | SvIsUV(sv) |
581 | SvIsUV(sv) |
514 | ? snprintf (enc->cur, 32, "%"UVuf, (UV)SvUVX (sv)) |
582 | ? snprintf (enc->cur, sizeof (UV) * 3, "%"UVuf, (UV)SvUVX (sv)) |
515 | : snprintf (enc->cur, 32, "%"IVdf, (IV)SvIVX (sv)); |
583 | : snprintf (enc->cur, sizeof (UV) * 3, "%"IVdf, (IV)SvIVX (sv)); |
516 | } |
584 | } |
517 | else |
585 | else |
518 | { |
586 | { |
519 | // optimise the "small number case" |
587 | // optimise the "small number case" |
520 | // code will likely be branchless and use only a single multiplication |
588 | // code will likely be branchless and use only a single multiplication |
521 | I32 i = SvIV (sv); |
589 | I32 i = SvIV (sv); |
522 | U32 u; |
590 | U32 u; |
|
|
591 | char digit, nz = 0; |
523 | |
592 | |
524 | need (enc, 6); |
593 | need (enc, 6); |
525 | |
594 | |
526 | *enc->cur = '-'; enc->cur += i < 0 ? 1 : 0; |
595 | *enc->cur = '-'; enc->cur += i < 0 ? 1 : 0; |
527 | u = i < 0 ? -i : i; |
596 | u = i < 0 ? -i : i; |
528 | |
597 | |
529 | // convert to 4.28 fixed-point representation |
598 | // convert to 4.28 fixed-point representation |
530 | u = u * ((0xfffffff + 10000) / 10000); // 10**5, 5 fractional digits |
599 | u = u * ((0xfffffff + 10000) / 10000); // 10**5, 5 fractional digits |
531 | |
600 | |
532 | char digit, nz = 0; |
601 | // now output digit by digit, each time masking out the integer part |
533 | |
602 | // and multiplying by 5 while moving the decimal point one to the right, |
|
|
603 | // resulting in a net multiplication by 10. |
|
|
604 | // we always write the digit to memory but conditionally increment |
|
|
605 | // the pointer, to ease the usage of conditional move instructions. |
534 | digit = u >> 28; *enc->cur = digit + '0'; nz |= digit; enc->cur += nz ? 1 : 0; u = (u & 0xfffffff) * 5; |
606 | digit = u >> 28; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0xfffffff) * 5; |
535 | digit = u >> 27; *enc->cur = digit + '0'; nz |= digit; enc->cur += nz ? 1 : 0; u = (u & 0x7ffffff) * 5; |
607 | digit = u >> 27; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x7ffffff) * 5; |
536 | digit = u >> 26; *enc->cur = digit + '0'; nz |= digit; enc->cur += nz ? 1 : 0; u = (u & 0x3ffffff) * 5; |
608 | digit = u >> 26; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x3ffffff) * 5; |
537 | digit = u >> 25; *enc->cur = digit + '0'; nz |= digit; enc->cur += nz ? 1 : 0; u = (u & 0x1ffffff) * 5; |
609 | digit = u >> 25; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x1ffffff) * 5; |
538 | digit = u >> 24; *enc->cur = digit + '0'; nz |= digit; enc->cur += 1; |
610 | digit = u >> 24; *enc->cur = digit + '0'; enc->cur += 1; // correctly generate '0' |
539 | } |
611 | } |
540 | } |
612 | } |
541 | else if (SvROK (sv)) |
613 | else if (SvROK (sv)) |
542 | encode_rv (enc, SvRV (sv)); |
614 | encode_rv (enc, SvRV (sv)); |
543 | else if (!SvOK (sv)) |
615 | else if (!SvOK (sv)) |
… | |
… | |
546 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
618 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
547 | SvPV_nolen (sv), SvFLAGS (sv)); |
619 | SvPV_nolen (sv), SvFLAGS (sv)); |
548 | } |
620 | } |
549 | |
621 | |
550 | static SV * |
622 | static SV * |
551 | encode_json (SV *scalar, U32 flags) |
623 | encode_json (SV *scalar, struct json *json) |
552 | { |
624 | { |
553 | enc_t enc; |
625 | enc_t enc; |
554 | |
626 | |
555 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
627 | if (!(json->flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
556 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
628 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
557 | |
629 | |
558 | enc.flags = flags; |
630 | enc.json = *json; |
559 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
631 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
560 | enc.cur = SvPVX (enc.sv); |
632 | enc.cur = SvPVX (enc.sv); |
561 | enc.end = SvEND (enc.sv); |
633 | enc.end = SvEND (enc.sv); |
562 | enc.indent = 0; |
634 | enc.indent = 0; |
563 | enc.maxdepth = DEC_DEPTH (flags); |
635 | enc.maxdepth = DEC_DEPTH (enc.json.flags); |
564 | |
636 | |
565 | SvPOK_only (enc.sv); |
637 | SvPOK_only (enc.sv); |
566 | encode_sv (&enc, scalar); |
638 | encode_sv (&enc, scalar); |
567 | |
639 | |
568 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
640 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
569 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
641 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
570 | |
642 | |
571 | if (!(flags & (F_ASCII | F_LATIN1 | F_UTF8))) |
643 | if (!(enc.json.flags & (F_ASCII | F_LATIN1 | F_UTF8))) |
572 | SvUTF8_on (enc.sv); |
644 | SvUTF8_on (enc.sv); |
573 | |
645 | |
574 | if (enc.flags & F_SHRINK) |
646 | if (enc.json.flags & F_SHRINK) |
575 | shrink (enc.sv); |
647 | shrink (enc.sv); |
576 | |
648 | |
577 | return enc.sv; |
649 | return enc.sv; |
578 | } |
650 | } |
579 | |
651 | |
… | |
… | |
584 | typedef struct |
656 | typedef struct |
585 | { |
657 | { |
586 | char *cur; // current parser pointer |
658 | char *cur; // current parser pointer |
587 | char *end; // end of input string |
659 | char *end; // end of input string |
588 | const char *err; // parse error, if != 0 |
660 | const char *err; // parse error, if != 0 |
589 | U32 flags; // F_* |
661 | struct json json; |
590 | U32 depth; // recursion depth |
662 | U32 depth; // recursion depth |
591 | U32 maxdepth; // recursion depth limit |
663 | U32 maxdepth; // recursion depth limit |
592 | } dec_t; |
664 | } dec_t; |
593 | |
665 | |
594 | inline void |
666 | inline void |
… | |
… | |
625 | decode_4hex (dec_t *dec) |
697 | decode_4hex (dec_t *dec) |
626 | { |
698 | { |
627 | signed char d1, d2, d3, d4; |
699 | signed char d1, d2, d3, d4; |
628 | unsigned char *cur = (unsigned char *)dec->cur; |
700 | unsigned char *cur = (unsigned char *)dec->cur; |
629 | |
701 | |
630 | d1 = decode_hexdigit [cur [0]]; if (expect_false (d1 < 0)) ERR ("four hexadecimal digits expected"); |
702 | 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"); |
703 | 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"); |
704 | 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"); |
705 | d4 = decode_hexdigit [cur [3]]; if (expect_false (d4 < 0)) ERR ("exactly four hexadecimal digits expected"); |
634 | |
706 | |
635 | dec->cur += 4; |
707 | dec->cur += 4; |
636 | |
708 | |
637 | return ((UV)d1) << 12 |
709 | return ((UV)d1) << 12 |
638 | | ((UV)d2) << 8 |
710 | | ((UV)d2) << 8 |
… | |
… | |
646 | static SV * |
718 | static SV * |
647 | decode_str (dec_t *dec) |
719 | decode_str (dec_t *dec) |
648 | { |
720 | { |
649 | SV *sv = 0; |
721 | SV *sv = 0; |
650 | int utf8 = 0; |
722 | int utf8 = 0; |
|
|
723 | char *dec_cur = dec->cur; |
651 | |
724 | |
652 | do |
725 | do |
653 | { |
726 | { |
654 | char buf [SHORT_STRING_LEN + UTF8_MAXBYTES]; |
727 | char buf [SHORT_STRING_LEN + UTF8_MAXBYTES]; |
655 | char *cur = buf; |
728 | char *cur = buf; |
656 | |
729 | |
657 | do |
730 | do |
658 | { |
731 | { |
659 | unsigned char ch = *(unsigned char *)dec->cur++; |
732 | unsigned char ch = *(unsigned char *)dec_cur++; |
660 | |
733 | |
661 | if (expect_false (ch == '"')) |
734 | if (expect_false (ch == '"')) |
662 | { |
735 | { |
663 | --dec->cur; |
736 | --dec_cur; |
664 | break; |
737 | break; |
665 | } |
738 | } |
666 | else if (expect_false (ch == '\\')) |
739 | else if (expect_false (ch == '\\')) |
667 | { |
740 | { |
668 | switch (*dec->cur) |
741 | switch (*dec_cur) |
669 | { |
742 | { |
670 | case '\\': |
743 | case '\\': |
671 | case '/': |
744 | case '/': |
672 | case '"': *cur++ = *dec->cur++; break; |
745 | case '"': *cur++ = *dec_cur++; break; |
673 | |
746 | |
674 | case 'b': ++dec->cur; *cur++ = '\010'; break; |
747 | case 'b': ++dec_cur; *cur++ = '\010'; break; |
675 | case 't': ++dec->cur; *cur++ = '\011'; break; |
748 | case 't': ++dec_cur; *cur++ = '\011'; break; |
676 | case 'n': ++dec->cur; *cur++ = '\012'; break; |
749 | case 'n': ++dec_cur; *cur++ = '\012'; break; |
677 | case 'f': ++dec->cur; *cur++ = '\014'; break; |
750 | case 'f': ++dec_cur; *cur++ = '\014'; break; |
678 | case 'r': ++dec->cur; *cur++ = '\015'; break; |
751 | case 'r': ++dec_cur; *cur++ = '\015'; break; |
679 | |
752 | |
680 | case 'u': |
753 | case 'u': |
681 | { |
754 | { |
682 | UV lo, hi; |
755 | UV lo, hi; |
683 | ++dec->cur; |
756 | ++dec_cur; |
684 | |
757 | |
|
|
758 | dec->cur = dec_cur; |
685 | hi = decode_4hex (dec); |
759 | hi = decode_4hex (dec); |
|
|
760 | dec_cur = dec->cur; |
686 | if (hi == (UV)-1) |
761 | if (hi == (UV)-1) |
687 | goto fail; |
762 | goto fail; |
688 | |
763 | |
689 | // possibly a surrogate pair |
764 | // possibly a surrogate pair |
690 | if (hi >= 0xd800) |
765 | if (hi >= 0xd800) |
691 | if (hi < 0xdc00) |
766 | if (hi < 0xdc00) |
692 | { |
767 | { |
693 | if (dec->cur [0] != '\\' || dec->cur [1] != 'u') |
768 | if (dec_cur [0] != '\\' || dec_cur [1] != 'u') |
694 | ERR ("missing low surrogate character in surrogate pair"); |
769 | ERR ("missing low surrogate character in surrogate pair"); |
695 | |
770 | |
696 | dec->cur += 2; |
771 | dec_cur += 2; |
697 | |
772 | |
|
|
773 | dec->cur = dec_cur; |
698 | lo = decode_4hex (dec); |
774 | lo = decode_4hex (dec); |
|
|
775 | dec_cur = dec->cur; |
699 | if (lo == (UV)-1) |
776 | if (lo == (UV)-1) |
700 | goto fail; |
777 | goto fail; |
701 | |
778 | |
702 | if (lo < 0xdc00 || lo >= 0xe000) |
779 | if (lo < 0xdc00 || lo >= 0xe000) |
703 | ERR ("surrogate pair expected"); |
780 | ERR ("surrogate pair expected"); |
… | |
… | |
717 | *cur++ = hi; |
794 | *cur++ = hi; |
718 | } |
795 | } |
719 | break; |
796 | break; |
720 | |
797 | |
721 | default: |
798 | default: |
722 | --dec->cur; |
799 | --dec_cur; |
723 | ERR ("illegal backslash escape sequence in string"); |
800 | ERR ("illegal backslash escape sequence in string"); |
724 | } |
801 | } |
725 | } |
802 | } |
726 | else if (expect_true (ch >= 0x20 && ch <= 0x7f)) |
803 | else if (expect_true (ch >= 0x20 && ch <= 0x7f)) |
727 | *cur++ = ch; |
804 | *cur++ = ch; |
728 | else if (ch >= 0x80) |
805 | else if (ch >= 0x80) |
729 | { |
806 | { |
730 | STRLEN clen; |
807 | STRLEN clen; |
731 | UV uch; |
808 | UV uch; |
732 | |
809 | |
733 | --dec->cur; |
810 | --dec_cur; |
734 | |
811 | |
735 | uch = decode_utf8 (dec->cur, dec->end - dec->cur, &clen); |
812 | uch = decode_utf8 (dec_cur, dec->end - dec_cur, &clen); |
736 | if (clen == (STRLEN)-1) |
813 | if (clen == (STRLEN)-1) |
737 | ERR ("malformed UTF-8 character in JSON string"); |
814 | ERR ("malformed UTF-8 character in JSON string"); |
738 | |
815 | |
739 | do |
816 | do |
740 | *cur++ = *dec->cur++; |
817 | *cur++ = *dec_cur++; |
741 | while (--clen); |
818 | while (--clen); |
742 | |
819 | |
743 | utf8 = 1; |
820 | utf8 = 1; |
744 | } |
821 | } |
745 | else |
822 | else |
746 | { |
823 | { |
747 | --dec->cur; |
824 | --dec_cur; |
748 | |
825 | |
749 | if (!ch) |
826 | if (!ch) |
750 | ERR ("unexpected end of string while parsing JSON string"); |
827 | ERR ("unexpected end of string while parsing JSON string"); |
751 | else |
828 | else |
752 | ERR ("invalid character encountered while parsing JSON string"); |
829 | ERR ("invalid character encountered while parsing JSON string"); |
… | |
… | |
765 | } |
842 | } |
766 | else |
843 | else |
767 | sv = newSVpvn (buf, len); |
844 | sv = newSVpvn (buf, len); |
768 | } |
845 | } |
769 | } |
846 | } |
770 | while (*dec->cur != '"'); |
847 | while (*dec_cur != '"'); |
771 | |
848 | |
772 | ++dec->cur; |
849 | ++dec_cur; |
773 | |
850 | |
774 | if (sv) |
851 | if (sv) |
775 | { |
852 | { |
776 | SvPOK_only (sv); |
853 | SvPOK_only (sv); |
777 | *SvEND (sv) = 0; |
854 | *SvEND (sv) = 0; |
… | |
… | |
780 | SvUTF8_on (sv); |
857 | SvUTF8_on (sv); |
781 | } |
858 | } |
782 | else |
859 | else |
783 | sv = newSVpvn ("", 0); |
860 | sv = newSVpvn ("", 0); |
784 | |
861 | |
|
|
862 | dec->cur = dec_cur; |
785 | return sv; |
863 | return sv; |
786 | |
864 | |
787 | fail: |
865 | fail: |
|
|
866 | dec->cur = dec_cur; |
788 | return 0; |
867 | return 0; |
789 | } |
868 | } |
790 | |
869 | |
791 | static SV * |
870 | static SV * |
792 | decode_num (dec_t *dec) |
871 | decode_num (dec_t *dec) |
… | |
… | |
854 | { |
933 | { |
855 | // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so |
934 | // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so |
856 | if (*start == '-') |
935 | if (*start == '-') |
857 | switch (dec->cur - start) |
936 | switch (dec->cur - start) |
858 | { |
937 | { |
859 | case 2: return newSViv (-( start [1] - '0' )); |
938 | case 2: return newSViv (-( start [1] - '0' * 1)); |
860 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
939 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
861 | case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); |
940 | case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); |
862 | case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
941 | case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
863 | } |
942 | } |
864 | else |
943 | else |
865 | switch (dec->cur - start) |
944 | switch (dec->cur - start) |
866 | { |
945 | { |
867 | case 1: return newSViv ( start [0] - '0' ); |
946 | case 1: return newSViv ( start [0] - '0' * 1); |
868 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
947 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
869 | case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); |
948 | case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); |
870 | case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
949 | case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
871 | } |
950 | } |
872 | |
951 | |
… | |
… | |
879 | if (uv < (UV)IV_MIN) |
958 | if (uv < (UV)IV_MIN) |
880 | return newSViv (-(IV)uv); |
959 | return newSViv (-(IV)uv); |
881 | } |
960 | } |
882 | else |
961 | else |
883 | return newSVuv (uv); |
962 | return newSVuv (uv); |
|
|
963 | |
|
|
964 | // here would likely be the place for bigint support |
884 | } |
965 | } |
885 | } |
966 | } |
886 | |
967 | |
|
|
968 | // if we ever support bigint or bigfloat, this is the place for bigfloat |
887 | return newSVnv (Atof (start)); |
969 | return newSVnv (Atof (start)); |
888 | |
970 | |
889 | fail: |
971 | fail: |
890 | return 0; |
972 | return 0; |
891 | } |
973 | } |
… | |
… | |
945 | if (*dec->cur == '}') |
1027 | if (*dec->cur == '}') |
946 | ++dec->cur; |
1028 | ++dec->cur; |
947 | else |
1029 | else |
948 | for (;;) |
1030 | for (;;) |
949 | { |
1031 | { |
950 | SV *key, *value; |
|
|
951 | |
|
|
952 | decode_ws (dec); EXPECT_CH ('"'); |
1032 | decode_ws (dec); EXPECT_CH ('"'); |
953 | |
1033 | |
954 | key = decode_str (dec); |
1034 | // heuristic: assume that |
955 | if (!key) |
1035 | // a) decode_str + hv_store_ent are abysmally slow. |
956 | goto fail; |
1036 | // b) most hash keys are short, simple ascii text. |
|
|
1037 | // => try to "fast-match" such strings to avoid |
|
|
1038 | // the overhead of decode_str + hv_store_ent. |
|
|
1039 | { |
|
|
1040 | SV *value; |
|
|
1041 | char *p = dec->cur; |
|
|
1042 | char *e = p + 24; // only try up to 24 bytes |
957 | |
1043 | |
958 | decode_ws (dec); EXPECT_CH (':'); |
1044 | for (;;) |
959 | |
|
|
960 | value = decode_sv (dec); |
|
|
961 | if (!value) |
|
|
962 | { |
1045 | { |
|
|
1046 | // the >= 0x80 is true on most architectures |
|
|
1047 | if (p == e || *p < 0x20 || *p >= 0x80 || *p == '\\') |
|
|
1048 | { |
|
|
1049 | // slow path, back up and use decode_str |
|
|
1050 | SV *key = decode_str (dec); |
|
|
1051 | if (!key) |
|
|
1052 | goto fail; |
|
|
1053 | |
|
|
1054 | decode_ws (dec); EXPECT_CH (':'); |
|
|
1055 | |
|
|
1056 | value = decode_sv (dec); |
|
|
1057 | if (!value) |
|
|
1058 | { |
|
|
1059 | SvREFCNT_dec (key); |
|
|
1060 | goto fail; |
|
|
1061 | } |
|
|
1062 | |
|
|
1063 | hv_store_ent (hv, key, value, 0); |
963 | SvREFCNT_dec (key); |
1064 | SvREFCNT_dec (key); |
|
|
1065 | |
|
|
1066 | break; |
|
|
1067 | } |
|
|
1068 | else if (*p == '"') |
|
|
1069 | { |
|
|
1070 | // fast path, got a simple key |
|
|
1071 | char *key = dec->cur; |
|
|
1072 | int len = p - key; |
|
|
1073 | dec->cur = p + 1; |
|
|
1074 | |
|
|
1075 | decode_ws (dec); EXPECT_CH (':'); |
|
|
1076 | |
|
|
1077 | value = decode_sv (dec); |
|
|
1078 | if (!value) |
964 | goto fail; |
1079 | goto fail; |
|
|
1080 | |
|
|
1081 | hv_store (hv, key, len, value, 0); |
|
|
1082 | |
|
|
1083 | break; |
|
|
1084 | } |
|
|
1085 | |
|
|
1086 | ++p; |
965 | } |
1087 | } |
966 | |
1088 | } |
967 | hv_store_ent (hv, key, value, 0); |
|
|
968 | SvREFCNT_dec (key); |
|
|
969 | |
1089 | |
970 | decode_ws (dec); |
1090 | decode_ws (dec); |
971 | |
1091 | |
972 | if (*dec->cur == '}') |
1092 | if (*dec->cur == '}') |
973 | { |
1093 | { |
… | |
… | |
992 | |
1112 | |
993 | static SV * |
1113 | static SV * |
994 | decode_sv (dec_t *dec) |
1114 | decode_sv (dec_t *dec) |
995 | { |
1115 | { |
996 | decode_ws (dec); |
1116 | decode_ws (dec); |
|
|
1117 | |
|
|
1118 | // the beauty of JSON: you need exactly one character lookahead |
|
|
1119 | // to parse anything. |
997 | switch (*dec->cur) |
1120 | switch (*dec->cur) |
998 | { |
1121 | { |
999 | case '"': ++dec->cur; return decode_str (dec); |
1122 | case '"': ++dec->cur; return decode_str (dec); |
1000 | case '[': ++dec->cur; return decode_av (dec); |
1123 | case '[': ++dec->cur; return decode_av (dec); |
1001 | case '{': ++dec->cur; return decode_hv (dec); |
1124 | case '{': ++dec->cur; return decode_hv (dec); |
… | |
… | |
1007 | |
1130 | |
1008 | case 't': |
1131 | case 't': |
1009 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) |
1132 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) |
1010 | { |
1133 | { |
1011 | dec->cur += 4; |
1134 | dec->cur += 4; |
1012 | return newSViv (1); |
1135 | return SvREFCNT_inc (json_true); |
1013 | } |
1136 | } |
1014 | else |
1137 | else |
1015 | ERR ("'true' expected"); |
1138 | ERR ("'true' expected"); |
1016 | |
1139 | |
1017 | break; |
1140 | break; |
1018 | |
1141 | |
1019 | case 'f': |
1142 | case 'f': |
1020 | if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) |
1143 | if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) |
1021 | { |
1144 | { |
1022 | dec->cur += 5; |
1145 | dec->cur += 5; |
1023 | return newSViv (0); |
1146 | return SvREFCNT_inc (json_false); |
1024 | } |
1147 | } |
1025 | else |
1148 | else |
1026 | ERR ("'false' expected"); |
1149 | ERR ("'false' expected"); |
1027 | |
1150 | |
1028 | break; |
1151 | break; |
… | |
… | |
1046 | fail: |
1169 | fail: |
1047 | return 0; |
1170 | return 0; |
1048 | } |
1171 | } |
1049 | |
1172 | |
1050 | static SV * |
1173 | static SV * |
1051 | decode_json (SV *string, U32 flags, UV *offset_return) |
1174 | decode_json (SV *string, struct json *json, UV *offset_return) |
1052 | { |
1175 | { |
1053 | dec_t dec; |
1176 | dec_t dec; |
1054 | UV offset; |
1177 | UV offset; |
1055 | SV *sv; |
1178 | SV *sv; |
1056 | |
1179 | |
1057 | SvGETMAGIC (string); |
1180 | SvGETMAGIC (string); |
1058 | SvUPGRADE (string, SVt_PV); |
1181 | SvUPGRADE (string, SVt_PV); |
1059 | |
1182 | |
|
|
1183 | if (json->flags & F_MAXSIZE && SvCUR (string) > DEC_SIZE (json->flags)) |
|
|
1184 | croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", |
|
|
1185 | (unsigned long)SvCUR (string), (unsigned long)DEC_SIZE (json->flags)); |
|
|
1186 | |
1060 | if (flags & F_UTF8) |
1187 | if (json->flags & F_UTF8) |
1061 | sv_utf8_downgrade (string, 0); |
1188 | sv_utf8_downgrade (string, 0); |
1062 | else |
1189 | else |
1063 | sv_utf8_upgrade (string); |
1190 | sv_utf8_upgrade (string); |
1064 | |
1191 | |
1065 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
1192 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
1066 | |
1193 | |
1067 | dec.flags = flags; |
1194 | dec.json = *json; |
1068 | dec.cur = SvPVX (string); |
1195 | dec.cur = SvPVX (string); |
1069 | dec.end = SvEND (string); |
1196 | dec.end = SvEND (string); |
1070 | dec.err = 0; |
1197 | dec.err = 0; |
1071 | dec.depth = 0; |
1198 | dec.depth = 0; |
1072 | dec.maxdepth = DEC_DEPTH (dec.flags); |
1199 | dec.maxdepth = DEC_DEPTH (dec.json.flags); |
1073 | |
1200 | |
1074 | *dec.end = 0; // this should basically be a nop, too, but make sure it's there |
1201 | *dec.end = 0; // this should basically be a nop, too, but make sure it's there |
1075 | sv = decode_sv (&dec); |
1202 | sv = decode_sv (&dec); |
1076 | |
1203 | |
1077 | if (!(offset_return || !sv)) |
1204 | if (!(offset_return || !sv)) |
… | |
… | |
1087 | } |
1214 | } |
1088 | } |
1215 | } |
1089 | |
1216 | |
1090 | if (offset_return || !sv) |
1217 | if (offset_return || !sv) |
1091 | { |
1218 | { |
1092 | offset = dec.flags & F_UTF8 |
1219 | offset = dec.json.flags & F_UTF8 |
1093 | ? dec.cur - SvPVX (string) |
1220 | ? dec.cur - SvPVX (string) |
1094 | : utf8_distance (dec.cur, SvPVX (string)); |
1221 | : utf8_distance (dec.cur, SvPVX (string)); |
1095 | |
1222 | |
1096 | if (offset_return) |
1223 | if (offset_return) |
1097 | *offset_return = offset; |
1224 | *offset_return = offset; |
… | |
… | |
1116 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1243 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1117 | } |
1244 | } |
1118 | |
1245 | |
1119 | sv = sv_2mortal (sv); |
1246 | sv = sv_2mortal (sv); |
1120 | |
1247 | |
1121 | if (!(dec.flags & F_ALLOW_NONREF) && !SvROK (sv)) |
1248 | if (!(dec.json.flags & F_ALLOW_NONREF) && !SvROK (sv)) |
1122 | croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)"); |
1249 | croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)"); |
1123 | |
1250 | |
1124 | return sv; |
1251 | return sv; |
1125 | } |
1252 | } |
1126 | |
1253 | |
… | |
… | |
1130 | MODULE = JSON::XS PACKAGE = JSON::XS |
1257 | MODULE = JSON::XS PACKAGE = JSON::XS |
1131 | |
1258 | |
1132 | BOOT: |
1259 | BOOT: |
1133 | { |
1260 | { |
1134 | int i; |
1261 | int i; |
1135 | |
|
|
1136 | memset (decode_hexdigit, 0xff, 256); |
|
|
1137 | |
1262 | |
1138 | for (i = 0; i < 256; ++i) |
1263 | for (i = 0; i < 256; ++i) |
1139 | decode_hexdigit [i] = |
1264 | decode_hexdigit [i] = |
1140 | i >= '0' && i <= '9' ? i - '0' |
1265 | i >= '0' && i <= '9' ? i - '0' |
1141 | : i >= 'a' && i <= 'f' ? i - 'a' + 10 |
1266 | : i >= 'a' && i <= 'f' ? i - 'a' + 10 |
1142 | : i >= 'A' && i <= 'F' ? i - 'A' + 10 |
1267 | : i >= 'A' && i <= 'F' ? i - 'A' + 10 |
1143 | : -1; |
1268 | : -1; |
1144 | |
1269 | |
1145 | json_stash = gv_stashpv ("JSON::XS", 1); |
1270 | json_stash = gv_stashpv ("JSON::XS" , 1); |
|
|
1271 | json_boolean_stash = gv_stashpv ("JSON::XS::Boolean", 1); |
|
|
1272 | |
|
|
1273 | json_true = get_sv ("JSON::XS::true" , 1); SvREADONLY_on (json_true ); |
|
|
1274 | json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false); |
1146 | } |
1275 | } |
1147 | |
1276 | |
1148 | PROTOTYPES: DISABLE |
1277 | PROTOTYPES: DISABLE |
1149 | |
1278 | |
1150 | SV *new (char *dummy) |
1279 | SV *new (char *dummy) |
… | |
… | |
1153 | OUTPUT: |
1282 | OUTPUT: |
1154 | RETVAL |
1283 | RETVAL |
1155 | |
1284 | |
1156 | SV *ascii (SV *self, int enable = 1) |
1285 | SV *ascii (SV *self, int enable = 1) |
1157 | ALIAS: |
1286 | ALIAS: |
1158 | ascii = F_ASCII |
1287 | ascii = F_ASCII |
1159 | latin1 = F_LATIN1 |
1288 | latin1 = F_LATIN1 |
1160 | utf8 = F_UTF8 |
1289 | utf8 = F_UTF8 |
1161 | indent = F_INDENT |
1290 | indent = F_INDENT |
1162 | canonical = F_CANONICAL |
1291 | canonical = F_CANONICAL |
1163 | space_before = F_SPACE_BEFORE |
1292 | space_before = F_SPACE_BEFORE |
1164 | space_after = F_SPACE_AFTER |
1293 | space_after = F_SPACE_AFTER |
1165 | pretty = F_PRETTY |
1294 | pretty = F_PRETTY |
1166 | allow_nonref = F_ALLOW_NONREF |
1295 | allow_nonref = F_ALLOW_NONREF |
1167 | shrink = F_SHRINK |
1296 | shrink = F_SHRINK |
|
|
1297 | allow_blessed = F_ALLOW_BLESSED |
|
|
1298 | convert_blessed = F_CONV_BLESSED |
1168 | CODE: |
1299 | CODE: |
1169 | { |
1300 | { |
1170 | UV *uv = SvJSON (self); |
1301 | UV *uv = SvJSON (self); |
1171 | if (enable) |
1302 | if (enable) |
1172 | *uv |= ix; |
1303 | *uv |= ix; |
… | |
… | |
1194 | RETVAL = newSVsv (self); |
1325 | RETVAL = newSVsv (self); |
1195 | } |
1326 | } |
1196 | OUTPUT: |
1327 | OUTPUT: |
1197 | RETVAL |
1328 | RETVAL |
1198 | |
1329 | |
|
|
1330 | SV *max_size (SV *self, UV max_size = 0) |
|
|
1331 | CODE: |
|
|
1332 | { |
|
|
1333 | UV *uv = SvJSON (self); |
|
|
1334 | UV log2 = 0; |
|
|
1335 | |
|
|
1336 | if (max_size > 0x80000000UL) max_size = 0x80000000UL; |
|
|
1337 | if (max_size == 1) max_size = 2; |
|
|
1338 | |
|
|
1339 | while ((1UL << log2) < max_size) |
|
|
1340 | ++log2; |
|
|
1341 | |
|
|
1342 | *uv = *uv & ~F_MAXSIZE | (log2 << S_MAXSIZE); |
|
|
1343 | |
|
|
1344 | RETVAL = newSVsv (self); |
|
|
1345 | } |
|
|
1346 | OUTPUT: |
|
|
1347 | RETVAL |
|
|
1348 | |
1199 | void encode (SV *self, SV *scalar) |
1349 | void encode (SV *self, SV *scalar) |
1200 | PPCODE: |
1350 | PPCODE: |
|
|
1351 | { |
|
|
1352 | struct json json = { *SvJSON (self) }; |
1201 | XPUSHs (encode_json (scalar, *SvJSON (self))); |
1353 | XPUSHs (encode_json (scalar, &json)); |
|
|
1354 | } |
1202 | |
1355 | |
1203 | void decode (SV *self, SV *jsonstr) |
1356 | void decode (SV *self, SV *jsonstr) |
1204 | PPCODE: |
1357 | PPCODE: |
|
|
1358 | { |
|
|
1359 | struct json json = { *SvJSON (self) }; |
1205 | XPUSHs (decode_json (jsonstr, *SvJSON (self), 0)); |
1360 | XPUSHs (decode_json (jsonstr, &json, 0)); |
|
|
1361 | } |
1206 | |
1362 | |
1207 | void decode_prefix (SV *self, SV *jsonstr) |
1363 | void decode_prefix (SV *self, SV *jsonstr) |
1208 | PPCODE: |
1364 | PPCODE: |
1209 | { |
1365 | { |
1210 | UV offset; |
1366 | UV offset; |
|
|
1367 | struct json json = { *SvJSON (self) }; |
1211 | EXTEND (SP, 2); |
1368 | EXTEND (SP, 2); |
1212 | PUSHs (decode_json (jsonstr, *SvJSON (self), &offset)); |
1369 | PUSHs (decode_json (jsonstr, &json, &offset)); |
1213 | PUSHs (sv_2mortal (newSVuv (offset))); |
1370 | PUSHs (sv_2mortal (newSVuv (offset))); |
1214 | } |
1371 | } |
1215 | |
1372 | |
1216 | PROTOTYPES: ENABLE |
1373 | PROTOTYPES: ENABLE |
1217 | |
1374 | |
1218 | void to_json (SV *scalar) |
1375 | void to_json (SV *scalar) |
1219 | ALIAS: |
|
|
1220 | objToJson = 0 |
|
|
1221 | PPCODE: |
1376 | PPCODE: |
|
|
1377 | { |
|
|
1378 | struct json json = { F_DEFAULT | F_UTF8 }; |
1222 | XPUSHs (encode_json (scalar, F_DEFAULT | F_UTF8)); |
1379 | XPUSHs (encode_json (scalar, &json)); |
|
|
1380 | } |
1223 | |
1381 | |
1224 | void from_json (SV *jsonstr) |
1382 | void from_json (SV *jsonstr) |
1225 | ALIAS: |
|
|
1226 | jsonToObj = 0 |
|
|
1227 | PPCODE: |
1383 | PPCODE: |
|
|
1384 | { |
|
|
1385 | struct json json = { F_DEFAULT | F_UTF8 }; |
1228 | XPUSHs (decode_json (jsonstr, F_DEFAULT | F_UTF8, 0)); |
1386 | XPUSHs (decode_json (jsonstr, &json, 0)); |
|
|
1387 | } |
1229 | |
1388 | |