… | |
… | |
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 |
10 | #define F_UTF8 0x00000002 |
15 | #define F_UTF8 0x00000002UL |
11 | #define F_INDENT 0x00000004 |
16 | #define F_INDENT 0x00000004UL |
12 | #define F_CANONICAL 0x00000008 |
17 | #define F_CANONICAL 0x00000008UL |
13 | #define F_SPACE_BEFORE 0x00000010 |
18 | #define F_SPACE_BEFORE 0x00000010UL |
14 | #define F_SPACE_AFTER 0x00000020 |
19 | #define F_SPACE_AFTER 0x00000020UL |
15 | #define F_ALLOW_NONREF 0x00000080 |
20 | #define F_ALLOW_NONREF 0x00000080UL |
16 | #define F_SHRINK 0x00000100 |
21 | #define F_SHRINK 0x00000100UL |
|
|
22 | #define F_MAXDEPTH 0xf8000000UL |
|
|
23 | #define S_MAXDEPTH 27 |
|
|
24 | |
|
|
25 | #define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) |
|
|
26 | |
|
|
27 | // F_SELFCONVERT? <=> to_json/toJson |
|
|
28 | // F_BLESSED? <=> { $__class__$ => } |
17 | |
29 | |
18 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
30 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
19 | #define F_DEFAULT 0 |
31 | #define F_DEFAULT (9UL << S_MAXDEPTH) |
20 | |
32 | |
21 | #define INIT_SIZE 32 // initial scalar size to be allocated |
33 | #define INIT_SIZE 32 // initial scalar size to be allocated |
22 | #define INDENT_STEP 3 // spaces per indentation level |
34 | #define INDENT_STEP 3 // spaces per indentation level |
23 | |
35 | |
24 | #define UTF8_MAX_LEN 11 // for perls UTF-X: max. number of octets per character |
36 | #define UTF8_MAX_LEN 11 // for perls UTF-X: max. number of octets per character |
25 | #define SHORT_STRING_LEN 256 // special-case strings of up to this size |
37 | #define SHORT_STRING_LEN 512 // special-case strings of up to this size |
26 | |
38 | |
27 | #define SB do { |
39 | #define SB do { |
28 | #define SE } while (0) |
40 | #define SE } while (0) |
29 | |
41 | |
30 | static HV *json_stash; // JSON::XS:: |
42 | static HV *json_stash; // JSON::XS:: |
… | |
… | |
53 | SvPV_renew (sv, SvCUR (sv) + 1); |
65 | SvPV_renew (sv, SvCUR (sv) + 1); |
54 | #endif |
66 | #endif |
55 | } |
67 | } |
56 | } |
68 | } |
57 | |
69 | |
|
|
70 | // decode an utf-8 character and return it, or (UV)-1 in |
|
|
71 | // case of an error. |
|
|
72 | // we special-case "safe" characters from U+80 .. U+7FF, |
|
|
73 | // but use the very good perl function to parse anything else. |
|
|
74 | // note that we never call this function for a ascii codepoints |
|
|
75 | static UV |
|
|
76 | decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen) |
|
|
77 | { |
|
|
78 | if (s[0] > 0xdf || s[0] < 0xc2) |
|
|
79 | return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY); |
|
|
80 | else if (len > 1 && s[1] >= 0x80 && s[1] <= 0xbf) |
|
|
81 | { |
|
|
82 | *clen = 2; |
|
|
83 | return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); |
|
|
84 | } |
|
|
85 | else |
|
|
86 | { |
|
|
87 | *clen = (STRLEN)-1; |
|
|
88 | return (UV)-1; |
|
|
89 | } |
|
|
90 | } |
|
|
91 | |
58 | ///////////////////////////////////////////////////////////////////////////// |
92 | ///////////////////////////////////////////////////////////////////////////// |
59 | // encoder |
93 | // encoder |
60 | |
94 | |
61 | // structure used for encoding JSON |
95 | // structure used for encoding JSON |
62 | typedef struct |
96 | typedef struct |
63 | { |
97 | { |
64 | char *cur; // SvPVX (sv) + current output position |
98 | char *cur; // SvPVX (sv) + current output position |
65 | char *end; // SvEND (sv) |
99 | char *end; // SvEND (sv) |
66 | SV *sv; // result scalar |
100 | SV *sv; // result scalar |
67 | UV flags; // F_* |
101 | U32 flags; // F_* |
68 | int indent; // indentation level |
102 | U32 indent; // indentation level |
69 | int max_depth; // max. recursion level |
103 | U32 maxdepth; // max. indentation/recursion level |
70 | } enc_t; |
104 | } enc_t; |
71 | |
105 | |
72 | static void |
106 | static void |
73 | need (enc_t *enc, STRLEN len) |
107 | need (enc_t *enc, STRLEN len) |
74 | { |
108 | { |
75 | if (enc->cur + len >= enc->end) |
109 | if (enc->cur + len >= enc->end) |
76 | { |
110 | { |
77 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
111 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
78 | SvGROW (enc->sv, cur + len + 1); |
112 | SvGROW (enc->sv, cur + len + 1); |
79 | enc->cur = SvPVX (enc->sv) + cur; |
113 | enc->cur = SvPVX (enc->sv) + cur; |
80 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv); |
114 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
81 | } |
115 | } |
82 | } |
116 | } |
83 | |
117 | |
84 | static void |
118 | static void |
85 | encode_ch (enc_t *enc, char ch) |
119 | encode_ch (enc_t *enc, char ch) |
… | |
… | |
133 | STRLEN clen; |
167 | STRLEN clen; |
134 | UV uch; |
168 | UV uch; |
135 | |
169 | |
136 | if (is_utf8) |
170 | if (is_utf8) |
137 | { |
171 | { |
138 | uch = utf8n_to_uvuni (str, end - str, &clen, UTF8_CHECK_ONLY); |
172 | //uch = utf8n_to_uvuni (str, end - str, &clen, UTF8_CHECK_ONLY); |
|
|
173 | uch = decode_utf8 (str, end - str, &clen); |
139 | if (clen == (STRLEN)-1) |
174 | if (clen == (STRLEN)-1) |
140 | croak ("malformed or illegal unicode character in string [%.11s], cannot convert to JSON", str); |
175 | croak ("malformed or illegal unicode character in string [%.11s], cannot convert to JSON", str); |
141 | } |
176 | } |
142 | else |
177 | else |
143 | { |
178 | { |
… | |
… | |
241 | static void |
276 | static void |
242 | encode_av (enc_t *enc, AV *av) |
277 | encode_av (enc_t *enc, AV *av) |
243 | { |
278 | { |
244 | int i, len = av_len (av); |
279 | int i, len = av_len (av); |
245 | |
280 | |
|
|
281 | if (enc->indent >= enc->maxdepth) |
|
|
282 | croak ("data structure too deep (hit recursion limit)"); |
|
|
283 | |
246 | encode_ch (enc, '['); encode_nl (enc); |
284 | encode_ch (enc, '['); encode_nl (enc); |
247 | ++enc->indent; |
285 | ++enc->indent; |
248 | |
286 | |
249 | for (i = 0; i <= len; ++i) |
287 | for (i = 0; i <= len; ++i) |
250 | { |
288 | { |
… | |
… | |
315 | |
353 | |
316 | static void |
354 | static void |
317 | encode_hv (enc_t *enc, HV *hv) |
355 | encode_hv (enc_t *enc, HV *hv) |
318 | { |
356 | { |
319 | int count, i; |
357 | int count, i; |
|
|
358 | |
|
|
359 | if (enc->indent >= enc->maxdepth) |
|
|
360 | croak ("data structure too deep (hit recursion limit)"); |
320 | |
361 | |
321 | encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; |
362 | encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; |
322 | |
363 | |
323 | if ((count = hv_iterinit (hv))) |
364 | if ((count = hv_iterinit (hv))) |
324 | { |
365 | { |
… | |
… | |
372 | |
413 | |
373 | encode_nl (enc); |
414 | encode_nl (enc); |
374 | } |
415 | } |
375 | else |
416 | else |
376 | { |
417 | { |
377 | SV *sv; |
|
|
378 | HE *he = hv_iternext (hv); |
418 | HE *he = hv_iternext (hv); |
379 | |
419 | |
380 | for (;;) |
420 | for (;;) |
381 | { |
421 | { |
382 | encode_indent (enc); |
422 | encode_indent (enc); |
… | |
… | |
391 | encode_nl (enc); |
431 | encode_nl (enc); |
392 | } |
432 | } |
393 | } |
433 | } |
394 | |
434 | |
395 | --enc->indent; encode_indent (enc); encode_ch (enc, '}'); |
435 | --enc->indent; encode_indent (enc); encode_ch (enc, '}'); |
|
|
436 | } |
|
|
437 | |
|
|
438 | // encode objects, arrays and special \0=false and \1=true values. |
|
|
439 | static void |
|
|
440 | encode_rv (enc_t *enc, SV *sv) |
|
|
441 | { |
|
|
442 | svtype svt; |
|
|
443 | |
|
|
444 | SvGETMAGIC (sv); |
|
|
445 | svt = SvTYPE (sv); |
|
|
446 | |
|
|
447 | if (svt == SVt_PVHV) |
|
|
448 | encode_hv (enc, (HV *)sv); |
|
|
449 | else if (svt == SVt_PVAV) |
|
|
450 | encode_av (enc, (AV *)sv); |
|
|
451 | else if (svt < SVt_PVAV) |
|
|
452 | { |
|
|
453 | if (SvNIOK (sv) && SvIV (sv) == 0) |
|
|
454 | encode_str (enc, "false", 5, 0); |
|
|
455 | else if (SvNIOK (sv) && SvIV (sv) == 1) |
|
|
456 | encode_str (enc, "true", 4, 0); |
|
|
457 | else |
|
|
458 | croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", |
|
|
459 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
|
|
460 | } |
|
|
461 | else |
|
|
462 | croak ("encountered %s, but JSON can only represent references to arrays or hashes", |
|
|
463 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
396 | } |
464 | } |
397 | |
465 | |
398 | static void |
466 | static void |
399 | encode_sv (enc_t *enc, SV *sv) |
467 | encode_sv (enc_t *enc, SV *sv) |
400 | { |
468 | { |
… | |
… | |
421 | SvIsUV(sv) |
489 | SvIsUV(sv) |
422 | ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv)) |
490 | ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv)) |
423 | : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv)); |
491 | : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv)); |
424 | } |
492 | } |
425 | else if (SvROK (sv)) |
493 | else if (SvROK (sv)) |
426 | { |
494 | encode_rv (enc, SvRV (sv)); |
427 | SV *rv = SvRV (sv); |
|
|
428 | |
|
|
429 | if (enc->indent >= enc->max_depth) |
|
|
430 | croak ("data structure too deep (hit recursion limit)"); |
|
|
431 | |
|
|
432 | switch (SvTYPE (rv)) |
|
|
433 | { |
|
|
434 | case SVt_PVAV: encode_av (enc, (AV *)rv); break; |
|
|
435 | case SVt_PVHV: encode_hv (enc, (HV *)rv); break; |
|
|
436 | |
|
|
437 | default: |
|
|
438 | croak ("encountered %s, but JSON can only represent references to arrays or hashes", |
|
|
439 | SvPV_nolen (sv)); |
|
|
440 | } |
|
|
441 | } |
|
|
442 | else if (!SvOK (sv)) |
495 | else if (!SvOK (sv)) |
443 | encode_str (enc, "null", 4, 0); |
496 | encode_str (enc, "null", 4, 0); |
444 | else |
497 | else |
445 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
498 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
446 | SvPV_nolen (sv), SvFLAGS (sv)); |
499 | SvPV_nolen (sv), SvFLAGS (sv)); |
447 | } |
500 | } |
448 | |
501 | |
449 | static SV * |
502 | static SV * |
450 | encode_json (SV *scalar, UV flags) |
503 | encode_json (SV *scalar, U32 flags) |
451 | { |
504 | { |
|
|
505 | enc_t enc; |
|
|
506 | |
452 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
507 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
453 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
508 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
454 | |
509 | |
455 | enc_t enc; |
|
|
456 | enc.flags = flags; |
510 | enc.flags = flags; |
457 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
511 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
458 | enc.cur = SvPVX (enc.sv); |
512 | enc.cur = SvPVX (enc.sv); |
459 | enc.end = SvEND (enc.sv); |
513 | enc.end = SvEND (enc.sv); |
460 | enc.indent = 0; |
514 | enc.indent = 0; |
461 | enc.max_depth = 0x7fffffffUL; |
515 | enc.maxdepth = DEC_DEPTH (flags); |
462 | |
516 | |
463 | SvPOK_only (enc.sv); |
517 | SvPOK_only (enc.sv); |
464 | encode_sv (&enc, scalar); |
518 | encode_sv (&enc, scalar); |
465 | |
519 | |
466 | if (!(flags & (F_ASCII | F_UTF8))) |
520 | if (!(flags & (F_ASCII | F_UTF8))) |
467 | SvUTF8_on (enc.sv); |
521 | SvUTF8_on (enc.sv); |
468 | |
522 | |
469 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
523 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
|
|
524 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
470 | |
525 | |
471 | if (enc.flags & F_SHRINK) |
526 | if (enc.flags & F_SHRINK) |
472 | shrink (enc.sv); |
527 | shrink (enc.sv); |
473 | |
528 | |
474 | return enc.sv; |
529 | return enc.sv; |
… | |
… | |
481 | typedef struct |
536 | typedef struct |
482 | { |
537 | { |
483 | char *cur; // current parser pointer |
538 | char *cur; // current parser pointer |
484 | char *end; // end of input string |
539 | char *end; // end of input string |
485 | const char *err; // parse error, if != 0 |
540 | const char *err; // parse error, if != 0 |
486 | UV flags; // F_* |
541 | U32 flags; // F_* |
|
|
542 | U32 depth; // recursion depth |
|
|
543 | U32 maxdepth; // recursion depth limit |
487 | } dec_t; |
544 | } dec_t; |
488 | |
545 | |
489 | static void |
546 | static void |
490 | decode_ws (dec_t *dec) |
547 | decode_ws (dec_t *dec) |
491 | { |
548 | { |
… | |
… | |
500 | ++dec->cur; |
557 | ++dec->cur; |
501 | } |
558 | } |
502 | } |
559 | } |
503 | |
560 | |
504 | #define ERR(reason) SB dec->err = reason; goto fail; SE |
561 | #define ERR(reason) SB dec->err = reason; goto fail; SE |
|
|
562 | |
505 | #define EXPECT_CH(ch) SB \ |
563 | #define EXPECT_CH(ch) SB \ |
506 | if (*dec->cur != ch) \ |
564 | if (*dec->cur != ch) \ |
507 | ERR (# ch " expected"); \ |
565 | ERR (# ch " expected"); \ |
508 | ++dec->cur; \ |
566 | ++dec->cur; \ |
509 | SE |
567 | SE |
|
|
568 | |
|
|
569 | #define DEC_INC_DEPTH if (++dec->depth > dec->maxdepth) ERR ("json datastructure exceeds maximum nesting level (set a higher max_depth)") |
|
|
570 | #define DEC_DEC_DEPTH --dec->depth |
510 | |
571 | |
511 | static SV *decode_sv (dec_t *dec); |
572 | static SV *decode_sv (dec_t *dec); |
512 | |
573 | |
513 | static signed char decode_hexdigit[256]; |
574 | static signed char decode_hexdigit[256]; |
514 | |
575 | |
… | |
… | |
616 | } |
677 | } |
617 | else if (ch >= 0x20 && ch <= 0x7f) |
678 | else if (ch >= 0x20 && ch <= 0x7f) |
618 | *cur++ = ch; |
679 | *cur++ = ch; |
619 | else if (ch >= 0x80) |
680 | else if (ch >= 0x80) |
620 | { |
681 | { |
|
|
682 | STRLEN clen; |
|
|
683 | UV uch; |
|
|
684 | |
621 | --dec->cur; |
685 | --dec->cur; |
622 | |
686 | |
623 | STRLEN clen; |
|
|
624 | UV uch = utf8n_to_uvuni (dec->cur, dec->end - dec->cur, &clen, UTF8_CHECK_ONLY); |
687 | uch = decode_utf8 (dec->cur, dec->end - dec->cur, &clen); |
625 | if (clen == (STRLEN)-1) |
688 | if (clen == (STRLEN)-1) |
626 | ERR ("malformed UTF-8 character in JSON string"); |
689 | ERR ("malformed UTF-8 character in JSON string"); |
627 | |
690 | |
628 | do |
691 | do |
629 | { |
|
|
630 | *cur++ = *dec->cur++; |
692 | *cur++ = *dec->cur++; |
631 | } |
|
|
632 | while (--clen); |
693 | while (--clen); |
633 | |
694 | |
634 | utf8 = 1; |
695 | utf8 = 1; |
635 | } |
696 | } |
636 | else if (!ch) |
|
|
637 | ERR ("unexpected end of string while parsing json string"); |
|
|
638 | else |
697 | else |
|
|
698 | { |
|
|
699 | --dec->cur; |
|
|
700 | |
|
|
701 | if (!ch) |
|
|
702 | ERR ("unexpected end of string while parsing JSON string"); |
|
|
703 | else |
639 | ERR ("invalid character encountered"); |
704 | ERR ("invalid character encountered while parsing JSON string"); |
640 | |
705 | } |
641 | } |
706 | } |
642 | while (cur < buf + SHORT_STRING_LEN); |
707 | while (cur < buf + SHORT_STRING_LEN); |
643 | |
708 | |
|
|
709 | { |
644 | STRLEN len = cur - buf; |
710 | STRLEN len = cur - buf; |
645 | |
711 | |
646 | if (sv) |
712 | if (sv) |
647 | { |
713 | { |
648 | SvGROW (sv, SvCUR (sv) + len + 1); |
714 | SvGROW (sv, SvCUR (sv) + len + 1); |
649 | memcpy (SvPVX (sv) + SvCUR (sv), buf, len); |
715 | memcpy (SvPVX (sv) + SvCUR (sv), buf, len); |
650 | SvCUR_set (sv, SvCUR (sv) + len); |
716 | SvCUR_set (sv, SvCUR (sv) + len); |
651 | } |
717 | } |
652 | else |
718 | else |
653 | sv = newSVpvn (buf, len); |
719 | sv = newSVpvn (buf, len); |
|
|
720 | } |
654 | } |
721 | } |
655 | while (*dec->cur != '"'); |
722 | while (*dec->cur != '"'); |
656 | |
723 | |
657 | ++dec->cur; |
724 | ++dec->cur; |
658 | |
725 | |
… | |
… | |
758 | static SV * |
825 | static SV * |
759 | decode_av (dec_t *dec) |
826 | decode_av (dec_t *dec) |
760 | { |
827 | { |
761 | AV *av = newAV (); |
828 | AV *av = newAV (); |
762 | |
829 | |
|
|
830 | DEC_INC_DEPTH; |
763 | decode_ws (dec); |
831 | decode_ws (dec); |
|
|
832 | |
764 | if (*dec->cur == ']') |
833 | if (*dec->cur == ']') |
765 | ++dec->cur; |
834 | ++dec->cur; |
766 | else |
835 | else |
767 | for (;;) |
836 | for (;;) |
768 | { |
837 | { |
… | |
… | |
786 | ERR (", or ] expected while parsing array"); |
855 | ERR (", or ] expected while parsing array"); |
787 | |
856 | |
788 | ++dec->cur; |
857 | ++dec->cur; |
789 | } |
858 | } |
790 | |
859 | |
|
|
860 | DEC_DEC_DEPTH; |
791 | return newRV_noinc ((SV *)av); |
861 | return newRV_noinc ((SV *)av); |
792 | |
862 | |
793 | fail: |
863 | fail: |
794 | SvREFCNT_dec (av); |
864 | SvREFCNT_dec (av); |
|
|
865 | DEC_DEC_DEPTH; |
795 | return 0; |
866 | return 0; |
796 | } |
867 | } |
797 | |
868 | |
798 | static SV * |
869 | static SV * |
799 | decode_hv (dec_t *dec) |
870 | decode_hv (dec_t *dec) |
800 | { |
871 | { |
801 | HV *hv = newHV (); |
872 | HV *hv = newHV (); |
802 | |
873 | |
|
|
874 | DEC_INC_DEPTH; |
803 | decode_ws (dec); |
875 | decode_ws (dec); |
|
|
876 | |
804 | if (*dec->cur == '}') |
877 | if (*dec->cur == '}') |
805 | ++dec->cur; |
878 | ++dec->cur; |
806 | else |
879 | else |
807 | for (;;) |
880 | for (;;) |
808 | { |
881 | { |
… | |
… | |
821 | { |
894 | { |
822 | SvREFCNT_dec (key); |
895 | SvREFCNT_dec (key); |
823 | goto fail; |
896 | goto fail; |
824 | } |
897 | } |
825 | |
898 | |
826 | //TODO: optimise |
|
|
827 | hv_store_ent (hv, key, value, 0); |
899 | hv_store_ent (hv, key, value, 0); |
|
|
900 | SvREFCNT_dec (key); |
828 | |
901 | |
829 | decode_ws (dec); |
902 | decode_ws (dec); |
830 | |
903 | |
831 | if (*dec->cur == '}') |
904 | if (*dec->cur == '}') |
832 | { |
905 | { |
… | |
… | |
838 | ERR (", or } expected while parsing object/hash"); |
911 | ERR (", or } expected while parsing object/hash"); |
839 | |
912 | |
840 | ++dec->cur; |
913 | ++dec->cur; |
841 | } |
914 | } |
842 | |
915 | |
|
|
916 | DEC_DEC_DEPTH; |
843 | return newRV_noinc ((SV *)hv); |
917 | return newRV_noinc ((SV *)hv); |
844 | |
918 | |
845 | fail: |
919 | fail: |
846 | SvREFCNT_dec (hv); |
920 | SvREFCNT_dec (hv); |
|
|
921 | DEC_DEC_DEPTH; |
847 | return 0; |
922 | return 0; |
848 | } |
923 | } |
849 | |
924 | |
850 | static SV * |
925 | static SV * |
851 | decode_sv (dec_t *dec) |
926 | decode_sv (dec_t *dec) |
… | |
… | |
894 | ERR ("'null' expected"); |
969 | ERR ("'null' expected"); |
895 | |
970 | |
896 | break; |
971 | break; |
897 | |
972 | |
898 | default: |
973 | default: |
899 | ERR ("malformed json string, neither array, object, number, string or atom"); |
974 | ERR ("malformed JSON string, neither array, object, number, string or atom"); |
900 | break; |
975 | break; |
901 | } |
976 | } |
902 | |
977 | |
903 | fail: |
978 | fail: |
904 | return 0; |
979 | return 0; |
905 | } |
980 | } |
906 | |
981 | |
907 | static SV * |
982 | static SV * |
908 | decode_json (SV *string, UV flags) |
983 | decode_json (SV *string, U32 flags) |
909 | { |
984 | { |
|
|
985 | dec_t dec; |
910 | SV *sv; |
986 | SV *sv; |
|
|
987 | |
|
|
988 | SvUPGRADE (string, SVt_PV); |
911 | |
989 | |
912 | if (flags & F_UTF8) |
990 | if (flags & F_UTF8) |
913 | sv_utf8_downgrade (string, 0); |
991 | sv_utf8_downgrade (string, 0); |
914 | else |
992 | else |
915 | sv_utf8_upgrade (string); |
993 | sv_utf8_upgrade (string); |
916 | |
994 | |
917 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
995 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
918 | |
996 | |
919 | dec_t dec; |
|
|
920 | dec.flags = flags; |
997 | dec.flags = flags; |
921 | dec.cur = SvPVX (string); |
998 | dec.cur = SvPVX (string); |
922 | dec.end = SvEND (string); |
999 | dec.end = SvEND (string); |
923 | dec.err = 0; |
1000 | dec.err = 0; |
|
|
1001 | dec.depth = 0; |
|
|
1002 | dec.maxdepth = DEC_DEPTH (dec.flags); |
924 | |
1003 | |
|
|
1004 | *dec.end = 0; // this should basically be a nop, too, but make sure its there |
925 | sv = decode_sv (&dec); |
1005 | sv = decode_sv (&dec); |
926 | |
1006 | |
927 | if (!sv) |
1007 | if (!sv) |
928 | { |
1008 | { |
929 | IV offset = dec.flags & F_UTF8 |
1009 | IV offset = dec.flags & F_UTF8 |
… | |
… | |
938 | SAVEVPTR (PL_curcop); |
1018 | SAVEVPTR (PL_curcop); |
939 | PL_curcop = &cop; |
1019 | PL_curcop = &cop; |
940 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
1020 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
941 | LEAVE; |
1021 | LEAVE; |
942 | |
1022 | |
943 | croak ("%s, at character offset %d (%s)", |
1023 | croak ("%s, at character offset %d [\"%s\"]", |
944 | dec.err, |
1024 | dec.err, |
945 | (int)offset, |
1025 | (int)offset, |
946 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1026 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
947 | } |
1027 | } |
948 | |
1028 | |
… | |
… | |
962 | BOOT: |
1042 | BOOT: |
963 | { |
1043 | { |
964 | int i; |
1044 | int i; |
965 | |
1045 | |
966 | memset (decode_hexdigit, 0xff, 256); |
1046 | memset (decode_hexdigit, 0xff, 256); |
|
|
1047 | |
967 | for (i = 10; i--; ) |
1048 | for (i = 0; i < 256; ++i) |
968 | decode_hexdigit ['0' + i] = i; |
1049 | decode_hexdigit [i] = |
969 | |
1050 | i >= '0' && i <= '9' ? i - '0' |
970 | for (i = 7; i--; ) |
1051 | : i >= 'a' && i <= 'f' ? i - 'a' + 10 |
971 | { |
1052 | : i >= 'A' && i <= 'F' ? i - 'A' + 10 |
972 | decode_hexdigit ['a' + i] = 10 + i; |
1053 | : -1; |
973 | decode_hexdigit ['A' + i] = 10 + i; |
|
|
974 | } |
|
|
975 | |
1054 | |
976 | json_stash = gv_stashpv ("JSON::XS", 1); |
1055 | json_stash = gv_stashpv ("JSON::XS", 1); |
977 | } |
1056 | } |
978 | |
1057 | |
979 | PROTOTYPES: DISABLE |
1058 | PROTOTYPES: DISABLE |
… | |
… | |
1006 | RETVAL = newSVsv (self); |
1085 | RETVAL = newSVsv (self); |
1007 | } |
1086 | } |
1008 | OUTPUT: |
1087 | OUTPUT: |
1009 | RETVAL |
1088 | RETVAL |
1010 | |
1089 | |
|
|
1090 | SV *max_depth (SV *self, UV max_depth = 0x80000000UL) |
|
|
1091 | CODE: |
|
|
1092 | { |
|
|
1093 | UV *uv = SvJSON (self); |
|
|
1094 | UV log2 = 0; |
|
|
1095 | |
|
|
1096 | if (max_depth > 0x80000000UL) max_depth = 0x80000000UL; |
|
|
1097 | |
|
|
1098 | while ((1UL << log2) < max_depth) |
|
|
1099 | ++log2; |
|
|
1100 | |
|
|
1101 | *uv = *uv & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); |
|
|
1102 | |
|
|
1103 | RETVAL = newSVsv (self); |
|
|
1104 | } |
|
|
1105 | OUTPUT: |
|
|
1106 | RETVAL |
|
|
1107 | |
1011 | void encode (SV *self, SV *scalar) |
1108 | void encode (SV *self, SV *scalar) |
1012 | PPCODE: |
1109 | PPCODE: |
1013 | XPUSHs (encode_json (scalar, *SvJSON (self))); |
1110 | XPUSHs (encode_json (scalar, *SvJSON (self))); |
1014 | |
1111 | |
1015 | void decode (SV *self, SV *jsonstr) |
1112 | void decode (SV *self, SV *jsonstr) |
… | |
… | |
1017 | XPUSHs (decode_json (jsonstr, *SvJSON (self))); |
1114 | XPUSHs (decode_json (jsonstr, *SvJSON (self))); |
1018 | |
1115 | |
1019 | PROTOTYPES: ENABLE |
1116 | PROTOTYPES: ENABLE |
1020 | |
1117 | |
1021 | void to_json (SV *scalar) |
1118 | void to_json (SV *scalar) |
|
|
1119 | ALIAS: |
|
|
1120 | objToJson = 0 |
1022 | PPCODE: |
1121 | PPCODE: |
1023 | XPUSHs (encode_json (scalar, F_UTF8)); |
1122 | XPUSHs (encode_json (scalar, F_DEFAULT | F_UTF8)); |
1024 | |
1123 | |
1025 | void from_json (SV *jsonstr) |
1124 | void from_json (SV *jsonstr) |
|
|
1125 | ALIAS: |
|
|
1126 | jsonToObj = 0 |
1026 | PPCODE: |
1127 | PPCODE: |
1027 | XPUSHs (decode_json (jsonstr, F_UTF8)); |
1128 | XPUSHs (decode_json (jsonstr, F_DEFAULT | F_UTF8)); |
1028 | |
1129 | |