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