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