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