… | |
… | |
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 | |
9 | #define F_ASCII 0x00000001 |
14 | #define F_ASCII 0x00000001UL |
|
|
15 | #define F_LATIN1 0x00000002UL |
10 | #define F_UTF8 0x00000002 |
16 | #define F_UTF8 0x00000004UL |
11 | #define F_INDENT 0x00000004 |
17 | #define F_INDENT 0x00000008UL |
12 | #define F_CANONICAL 0x00000008 |
18 | #define F_CANONICAL 0x00000010UL |
13 | #define F_SPACE_BEFORE 0x00000010 |
19 | #define F_SPACE_BEFORE 0x00000020UL |
14 | #define F_SPACE_AFTER 0x00000020 |
20 | #define F_SPACE_AFTER 0x00000040UL |
15 | #define F_ALLOW_NONREF 0x00000080 |
21 | #define F_ALLOW_NONREF 0x00000100UL |
16 | #define F_SHRINK 0x00000100 |
22 | #define F_SHRINK 0x00000200UL |
|
|
23 | #define F_MAXDEPTH 0xf8000000UL |
|
|
24 | #define S_MAXDEPTH 27 |
17 | |
25 | |
18 | // F_SKIPINVALID? |
26 | #define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) |
19 | // F_EXECCODEREF? |
27 | |
20 | // F_SELFCONVERT? <=> { &__class__ => } |
28 | // F_SELFCONVERT? <=> to_json/toJson |
|
|
29 | // F_BLESSED? <=> { $__class__$ => } |
21 | |
30 | |
22 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
31 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
23 | #define F_DEFAULT 0 |
32 | #define F_DEFAULT (9UL << S_MAXDEPTH) |
24 | |
33 | |
25 | #define INIT_SIZE 32 // initial scalar size to be allocated |
34 | #define INIT_SIZE 32 // initial scalar size to be allocated |
26 | #define INDENT_STEP 3 // spaces per indentation level |
35 | #define INDENT_STEP 3 // spaces per indentation level |
27 | |
36 | |
28 | #define UTF8_MAX_LEN 11 // for perls UTF-X: max. number of octets per character |
|
|
29 | #define SHORT_STRING_LEN 512 // special-case strings of up to this size |
37 | #define SHORT_STRING_LEN 512 // special-case strings of up to this size |
30 | |
38 | |
31 | #define SB do { |
39 | #define SB do { |
32 | #define SE } while (0) |
40 | #define SE } while (0) |
33 | |
41 | |
… | |
… | |
73 | { |
81 | { |
74 | *clen = 2; |
82 | *clen = 2; |
75 | return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); |
83 | return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); |
76 | } |
84 | } |
77 | else |
85 | else |
|
|
86 | { |
|
|
87 | *clen = (STRLEN)-1; |
78 | return (UV)-1; |
88 | return (UV)-1; |
|
|
89 | } |
79 | } |
90 | } |
80 | |
91 | |
81 | ///////////////////////////////////////////////////////////////////////////// |
92 | ///////////////////////////////////////////////////////////////////////////// |
82 | // encoder |
93 | // encoder |
83 | |
94 | |
… | |
… | |
85 | typedef struct |
96 | typedef struct |
86 | { |
97 | { |
87 | char *cur; // SvPVX (sv) + current output position |
98 | char *cur; // SvPVX (sv) + current output position |
88 | char *end; // SvEND (sv) |
99 | char *end; // SvEND (sv) |
89 | SV *sv; // result scalar |
100 | SV *sv; // result scalar |
90 | UV flags; // F_* |
101 | U32 flags; // F_* |
91 | int indent; // indentation level |
102 | U32 indent; // indentation level |
92 | int max_depth; // max. recursion level |
103 | U32 maxdepth; // max. indentation/recursion level |
93 | } enc_t; |
104 | } enc_t; |
94 | |
105 | |
95 | static void |
106 | static void |
96 | need (enc_t *enc, STRLEN len) |
107 | need (enc_t *enc, STRLEN len) |
97 | { |
108 | { |
98 | if (enc->cur + len >= enc->end) |
109 | if (enc->cur + len >= enc->end) |
99 | { |
110 | { |
100 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
111 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
101 | SvGROW (enc->sv, cur + len + 1); |
112 | SvGROW (enc->sv, cur + len + 1); |
102 | enc->cur = SvPVX (enc->sv) + cur; |
113 | enc->cur = SvPVX (enc->sv) + cur; |
103 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv); |
114 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
104 | } |
115 | } |
105 | } |
116 | } |
106 | |
117 | |
107 | static void |
118 | static void |
108 | encode_ch (enc_t *enc, char ch) |
119 | encode_ch (enc_t *enc, char ch) |
… | |
… | |
170 | } |
181 | } |
171 | |
182 | |
172 | if (uch > 0x10FFFFUL) |
183 | if (uch > 0x10FFFFUL) |
173 | croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
184 | croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
174 | |
185 | |
175 | if (uch < 0x80 || enc->flags & F_ASCII) |
186 | if (uch < 0x80 || enc->flags & F_ASCII || (enc->flags & F_LATIN1 && uch > 0xFF)) |
176 | { |
187 | { |
177 | if (uch > 0xFFFFUL) |
188 | if (uch > 0xFFFFUL) |
178 | { |
189 | { |
179 | need (enc, len += 11); |
190 | need (enc, len += 11); |
180 | sprintf (enc->cur, "\\u%04x\\u%04x", |
191 | sprintf (enc->cur, "\\u%04x\\u%04x", |
… | |
… | |
194 | *enc->cur++ = hexdigit [(uch >> 0) & 15]; |
205 | *enc->cur++ = hexdigit [(uch >> 0) & 15]; |
195 | } |
206 | } |
196 | |
207 | |
197 | str += clen; |
208 | str += clen; |
198 | } |
209 | } |
|
|
210 | else if (enc->flags & F_LATIN1) |
|
|
211 | { |
|
|
212 | *enc->cur++ = uch; |
|
|
213 | str += clen; |
|
|
214 | } |
199 | else if (is_utf8) |
215 | else if (is_utf8) |
200 | { |
216 | { |
201 | need (enc, len += clen); |
217 | need (enc, len += clen); |
202 | do |
218 | do |
203 | { |
219 | { |
… | |
… | |
205 | } |
221 | } |
206 | while (--clen); |
222 | while (--clen); |
207 | } |
223 | } |
208 | else |
224 | else |
209 | { |
225 | { |
210 | need (enc, len += UTF8_MAX_LEN - 1); // never more than 11 bytes needed |
226 | need (enc, len += UTF8_MAXBYTES - 1); // never more than 11 bytes needed |
211 | enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
227 | enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
212 | ++str; |
228 | ++str; |
213 | } |
229 | } |
214 | } |
230 | } |
215 | } |
231 | } |
… | |
… | |
265 | static void |
281 | static void |
266 | encode_av (enc_t *enc, AV *av) |
282 | encode_av (enc_t *enc, AV *av) |
267 | { |
283 | { |
268 | int i, len = av_len (av); |
284 | int i, len = av_len (av); |
269 | |
285 | |
|
|
286 | if (enc->indent >= enc->maxdepth) |
|
|
287 | croak ("data structure too deep (hit recursion limit)"); |
|
|
288 | |
270 | encode_ch (enc, '['); encode_nl (enc); |
289 | encode_ch (enc, '['); encode_nl (enc); |
271 | ++enc->indent; |
290 | ++enc->indent; |
272 | |
291 | |
273 | for (i = 0; i <= len; ++i) |
292 | for (i = 0; i <= len; ++i) |
274 | { |
293 | { |
… | |
… | |
339 | |
358 | |
340 | static void |
359 | static void |
341 | encode_hv (enc_t *enc, HV *hv) |
360 | encode_hv (enc_t *enc, HV *hv) |
342 | { |
361 | { |
343 | int count, i; |
362 | int count, i; |
|
|
363 | |
|
|
364 | if (enc->indent >= enc->maxdepth) |
|
|
365 | croak ("data structure too deep (hit recursion limit)"); |
344 | |
366 | |
345 | encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; |
367 | encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; |
346 | |
368 | |
347 | if ((count = hv_iterinit (hv))) |
369 | if ((count = hv_iterinit (hv))) |
348 | { |
370 | { |
… | |
… | |
396 | |
418 | |
397 | encode_nl (enc); |
419 | encode_nl (enc); |
398 | } |
420 | } |
399 | else |
421 | else |
400 | { |
422 | { |
401 | SV *sv; |
|
|
402 | HE *he = hv_iternext (hv); |
423 | HE *he = hv_iternext (hv); |
403 | |
424 | |
404 | for (;;) |
425 | for (;;) |
405 | { |
426 | { |
406 | encode_indent (enc); |
427 | encode_indent (enc); |
… | |
… | |
415 | encode_nl (enc); |
436 | encode_nl (enc); |
416 | } |
437 | } |
417 | } |
438 | } |
418 | |
439 | |
419 | --enc->indent; encode_indent (enc); encode_ch (enc, '}'); |
440 | --enc->indent; encode_indent (enc); encode_ch (enc, '}'); |
|
|
441 | } |
|
|
442 | |
|
|
443 | // encode objects, arrays and special \0=false and \1=true values. |
|
|
444 | static void |
|
|
445 | encode_rv (enc_t *enc, SV *sv) |
|
|
446 | { |
|
|
447 | svtype svt; |
|
|
448 | |
|
|
449 | SvGETMAGIC (sv); |
|
|
450 | svt = SvTYPE (sv); |
|
|
451 | |
|
|
452 | if (svt == SVt_PVHV) |
|
|
453 | encode_hv (enc, (HV *)sv); |
|
|
454 | else if (svt == SVt_PVAV) |
|
|
455 | encode_av (enc, (AV *)sv); |
|
|
456 | else if (svt < SVt_PVAV) |
|
|
457 | { |
|
|
458 | if (SvNIOK (sv) && SvIV (sv) == 0) |
|
|
459 | encode_str (enc, "false", 5, 0); |
|
|
460 | else if (SvNIOK (sv) && SvIV (sv) == 1) |
|
|
461 | encode_str (enc, "true", 4, 0); |
|
|
462 | else |
|
|
463 | croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", |
|
|
464 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
|
|
465 | } |
|
|
466 | else |
|
|
467 | croak ("encountered %s, but JSON can only represent references to arrays or hashes", |
|
|
468 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
420 | } |
469 | } |
421 | |
470 | |
422 | static void |
471 | static void |
423 | encode_sv (enc_t *enc, SV *sv) |
472 | encode_sv (enc_t *enc, SV *sv) |
424 | { |
473 | { |
… | |
… | |
445 | SvIsUV(sv) |
494 | SvIsUV(sv) |
446 | ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv)) |
495 | ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv)) |
447 | : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv)); |
496 | : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv)); |
448 | } |
497 | } |
449 | else if (SvROK (sv)) |
498 | else if (SvROK (sv)) |
450 | { |
499 | encode_rv (enc, SvRV (sv)); |
451 | SV *rv = SvRV (sv); |
|
|
452 | |
|
|
453 | if (enc->indent >= enc->max_depth) |
|
|
454 | croak ("data structure too deep (hit recursion limit)"); |
|
|
455 | |
|
|
456 | switch (SvTYPE (rv)) |
|
|
457 | { |
|
|
458 | case SVt_PVAV: encode_av (enc, (AV *)rv); break; |
|
|
459 | case SVt_PVHV: encode_hv (enc, (HV *)rv); break; |
|
|
460 | |
|
|
461 | default: |
|
|
462 | croak ("encountered %s, but JSON can only represent references to arrays or hashes", |
|
|
463 | SvPV_nolen (sv)); |
|
|
464 | } |
|
|
465 | } |
|
|
466 | else if (!SvOK (sv)) |
500 | else if (!SvOK (sv)) |
467 | encode_str (enc, "null", 4, 0); |
501 | encode_str (enc, "null", 4, 0); |
468 | else |
502 | else |
469 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
503 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
470 | SvPV_nolen (sv), SvFLAGS (sv)); |
504 | SvPV_nolen (sv), SvFLAGS (sv)); |
471 | } |
505 | } |
472 | |
506 | |
473 | static SV * |
507 | static SV * |
474 | encode_json (SV *scalar, UV flags) |
508 | encode_json (SV *scalar, U32 flags) |
475 | { |
509 | { |
|
|
510 | enc_t enc; |
|
|
511 | |
476 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
512 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
477 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
513 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
478 | |
514 | |
479 | enc_t enc; |
|
|
480 | enc.flags = flags; |
515 | enc.flags = flags; |
481 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
516 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
482 | enc.cur = SvPVX (enc.sv); |
517 | enc.cur = SvPVX (enc.sv); |
483 | enc.end = SvEND (enc.sv); |
518 | enc.end = SvEND (enc.sv); |
484 | enc.indent = 0; |
519 | enc.indent = 0; |
485 | enc.max_depth = 0x7fffffffUL; |
520 | enc.maxdepth = DEC_DEPTH (flags); |
486 | |
521 | |
487 | SvPOK_only (enc.sv); |
522 | SvPOK_only (enc.sv); |
488 | encode_sv (&enc, scalar); |
523 | encode_sv (&enc, scalar); |
489 | |
524 | |
|
|
525 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
|
|
526 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
|
|
527 | |
490 | if (!(flags & (F_ASCII | F_UTF8))) |
528 | if (!(flags & (F_ASCII | F_LATIN1 | F_UTF8))) |
491 | SvUTF8_on (enc.sv); |
529 | SvUTF8_on (enc.sv); |
492 | |
|
|
493 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
|
|
494 | |
530 | |
495 | if (enc.flags & F_SHRINK) |
531 | if (enc.flags & F_SHRINK) |
496 | shrink (enc.sv); |
532 | shrink (enc.sv); |
497 | |
533 | |
498 | return enc.sv; |
534 | return enc.sv; |
… | |
… | |
505 | typedef struct |
541 | typedef struct |
506 | { |
542 | { |
507 | char *cur; // current parser pointer |
543 | char *cur; // current parser pointer |
508 | char *end; // end of input string |
544 | char *end; // end of input string |
509 | const char *err; // parse error, if != 0 |
545 | const char *err; // parse error, if != 0 |
510 | UV flags; // F_* |
546 | U32 flags; // F_* |
|
|
547 | U32 depth; // recursion depth |
|
|
548 | U32 maxdepth; // recursion depth limit |
511 | } dec_t; |
549 | } dec_t; |
512 | |
550 | |
513 | static void |
551 | static void |
514 | decode_ws (dec_t *dec) |
552 | decode_ws (dec_t *dec) |
515 | { |
553 | { |
… | |
… | |
524 | ++dec->cur; |
562 | ++dec->cur; |
525 | } |
563 | } |
526 | } |
564 | } |
527 | |
565 | |
528 | #define ERR(reason) SB dec->err = reason; goto fail; SE |
566 | #define ERR(reason) SB dec->err = reason; goto fail; SE |
|
|
567 | |
529 | #define EXPECT_CH(ch) SB \ |
568 | #define EXPECT_CH(ch) SB \ |
530 | if (*dec->cur != ch) \ |
569 | if (*dec->cur != ch) \ |
531 | ERR (# ch " expected"); \ |
570 | ERR (# ch " expected"); \ |
532 | ++dec->cur; \ |
571 | ++dec->cur; \ |
533 | SE |
572 | SE |
534 | |
573 | |
|
|
574 | #define DEC_INC_DEPTH if (++dec->depth > dec->maxdepth) ERR ("json datastructure exceeds maximum nesting level (set a higher max_depth)") |
|
|
575 | #define DEC_DEC_DEPTH --dec->depth |
|
|
576 | |
535 | static SV *decode_sv (dec_t *dec); |
577 | static SV *decode_sv (dec_t *dec); |
536 | |
578 | |
537 | static signed char decode_hexdigit[256]; |
579 | static signed char decode_hexdigit[256]; |
538 | |
580 | |
539 | static UV |
581 | static UV |
… | |
… | |
564 | SV *sv = 0; |
606 | SV *sv = 0; |
565 | int utf8 = 0; |
607 | int utf8 = 0; |
566 | |
608 | |
567 | do |
609 | do |
568 | { |
610 | { |
569 | char buf [SHORT_STRING_LEN + UTF8_MAX_LEN]; |
611 | char buf [SHORT_STRING_LEN + UTF8_MAXBYTES]; |
570 | char *cur = buf; |
612 | char *cur = buf; |
571 | |
613 | |
572 | do |
614 | do |
573 | { |
615 | { |
574 | unsigned char ch = *(unsigned char *)dec->cur++; |
616 | unsigned char ch = *(unsigned char *)dec->cur++; |
… | |
… | |
640 | } |
682 | } |
641 | else if (ch >= 0x20 && ch <= 0x7f) |
683 | else if (ch >= 0x20 && ch <= 0x7f) |
642 | *cur++ = ch; |
684 | *cur++ = ch; |
643 | else if (ch >= 0x80) |
685 | else if (ch >= 0x80) |
644 | { |
686 | { |
|
|
687 | STRLEN clen; |
|
|
688 | UV uch; |
|
|
689 | |
645 | --dec->cur; |
690 | --dec->cur; |
646 | |
691 | |
647 | STRLEN clen; |
|
|
648 | UV uch = decode_utf8 (dec->cur, dec->end - dec->cur, &clen); |
692 | uch = decode_utf8 (dec->cur, dec->end - dec->cur, &clen); |
649 | if (clen == (STRLEN)-1) |
693 | if (clen == (STRLEN)-1) |
650 | ERR ("malformed UTF-8 character in JSON string"); |
694 | ERR ("malformed UTF-8 character in JSON string"); |
651 | |
695 | |
652 | do |
696 | do |
653 | { |
|
|
654 | *cur++ = *dec->cur++; |
697 | *cur++ = *dec->cur++; |
655 | } |
|
|
656 | while (--clen); |
698 | while (--clen); |
657 | |
699 | |
658 | utf8 = 1; |
700 | utf8 = 1; |
659 | } |
701 | } |
660 | else if (!ch) |
|
|
661 | ERR ("unexpected end of string while parsing json string"); |
|
|
662 | else |
702 | else |
|
|
703 | { |
|
|
704 | --dec->cur; |
|
|
705 | |
|
|
706 | if (!ch) |
|
|
707 | ERR ("unexpected end of string while parsing JSON string"); |
|
|
708 | else |
663 | ERR ("invalid character encountered"); |
709 | ERR ("invalid character encountered while parsing JSON string"); |
664 | |
710 | } |
665 | } |
711 | } |
666 | while (cur < buf + SHORT_STRING_LEN); |
712 | while (cur < buf + SHORT_STRING_LEN); |
667 | |
713 | |
|
|
714 | { |
668 | STRLEN len = cur - buf; |
715 | STRLEN len = cur - buf; |
669 | |
716 | |
670 | if (sv) |
717 | if (sv) |
671 | { |
718 | { |
672 | SvGROW (sv, SvCUR (sv) + len + 1); |
719 | SvGROW (sv, SvCUR (sv) + len + 1); |
673 | memcpy (SvPVX (sv) + SvCUR (sv), buf, len); |
720 | memcpy (SvPVX (sv) + SvCUR (sv), buf, len); |
674 | SvCUR_set (sv, SvCUR (sv) + len); |
721 | SvCUR_set (sv, SvCUR (sv) + len); |
675 | } |
722 | } |
676 | else |
723 | else |
677 | sv = newSVpvn (buf, len); |
724 | sv = newSVpvn (buf, len); |
|
|
725 | } |
678 | } |
726 | } |
679 | while (*dec->cur != '"'); |
727 | while (*dec->cur != '"'); |
680 | |
728 | |
681 | ++dec->cur; |
729 | ++dec->cur; |
682 | |
730 | |
… | |
… | |
782 | static SV * |
830 | static SV * |
783 | decode_av (dec_t *dec) |
831 | decode_av (dec_t *dec) |
784 | { |
832 | { |
785 | AV *av = newAV (); |
833 | AV *av = newAV (); |
786 | |
834 | |
|
|
835 | DEC_INC_DEPTH; |
787 | decode_ws (dec); |
836 | decode_ws (dec); |
|
|
837 | |
788 | if (*dec->cur == ']') |
838 | if (*dec->cur == ']') |
789 | ++dec->cur; |
839 | ++dec->cur; |
790 | else |
840 | else |
791 | for (;;) |
841 | for (;;) |
792 | { |
842 | { |
… | |
… | |
810 | ERR (", or ] expected while parsing array"); |
860 | ERR (", or ] expected while parsing array"); |
811 | |
861 | |
812 | ++dec->cur; |
862 | ++dec->cur; |
813 | } |
863 | } |
814 | |
864 | |
|
|
865 | DEC_DEC_DEPTH; |
815 | return newRV_noinc ((SV *)av); |
866 | return newRV_noinc ((SV *)av); |
816 | |
867 | |
817 | fail: |
868 | fail: |
818 | SvREFCNT_dec (av); |
869 | SvREFCNT_dec (av); |
|
|
870 | DEC_DEC_DEPTH; |
819 | return 0; |
871 | return 0; |
820 | } |
872 | } |
821 | |
873 | |
822 | static SV * |
874 | static SV * |
823 | decode_hv (dec_t *dec) |
875 | decode_hv (dec_t *dec) |
824 | { |
876 | { |
825 | HV *hv = newHV (); |
877 | HV *hv = newHV (); |
826 | |
878 | |
|
|
879 | DEC_INC_DEPTH; |
827 | decode_ws (dec); |
880 | decode_ws (dec); |
|
|
881 | |
828 | if (*dec->cur == '}') |
882 | if (*dec->cur == '}') |
829 | ++dec->cur; |
883 | ++dec->cur; |
830 | else |
884 | else |
831 | for (;;) |
885 | for (;;) |
832 | { |
886 | { |
… | |
… | |
845 | { |
899 | { |
846 | SvREFCNT_dec (key); |
900 | SvREFCNT_dec (key); |
847 | goto fail; |
901 | goto fail; |
848 | } |
902 | } |
849 | |
903 | |
850 | //TODO: optimise |
|
|
851 | hv_store_ent (hv, key, value, 0); |
904 | hv_store_ent (hv, key, value, 0); |
|
|
905 | SvREFCNT_dec (key); |
852 | |
906 | |
853 | decode_ws (dec); |
907 | decode_ws (dec); |
854 | |
908 | |
855 | if (*dec->cur == '}') |
909 | if (*dec->cur == '}') |
856 | { |
910 | { |
… | |
… | |
862 | ERR (", or } expected while parsing object/hash"); |
916 | ERR (", or } expected while parsing object/hash"); |
863 | |
917 | |
864 | ++dec->cur; |
918 | ++dec->cur; |
865 | } |
919 | } |
866 | |
920 | |
|
|
921 | DEC_DEC_DEPTH; |
867 | return newRV_noinc ((SV *)hv); |
922 | return newRV_noinc ((SV *)hv); |
868 | |
923 | |
869 | fail: |
924 | fail: |
870 | SvREFCNT_dec (hv); |
925 | SvREFCNT_dec (hv); |
|
|
926 | DEC_DEC_DEPTH; |
871 | return 0; |
927 | return 0; |
872 | } |
928 | } |
873 | |
929 | |
874 | static SV * |
930 | static SV * |
875 | decode_sv (dec_t *dec) |
931 | decode_sv (dec_t *dec) |
… | |
… | |
918 | ERR ("'null' expected"); |
974 | ERR ("'null' expected"); |
919 | |
975 | |
920 | break; |
976 | break; |
921 | |
977 | |
922 | default: |
978 | default: |
923 | ERR ("malformed json string, neither array, object, number, string or atom"); |
979 | ERR ("malformed JSON string, neither array, object, number, string or atom"); |
924 | break; |
980 | break; |
925 | } |
981 | } |
926 | |
982 | |
927 | fail: |
983 | fail: |
928 | return 0; |
984 | return 0; |
929 | } |
985 | } |
930 | |
986 | |
931 | static SV * |
987 | static SV * |
932 | decode_json (SV *string, UV flags) |
988 | decode_json (SV *string, U32 flags, UV *offset_return) |
933 | { |
989 | { |
|
|
990 | dec_t dec; |
|
|
991 | UV offset; |
934 | SV *sv; |
992 | SV *sv; |
|
|
993 | |
|
|
994 | SvGETMAGIC (string); |
|
|
995 | SvUPGRADE (string, SVt_PV); |
935 | |
996 | |
936 | if (flags & F_UTF8) |
997 | if (flags & F_UTF8) |
937 | sv_utf8_downgrade (string, 0); |
998 | sv_utf8_downgrade (string, 0); |
938 | else |
999 | else |
939 | sv_utf8_upgrade (string); |
1000 | sv_utf8_upgrade (string); |
940 | |
1001 | |
941 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
1002 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
942 | |
1003 | |
943 | dec_t dec; |
|
|
944 | dec.flags = flags; |
1004 | dec.flags = flags; |
945 | dec.cur = SvPVX (string); |
1005 | dec.cur = SvPVX (string); |
946 | dec.end = SvEND (string); |
1006 | dec.end = SvEND (string); |
947 | dec.err = 0; |
1007 | dec.err = 0; |
|
|
1008 | dec.depth = 0; |
|
|
1009 | dec.maxdepth = DEC_DEPTH (dec.flags); |
948 | |
1010 | |
|
|
1011 | *dec.end = 0; // this should basically be a nop, too, but make sure it's there |
949 | sv = decode_sv (&dec); |
1012 | sv = decode_sv (&dec); |
950 | |
1013 | |
|
|
1014 | if (offset_return || !sv) |
|
|
1015 | { |
|
|
1016 | offset = dec.flags & F_UTF8 |
|
|
1017 | ? dec.cur - SvPVX (string) |
|
|
1018 | : utf8_distance (dec.cur, SvPVX (string)); |
|
|
1019 | |
|
|
1020 | if (offset_return) |
|
|
1021 | *offset_return = offset; |
|
|
1022 | } |
|
|
1023 | else |
|
|
1024 | { |
|
|
1025 | // check for trailing garbage |
|
|
1026 | decode_ws (&dec); |
|
|
1027 | |
|
|
1028 | if (*dec.cur) |
|
|
1029 | { |
|
|
1030 | dec.err = "garbage after JSON object"; |
|
|
1031 | SvREFCNT_dec (sv); |
|
|
1032 | sv = 0; |
|
|
1033 | } |
|
|
1034 | } |
|
|
1035 | |
951 | if (!sv) |
1036 | if (!sv) |
952 | { |
1037 | { |
953 | IV offset = dec.flags & F_UTF8 |
|
|
954 | ? dec.cur - SvPVX (string) |
|
|
955 | : utf8_distance (dec.cur, SvPVX (string)); |
|
|
956 | SV *uni = sv_newmortal (); |
1038 | SV *uni = sv_newmortal (); |
957 | |
1039 | |
958 | // horrible hack to silence warning inside pv_uni_display |
1040 | // horrible hack to silence warning inside pv_uni_display |
959 | COP cop = *PL_curcop; |
1041 | COP cop = *PL_curcop; |
960 | cop.cop_warnings = pWARN_NONE; |
1042 | cop.cop_warnings = pWARN_NONE; |
… | |
… | |
962 | SAVEVPTR (PL_curcop); |
1044 | SAVEVPTR (PL_curcop); |
963 | PL_curcop = &cop; |
1045 | PL_curcop = &cop; |
964 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
1046 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
965 | LEAVE; |
1047 | LEAVE; |
966 | |
1048 | |
967 | croak ("%s, at character offset %d (%s)", |
1049 | croak ("%s, at character offset %d [\"%s\"]", |
968 | dec.err, |
1050 | dec.err, |
969 | (int)offset, |
1051 | (int)offset, |
970 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1052 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
971 | } |
1053 | } |
972 | |
1054 | |
… | |
… | |
986 | BOOT: |
1068 | BOOT: |
987 | { |
1069 | { |
988 | int i; |
1070 | int i; |
989 | |
1071 | |
990 | memset (decode_hexdigit, 0xff, 256); |
1072 | memset (decode_hexdigit, 0xff, 256); |
|
|
1073 | |
991 | for (i = 10; i--; ) |
1074 | for (i = 0; i < 256; ++i) |
992 | decode_hexdigit ['0' + i] = i; |
1075 | decode_hexdigit [i] = |
993 | |
1076 | i >= '0' && i <= '9' ? i - '0' |
994 | for (i = 7; i--; ) |
1077 | : i >= 'a' && i <= 'f' ? i - 'a' + 10 |
995 | { |
1078 | : i >= 'A' && i <= 'F' ? i - 'A' + 10 |
996 | decode_hexdigit ['a' + i] = 10 + i; |
1079 | : -1; |
997 | decode_hexdigit ['A' + i] = 10 + i; |
|
|
998 | } |
|
|
999 | |
1080 | |
1000 | json_stash = gv_stashpv ("JSON::XS", 1); |
1081 | json_stash = gv_stashpv ("JSON::XS", 1); |
1001 | } |
1082 | } |
1002 | |
1083 | |
1003 | PROTOTYPES: DISABLE |
1084 | PROTOTYPES: DISABLE |
… | |
… | |
1009 | RETVAL |
1090 | RETVAL |
1010 | |
1091 | |
1011 | SV *ascii (SV *self, int enable = 1) |
1092 | SV *ascii (SV *self, int enable = 1) |
1012 | ALIAS: |
1093 | ALIAS: |
1013 | ascii = F_ASCII |
1094 | ascii = F_ASCII |
|
|
1095 | latin1 = F_LATIN1 |
1014 | utf8 = F_UTF8 |
1096 | utf8 = F_UTF8 |
1015 | indent = F_INDENT |
1097 | indent = F_INDENT |
1016 | canonical = F_CANONICAL |
1098 | canonical = F_CANONICAL |
1017 | space_before = F_SPACE_BEFORE |
1099 | space_before = F_SPACE_BEFORE |
1018 | space_after = F_SPACE_AFTER |
1100 | space_after = F_SPACE_AFTER |
… | |
… | |
1030 | RETVAL = newSVsv (self); |
1112 | RETVAL = newSVsv (self); |
1031 | } |
1113 | } |
1032 | OUTPUT: |
1114 | OUTPUT: |
1033 | RETVAL |
1115 | RETVAL |
1034 | |
1116 | |
|
|
1117 | SV *max_depth (SV *self, UV max_depth = 0x80000000UL) |
|
|
1118 | CODE: |
|
|
1119 | { |
|
|
1120 | UV *uv = SvJSON (self); |
|
|
1121 | UV log2 = 0; |
|
|
1122 | |
|
|
1123 | if (max_depth > 0x80000000UL) max_depth = 0x80000000UL; |
|
|
1124 | |
|
|
1125 | while ((1UL << log2) < max_depth) |
|
|
1126 | ++log2; |
|
|
1127 | |
|
|
1128 | *uv = *uv & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); |
|
|
1129 | |
|
|
1130 | RETVAL = newSVsv (self); |
|
|
1131 | } |
|
|
1132 | OUTPUT: |
|
|
1133 | RETVAL |
|
|
1134 | |
1035 | void encode (SV *self, SV *scalar) |
1135 | void encode (SV *self, SV *scalar) |
1036 | PPCODE: |
1136 | PPCODE: |
1037 | XPUSHs (encode_json (scalar, *SvJSON (self))); |
1137 | XPUSHs (encode_json (scalar, *SvJSON (self))); |
1038 | |
1138 | |
1039 | void decode (SV *self, SV *jsonstr) |
1139 | void decode (SV *self, SV *jsonstr) |
1040 | PPCODE: |
1140 | PPCODE: |
1041 | XPUSHs (decode_json (jsonstr, *SvJSON (self))); |
1141 | XPUSHs (decode_json (jsonstr, *SvJSON (self), 0)); |
|
|
1142 | |
|
|
1143 | void decode_prefix (SV *self, SV *jsonstr) |
|
|
1144 | PPCODE: |
|
|
1145 | { |
|
|
1146 | UV offset; |
|
|
1147 | EXTEND (SP, 2); |
|
|
1148 | PUSHs (decode_json (jsonstr, *SvJSON (self), &offset)); |
|
|
1149 | PUSHs (sv_2mortal (newSVuv (offset))); |
|
|
1150 | } |
1042 | |
1151 | |
1043 | PROTOTYPES: ENABLE |
1152 | PROTOTYPES: ENABLE |
1044 | |
1153 | |
1045 | void to_json (SV *scalar) |
1154 | void to_json (SV *scalar) |
1046 | ALIAS: |
1155 | ALIAS: |
1047 | objToJson = 0 |
1156 | objToJson = 0 |
1048 | PPCODE: |
1157 | PPCODE: |
1049 | XPUSHs (encode_json (scalar, F_UTF8)); |
1158 | XPUSHs (encode_json (scalar, F_DEFAULT | F_UTF8)); |
1050 | |
1159 | |
1051 | void from_json (SV *jsonstr) |
1160 | void from_json (SV *jsonstr) |
1052 | ALIAS: |
1161 | ALIAS: |
1053 | jsonToObj = 0 |
1162 | jsonToObj = 0 |
1054 | PPCODE: |
1163 | PPCODE: |
1055 | XPUSHs (decode_json (jsonstr, F_UTF8)); |
1164 | XPUSHs (decode_json (jsonstr, F_DEFAULT | F_UTF8, 0)); |
1056 | |
1165 | |