… | |
… | |
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 |
… | |
… | |
40 | // structure used for decoding JSON |
41 | // structure used for decoding JSON |
41 | typedef struct |
42 | typedef struct |
42 | { |
43 | { |
43 | char *cur; |
44 | char *cur; |
44 | char *end; |
45 | char *end; |
45 | char *err; |
46 | const char *err; |
46 | UV flags; |
47 | UV flags; |
47 | } dec_t; |
48 | } dec_t; |
48 | |
49 | |
49 | static UV * |
50 | static UV * |
50 | SvJSON (SV *sv) |
51 | SvJSON (SV *sv) |
51 | { |
52 | { |
52 | if (!(SvROK (sv) && SvOBJECT (SvRV (sv)) && SvSTASH (SvRV (sv)) == json_stash)) |
53 | if (!(SvROK (sv) && SvOBJECT (SvRV (sv)) && SvSTASH (SvRV (sv)) == json_stash)) |
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)); |
|
|
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 |
56 | } |
66 | } |
57 | |
67 | |
58 | ///////////////////////////////////////////////////////////////////////////// |
68 | ///////////////////////////////////////////////////////////////////////////// |
59 | |
69 | |
60 | static void |
70 | static void |
… | |
… | |
63 | if (enc->cur + len >= enc->end) |
73 | if (enc->cur + len >= enc->end) |
64 | { |
74 | { |
65 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
75 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
66 | SvGROW (enc->sv, cur + len + 1); |
76 | SvGROW (enc->sv, cur + len + 1); |
67 | enc->cur = SvPVX (enc->sv) + cur; |
77 | enc->cur = SvPVX (enc->sv) + cur; |
68 | enc->end = SvEND (enc->sv); |
78 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv); |
69 | } |
79 | } |
70 | } |
80 | } |
71 | |
81 | |
72 | static void |
82 | static void |
73 | encode_ch (enc_t *enc, char ch) |
83 | encode_ch (enc_t *enc, char ch) |
… | |
… | |
79 | static void |
89 | static void |
80 | encode_str (enc_t *enc, char *str, STRLEN len, int is_utf8) |
90 | encode_str (enc_t *enc, char *str, STRLEN len, int is_utf8) |
81 | { |
91 | { |
82 | char *end = str + len; |
92 | char *end = str + len; |
83 | |
93 | |
|
|
94 | need (enc, len); |
|
|
95 | |
84 | while (str < end) |
96 | while (str < end) |
85 | { |
97 | { |
86 | unsigned char ch = *(unsigned char *)str; |
98 | unsigned char ch = *(unsigned char *)str; |
|
|
99 | |
87 | if (ch >= 0x20 && ch < 0x80) // most common case |
100 | if (ch >= 0x20 && ch < 0x80) // most common case |
88 | { |
101 | { |
|
|
102 | if (ch == '"') // but with slow exceptions |
|
|
103 | { |
|
|
104 | need (enc, len += 1); |
|
|
105 | *enc->cur++ = '\\'; |
|
|
106 | *enc->cur++ = '"'; |
|
|
107 | } |
|
|
108 | else if (ch == '\\') |
|
|
109 | { |
|
|
110 | need (enc, len += 1); |
|
|
111 | *enc->cur++ = '\\'; |
|
|
112 | *enc->cur++ = '\\'; |
|
|
113 | } |
|
|
114 | else |
89 | *enc->cur++ = ch; |
115 | *enc->cur++ = ch; |
|
|
116 | |
90 | str++; |
117 | ++str; |
91 | } |
118 | } |
92 | else |
119 | else |
93 | { |
120 | { |
94 | STRLEN clen; |
121 | switch (ch) |
95 | UV uch; |
|
|
96 | |
|
|
97 | if (is_utf8) |
|
|
98 | { |
122 | { |
99 | 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; |
100 | if (clen < 0) |
124 | case '\011': need (enc, len += 1); *enc->cur++ = '\\'; *enc->cur++ = 't'; ++str; break; |
101 | 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; |
102 | } |
126 | case '\014': need (enc, len += 1); *enc->cur++ = '\\'; *enc->cur++ = 'f'; ++str; break; |
103 | else |
127 | case '\015': need (enc, len += 1); *enc->cur++ = '\\'; *enc->cur++ = 'r'; ++str; break; |
104 | { |
|
|
105 | uch = ch; |
|
|
106 | clen = 1; |
|
|
107 | } |
|
|
108 | |
128 | |
109 | need (enc, len += 6); |
129 | default: |
110 | |
|
|
111 | if (uch < 0xa0 || enc->flags & F_ASCII) |
|
|
112 | { |
|
|
113 | if (uch > 0xFFFFUL) |
|
|
114 | { |
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 UTF-8 character in string, cannot convert to JSON"); |
|
|
139 | } |
|
|
140 | else |
|
|
141 | { |
|
|
142 | uch = ch; |
115 | len += 6; |
143 | clen = 1; |
|
|
144 | } |
|
|
145 | |
|
|
146 | if (uch < 0x80 || enc->flags & F_ASCII) |
|
|
147 | { |
|
|
148 | if (uch > 0xFFFFUL) |
|
|
149 | { |
116 | need (enc, len += 6); |
150 | need (enc, len += 11); |
117 | sprintf (enc->cur, "\\u%04x\\u%04x", |
151 | sprintf (enc->cur, "\\u%04x\\u%04x", |
118 | (uch - 0x10000) / 0x400 + 0xD800, |
152 | (uch - 0x10000) / 0x400 + 0xD800, |
119 | (uch - 0x10000) % 0x400 + 0xDC00); |
153 | (uch - 0x10000) % 0x400 + 0xDC00); |
120 | enc->cur += 12; |
154 | enc->cur += 12; |
|
|
155 | } |
|
|
156 | else |
|
|
157 | { |
|
|
158 | static char hexdigit [16] = "0123456789abcdef"; |
|
|
159 | need (enc, len += 5); |
|
|
160 | *enc->cur++ = '\\'; |
|
|
161 | *enc->cur++ = 'u'; |
|
|
162 | *enc->cur++ = hexdigit [ uch >> 12 ]; |
|
|
163 | *enc->cur++ = hexdigit [(uch >> 8) & 15]; |
|
|
164 | *enc->cur++ = hexdigit [(uch >> 4) & 15]; |
|
|
165 | *enc->cur++ = hexdigit [(uch >> 0) & 15]; |
|
|
166 | } |
|
|
167 | |
|
|
168 | str += clen; |
121 | } |
169 | } |
|
|
170 | else if (is_utf8) |
|
|
171 | { |
|
|
172 | need (enc, len += clen); |
|
|
173 | do |
|
|
174 | { |
|
|
175 | *enc->cur++ = *str++; |
|
|
176 | } |
|
|
177 | while (--clen); |
|
|
178 | } |
122 | else |
179 | else |
123 | { |
180 | { |
124 | sprintf (enc->cur, "\\u%04x", uch); |
181 | need (enc, len += 10); // never more than 11 bytes needed |
125 | enc->cur += 6; |
182 | enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
|
|
183 | ++str; |
|
|
184 | } |
126 | } |
185 | } |
127 | } |
186 | } |
128 | else if (is_utf8) |
|
|
129 | { |
|
|
130 | memcpy (enc->cur, str, clen); |
|
|
131 | enc->cur += clen; |
|
|
132 | } |
|
|
133 | else |
|
|
134 | enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
|
|
135 | |
|
|
136 | str += clen; |
|
|
137 | } |
187 | } |
138 | |
188 | |
139 | --len; |
189 | --len; |
140 | } |
190 | } |
141 | } |
191 | } |
… | |
… | |
192 | |
242 | |
193 | if (HeKLEN (he) == HEf_SVKEY) |
243 | if (HeKLEN (he) == HEf_SVKEY) |
194 | { |
244 | { |
195 | SV *sv = HeSVKEY (he); |
245 | SV *sv = HeSVKEY (he); |
196 | STRLEN len; |
246 | STRLEN len; |
|
|
247 | char *str; |
|
|
248 | |
|
|
249 | SvGETMAGIC (sv); |
197 | char *str = SvPV (sv, len); |
250 | str = SvPV (sv, len); |
198 | |
251 | |
199 | encode_str (enc, str, len, SvUTF8 (sv)); |
252 | encode_str (enc, str, len, SvUTF8 (sv)); |
200 | } |
253 | } |
201 | else |
254 | else |
202 | encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); |
255 | encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); |
… | |
… | |
313 | } |
366 | } |
314 | |
367 | |
315 | static void |
368 | static void |
316 | encode_sv (enc_t *enc, SV *sv) |
369 | encode_sv (enc_t *enc, SV *sv) |
317 | { |
370 | { |
|
|
371 | SvGETMAGIC (sv); |
|
|
372 | |
318 | if (SvPOKp (sv)) |
373 | if (SvPOKp (sv)) |
319 | { |
374 | { |
320 | STRLEN len; |
375 | STRLEN len; |
321 | char *str = SvPV (sv, len); |
376 | char *str = SvPV (sv, len); |
322 | encode_ch (enc, '"'); |
377 | encode_ch (enc, '"'); |
… | |
… | |
378 | |
433 | |
379 | if (!(flags & (F_ASCII | F_UTF8))) |
434 | if (!(flags & (F_ASCII | F_UTF8))) |
380 | SvUTF8_on (enc.sv); |
435 | SvUTF8_on (enc.sv); |
381 | |
436 | |
382 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
437 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
|
|
438 | |
|
|
439 | if (enc.flags & F_SHRINK) |
|
|
440 | shrink (enc.sv); |
|
|
441 | |
383 | return enc.sv; |
442 | return enc.sv; |
384 | } |
443 | } |
385 | |
444 | |
386 | ///////////////////////////////////////////////////////////////////////////// |
445 | ///////////////////////////////////////////////////////////////////////////// |
387 | |
446 | |
… | |
… | |
402 | ++dec->cur; \ |
461 | ++dec->cur; \ |
403 | SE |
462 | SE |
404 | |
463 | |
405 | static SV *decode_sv (dec_t *dec); |
464 | static SV *decode_sv (dec_t *dec); |
406 | |
465 | |
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]; |
466 | static signed char decode_hexdigit[256]; |
413 | |
467 | |
414 | static UV |
468 | static UV |
415 | decode_4hex (dec_t *dec) |
469 | decode_4hex (dec_t *dec) |
416 | { |
470 | { |
… | |
… | |
434 | |
488 | |
435 | fail: |
489 | fail: |
436 | return (UV)-1; |
490 | return (UV)-1; |
437 | } |
491 | } |
438 | |
492 | |
|
|
493 | #define APPEND_GROW(n) SB \ |
|
|
494 | if (cur + (n) >= end) \ |
|
|
495 | { \ |
|
|
496 | STRLEN ofs = cur - SvPVX (sv); \ |
|
|
497 | SvGROW (sv, ofs + (n) + 1); \ |
|
|
498 | cur = SvPVX (sv) + ofs; \ |
|
|
499 | end = SvEND (sv); \ |
|
|
500 | } \ |
|
|
501 | SE |
|
|
502 | |
|
|
503 | #define APPEND_CH(ch) SB \ |
|
|
504 | APPEND_GROW (1); \ |
|
|
505 | *cur++ = (ch); \ |
|
|
506 | SE |
|
|
507 | |
439 | static SV * |
508 | static SV * |
440 | decode_str (dec_t *dec) |
509 | decode_str (dec_t *dec) |
441 | { |
510 | { |
442 | SV *sv = NEWSV (0,2); |
511 | SV *sv = NEWSV (0,2); |
443 | STRLEN cur = 0; |
|
|
444 | int utf8 = 0; |
512 | int utf8 = 0; |
|
|
513 | char *cur = SvPVX (sv); |
|
|
514 | char *end = SvEND (sv); |
445 | |
515 | |
446 | for (;;) |
516 | for (;;) |
447 | { |
517 | { |
448 | unsigned char ch = *(unsigned char *)dec->cur; |
518 | unsigned char ch = *(unsigned char *)dec->cur; |
449 | |
519 | |
… | |
… | |
474 | |
544 | |
475 | // possibly a surrogate pair |
545 | // possibly a surrogate pair |
476 | if (hi >= 0xd800 && hi < 0xdc00) |
546 | if (hi >= 0xd800 && hi < 0xdc00) |
477 | { |
547 | { |
478 | if (dec->cur [0] != '\\' || dec->cur [1] != 'u') |
548 | if (dec->cur [0] != '\\' || dec->cur [1] != 'u') |
479 | ERR ("illegal surrogate character"); |
549 | ERR ("missing low surrogate character in surrogate pair"); |
480 | |
550 | |
481 | dec->cur += 2; |
551 | dec->cur += 2; |
482 | |
552 | |
483 | lo = decode_4hex (dec); |
553 | lo = decode_4hex (dec); |
484 | if (lo == (UV)-1) |
554 | if (lo == (UV)-1) |
… | |
… | |
487 | if (lo < 0xdc00 || lo >= 0xe000) |
557 | if (lo < 0xdc00 || lo >= 0xe000) |
488 | ERR ("surrogate pair expected"); |
558 | ERR ("surrogate pair expected"); |
489 | |
559 | |
490 | hi = (hi - 0xD800) * 0x400 + (lo - 0xDC00) + 0x10000; |
560 | hi = (hi - 0xD800) * 0x400 + (lo - 0xDC00) + 0x10000; |
491 | } |
561 | } |
492 | else if (lo >= 0xdc00 && lo < 0xe000) |
562 | else if (hi >= 0xdc00 && hi < 0xe000) |
493 | ERR ("illegal surrogate character"); |
563 | ERR ("missing high surrogate character in surrogate pair"); |
494 | |
564 | |
495 | if (hi >= 0x80) |
565 | if (hi >= 0x80) |
496 | { |
566 | { |
497 | utf8 = 1; |
567 | utf8 = 1; |
498 | |
568 | |
499 | SvGROW (sv, cur + 4 + 1); // at most 4 bytes for 21 bits |
569 | APPEND_GROW (4); // at most 4 bytes for 21 bits |
500 | cur = (char *)uvuni_to_utf8_flags (SvPVX (sv) + cur, hi, 0) - SvPVX (sv); |
570 | cur = (char *)uvuni_to_utf8_flags (cur, hi, 0); |
501 | } |
571 | } |
502 | else |
572 | else |
503 | APPEND_CH (hi); |
573 | APPEND_CH (hi); |
504 | } |
574 | } |
505 | break; |
575 | break; |
|
|
576 | |
|
|
577 | default: |
|
|
578 | --dec->cur; |
|
|
579 | ERR ("illegal backslash escape sequence in string"); |
506 | } |
580 | } |
507 | } |
581 | } |
508 | else if (ch >= 0x20 && ch <= 0x7f) |
582 | else if (ch >= 0x20 && ch <= 0x7f) |
509 | APPEND_CH (*dec->cur++); |
583 | APPEND_CH (*dec->cur++); |
510 | else if (ch >= 0x80) |
584 | else if (ch >= 0x80) |
511 | { |
585 | { |
512 | STRLEN clen; |
586 | STRLEN clen; |
513 | UV uch = utf8n_to_uvuni (dec->cur, dec->end - dec->cur, &clen, UTF8_CHECK_ONLY); |
587 | UV uch = utf8n_to_uvuni (dec->cur, dec->end - dec->cur, &clen, UTF8_CHECK_ONLY); |
514 | if (clen < 0) |
588 | if (clen == (STRLEN)-1) |
515 | ERR ("malformed UTF-8 character in string, cannot convert to JSON"); |
589 | ERR ("malformed UTF-8 character in JSON string"); |
516 | |
590 | |
517 | SvGROW (sv, cur + clen + 1); // at most 4 bytes for 21 bits |
591 | APPEND_GROW (clen); |
518 | memcpy (SvPVX (sv) + cur, dec->cur, clen); |
592 | do |
519 | dec->cur += clen; |
593 | { |
|
|
594 | *cur++ = *dec->cur++; |
|
|
595 | } |
|
|
596 | while (--clen); |
|
|
597 | |
|
|
598 | utf8 = 1; |
520 | } |
599 | } |
|
|
600 | else if (dec->cur == dec->end) |
|
|
601 | ERR ("unexpected end of string while parsing json string"); |
521 | else |
602 | else |
522 | ERR ("invalid character encountered"); |
603 | ERR ("invalid character encountered"); |
523 | } |
604 | } |
524 | |
605 | |
525 | ++dec->cur; |
606 | ++dec->cur; |
526 | |
607 | |
|
|
608 | SvCUR_set (sv, cur - SvPVX (sv)); |
|
|
609 | |
527 | SvPOK_only (sv); |
610 | SvPOK_only (sv); |
528 | |
|
|
529 | SvCUR_set (sv, cur); |
|
|
530 | *SvEND (sv) = 0; |
611 | *SvEND (sv) = 0; |
531 | |
612 | |
532 | if (utf8) |
613 | if (utf8) |
533 | SvUTF8_on (sv); |
614 | SvUTF8_on (sv); |
|
|
615 | |
|
|
616 | if (dec->flags & F_SHRINK) |
|
|
617 | shrink (sv); |
534 | |
618 | |
535 | return sv; |
619 | return sv; |
536 | |
620 | |
537 | fail: |
621 | fail: |
538 | SvREFCNT_dec (sv); |
622 | SvREFCNT_dec (sv); |
… | |
… | |
553 | { |
637 | { |
554 | ++dec->cur; |
638 | ++dec->cur; |
555 | if (*dec->cur >= '0' && *dec->cur <= '9') |
639 | if (*dec->cur >= '0' && *dec->cur <= '9') |
556 | ERR ("malformed number (leading zero must not be followed by another digit)"); |
640 | ERR ("malformed number (leading zero must not be followed by another digit)"); |
557 | } |
641 | } |
558 | |
642 | else if (*dec->cur < '0' || *dec->cur > '9') |
559 | // int |
643 | ERR ("malformed number (no digits after initial minus)"); |
|
|
644 | else |
|
|
645 | do |
|
|
646 | { |
|
|
647 | ++dec->cur; |
|
|
648 | } |
560 | while (*dec->cur >= '0' && *dec->cur <= '9') |
649 | while (*dec->cur >= '0' && *dec->cur <= '9'); |
561 | ++dec->cur; |
|
|
562 | |
650 | |
563 | // [frac] |
651 | // [frac] |
564 | if (*dec->cur == '.') |
652 | if (*dec->cur == '.') |
565 | { |
653 | { |
566 | is_nv = 1; |
654 | ++dec->cur; |
|
|
655 | |
|
|
656 | if (*dec->cur < '0' || *dec->cur > '9') |
|
|
657 | ERR ("malformed number (no digits after decimal point)"); |
567 | |
658 | |
568 | do |
659 | do |
569 | { |
660 | { |
570 | ++dec->cur; |
661 | ++dec->cur; |
571 | } |
662 | } |
572 | while (*dec->cur >= '0' && *dec->cur <= '9'); |
663 | while (*dec->cur >= '0' && *dec->cur <= '9'); |
|
|
664 | |
|
|
665 | is_nv = 1; |
573 | } |
666 | } |
574 | |
667 | |
575 | // [exp] |
668 | // [exp] |
576 | if (*dec->cur == 'e' || *dec->cur == 'E') |
669 | if (*dec->cur == 'e' || *dec->cur == 'E') |
577 | { |
670 | { |
578 | is_nv = 1; |
|
|
579 | |
|
|
580 | ++dec->cur; |
671 | ++dec->cur; |
|
|
672 | |
581 | if (*dec->cur == '-' || *dec->cur == '+') |
673 | if (*dec->cur == '-' || *dec->cur == '+') |
582 | ++dec->cur; |
674 | ++dec->cur; |
583 | |
675 | |
|
|
676 | if (*dec->cur < '0' || *dec->cur > '9') |
|
|
677 | ERR ("malformed number (no digits after exp sign)"); |
|
|
678 | |
|
|
679 | do |
|
|
680 | { |
|
|
681 | ++dec->cur; |
|
|
682 | } |
584 | while (*dec->cur >= '0' && *dec->cur <= '9') |
683 | while (*dec->cur >= '0' && *dec->cur <= '9'); |
585 | ++dec->cur; |
684 | |
|
|
685 | is_nv = 1; |
586 | } |
686 | } |
587 | |
687 | |
588 | if (!is_nv) |
688 | if (!is_nv) |
589 | { |
689 | { |
590 | UV uv; |
690 | UV uv; |
… | |
… | |
608 | static SV * |
708 | static SV * |
609 | decode_av (dec_t *dec) |
709 | decode_av (dec_t *dec) |
610 | { |
710 | { |
611 | AV *av = newAV (); |
711 | AV *av = newAV (); |
612 | |
712 | |
|
|
713 | WS; |
|
|
714 | if (*dec->cur == ']') |
|
|
715 | ++dec->cur; |
|
|
716 | else |
613 | for (;;) |
717 | for (;;) |
614 | { |
718 | { |
615 | SV *value; |
719 | SV *value; |
616 | |
720 | |
617 | value = decode_sv (dec); |
721 | value = decode_sv (dec); |
618 | if (!value) |
722 | if (!value) |
619 | goto fail; |
723 | goto fail; |
620 | |
724 | |
621 | av_push (av, value); |
725 | av_push (av, value); |
622 | |
726 | |
623 | WS; |
727 | WS; |
624 | |
728 | |
625 | if (*dec->cur == ']') |
729 | if (*dec->cur == ']') |
626 | { |
730 | { |
627 | ++dec->cur; |
731 | ++dec->cur; |
628 | break; |
732 | break; |
|
|
733 | } |
629 | } |
734 | |
630 | |
|
|
631 | if (*dec->cur != ',') |
735 | if (*dec->cur != ',') |
632 | ERR (", or ] expected while parsing array"); |
736 | ERR (", or ] expected while parsing array"); |
633 | |
737 | |
634 | ++dec->cur; |
738 | ++dec->cur; |
635 | } |
739 | } |
636 | |
740 | |
637 | return newRV_noinc ((SV *)av); |
741 | return newRV_noinc ((SV *)av); |
638 | |
742 | |
639 | fail: |
743 | fail: |
640 | SvREFCNT_dec (av); |
744 | SvREFCNT_dec (av); |
… | |
… | |
644 | static SV * |
748 | static SV * |
645 | decode_hv (dec_t *dec) |
749 | decode_hv (dec_t *dec) |
646 | { |
750 | { |
647 | HV *hv = newHV (); |
751 | HV *hv = newHV (); |
648 | |
752 | |
|
|
753 | WS; |
|
|
754 | if (*dec->cur == '}') |
|
|
755 | ++dec->cur; |
|
|
756 | else |
649 | for (;;) |
757 | for (;;) |
650 | { |
758 | { |
651 | SV *key, *value; |
759 | SV *key, *value; |
652 | |
760 | |
653 | WS; EXPECT_CH ('"'); |
761 | WS; EXPECT_CH ('"'); |
654 | |
762 | |
655 | key = decode_str (dec); |
763 | key = decode_str (dec); |
656 | if (!key) |
764 | 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; |
765 | goto fail; |
|
|
766 | |
|
|
767 | WS; EXPECT_CH (':'); |
|
|
768 | |
|
|
769 | value = decode_sv (dec); |
|
|
770 | if (!value) |
|
|
771 | { |
|
|
772 | SvREFCNT_dec (key); |
|
|
773 | goto fail; |
666 | } |
774 | } |
667 | |
775 | |
668 | //TODO: optimise |
776 | //TODO: optimise |
669 | hv_store_ent (hv, key, value, 0); |
777 | hv_store_ent (hv, key, value, 0); |
670 | |
778 | |
671 | WS; |
779 | WS; |
672 | |
780 | |
673 | if (*dec->cur == '}') |
781 | if (*dec->cur == '}') |
674 | { |
782 | { |
675 | ++dec->cur; |
783 | ++dec->cur; |
676 | break; |
784 | break; |
677 | } |
785 | } |
678 | |
786 | |
679 | if (*dec->cur != ',') |
787 | if (*dec->cur != ',') |
680 | ERR (", or } expected while parsing object/hash"); |
788 | ERR (", or } expected while parsing object/hash"); |
681 | |
789 | |
682 | ++dec->cur; |
790 | ++dec->cur; |
683 | } |
791 | } |
684 | |
792 | |
685 | return newRV_noinc ((SV *)hv); |
793 | return newRV_noinc ((SV *)hv); |
686 | |
794 | |
687 | fail: |
795 | fail: |
688 | SvREFCNT_dec (hv); |
796 | SvREFCNT_dec (hv); |
… | |
… | |
728 | |
836 | |
729 | case 'n': |
837 | case 'n': |
730 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "null", 4)) |
838 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "null", 4)) |
731 | { |
839 | { |
732 | dec->cur += 4; |
840 | dec->cur += 4; |
733 | return newSViv (1); |
841 | return newSVsv (&PL_sv_undef); |
734 | } |
842 | } |
735 | else |
843 | else |
736 | ERR ("'null' expected"); |
844 | ERR ("'null' expected"); |
737 | |
845 | |
738 | break; |
846 | break; |
739 | |
847 | |
740 | default: |
848 | default: |
741 | ERR ("malformed json string"); |
849 | ERR ("malformed json string, neither array, object, number, string or atom"); |
742 | break; |
850 | break; |
743 | } |
851 | } |
744 | |
852 | |
745 | fail: |
853 | fail: |
746 | return 0; |
854 | return 0; |
… | |
… | |
749 | static SV * |
857 | static SV * |
750 | decode_json (SV *string, UV flags) |
858 | decode_json (SV *string, UV flags) |
751 | { |
859 | { |
752 | SV *sv; |
860 | SV *sv; |
753 | |
861 | |
754 | if (!(flags & F_UTF8)) |
862 | if (flags & F_UTF8) |
|
|
863 | sv_utf8_downgrade (string, 0); |
|
|
864 | else |
755 | sv_utf8_upgrade (string); |
865 | sv_utf8_upgrade (string); |
756 | |
866 | |
757 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
867 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
758 | |
868 | |
759 | dec_t dec; |
869 | dec_t dec; |
760 | dec.flags = flags; |
870 | dec.flags = flags; |
761 | dec.cur = SvPVX (string); |
871 | dec.cur = SvPVX (string); |
762 | dec.end = SvEND (string); |
872 | dec.end = SvEND (string); |
763 | dec.err = 0; |
873 | dec.err = 0; |
764 | |
874 | |
765 | *dec.end = 1; // invalid anywhere |
|
|
766 | sv = decode_sv (&dec); |
875 | sv = decode_sv (&dec); |
767 | *dec.end = 0; |
|
|
768 | |
876 | |
769 | if (!sv) |
877 | if (!sv) |
770 | { |
878 | { |
|
|
879 | IV offset = dec.flags & F_UTF8 |
|
|
880 | ? dec.cur - SvPVX (string) |
771 | IV offset = utf8_distance (dec.cur, SvPVX (string)); |
881 | : utf8_distance (dec.cur, SvPVX (string)); |
772 | SV *uni = sv_newmortal (); |
882 | SV *uni = sv_newmortal (); |
|
|
883 | // horrible hack to silence warning inside pv_uni_display |
|
|
884 | COP cop; |
|
|
885 | memset (&cop, 0, sizeof (cop)); |
|
|
886 | cop.cop_warnings = pWARN_NONE; |
|
|
887 | SAVEVPTR (PL_curcop); |
|
|
888 | PL_curcop = &cop; |
773 | |
889 | |
774 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
890 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
775 | croak ("%s, at character %d (%s)", |
891 | croak ("%s, at character offset %d (%s)", |
776 | dec.err, |
892 | dec.err, |
777 | (int)offset, |
893 | (int)offset, |
778 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
894 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
779 | } |
895 | } |
780 | |
896 | |
… | |
… | |
794 | |
910 | |
795 | memset (decode_hexdigit, 0xff, 256); |
911 | memset (decode_hexdigit, 0xff, 256); |
796 | for (i = 10; i--; ) |
912 | for (i = 10; i--; ) |
797 | decode_hexdigit ['0' + i] = i; |
913 | decode_hexdigit ['0' + i] = i; |
798 | |
914 | |
799 | for (i = 6; --i; ) |
915 | for (i = 7; i--; ) |
800 | { |
916 | { |
801 | decode_hexdigit ['a' + i] = 10 + i; |
917 | decode_hexdigit ['a' + i] = 10 + i; |
802 | decode_hexdigit ['A' + i] = 10 + i; |
918 | decode_hexdigit ['A' + i] = 10 + i; |
803 | } |
919 | } |
804 | |
920 | |
805 | json_stash = gv_stashpv ("JSON::XS", 1); |
921 | json_stash = gv_stashpv ("JSON::XS", 1); |
806 | } |
922 | } |
|
|
923 | |
|
|
924 | PROTOTYPES: DISABLE |
807 | |
925 | |
808 | SV *new (char *dummy) |
926 | SV *new (char *dummy) |
809 | CODE: |
927 | CODE: |
810 | RETVAL = sv_bless (newRV_noinc (newSVuv (F_DEFAULT)), json_stash); |
928 | RETVAL = sv_bless (newRV_noinc (newSVuv (F_DEFAULT)), json_stash); |
811 | OUTPUT: |
929 | OUTPUT: |
812 | RETVAL |
930 | RETVAL |
813 | |
931 | |
814 | SV *ascii (SV *self, int enable) |
932 | SV *ascii (SV *self, int enable = 1) |
815 | ALIAS: |
933 | ALIAS: |
816 | ascii = F_ASCII |
934 | ascii = F_ASCII |
817 | utf8 = F_UTF8 |
935 | utf8 = F_UTF8 |
818 | indent = F_INDENT |
936 | indent = F_INDENT |
819 | canonical = F_CANONICAL |
937 | canonical = F_CANONICAL |
820 | space_before = F_SPACE_BEFORE |
938 | space_before = F_SPACE_BEFORE |
821 | space_after = F_SPACE_AFTER |
939 | space_after = F_SPACE_AFTER |
822 | json_rpc = F_JSON_RPC |
940 | json_rpc = F_JSON_RPC |
823 | pretty = F_PRETTY |
941 | pretty = F_PRETTY |
824 | allow_nonref = F_ALLOW_NONREF |
942 | allow_nonref = F_ALLOW_NONREF |
|
|
943 | shrink = F_SHRINK |
825 | CODE: |
944 | CODE: |
826 | { |
945 | { |
827 | UV *uv = SvJSON (self); |
946 | UV *uv = SvJSON (self); |
828 | if (enable) |
947 | if (enable) |
829 | *uv |= ix; |
948 | *uv |= ix; |
… | |
… | |
841 | |
960 | |
842 | void decode (SV *self, SV *jsonstr) |
961 | void decode (SV *self, SV *jsonstr) |
843 | PPCODE: |
962 | PPCODE: |
844 | XPUSHs (decode_json (jsonstr, *SvJSON (self))); |
963 | XPUSHs (decode_json (jsonstr, *SvJSON (self))); |
845 | |
964 | |
|
|
965 | PROTOTYPES: ENABLE |
|
|
966 | |
846 | void to_json (SV *scalar) |
967 | void to_json (SV *scalar) |
847 | PPCODE: |
968 | PPCODE: |
848 | XPUSHs (encode_json (scalar, F_UTF8)); |
969 | XPUSHs (encode_json (scalar, F_UTF8)); |
849 | |
970 | |
850 | void from_json (SV *jsonstr) |
971 | void from_json (SV *jsonstr) |