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