… | |
… | |
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 | #define F_ALLOW_NONREF 0x00000080 |
|
|
17 | #define F_SHRINK 0x00000100 |
17 | |
18 | |
18 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
19 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
19 | #define F_DEFAULT 0 |
20 | #define F_DEFAULT 0 |
20 | |
21 | |
21 | #define INIT_SIZE 32 // initial scalar size to be allocated |
22 | #define INIT_SIZE 32 // initial scalar size to be allocated |
… | |
… | |
53 | croak ("object is not of type JSON::XS"); |
54 | croak ("object is not of type JSON::XS"); |
54 | |
55 | |
55 | return &SvUVX (SvRV (sv)); |
56 | return &SvUVX (SvRV (sv)); |
56 | } |
57 | } |
57 | |
58 | |
|
|
59 | static void |
|
|
60 | shrink (SV *sv) |
|
|
61 | { |
|
|
62 | sv_utf8_downgrade (sv, 1); |
|
|
63 | #ifdef SvPV_shrink_to_cur |
|
|
64 | SvPV_shrink_to_cur (sv); |
|
|
65 | #endif |
|
|
66 | } |
|
|
67 | |
58 | ///////////////////////////////////////////////////////////////////////////// |
68 | ///////////////////////////////////////////////////////////////////////////// |
59 | |
69 | |
60 | static void |
70 | static void |
61 | need (enc_t *enc, STRLEN len) |
71 | need (enc_t *enc, STRLEN len) |
62 | { |
72 | { |
… | |
… | |
85 | |
95 | |
86 | while (str < end) |
96 | while (str < end) |
87 | { |
97 | { |
88 | unsigned char ch = *(unsigned char *)str; |
98 | unsigned char ch = *(unsigned char *)str; |
89 | |
99 | |
90 | if (ch == '"') |
100 | if (ch >= 0x20 && ch < 0x80) // most common case |
91 | { |
101 | { |
|
|
102 | if (ch == '"') // but with slow exceptions |
|
|
103 | { |
92 | need (enc, len += 1); |
104 | need (enc, len += 1); |
93 | *enc->cur++ = '\\'; |
105 | *enc->cur++ = '\\'; |
94 | *enc->cur++ = '"'; |
106 | *enc->cur++ = '"'; |
95 | ++str; |
|
|
96 | } |
107 | } |
97 | else if (ch == '\\') |
108 | else if (ch == '\\') |
98 | { |
109 | { |
99 | need (enc, len += 1); |
110 | need (enc, len += 1); |
100 | *enc->cur++ = '\\'; |
111 | *enc->cur++ = '\\'; |
101 | *enc->cur++ = '\\'; |
112 | *enc->cur++ = '\\'; |
102 | ++str; |
|
|
103 | } |
113 | } |
104 | else if (ch >= 0x20 && ch < 0x80) // most common case |
114 | else |
105 | { |
|
|
106 | *enc->cur++ = ch; |
115 | *enc->cur++ = ch; |
107 | ++str; |
116 | |
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; |
117 | ++str; |
122 | } |
118 | } |
123 | else |
119 | else |
124 | { |
120 | { |
125 | STRLEN clen; |
121 | switch (ch) |
126 | UV uch; |
|
|
127 | |
|
|
128 | if (is_utf8) |
|
|
129 | { |
122 | { |
130 | uch = utf8n_to_uvuni (str, end - str, &clen, UTF8_CHECK_ONLY); |
123 | case '\010': need (enc, len += 1); *enc->cur++ = '\\'; *enc->cur++ = 'b'; ++str; break; |
131 | if (clen < 0) |
124 | case '\011': need (enc, len += 1); *enc->cur++ = '\\'; *enc->cur++ = 't'; ++str; break; |
132 | croak ("malformed UTF-8 character in string, cannot convert to JSON"); |
125 | case '\012': need (enc, len += 1); *enc->cur++ = '\\'; *enc->cur++ = 'n'; ++str; break; |
133 | } |
126 | case '\014': need (enc, len += 1); *enc->cur++ = '\\'; *enc->cur++ = 'f'; ++str; break; |
134 | else |
127 | case '\015': need (enc, len += 1); *enc->cur++ = '\\'; *enc->cur++ = 'r'; ++str; break; |
135 | { |
|
|
136 | uch = ch; |
|
|
137 | clen = 1; |
|
|
138 | } |
|
|
139 | |
128 | |
140 | if (uch < 0x80 || enc->flags & F_ASCII) |
129 | default: |
141 | { |
|
|
142 | if (uch > 0xFFFFUL) |
|
|
143 | { |
130 | { |
|
|
131 | STRLEN clen; |
|
|
132 | UV uch; |
|
|
133 | |
|
|
134 | if (is_utf8) |
|
|
135 | { |
|
|
136 | uch = utf8n_to_uvuni (str, end - str, &clen, UTF8_CHECK_ONLY); |
|
|
137 | if (clen == (STRLEN)-1) |
|
|
138 | croak ("malformed or illegal unicode character in string [%.11s], cannot convert to JSON", str); |
|
|
139 | } |
|
|
140 | else |
|
|
141 | { |
|
|
142 | uch = ch; |
|
|
143 | clen = 1; |
|
|
144 | } |
|
|
145 | |
|
|
146 | if (uch > 0x10FFFFUL) |
|
|
147 | croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
|
|
148 | |
|
|
149 | if (uch < 0x80 || enc->flags & F_ASCII) |
|
|
150 | { |
|
|
151 | if (uch > 0xFFFFUL) |
|
|
152 | { |
144 | need (enc, len += 11); |
153 | need (enc, len += 11); |
145 | sprintf (enc->cur, "\\u%04x\\u%04x", |
154 | sprintf (enc->cur, "\\u%04x\\u%04x", |
146 | (uch - 0x10000) / 0x400 + 0xD800, |
155 | (uch - 0x10000) / 0x400 + 0xD800, |
147 | (uch - 0x10000) % 0x400 + 0xDC00); |
156 | (uch - 0x10000) % 0x400 + 0xDC00); |
148 | enc->cur += 12; |
157 | enc->cur += 12; |
|
|
158 | } |
|
|
159 | else |
|
|
160 | { |
|
|
161 | static char hexdigit [16] = "0123456789abcdef"; |
|
|
162 | need (enc, len += 5); |
|
|
163 | *enc->cur++ = '\\'; |
|
|
164 | *enc->cur++ = 'u'; |
|
|
165 | *enc->cur++ = hexdigit [ uch >> 12 ]; |
|
|
166 | *enc->cur++ = hexdigit [(uch >> 8) & 15]; |
|
|
167 | *enc->cur++ = hexdigit [(uch >> 4) & 15]; |
|
|
168 | *enc->cur++ = hexdigit [(uch >> 0) & 15]; |
|
|
169 | } |
|
|
170 | |
|
|
171 | str += clen; |
|
|
172 | } |
|
|
173 | else if (is_utf8) |
|
|
174 | { |
|
|
175 | need (enc, len += clen); |
|
|
176 | do |
|
|
177 | { |
|
|
178 | *enc->cur++ = *str++; |
|
|
179 | } |
|
|
180 | while (--clen); |
|
|
181 | } |
|
|
182 | else |
|
|
183 | { |
|
|
184 | need (enc, len += 10); // never more than 11 bytes needed |
|
|
185 | enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
|
|
186 | ++str; |
|
|
187 | } |
149 | } |
188 | } |
150 | else |
|
|
151 | { |
|
|
152 | static char hexdigit [16] = "0123456789abcdef"; |
|
|
153 | need (enc, len += 5); |
|
|
154 | *enc->cur++ = '\\'; |
|
|
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]; |
|
|
160 | } |
|
|
161 | |
|
|
162 | str += clen; |
|
|
163 | } |
|
|
164 | else if (is_utf8) |
|
|
165 | { |
|
|
166 | need (enc, len += clen); |
|
|
167 | while (clen--) |
|
|
168 | *enc->cur++ = *str++; |
|
|
169 | } |
|
|
170 | else |
|
|
171 | { |
|
|
172 | need (enc, 10); // never more than 11 bytes needed |
|
|
173 | enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
|
|
174 | ++str; |
|
|
175 | } |
189 | } |
176 | } |
190 | } |
177 | |
191 | |
178 | --len; |
192 | --len; |
179 | } |
193 | } |
… | |
… | |
306 | |
320 | |
307 | if (fast) |
321 | if (fast) |
308 | qsort (hes, count, sizeof (HE *), he_cmp_fast); |
322 | qsort (hes, count, sizeof (HE *), he_cmp_fast); |
309 | else |
323 | else |
310 | { |
324 | { |
311 | // hack to disable "use bytes" |
325 | // hack to forcefully disable "use bytes" |
312 | COP *oldcop = PL_curcop, cop; |
326 | COP cop = *PL_curcop; |
313 | cop.op_private = 0; |
327 | cop.op_private = 0; |
|
|
328 | |
|
|
329 | ENTER; |
|
|
330 | SAVETMPS; |
|
|
331 | |
|
|
332 | SAVEVPTR (PL_curcop); |
314 | PL_curcop = &cop; |
333 | PL_curcop = &cop; |
315 | |
334 | |
316 | SAVETMPS; |
|
|
317 | qsort (hes, count, sizeof (HE *), he_cmp_slow); |
335 | qsort (hes, count, sizeof (HE *), he_cmp_slow); |
|
|
336 | |
318 | FREETMPS; |
337 | FREETMPS; |
319 | |
338 | LEAVE; |
320 | PL_curcop = oldcop; |
|
|
321 | } |
339 | } |
322 | |
340 | |
323 | for (i = 0; i < count; ++i) |
341 | for (i = 0; i < count; ++i) |
324 | { |
342 | { |
325 | INDENT; |
343 | INDENT; |
… | |
… | |
381 | ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv)) |
399 | ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv)) |
382 | : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv)); |
400 | : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv)); |
383 | } |
401 | } |
384 | else if (SvROK (sv)) |
402 | else if (SvROK (sv)) |
385 | { |
403 | { |
|
|
404 | SV *rv = SvRV (sv); |
|
|
405 | |
386 | if (!--enc->max_recurse) |
406 | if (!--enc->max_recurse) |
387 | croak ("data structure too deep (hit recursion limit)"); |
407 | croak ("data structure too deep (hit recursion limit)"); |
388 | |
408 | |
389 | sv = SvRV (sv); |
|
|
390 | |
|
|
391 | switch (SvTYPE (sv)) |
409 | switch (SvTYPE (rv)) |
392 | { |
410 | { |
393 | case SVt_PVAV: encode_av (enc, (AV *)sv); break; |
411 | case SVt_PVAV: encode_av (enc, (AV *)rv); break; |
394 | case SVt_PVHV: encode_hv (enc, (HV *)sv); break; |
412 | case SVt_PVHV: encode_hv (enc, (HV *)rv); break; |
395 | |
413 | |
396 | default: |
414 | default: |
397 | croak ("JSON can only represent references to arrays or hashes"); |
415 | croak ("encountered %s, but JSON can only represent references to arrays or hashes", |
|
|
416 | SvPV_nolen (sv)); |
398 | } |
417 | } |
399 | } |
418 | } |
400 | else if (!SvOK (sv)) |
419 | else if (!SvOK (sv)) |
401 | encode_str (enc, "null", 4, 0); |
420 | encode_str (enc, "null", 4, 0); |
402 | else |
421 | else |
403 | croak ("encountered perl type that JSON cannot handle"); |
422 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
|
|
423 | SvPV_nolen (sv), SvFLAGS (sv)); |
404 | } |
424 | } |
405 | |
425 | |
406 | static SV * |
426 | static SV * |
407 | encode_json (SV *scalar, UV flags) |
427 | encode_json (SV *scalar, UV flags) |
408 | { |
428 | { |
409 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
429 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
410 | croak ("hash- or arraref required (not a simple scalar, use allow_nonref to allow this)"); |
430 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
411 | |
431 | |
412 | enc_t enc; |
432 | enc_t enc; |
413 | enc.flags = flags; |
433 | enc.flags = flags; |
414 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
434 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
415 | enc.cur = SvPVX (enc.sv); |
435 | enc.cur = SvPVX (enc.sv); |
… | |
… | |
422 | |
442 | |
423 | if (!(flags & (F_ASCII | F_UTF8))) |
443 | if (!(flags & (F_ASCII | F_UTF8))) |
424 | SvUTF8_on (enc.sv); |
444 | SvUTF8_on (enc.sv); |
425 | |
445 | |
426 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
446 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
|
|
447 | |
|
|
448 | if (enc.flags & F_SHRINK) |
|
|
449 | shrink (enc.sv); |
|
|
450 | |
427 | return enc.sv; |
451 | return enc.sv; |
428 | } |
452 | } |
429 | |
453 | |
430 | ///////////////////////////////////////////////////////////////////////////// |
454 | ///////////////////////////////////////////////////////////////////////////// |
431 | |
455 | |
… | |
… | |
529 | |
553 | |
530 | // possibly a surrogate pair |
554 | // possibly a surrogate pair |
531 | if (hi >= 0xd800 && hi < 0xdc00) |
555 | if (hi >= 0xd800 && hi < 0xdc00) |
532 | { |
556 | { |
533 | if (dec->cur [0] != '\\' || dec->cur [1] != 'u') |
557 | if (dec->cur [0] != '\\' || dec->cur [1] != 'u') |
534 | ERR ("illegal surrogate character"); |
558 | ERR ("missing low surrogate character in surrogate pair"); |
535 | |
559 | |
536 | dec->cur += 2; |
560 | dec->cur += 2; |
537 | |
561 | |
538 | lo = decode_4hex (dec); |
562 | lo = decode_4hex (dec); |
539 | if (lo == (UV)-1) |
563 | if (lo == (UV)-1) |
… | |
… | |
542 | if (lo < 0xdc00 || lo >= 0xe000) |
566 | if (lo < 0xdc00 || lo >= 0xe000) |
543 | ERR ("surrogate pair expected"); |
567 | ERR ("surrogate pair expected"); |
544 | |
568 | |
545 | hi = (hi - 0xD800) * 0x400 + (lo - 0xDC00) + 0x10000; |
569 | hi = (hi - 0xD800) * 0x400 + (lo - 0xDC00) + 0x10000; |
546 | } |
570 | } |
547 | else if (lo >= 0xdc00 && lo < 0xe000) |
571 | else if (hi >= 0xdc00 && hi < 0xe000) |
548 | ERR ("illegal surrogate character"); |
572 | ERR ("missing high surrogate character in surrogate pair"); |
549 | |
573 | |
550 | if (hi >= 0x80) |
574 | if (hi >= 0x80) |
551 | { |
575 | { |
552 | utf8 = 1; |
576 | utf8 = 1; |
553 | |
577 | |
… | |
… | |
556 | } |
580 | } |
557 | else |
581 | else |
558 | APPEND_CH (hi); |
582 | APPEND_CH (hi); |
559 | } |
583 | } |
560 | break; |
584 | break; |
|
|
585 | |
|
|
586 | default: |
|
|
587 | --dec->cur; |
|
|
588 | ERR ("illegal backslash escape sequence in string"); |
561 | } |
589 | } |
562 | } |
590 | } |
563 | else if (ch >= 0x20 && ch <= 0x7f) |
591 | else if (ch >= 0x20 && ch <= 0x7f) |
564 | APPEND_CH (*dec->cur++); |
592 | APPEND_CH (*dec->cur++); |
565 | else if (ch >= 0x80) |
593 | else if (ch >= 0x80) |
566 | { |
594 | { |
567 | STRLEN clen; |
595 | STRLEN clen; |
568 | UV uch = utf8n_to_uvuni (dec->cur, dec->end - dec->cur, &clen, UTF8_CHECK_ONLY); |
596 | UV uch = utf8n_to_uvuni (dec->cur, dec->end - dec->cur, &clen, UTF8_CHECK_ONLY); |
569 | if (clen < 0) |
597 | if (clen == (STRLEN)-1) |
570 | ERR ("malformed UTF-8 character in string, cannot convert to JSON"); |
598 | ERR ("malformed UTF-8 character in JSON string"); |
571 | |
599 | |
572 | APPEND_GROW (clen); |
600 | APPEND_GROW (clen); |
573 | memcpy (cur, dec->cur, clen); |
601 | do |
574 | cur += clen; |
602 | { |
575 | dec->cur += clen; |
603 | *cur++ = *dec->cur++; |
|
|
604 | } |
|
|
605 | while (--clen); |
|
|
606 | |
|
|
607 | utf8 = 1; |
576 | } |
608 | } |
|
|
609 | else if (dec->cur == dec->end) |
|
|
610 | ERR ("unexpected end of string while parsing json string"); |
577 | else |
611 | else |
578 | ERR ("invalid character encountered"); |
612 | ERR ("invalid character encountered"); |
579 | } |
613 | } |
580 | |
614 | |
581 | ++dec->cur; |
615 | ++dec->cur; |
… | |
… | |
585 | SvPOK_only (sv); |
619 | SvPOK_only (sv); |
586 | *SvEND (sv) = 0; |
620 | *SvEND (sv) = 0; |
587 | |
621 | |
588 | if (utf8) |
622 | if (utf8) |
589 | SvUTF8_on (sv); |
623 | SvUTF8_on (sv); |
|
|
624 | |
|
|
625 | if (dec->flags & F_SHRINK) |
|
|
626 | shrink (sv); |
590 | |
627 | |
591 | return sv; |
628 | return sv; |
592 | |
629 | |
593 | fail: |
630 | fail: |
594 | SvREFCNT_dec (sv); |
631 | SvREFCNT_dec (sv); |
… | |
… | |
609 | { |
646 | { |
610 | ++dec->cur; |
647 | ++dec->cur; |
611 | if (*dec->cur >= '0' && *dec->cur <= '9') |
648 | if (*dec->cur >= '0' && *dec->cur <= '9') |
612 | ERR ("malformed number (leading zero must not be followed by another digit)"); |
649 | ERR ("malformed number (leading zero must not be followed by another digit)"); |
613 | } |
650 | } |
614 | |
651 | else if (*dec->cur < '0' || *dec->cur > '9') |
615 | // int |
652 | ERR ("malformed number (no digits after initial minus)"); |
|
|
653 | else |
|
|
654 | do |
|
|
655 | { |
|
|
656 | ++dec->cur; |
|
|
657 | } |
616 | while (*dec->cur >= '0' && *dec->cur <= '9') |
658 | while (*dec->cur >= '0' && *dec->cur <= '9'); |
617 | ++dec->cur; |
|
|
618 | |
659 | |
619 | // [frac] |
660 | // [frac] |
620 | if (*dec->cur == '.') |
661 | if (*dec->cur == '.') |
621 | { |
662 | { |
622 | is_nv = 1; |
663 | ++dec->cur; |
|
|
664 | |
|
|
665 | if (*dec->cur < '0' || *dec->cur > '9') |
|
|
666 | ERR ("malformed number (no digits after decimal point)"); |
623 | |
667 | |
624 | do |
668 | do |
625 | { |
669 | { |
626 | ++dec->cur; |
670 | ++dec->cur; |
627 | } |
671 | } |
628 | while (*dec->cur >= '0' && *dec->cur <= '9'); |
672 | while (*dec->cur >= '0' && *dec->cur <= '9'); |
|
|
673 | |
|
|
674 | is_nv = 1; |
629 | } |
675 | } |
630 | |
676 | |
631 | // [exp] |
677 | // [exp] |
632 | if (*dec->cur == 'e' || *dec->cur == 'E') |
678 | if (*dec->cur == 'e' || *dec->cur == 'E') |
633 | { |
679 | { |
634 | is_nv = 1; |
|
|
635 | |
|
|
636 | ++dec->cur; |
680 | ++dec->cur; |
|
|
681 | |
637 | if (*dec->cur == '-' || *dec->cur == '+') |
682 | if (*dec->cur == '-' || *dec->cur == '+') |
638 | ++dec->cur; |
683 | ++dec->cur; |
639 | |
684 | |
|
|
685 | if (*dec->cur < '0' || *dec->cur > '9') |
|
|
686 | ERR ("malformed number (no digits after exp sign)"); |
|
|
687 | |
|
|
688 | do |
|
|
689 | { |
|
|
690 | ++dec->cur; |
|
|
691 | } |
640 | while (*dec->cur >= '0' && *dec->cur <= '9') |
692 | while (*dec->cur >= '0' && *dec->cur <= '9'); |
641 | ++dec->cur; |
693 | |
|
|
694 | is_nv = 1; |
642 | } |
695 | } |
643 | |
696 | |
644 | if (!is_nv) |
697 | if (!is_nv) |
645 | { |
698 | { |
646 | UV uv; |
699 | UV uv; |
… | |
… | |
664 | static SV * |
717 | static SV * |
665 | decode_av (dec_t *dec) |
718 | decode_av (dec_t *dec) |
666 | { |
719 | { |
667 | AV *av = newAV (); |
720 | AV *av = newAV (); |
668 | |
721 | |
|
|
722 | WS; |
|
|
723 | if (*dec->cur == ']') |
|
|
724 | ++dec->cur; |
|
|
725 | else |
669 | for (;;) |
726 | for (;;) |
670 | { |
727 | { |
671 | SV *value; |
728 | SV *value; |
672 | |
729 | |
673 | value = decode_sv (dec); |
730 | value = decode_sv (dec); |
674 | if (!value) |
731 | if (!value) |
675 | goto fail; |
732 | goto fail; |
676 | |
733 | |
677 | av_push (av, value); |
734 | av_push (av, value); |
678 | |
735 | |
679 | WS; |
736 | WS; |
680 | |
737 | |
681 | if (*dec->cur == ']') |
738 | if (*dec->cur == ']') |
682 | { |
739 | { |
683 | ++dec->cur; |
740 | ++dec->cur; |
684 | break; |
741 | break; |
|
|
742 | } |
685 | } |
743 | |
686 | |
|
|
687 | if (*dec->cur != ',') |
744 | if (*dec->cur != ',') |
688 | ERR (", or ] expected while parsing array"); |
745 | ERR (", or ] expected while parsing array"); |
689 | |
746 | |
690 | ++dec->cur; |
747 | ++dec->cur; |
691 | } |
748 | } |
692 | |
749 | |
693 | return newRV_noinc ((SV *)av); |
750 | return newRV_noinc ((SV *)av); |
694 | |
751 | |
695 | fail: |
752 | fail: |
696 | SvREFCNT_dec (av); |
753 | SvREFCNT_dec (av); |
… | |
… | |
700 | static SV * |
757 | static SV * |
701 | decode_hv (dec_t *dec) |
758 | decode_hv (dec_t *dec) |
702 | { |
759 | { |
703 | HV *hv = newHV (); |
760 | HV *hv = newHV (); |
704 | |
761 | |
|
|
762 | WS; |
|
|
763 | if (*dec->cur == '}') |
|
|
764 | ++dec->cur; |
|
|
765 | else |
705 | for (;;) |
766 | for (;;) |
706 | { |
767 | { |
707 | SV *key, *value; |
768 | SV *key, *value; |
708 | |
769 | |
709 | WS; EXPECT_CH ('"'); |
770 | WS; EXPECT_CH ('"'); |
710 | |
771 | |
711 | key = decode_str (dec); |
772 | key = decode_str (dec); |
712 | if (!key) |
773 | if (!key) |
713 | goto fail; |
|
|
714 | |
|
|
715 | WS; EXPECT_CH (':'); |
|
|
716 | |
|
|
717 | value = decode_sv (dec); |
|
|
718 | if (!value) |
|
|
719 | { |
|
|
720 | SvREFCNT_dec (key); |
|
|
721 | goto fail; |
774 | goto fail; |
|
|
775 | |
|
|
776 | WS; EXPECT_CH (':'); |
|
|
777 | |
|
|
778 | value = decode_sv (dec); |
|
|
779 | if (!value) |
|
|
780 | { |
|
|
781 | SvREFCNT_dec (key); |
|
|
782 | goto fail; |
722 | } |
783 | } |
723 | |
784 | |
724 | //TODO: optimise |
785 | //TODO: optimise |
725 | hv_store_ent (hv, key, value, 0); |
786 | hv_store_ent (hv, key, value, 0); |
726 | |
787 | |
727 | WS; |
788 | WS; |
728 | |
789 | |
729 | if (*dec->cur == '}') |
790 | if (*dec->cur == '}') |
730 | { |
791 | { |
731 | ++dec->cur; |
792 | ++dec->cur; |
732 | break; |
793 | break; |
733 | } |
794 | } |
734 | |
795 | |
735 | if (*dec->cur != ',') |
796 | if (*dec->cur != ',') |
736 | ERR (", or } expected while parsing object/hash"); |
797 | ERR (", or } expected while parsing object/hash"); |
737 | |
798 | |
738 | ++dec->cur; |
799 | ++dec->cur; |
739 | } |
800 | } |
740 | |
801 | |
741 | return newRV_noinc ((SV *)hv); |
802 | return newRV_noinc ((SV *)hv); |
742 | |
803 | |
743 | fail: |
804 | fail: |
744 | SvREFCNT_dec (hv); |
805 | SvREFCNT_dec (hv); |
… | |
… | |
784 | |
845 | |
785 | case 'n': |
846 | case 'n': |
786 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "null", 4)) |
847 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "null", 4)) |
787 | { |
848 | { |
788 | dec->cur += 4; |
849 | dec->cur += 4; |
789 | return newSViv (1); |
850 | return newSVsv (&PL_sv_undef); |
790 | } |
851 | } |
791 | else |
852 | else |
792 | ERR ("'null' expected"); |
853 | ERR ("'null' expected"); |
793 | |
854 | |
794 | break; |
855 | break; |
795 | |
856 | |
796 | default: |
857 | default: |
797 | ERR ("malformed json string"); |
858 | ERR ("malformed json string, neither array, object, number, string or atom"); |
798 | break; |
859 | break; |
799 | } |
860 | } |
800 | |
861 | |
801 | fail: |
862 | fail: |
802 | return 0; |
863 | return 0; |
… | |
… | |
805 | static SV * |
866 | static SV * |
806 | decode_json (SV *string, UV flags) |
867 | decode_json (SV *string, UV flags) |
807 | { |
868 | { |
808 | SV *sv; |
869 | SV *sv; |
809 | |
870 | |
810 | if (!(flags & F_UTF8)) |
871 | if (flags & F_UTF8) |
|
|
872 | sv_utf8_downgrade (string, 0); |
|
|
873 | else |
811 | sv_utf8_upgrade (string); |
874 | sv_utf8_upgrade (string); |
812 | |
875 | |
813 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
876 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
814 | |
877 | |
815 | dec_t dec; |
878 | dec_t dec; |
816 | dec.flags = flags; |
879 | dec.flags = flags; |
817 | dec.cur = SvPVX (string); |
880 | dec.cur = SvPVX (string); |
818 | dec.end = SvEND (string); |
881 | dec.end = SvEND (string); |
819 | dec.err = 0; |
882 | dec.err = 0; |
820 | |
883 | |
821 | *dec.end = 1; // invalid anywhere |
|
|
822 | sv = decode_sv (&dec); |
884 | sv = decode_sv (&dec); |
823 | *dec.end = 0; |
|
|
824 | |
885 | |
825 | if (!sv) |
886 | if (!sv) |
826 | { |
887 | { |
|
|
888 | IV offset = dec.flags & F_UTF8 |
|
|
889 | ? dec.cur - SvPVX (string) |
827 | IV offset = utf8_distance (dec.cur, SvPVX (string)); |
890 | : utf8_distance (dec.cur, SvPVX (string)); |
828 | SV *uni = sv_newmortal (); |
891 | SV *uni = sv_newmortal (); |
829 | |
892 | |
|
|
893 | // horrible hack to silence warning inside pv_uni_display |
|
|
894 | COP cop = *PL_curcop; |
|
|
895 | cop.cop_warnings = pWARN_NONE; |
|
|
896 | ENTER; |
|
|
897 | SAVEVPTR (PL_curcop); |
|
|
898 | PL_curcop = &cop; |
830 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
899 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
|
|
900 | LEAVE; |
|
|
901 | |
831 | croak ("%s, at character %d (%s)", |
902 | croak ("%s, at character offset %d (%s)", |
832 | dec.err, |
903 | dec.err, |
833 | (int)offset, |
904 | (int)offset, |
834 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
905 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
835 | } |
906 | } |
836 | |
907 | |
837 | sv = sv_2mortal (sv); |
908 | sv = sv_2mortal (sv); |
838 | |
909 | |
839 | if (!(dec.flags & F_ALLOW_NONREF) && !SvROK (sv)) |
910 | if (!(dec.flags & F_ALLOW_NONREF) && !SvROK (sv)) |
840 | croak ("JSON object or array expected (but number, string, true, false or null found, use allow_nonref to allow this)"); |
911 | croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)"); |
841 | |
912 | |
842 | return sv; |
913 | return sv; |
843 | } |
914 | } |
844 | |
915 | |
845 | MODULE = JSON::XS PACKAGE = JSON::XS |
916 | MODULE = JSON::XS PACKAGE = JSON::XS |
… | |
… | |
867 | CODE: |
938 | CODE: |
868 | RETVAL = sv_bless (newRV_noinc (newSVuv (F_DEFAULT)), json_stash); |
939 | RETVAL = sv_bless (newRV_noinc (newSVuv (F_DEFAULT)), json_stash); |
869 | OUTPUT: |
940 | OUTPUT: |
870 | RETVAL |
941 | RETVAL |
871 | |
942 | |
872 | SV *ascii (SV *self, int enable) |
943 | SV *ascii (SV *self, int enable = 1) |
873 | ALIAS: |
944 | ALIAS: |
874 | ascii = F_ASCII |
945 | ascii = F_ASCII |
875 | utf8 = F_UTF8 |
946 | utf8 = F_UTF8 |
876 | indent = F_INDENT |
947 | indent = F_INDENT |
877 | canonical = F_CANONICAL |
948 | canonical = F_CANONICAL |
878 | space_before = F_SPACE_BEFORE |
949 | space_before = F_SPACE_BEFORE |
879 | space_after = F_SPACE_AFTER |
950 | space_after = F_SPACE_AFTER |
880 | json_rpc = F_JSON_RPC |
951 | json_rpc = F_JSON_RPC |
881 | pretty = F_PRETTY |
952 | pretty = F_PRETTY |
882 | allow_nonref = F_ALLOW_NONREF |
953 | allow_nonref = F_ALLOW_NONREF |
|
|
954 | shrink = F_SHRINK |
883 | CODE: |
955 | CODE: |
884 | { |
956 | { |
885 | UV *uv = SvJSON (self); |
957 | UV *uv = SvJSON (self); |
886 | if (enable) |
958 | if (enable) |
887 | *uv |= ix; |
959 | *uv |= ix; |