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