… | |
… | |
40 | // structure used for decoding JSON |
40 | // structure used for decoding JSON |
41 | typedef struct |
41 | typedef struct |
42 | { |
42 | { |
43 | char *cur; |
43 | char *cur; |
44 | char *end; |
44 | char *end; |
45 | char *err; |
45 | const char *err; |
46 | UV flags; |
46 | UV flags; |
47 | } dec_t; |
47 | } dec_t; |
48 | |
48 | |
49 | static UV * |
49 | static UV * |
50 | SvJSON (SV *sv) |
50 | SvJSON (SV *sv) |
… | |
… | |
63 | if (enc->cur + len >= enc->end) |
63 | if (enc->cur + len >= enc->end) |
64 | { |
64 | { |
65 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
65 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
66 | SvGROW (enc->sv, cur + len + 1); |
66 | SvGROW (enc->sv, cur + len + 1); |
67 | enc->cur = SvPVX (enc->sv) + cur; |
67 | enc->cur = SvPVX (enc->sv) + cur; |
68 | enc->end = SvEND (enc->sv); |
68 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv); |
69 | } |
69 | } |
70 | } |
70 | } |
71 | |
71 | |
72 | static void |
72 | static void |
73 | encode_ch (enc_t *enc, char ch) |
73 | encode_ch (enc_t *enc, char ch) |
… | |
… | |
79 | static void |
79 | static void |
80 | encode_str (enc_t *enc, char *str, STRLEN len, int is_utf8) |
80 | encode_str (enc_t *enc, char *str, STRLEN len, int is_utf8) |
81 | { |
81 | { |
82 | char *end = str + len; |
82 | char *end = str + len; |
83 | |
83 | |
|
|
84 | need (enc, len); |
|
|
85 | |
84 | while (str < end) |
86 | while (str < end) |
85 | { |
87 | { |
86 | unsigned char ch = *(unsigned char *)str; |
88 | unsigned char ch = *(unsigned char *)str; |
|
|
89 | |
|
|
90 | if (ch == '"') |
|
|
91 | { |
|
|
92 | need (enc, len += 1); |
|
|
93 | *enc->cur++ = '\\'; |
|
|
94 | *enc->cur++ = '"'; |
|
|
95 | ++str; |
|
|
96 | } |
|
|
97 | else if (ch == '\\') |
|
|
98 | { |
|
|
99 | need (enc, len += 1); |
|
|
100 | *enc->cur++ = '\\'; |
|
|
101 | *enc->cur++ = '\\'; |
|
|
102 | ++str; |
|
|
103 | } |
87 | if (ch >= 0x20 && ch < 0x80) // most common case |
104 | else if (ch >= 0x20 && ch < 0x80) // most common case |
88 | { |
105 | { |
89 | *enc->cur++ = ch; |
106 | *enc->cur++ = ch; |
90 | str++; |
107 | ++str; |
|
|
108 | } |
|
|
109 | else if (ch == '\015') |
|
|
110 | { |
|
|
111 | need (enc, len += 1); |
|
|
112 | *enc->cur++ = '\\'; |
|
|
113 | *enc->cur++ = 'r'; |
|
|
114 | ++str; |
|
|
115 | } |
|
|
116 | else if (ch == '\012') |
|
|
117 | { |
|
|
118 | need (enc, len += 1); |
|
|
119 | *enc->cur++ = '\\'; |
|
|
120 | *enc->cur++ = 'n'; |
|
|
121 | ++str; |
91 | } |
122 | } |
92 | else |
123 | else |
93 | { |
124 | { |
94 | STRLEN clen; |
125 | STRLEN clen; |
95 | UV uch; |
126 | UV uch; |
96 | |
127 | |
97 | if (is_utf8) |
128 | if (is_utf8) |
98 | { |
129 | { |
99 | uch = utf8n_to_uvuni (str, end - str, &clen, UTF8_CHECK_ONLY); |
130 | uch = utf8n_to_uvuni (str, end - str, &clen, UTF8_CHECK_ONLY); |
100 | if (clen < 0) |
131 | if (clen == (STRLEN)-1) |
101 | croak ("malformed UTF-8 character in string, cannot convert to JSON"); |
132 | croak ("malformed UTF-8 character in string, cannot convert to JSON"); |
102 | } |
133 | } |
103 | else |
134 | else |
104 | { |
135 | { |
105 | uch = ch; |
136 | uch = ch; |
106 | clen = 1; |
137 | clen = 1; |
107 | } |
138 | } |
108 | |
139 | |
109 | need (enc, len += 6); |
|
|
110 | |
|
|
111 | if (uch < 0xa0 || enc->flags & F_ASCII) |
140 | if (uch < 0x80 || enc->flags & F_ASCII) |
112 | { |
141 | { |
113 | if (uch > 0xFFFFUL) |
142 | if (uch > 0xFFFFUL) |
114 | { |
143 | { |
115 | len += 6; |
|
|
116 | need (enc, len += 6); |
144 | need (enc, len += 11); |
117 | sprintf (enc->cur, "\\u%04x\\u%04x", |
145 | sprintf (enc->cur, "\\u%04x\\u%04x", |
118 | (uch - 0x10000) / 0x400 + 0xD800, |
146 | (uch - 0x10000) / 0x400 + 0xD800, |
119 | (uch - 0x10000) % 0x400 + 0xDC00); |
147 | (uch - 0x10000) % 0x400 + 0xDC00); |
120 | enc->cur += 12; |
148 | enc->cur += 12; |
121 | } |
149 | } |
122 | else |
150 | else |
123 | { |
151 | { |
124 | sprintf (enc->cur, "\\u%04x", uch); |
152 | static char hexdigit [16] = "0123456789abcdef"; |
|
|
153 | need (enc, len += 5); |
|
|
154 | *enc->cur++ = '\\'; |
125 | enc->cur += 6; |
155 | *enc->cur++ = 'u'; |
|
|
156 | *enc->cur++ = hexdigit [ uch >> 12 ]; |
|
|
157 | *enc->cur++ = hexdigit [(uch >> 8) & 15]; |
|
|
158 | *enc->cur++ = hexdigit [(uch >> 4) & 15]; |
|
|
159 | *enc->cur++ = hexdigit [(uch >> 0) & 15]; |
126 | } |
160 | } |
|
|
161 | |
|
|
162 | str += clen; |
127 | } |
163 | } |
128 | else if (is_utf8) |
164 | else if (is_utf8) |
129 | { |
165 | { |
130 | memcpy (enc->cur, str, clen); |
|
|
131 | enc->cur += clen; |
166 | need (enc, len += clen); |
|
|
167 | do |
|
|
168 | { |
|
|
169 | *enc->cur++ = *str++; |
|
|
170 | } |
|
|
171 | while (--clen); |
132 | } |
172 | } |
133 | else |
173 | else |
|
|
174 | { |
|
|
175 | need (enc, 10); // never more than 11 bytes needed |
134 | enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
176 | enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
135 | |
177 | ++str; |
136 | str += clen; |
178 | } |
137 | } |
179 | } |
138 | |
180 | |
139 | --len; |
181 | --len; |
140 | } |
182 | } |
141 | } |
183 | } |
… | |
… | |
192 | |
234 | |
193 | if (HeKLEN (he) == HEf_SVKEY) |
235 | if (HeKLEN (he) == HEf_SVKEY) |
194 | { |
236 | { |
195 | SV *sv = HeSVKEY (he); |
237 | SV *sv = HeSVKEY (he); |
196 | STRLEN len; |
238 | STRLEN len; |
|
|
239 | char *str; |
|
|
240 | |
|
|
241 | SvGETMAGIC (sv); |
197 | char *str = SvPV (sv, len); |
242 | str = SvPV (sv, len); |
198 | |
243 | |
199 | encode_str (enc, str, len, SvUTF8 (sv)); |
244 | encode_str (enc, str, len, SvUTF8 (sv)); |
200 | } |
245 | } |
201 | else |
246 | else |
202 | encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); |
247 | encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); |
… | |
… | |
313 | } |
358 | } |
314 | |
359 | |
315 | static void |
360 | static void |
316 | encode_sv (enc_t *enc, SV *sv) |
361 | encode_sv (enc_t *enc, SV *sv) |
317 | { |
362 | { |
|
|
363 | SvGETMAGIC (sv); |
|
|
364 | |
318 | if (SvPOKp (sv)) |
365 | if (SvPOKp (sv)) |
319 | { |
366 | { |
320 | STRLEN len; |
367 | STRLEN len; |
321 | char *str = SvPV (sv, len); |
368 | char *str = SvPV (sv, len); |
322 | encode_ch (enc, '"'); |
369 | encode_ch (enc, '"'); |
… | |
… | |
402 | ++dec->cur; \ |
449 | ++dec->cur; \ |
403 | SE |
450 | SE |
404 | |
451 | |
405 | static SV *decode_sv (dec_t *dec); |
452 | static SV *decode_sv (dec_t *dec); |
406 | |
453 | |
407 | #define APPEND_CH(ch) SB \ |
|
|
408 | SvGROW (sv, cur + 1 + 1); \ |
|
|
409 | SvPVX (sv)[cur++] = (ch); \ |
|
|
410 | SE |
|
|
411 | |
|
|
412 | static signed char decode_hexdigit[256]; |
454 | static signed char decode_hexdigit[256]; |
413 | |
455 | |
414 | static UV |
456 | static UV |
415 | decode_4hex (dec_t *dec) |
457 | decode_4hex (dec_t *dec) |
416 | { |
458 | { |
… | |
… | |
434 | |
476 | |
435 | fail: |
477 | fail: |
436 | return (UV)-1; |
478 | return (UV)-1; |
437 | } |
479 | } |
438 | |
480 | |
|
|
481 | #define APPEND_GROW(n) SB \ |
|
|
482 | if (cur + (n) >= end) \ |
|
|
483 | { \ |
|
|
484 | STRLEN ofs = cur - SvPVX (sv); \ |
|
|
485 | SvGROW (sv, ofs + (n) + 1); \ |
|
|
486 | cur = SvPVX (sv) + ofs; \ |
|
|
487 | end = SvEND (sv); \ |
|
|
488 | } \ |
|
|
489 | SE |
|
|
490 | |
|
|
491 | #define APPEND_CH(ch) SB \ |
|
|
492 | APPEND_GROW (1); \ |
|
|
493 | *cur++ = (ch); \ |
|
|
494 | SE |
|
|
495 | |
439 | static SV * |
496 | static SV * |
440 | decode_str (dec_t *dec) |
497 | decode_str (dec_t *dec) |
441 | { |
498 | { |
442 | SV *sv = NEWSV (0,2); |
499 | SV *sv = NEWSV (0,2); |
443 | STRLEN cur = 0; |
|
|
444 | int utf8 = 0; |
500 | int utf8 = 0; |
|
|
501 | char *cur = SvPVX (sv); |
|
|
502 | char *end = SvEND (sv); |
445 | |
503 | |
446 | for (;;) |
504 | for (;;) |
447 | { |
505 | { |
448 | unsigned char ch = *(unsigned char *)dec->cur; |
506 | unsigned char ch = *(unsigned char *)dec->cur; |
449 | |
507 | |
… | |
… | |
474 | |
532 | |
475 | // possibly a surrogate pair |
533 | // possibly a surrogate pair |
476 | if (hi >= 0xd800 && hi < 0xdc00) |
534 | if (hi >= 0xd800 && hi < 0xdc00) |
477 | { |
535 | { |
478 | if (dec->cur [0] != '\\' || dec->cur [1] != 'u') |
536 | if (dec->cur [0] != '\\' || dec->cur [1] != 'u') |
479 | ERR ("illegal surrogate character"); |
537 | ERR ("missing low surrogate character in surrogate pair"); |
480 | |
538 | |
481 | dec->cur += 2; |
539 | dec->cur += 2; |
482 | |
540 | |
483 | lo = decode_4hex (dec); |
541 | lo = decode_4hex (dec); |
484 | if (lo == (UV)-1) |
542 | if (lo == (UV)-1) |
… | |
… | |
487 | if (lo < 0xdc00 || lo >= 0xe000) |
545 | if (lo < 0xdc00 || lo >= 0xe000) |
488 | ERR ("surrogate pair expected"); |
546 | ERR ("surrogate pair expected"); |
489 | |
547 | |
490 | hi = (hi - 0xD800) * 0x400 + (lo - 0xDC00) + 0x10000; |
548 | hi = (hi - 0xD800) * 0x400 + (lo - 0xDC00) + 0x10000; |
491 | } |
549 | } |
492 | else if (lo >= 0xdc00 && lo < 0xe000) |
550 | else if (hi >= 0xdc00 && hi < 0xe000) |
493 | ERR ("illegal surrogate character"); |
551 | ERR ("missing high surrogate character in surrogate pair"); |
494 | |
552 | |
495 | if (hi >= 0x80) |
553 | if (hi >= 0x80) |
496 | { |
554 | { |
497 | utf8 = 1; |
555 | utf8 = 1; |
498 | |
556 | |
499 | SvGROW (sv, cur + 4 + 1); // at most 4 bytes for 21 bits |
557 | APPEND_GROW (4); // at most 4 bytes for 21 bits |
500 | cur = (char *)uvuni_to_utf8_flags (SvPVX (sv) + cur, hi, 0) - SvPVX (sv); |
558 | cur = (char *)uvuni_to_utf8_flags (cur, hi, 0); |
501 | } |
559 | } |
502 | else |
560 | else |
503 | APPEND_CH (hi); |
561 | APPEND_CH (hi); |
504 | } |
562 | } |
505 | break; |
563 | break; |
|
|
564 | |
|
|
565 | default: |
|
|
566 | --dec->cur; |
|
|
567 | ERR ("illegal backslash escape sequence in string"); |
506 | } |
568 | } |
507 | } |
569 | } |
508 | else if (ch >= 0x20 && ch <= 0x7f) |
570 | else if (ch >= 0x20 && ch <= 0x7f) |
509 | APPEND_CH (*dec->cur++); |
571 | APPEND_CH (*dec->cur++); |
510 | else if (ch >= 0x80) |
572 | else if (ch >= 0x80) |
511 | { |
573 | { |
512 | STRLEN clen; |
574 | STRLEN clen; |
513 | UV uch = utf8n_to_uvuni (dec->cur, dec->end - dec->cur, &clen, UTF8_CHECK_ONLY); |
575 | UV uch = utf8n_to_uvuni (dec->cur, dec->end - dec->cur, &clen, UTF8_CHECK_ONLY); |
514 | if (clen < 0) |
576 | if (clen == (STRLEN)-1) |
515 | ERR ("malformed UTF-8 character in string, cannot convert to JSON"); |
577 | ERR ("malformed UTF-8 character in string, cannot convert to JSON"); |
516 | |
578 | |
517 | SvGROW (sv, cur + clen + 1); // at most 4 bytes for 21 bits |
579 | APPEND_GROW (clen); |
518 | memcpy (SvPVX (sv) + cur, dec->cur, clen); |
580 | do |
519 | dec->cur += clen; |
581 | { |
|
|
582 | *cur++ = *dec->cur++; |
520 | } |
583 | } |
|
|
584 | while (--clen); |
|
|
585 | |
|
|
586 | utf8 = 1; |
|
|
587 | } |
|
|
588 | else if (dec->cur == dec->end) |
|
|
589 | ERR ("unexpected end of string while parsing json string"); |
521 | else |
590 | else |
522 | ERR ("invalid character encountered"); |
591 | ERR ("invalid character encountered"); |
523 | } |
592 | } |
524 | |
593 | |
525 | ++dec->cur; |
594 | ++dec->cur; |
526 | |
595 | |
|
|
596 | SvCUR_set (sv, cur - SvPVX (sv)); |
|
|
597 | |
527 | SvPOK_only (sv); |
598 | SvPOK_only (sv); |
528 | |
|
|
529 | SvCUR_set (sv, cur); |
|
|
530 | *SvEND (sv) = 0; |
599 | *SvEND (sv) = 0; |
531 | |
600 | |
532 | if (utf8) |
601 | if (utf8) |
533 | SvUTF8_on (sv); |
602 | SvUTF8_on (sv); |
534 | |
603 | |
… | |
… | |
553 | { |
622 | { |
554 | ++dec->cur; |
623 | ++dec->cur; |
555 | if (*dec->cur >= '0' && *dec->cur <= '9') |
624 | if (*dec->cur >= '0' && *dec->cur <= '9') |
556 | ERR ("malformed number (leading zero must not be followed by another digit)"); |
625 | ERR ("malformed number (leading zero must not be followed by another digit)"); |
557 | } |
626 | } |
558 | |
627 | else if (*dec->cur < '0' || *dec->cur > '9') |
559 | // int |
628 | ERR ("malformed number (no digits after initial minus)"); |
|
|
629 | else |
|
|
630 | do |
|
|
631 | { |
|
|
632 | ++dec->cur; |
|
|
633 | } |
560 | while (*dec->cur >= '0' && *dec->cur <= '9') |
634 | while (*dec->cur >= '0' && *dec->cur <= '9'); |
561 | ++dec->cur; |
|
|
562 | |
635 | |
563 | // [frac] |
636 | // [frac] |
564 | if (*dec->cur == '.') |
637 | if (*dec->cur == '.') |
565 | { |
638 | { |
|
|
639 | ++dec->cur; |
|
|
640 | |
|
|
641 | if (*dec->cur < '0' || *dec->cur > '9') |
|
|
642 | ERR ("malformed number (no digits after decimal point)"); |
|
|
643 | |
|
|
644 | do |
|
|
645 | { |
|
|
646 | ++dec->cur; |
|
|
647 | } |
|
|
648 | while (*dec->cur >= '0' && *dec->cur <= '9'); |
|
|
649 | |
566 | is_nv = 1; |
650 | is_nv = 1; |
567 | |
|
|
568 | do |
|
|
569 | { |
|
|
570 | ++dec->cur; |
|
|
571 | } |
|
|
572 | while (*dec->cur >= '0' && *dec->cur <= '9'); |
|
|
573 | } |
651 | } |
574 | |
652 | |
575 | // [exp] |
653 | // [exp] |
576 | if (*dec->cur == 'e' || *dec->cur == 'E') |
654 | if (*dec->cur == 'e' || *dec->cur == 'E') |
577 | { |
655 | { |
578 | is_nv = 1; |
|
|
579 | |
|
|
580 | ++dec->cur; |
656 | ++dec->cur; |
|
|
657 | |
581 | if (*dec->cur == '-' || *dec->cur == '+') |
658 | if (*dec->cur == '-' || *dec->cur == '+') |
582 | ++dec->cur; |
659 | ++dec->cur; |
583 | |
660 | |
|
|
661 | if (*dec->cur < '0' || *dec->cur > '9') |
|
|
662 | ERR ("malformed number (no digits after exp sign)"); |
|
|
663 | |
|
|
664 | do |
|
|
665 | { |
|
|
666 | ++dec->cur; |
|
|
667 | } |
584 | while (*dec->cur >= '0' && *dec->cur <= '9') |
668 | while (*dec->cur >= '0' && *dec->cur <= '9'); |
585 | ++dec->cur; |
669 | |
|
|
670 | is_nv = 1; |
586 | } |
671 | } |
587 | |
672 | |
588 | if (!is_nv) |
673 | if (!is_nv) |
589 | { |
674 | { |
590 | UV uv; |
675 | UV uv; |
… | |
… | |
608 | static SV * |
693 | static SV * |
609 | decode_av (dec_t *dec) |
694 | decode_av (dec_t *dec) |
610 | { |
695 | { |
611 | AV *av = newAV (); |
696 | AV *av = newAV (); |
612 | |
697 | |
|
|
698 | WS; |
|
|
699 | if (*dec->cur == ']') |
|
|
700 | ++dec->cur; |
|
|
701 | else |
613 | for (;;) |
702 | for (;;) |
614 | { |
703 | { |
615 | SV *value; |
704 | SV *value; |
616 | |
705 | |
617 | value = decode_sv (dec); |
706 | value = decode_sv (dec); |
618 | if (!value) |
707 | if (!value) |
619 | goto fail; |
708 | goto fail; |
620 | |
709 | |
621 | av_push (av, value); |
710 | av_push (av, value); |
622 | |
711 | |
623 | WS; |
712 | WS; |
624 | |
713 | |
625 | if (*dec->cur == ']') |
714 | if (*dec->cur == ']') |
626 | { |
715 | { |
627 | ++dec->cur; |
716 | ++dec->cur; |
628 | break; |
717 | break; |
|
|
718 | } |
629 | } |
719 | |
630 | |
|
|
631 | if (*dec->cur != ',') |
720 | if (*dec->cur != ',') |
632 | ERR (", or ] expected while parsing array"); |
721 | ERR (", or ] expected while parsing array"); |
633 | |
722 | |
634 | ++dec->cur; |
723 | ++dec->cur; |
635 | } |
724 | } |
636 | |
725 | |
637 | return newRV_noinc ((SV *)av); |
726 | return newRV_noinc ((SV *)av); |
638 | |
727 | |
639 | fail: |
728 | fail: |
640 | SvREFCNT_dec (av); |
729 | SvREFCNT_dec (av); |
… | |
… | |
644 | static SV * |
733 | static SV * |
645 | decode_hv (dec_t *dec) |
734 | decode_hv (dec_t *dec) |
646 | { |
735 | { |
647 | HV *hv = newHV (); |
736 | HV *hv = newHV (); |
648 | |
737 | |
|
|
738 | WS; |
|
|
739 | if (*dec->cur == '}') |
|
|
740 | ++dec->cur; |
|
|
741 | else |
649 | for (;;) |
742 | for (;;) |
650 | { |
743 | { |
651 | SV *key, *value; |
744 | SV *key, *value; |
652 | |
745 | |
653 | WS; EXPECT_CH ('"'); |
746 | WS; EXPECT_CH ('"'); |
654 | |
747 | |
655 | key = decode_str (dec); |
748 | key = decode_str (dec); |
656 | if (!key) |
749 | if (!key) |
657 | goto fail; |
|
|
658 | |
|
|
659 | WS; EXPECT_CH (':'); |
|
|
660 | |
|
|
661 | value = decode_sv (dec); |
|
|
662 | if (!value) |
|
|
663 | { |
|
|
664 | SvREFCNT_dec (key); |
|
|
665 | goto fail; |
750 | goto fail; |
|
|
751 | |
|
|
752 | WS; EXPECT_CH (':'); |
|
|
753 | |
|
|
754 | value = decode_sv (dec); |
|
|
755 | if (!value) |
|
|
756 | { |
|
|
757 | SvREFCNT_dec (key); |
|
|
758 | goto fail; |
666 | } |
759 | } |
667 | |
760 | |
668 | //TODO: optimise |
761 | //TODO: optimise |
669 | hv_store_ent (hv, key, value, 0); |
762 | hv_store_ent (hv, key, value, 0); |
670 | |
763 | |
671 | WS; |
764 | WS; |
672 | |
765 | |
673 | if (*dec->cur == '}') |
766 | if (*dec->cur == '}') |
674 | { |
767 | { |
675 | ++dec->cur; |
768 | ++dec->cur; |
676 | break; |
769 | break; |
677 | } |
770 | } |
678 | |
771 | |
679 | if (*dec->cur != ',') |
772 | if (*dec->cur != ',') |
680 | ERR (", or } expected while parsing object/hash"); |
773 | ERR (", or } expected while parsing object/hash"); |
681 | |
774 | |
682 | ++dec->cur; |
775 | ++dec->cur; |
683 | } |
776 | } |
684 | |
777 | |
685 | return newRV_noinc ((SV *)hv); |
778 | return newRV_noinc ((SV *)hv); |
686 | |
779 | |
687 | fail: |
780 | fail: |
688 | SvREFCNT_dec (hv); |
781 | SvREFCNT_dec (hv); |
… | |
… | |
728 | |
821 | |
729 | case 'n': |
822 | case 'n': |
730 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "null", 4)) |
823 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "null", 4)) |
731 | { |
824 | { |
732 | dec->cur += 4; |
825 | dec->cur += 4; |
733 | return newSViv (1); |
826 | return newSVsv (&PL_sv_undef); |
734 | } |
827 | } |
735 | else |
828 | else |
736 | ERR ("'null' expected"); |
829 | ERR ("'null' expected"); |
737 | |
830 | |
738 | break; |
831 | break; |
… | |
… | |
749 | static SV * |
842 | static SV * |
750 | decode_json (SV *string, UV flags) |
843 | decode_json (SV *string, UV flags) |
751 | { |
844 | { |
752 | SV *sv; |
845 | SV *sv; |
753 | |
846 | |
754 | if (!(flags & F_UTF8)) |
847 | if (flags & F_UTF8) |
|
|
848 | sv_utf8_downgrade (string, 0); |
|
|
849 | else |
755 | sv_utf8_upgrade (string); |
850 | sv_utf8_upgrade (string); |
756 | |
851 | |
757 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
852 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
758 | |
853 | |
759 | dec_t dec; |
854 | dec_t dec; |
760 | dec.flags = flags; |
855 | dec.flags = flags; |
761 | dec.cur = SvPVX (string); |
856 | dec.cur = SvPVX (string); |
762 | dec.end = SvEND (string); |
857 | dec.end = SvEND (string); |
763 | dec.err = 0; |
858 | dec.err = 0; |
764 | |
859 | |
765 | *dec.end = 1; // invalid anywhere |
|
|
766 | sv = decode_sv (&dec); |
860 | sv = decode_sv (&dec); |
767 | *dec.end = 0; |
|
|
768 | |
861 | |
769 | if (!sv) |
862 | if (!sv) |
770 | { |
863 | { |
771 | IV offset = utf8_distance (dec.cur, SvPVX (string)); |
864 | IV offset = utf8_distance (dec.cur, SvPVX (string)); |
772 | SV *uni = sv_newmortal (); |
865 | SV *uni = sv_newmortal (); |
|
|
866 | // horrible hack to silence warning inside pv_uni_display |
|
|
867 | COP cop; |
|
|
868 | memset (&cop, 0, sizeof (cop)); |
|
|
869 | cop.cop_warnings = pWARN_NONE; |
|
|
870 | SAVEVPTR (PL_curcop); |
|
|
871 | PL_curcop = &cop; |
773 | |
872 | |
774 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
873 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
775 | croak ("%s, at character %d (%s)", |
874 | croak ("%s, at character offset %d (%s)", |
776 | dec.err, |
875 | dec.err, |
777 | (int)offset, |
876 | (int)offset, |
778 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
877 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
779 | } |
878 | } |
780 | |
879 | |
… | |
… | |
794 | |
893 | |
795 | memset (decode_hexdigit, 0xff, 256); |
894 | memset (decode_hexdigit, 0xff, 256); |
796 | for (i = 10; i--; ) |
895 | for (i = 10; i--; ) |
797 | decode_hexdigit ['0' + i] = i; |
896 | decode_hexdigit ['0' + i] = i; |
798 | |
897 | |
799 | for (i = 6; --i; ) |
898 | for (i = 7; i--; ) |
800 | { |
899 | { |
801 | decode_hexdigit ['a' + i] = 10 + i; |
900 | decode_hexdigit ['a' + i] = 10 + i; |
802 | decode_hexdigit ['A' + i] = 10 + i; |
901 | decode_hexdigit ['A' + i] = 10 + i; |
803 | } |
902 | } |
804 | |
903 | |
805 | json_stash = gv_stashpv ("JSON::XS", 1); |
904 | json_stash = gv_stashpv ("JSON::XS", 1); |
806 | } |
905 | } |
|
|
906 | |
|
|
907 | PROTOTYPES: DISABLE |
807 | |
908 | |
808 | SV *new (char *dummy) |
909 | SV *new (char *dummy) |
809 | CODE: |
910 | CODE: |
810 | RETVAL = sv_bless (newRV_noinc (newSVuv (F_DEFAULT)), json_stash); |
911 | RETVAL = sv_bless (newRV_noinc (newSVuv (F_DEFAULT)), json_stash); |
811 | OUTPUT: |
912 | OUTPUT: |
… | |
… | |
841 | |
942 | |
842 | void decode (SV *self, SV *jsonstr) |
943 | void decode (SV *self, SV *jsonstr) |
843 | PPCODE: |
944 | PPCODE: |
844 | XPUSHs (decode_json (jsonstr, *SvJSON (self))); |
945 | XPUSHs (decode_json (jsonstr, *SvJSON (self))); |
845 | |
946 | |
|
|
947 | PROTOTYPES: ENABLE |
|
|
948 | |
846 | void to_json (SV *scalar) |
949 | void to_json (SV *scalar) |
847 | PPCODE: |
950 | PPCODE: |
848 | XPUSHs (encode_json (scalar, F_UTF8)); |
951 | XPUSHs (encode_json (scalar, F_UTF8)); |
849 | |
952 | |
850 | void from_json (SV *jsonstr) |
953 | void from_json (SV *jsonstr) |