… | |
… | |
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)); |
… | |
… | |
264 | |
317 | |
265 | if (fast) |
318 | if (fast) |
266 | qsort (hes, count, sizeof (HE *), he_cmp_fast); |
319 | qsort (hes, count, sizeof (HE *), he_cmp_fast); |
267 | else |
320 | else |
268 | { |
321 | { |
269 | // hack to disable "use bytes" |
322 | // hack to forcefully disable "use bytes" |
270 | COP *oldcop = PL_curcop, cop; |
323 | COP cop = *PL_curcop; |
271 | cop.op_private = 0; |
324 | cop.op_private = 0; |
|
|
325 | |
|
|
326 | ENTER; |
|
|
327 | SAVETMPS; |
|
|
328 | |
|
|
329 | SAVEVPTR (PL_curcop); |
272 | PL_curcop = &cop; |
330 | PL_curcop = &cop; |
273 | |
331 | |
274 | SAVETMPS; |
|
|
275 | qsort (hes, count, sizeof (HE *), he_cmp_slow); |
332 | qsort (hes, count, sizeof (HE *), he_cmp_slow); |
|
|
333 | |
276 | FREETMPS; |
334 | FREETMPS; |
277 | |
335 | LEAVE; |
278 | PL_curcop = oldcop; |
|
|
279 | } |
336 | } |
280 | |
337 | |
281 | for (i = 0; i < count; ++i) |
338 | for (i = 0; i < count; ++i) |
282 | { |
339 | { |
283 | INDENT; |
340 | INDENT; |
… | |
… | |
313 | } |
370 | } |
314 | |
371 | |
315 | static void |
372 | static void |
316 | encode_sv (enc_t *enc, SV *sv) |
373 | encode_sv (enc_t *enc, SV *sv) |
317 | { |
374 | { |
|
|
375 | SvGETMAGIC (sv); |
|
|
376 | |
318 | if (SvPOKp (sv)) |
377 | if (SvPOKp (sv)) |
319 | { |
378 | { |
320 | STRLEN len; |
379 | STRLEN len; |
321 | char *str = SvPV (sv, len); |
380 | char *str = SvPV (sv, len); |
322 | encode_ch (enc, '"'); |
381 | encode_ch (enc, '"'); |
… | |
… | |
378 | |
437 | |
379 | if (!(flags & (F_ASCII | F_UTF8))) |
438 | if (!(flags & (F_ASCII | F_UTF8))) |
380 | SvUTF8_on (enc.sv); |
439 | SvUTF8_on (enc.sv); |
381 | |
440 | |
382 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
441 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
|
|
442 | |
|
|
443 | if (enc.flags & F_SHRINK) |
|
|
444 | shrink (enc.sv); |
|
|
445 | |
383 | return enc.sv; |
446 | return enc.sv; |
384 | } |
447 | } |
385 | |
448 | |
386 | ///////////////////////////////////////////////////////////////////////////// |
449 | ///////////////////////////////////////////////////////////////////////////// |
387 | |
450 | |
… | |
… | |
402 | ++dec->cur; \ |
465 | ++dec->cur; \ |
403 | SE |
466 | SE |
404 | |
467 | |
405 | static SV *decode_sv (dec_t *dec); |
468 | static SV *decode_sv (dec_t *dec); |
406 | |
469 | |
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]; |
470 | static signed char decode_hexdigit[256]; |
413 | |
471 | |
414 | static UV |
472 | static UV |
415 | decode_4hex (dec_t *dec) |
473 | decode_4hex (dec_t *dec) |
416 | { |
474 | { |
… | |
… | |
434 | |
492 | |
435 | fail: |
493 | fail: |
436 | return (UV)-1; |
494 | return (UV)-1; |
437 | } |
495 | } |
438 | |
496 | |
|
|
497 | #define APPEND_GROW(n) SB \ |
|
|
498 | if (cur + (n) >= end) \ |
|
|
499 | { \ |
|
|
500 | STRLEN ofs = cur - SvPVX (sv); \ |
|
|
501 | SvGROW (sv, ofs + (n) + 1); \ |
|
|
502 | cur = SvPVX (sv) + ofs; \ |
|
|
503 | end = SvEND (sv); \ |
|
|
504 | } \ |
|
|
505 | SE |
|
|
506 | |
|
|
507 | #define APPEND_CH(ch) SB \ |
|
|
508 | APPEND_GROW (1); \ |
|
|
509 | *cur++ = (ch); \ |
|
|
510 | SE |
|
|
511 | |
439 | static SV * |
512 | static SV * |
440 | decode_str (dec_t *dec) |
513 | decode_str (dec_t *dec) |
441 | { |
514 | { |
442 | SV *sv = NEWSV (0,2); |
515 | SV *sv = NEWSV (0,2); |
443 | STRLEN cur = 0; |
|
|
444 | int utf8 = 0; |
516 | int utf8 = 0; |
|
|
517 | char *cur = SvPVX (sv); |
|
|
518 | char *end = SvEND (sv); |
445 | |
519 | |
446 | for (;;) |
520 | for (;;) |
447 | { |
521 | { |
448 | unsigned char ch = *(unsigned char *)dec->cur; |
522 | unsigned char ch = *(unsigned char *)dec->cur; |
449 | |
523 | |
… | |
… | |
474 | |
548 | |
475 | // possibly a surrogate pair |
549 | // possibly a surrogate pair |
476 | if (hi >= 0xd800 && hi < 0xdc00) |
550 | if (hi >= 0xd800 && hi < 0xdc00) |
477 | { |
551 | { |
478 | if (dec->cur [0] != '\\' || dec->cur [1] != 'u') |
552 | if (dec->cur [0] != '\\' || dec->cur [1] != 'u') |
479 | ERR ("illegal surrogate character"); |
553 | ERR ("missing low surrogate character in surrogate pair"); |
480 | |
554 | |
481 | dec->cur += 2; |
555 | dec->cur += 2; |
482 | |
556 | |
483 | lo = decode_4hex (dec); |
557 | lo = decode_4hex (dec); |
484 | if (lo == (UV)-1) |
558 | if (lo == (UV)-1) |
… | |
… | |
487 | if (lo < 0xdc00 || lo >= 0xe000) |
561 | if (lo < 0xdc00 || lo >= 0xe000) |
488 | ERR ("surrogate pair expected"); |
562 | ERR ("surrogate pair expected"); |
489 | |
563 | |
490 | hi = (hi - 0xD800) * 0x400 + (lo - 0xDC00) + 0x10000; |
564 | hi = (hi - 0xD800) * 0x400 + (lo - 0xDC00) + 0x10000; |
491 | } |
565 | } |
492 | else if (lo >= 0xdc00 && lo < 0xe000) |
566 | else if (hi >= 0xdc00 && hi < 0xe000) |
493 | ERR ("illegal surrogate character"); |
567 | ERR ("missing high surrogate character in surrogate pair"); |
494 | |
568 | |
495 | if (hi >= 0x80) |
569 | if (hi >= 0x80) |
496 | { |
570 | { |
497 | utf8 = 1; |
571 | utf8 = 1; |
498 | |
572 | |
499 | SvGROW (sv, cur + 4 + 1); // at most 4 bytes for 21 bits |
573 | APPEND_GROW (4); // at most 4 bytes for 21 bits |
500 | cur = (char *)uvuni_to_utf8_flags (SvPVX (sv) + cur, hi, 0) - SvPVX (sv); |
574 | cur = (char *)uvuni_to_utf8_flags (cur, hi, 0); |
501 | } |
575 | } |
502 | else |
576 | else |
503 | APPEND_CH (hi); |
577 | APPEND_CH (hi); |
504 | } |
578 | } |
505 | break; |
579 | break; |
|
|
580 | |
|
|
581 | default: |
|
|
582 | --dec->cur; |
|
|
583 | ERR ("illegal backslash escape sequence in string"); |
506 | } |
584 | } |
507 | } |
585 | } |
508 | else if (ch >= 0x20 && ch <= 0x7f) |
586 | else if (ch >= 0x20 && ch <= 0x7f) |
509 | APPEND_CH (*dec->cur++); |
587 | APPEND_CH (*dec->cur++); |
510 | else if (ch >= 0x80) |
588 | else if (ch >= 0x80) |
511 | { |
589 | { |
512 | STRLEN clen; |
590 | STRLEN clen; |
513 | UV uch = utf8n_to_uvuni (dec->cur, dec->end - dec->cur, &clen, UTF8_CHECK_ONLY); |
591 | UV uch = utf8n_to_uvuni (dec->cur, dec->end - dec->cur, &clen, UTF8_CHECK_ONLY); |
514 | if (clen < 0) |
592 | if (clen == (STRLEN)-1) |
515 | ERR ("malformed UTF-8 character in string, cannot convert to JSON"); |
593 | ERR ("malformed UTF-8 character in JSON string"); |
516 | |
594 | |
517 | SvGROW (sv, cur + clen + 1); // at most 4 bytes for 21 bits |
595 | APPEND_GROW (clen); |
518 | memcpy (SvPVX (sv) + cur, dec->cur, clen); |
596 | do |
519 | dec->cur += clen; |
597 | { |
|
|
598 | *cur++ = *dec->cur++; |
|
|
599 | } |
|
|
600 | while (--clen); |
|
|
601 | |
|
|
602 | utf8 = 1; |
520 | } |
603 | } |
|
|
604 | else if (dec->cur == dec->end) |
|
|
605 | ERR ("unexpected end of string while parsing json string"); |
521 | else |
606 | else |
522 | ERR ("invalid character encountered"); |
607 | ERR ("invalid character encountered"); |
523 | } |
608 | } |
524 | |
609 | |
525 | ++dec->cur; |
610 | ++dec->cur; |
526 | |
611 | |
|
|
612 | SvCUR_set (sv, cur - SvPVX (sv)); |
|
|
613 | |
527 | SvPOK_only (sv); |
614 | SvPOK_only (sv); |
528 | |
|
|
529 | SvCUR_set (sv, cur); |
|
|
530 | *SvEND (sv) = 0; |
615 | *SvEND (sv) = 0; |
531 | |
616 | |
532 | if (utf8) |
617 | if (utf8) |
533 | SvUTF8_on (sv); |
618 | SvUTF8_on (sv); |
|
|
619 | |
|
|
620 | if (dec->flags & F_SHRINK) |
|
|
621 | shrink (sv); |
534 | |
622 | |
535 | return sv; |
623 | return sv; |
536 | |
624 | |
537 | fail: |
625 | fail: |
538 | SvREFCNT_dec (sv); |
626 | SvREFCNT_dec (sv); |
… | |
… | |
553 | { |
641 | { |
554 | ++dec->cur; |
642 | ++dec->cur; |
555 | if (*dec->cur >= '0' && *dec->cur <= '9') |
643 | if (*dec->cur >= '0' && *dec->cur <= '9') |
556 | ERR ("malformed number (leading zero must not be followed by another digit)"); |
644 | ERR ("malformed number (leading zero must not be followed by another digit)"); |
557 | } |
645 | } |
558 | |
646 | else if (*dec->cur < '0' || *dec->cur > '9') |
559 | // int |
647 | ERR ("malformed number (no digits after initial minus)"); |
|
|
648 | else |
|
|
649 | do |
|
|
650 | { |
|
|
651 | ++dec->cur; |
|
|
652 | } |
560 | while (*dec->cur >= '0' && *dec->cur <= '9') |
653 | while (*dec->cur >= '0' && *dec->cur <= '9'); |
561 | ++dec->cur; |
|
|
562 | |
654 | |
563 | // [frac] |
655 | // [frac] |
564 | if (*dec->cur == '.') |
656 | if (*dec->cur == '.') |
565 | { |
657 | { |
566 | is_nv = 1; |
658 | ++dec->cur; |
|
|
659 | |
|
|
660 | if (*dec->cur < '0' || *dec->cur > '9') |
|
|
661 | ERR ("malformed number (no digits after decimal point)"); |
567 | |
662 | |
568 | do |
663 | do |
569 | { |
664 | { |
570 | ++dec->cur; |
665 | ++dec->cur; |
571 | } |
666 | } |
572 | while (*dec->cur >= '0' && *dec->cur <= '9'); |
667 | while (*dec->cur >= '0' && *dec->cur <= '9'); |
|
|
668 | |
|
|
669 | is_nv = 1; |
573 | } |
670 | } |
574 | |
671 | |
575 | // [exp] |
672 | // [exp] |
576 | if (*dec->cur == 'e' || *dec->cur == 'E') |
673 | if (*dec->cur == 'e' || *dec->cur == 'E') |
577 | { |
674 | { |
578 | is_nv = 1; |
|
|
579 | |
|
|
580 | ++dec->cur; |
675 | ++dec->cur; |
|
|
676 | |
581 | if (*dec->cur == '-' || *dec->cur == '+') |
677 | if (*dec->cur == '-' || *dec->cur == '+') |
582 | ++dec->cur; |
678 | ++dec->cur; |
583 | |
679 | |
|
|
680 | if (*dec->cur < '0' || *dec->cur > '9') |
|
|
681 | ERR ("malformed number (no digits after exp sign)"); |
|
|
682 | |
|
|
683 | do |
|
|
684 | { |
|
|
685 | ++dec->cur; |
|
|
686 | } |
584 | while (*dec->cur >= '0' && *dec->cur <= '9') |
687 | while (*dec->cur >= '0' && *dec->cur <= '9'); |
585 | ++dec->cur; |
688 | |
|
|
689 | is_nv = 1; |
586 | } |
690 | } |
587 | |
691 | |
588 | if (!is_nv) |
692 | if (!is_nv) |
589 | { |
693 | { |
590 | UV uv; |
694 | UV uv; |
… | |
… | |
608 | static SV * |
712 | static SV * |
609 | decode_av (dec_t *dec) |
713 | decode_av (dec_t *dec) |
610 | { |
714 | { |
611 | AV *av = newAV (); |
715 | AV *av = newAV (); |
612 | |
716 | |
|
|
717 | WS; |
|
|
718 | if (*dec->cur == ']') |
|
|
719 | ++dec->cur; |
|
|
720 | else |
613 | for (;;) |
721 | for (;;) |
614 | { |
722 | { |
615 | SV *value; |
723 | SV *value; |
616 | |
724 | |
617 | value = decode_sv (dec); |
725 | value = decode_sv (dec); |
618 | if (!value) |
726 | if (!value) |
619 | goto fail; |
727 | goto fail; |
620 | |
728 | |
621 | av_push (av, value); |
729 | av_push (av, value); |
622 | |
730 | |
623 | WS; |
731 | WS; |
624 | |
732 | |
625 | if (*dec->cur == ']') |
733 | if (*dec->cur == ']') |
626 | { |
734 | { |
627 | ++dec->cur; |
735 | ++dec->cur; |
628 | break; |
736 | break; |
|
|
737 | } |
629 | } |
738 | |
630 | |
|
|
631 | if (*dec->cur != ',') |
739 | if (*dec->cur != ',') |
632 | ERR (", or ] expected while parsing array"); |
740 | ERR (", or ] expected while parsing array"); |
633 | |
741 | |
634 | ++dec->cur; |
742 | ++dec->cur; |
635 | } |
743 | } |
636 | |
744 | |
637 | return newRV_noinc ((SV *)av); |
745 | return newRV_noinc ((SV *)av); |
638 | |
746 | |
639 | fail: |
747 | fail: |
640 | SvREFCNT_dec (av); |
748 | SvREFCNT_dec (av); |
… | |
… | |
644 | static SV * |
752 | static SV * |
645 | decode_hv (dec_t *dec) |
753 | decode_hv (dec_t *dec) |
646 | { |
754 | { |
647 | HV *hv = newHV (); |
755 | HV *hv = newHV (); |
648 | |
756 | |
|
|
757 | WS; |
|
|
758 | if (*dec->cur == '}') |
|
|
759 | ++dec->cur; |
|
|
760 | else |
649 | for (;;) |
761 | for (;;) |
650 | { |
762 | { |
651 | SV *key, *value; |
763 | SV *key, *value; |
652 | |
764 | |
653 | WS; EXPECT_CH ('"'); |
765 | WS; EXPECT_CH ('"'); |
654 | |
766 | |
655 | key = decode_str (dec); |
767 | key = decode_str (dec); |
656 | if (!key) |
768 | 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; |
769 | goto fail; |
|
|
770 | |
|
|
771 | WS; EXPECT_CH (':'); |
|
|
772 | |
|
|
773 | value = decode_sv (dec); |
|
|
774 | if (!value) |
|
|
775 | { |
|
|
776 | SvREFCNT_dec (key); |
|
|
777 | goto fail; |
666 | } |
778 | } |
667 | |
779 | |
668 | //TODO: optimise |
780 | //TODO: optimise |
669 | hv_store_ent (hv, key, value, 0); |
781 | hv_store_ent (hv, key, value, 0); |
670 | |
782 | |
671 | WS; |
783 | WS; |
672 | |
784 | |
673 | if (*dec->cur == '}') |
785 | if (*dec->cur == '}') |
674 | { |
786 | { |
675 | ++dec->cur; |
787 | ++dec->cur; |
676 | break; |
788 | break; |
677 | } |
789 | } |
678 | |
790 | |
679 | if (*dec->cur != ',') |
791 | if (*dec->cur != ',') |
680 | ERR (", or } expected while parsing object/hash"); |
792 | ERR (", or } expected while parsing object/hash"); |
681 | |
793 | |
682 | ++dec->cur; |
794 | ++dec->cur; |
683 | } |
795 | } |
684 | |
796 | |
685 | return newRV_noinc ((SV *)hv); |
797 | return newRV_noinc ((SV *)hv); |
686 | |
798 | |
687 | fail: |
799 | fail: |
688 | SvREFCNT_dec (hv); |
800 | SvREFCNT_dec (hv); |
… | |
… | |
728 | |
840 | |
729 | case 'n': |
841 | case 'n': |
730 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "null", 4)) |
842 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "null", 4)) |
731 | { |
843 | { |
732 | dec->cur += 4; |
844 | dec->cur += 4; |
733 | return newSViv (1); |
845 | return newSVsv (&PL_sv_undef); |
734 | } |
846 | } |
735 | else |
847 | else |
736 | ERR ("'null' expected"); |
848 | ERR ("'null' expected"); |
737 | |
849 | |
738 | break; |
850 | break; |
739 | |
851 | |
740 | default: |
852 | default: |
741 | ERR ("malformed json string"); |
853 | ERR ("malformed json string, neither array, object, number, string or atom"); |
742 | break; |
854 | break; |
743 | } |
855 | } |
744 | |
856 | |
745 | fail: |
857 | fail: |
746 | return 0; |
858 | return 0; |
… | |
… | |
749 | static SV * |
861 | static SV * |
750 | decode_json (SV *string, UV flags) |
862 | decode_json (SV *string, UV flags) |
751 | { |
863 | { |
752 | SV *sv; |
864 | SV *sv; |
753 | |
865 | |
754 | if (!(flags & F_UTF8)) |
866 | if (flags & F_UTF8) |
|
|
867 | sv_utf8_downgrade (string, 0); |
|
|
868 | else |
755 | sv_utf8_upgrade (string); |
869 | sv_utf8_upgrade (string); |
756 | |
870 | |
757 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
871 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
758 | |
872 | |
759 | dec_t dec; |
873 | dec_t dec; |
760 | dec.flags = flags; |
874 | dec.flags = flags; |
761 | dec.cur = SvPVX (string); |
875 | dec.cur = SvPVX (string); |
762 | dec.end = SvEND (string); |
876 | dec.end = SvEND (string); |
763 | dec.err = 0; |
877 | dec.err = 0; |
764 | |
878 | |
765 | *dec.end = 1; // invalid anywhere |
|
|
766 | sv = decode_sv (&dec); |
879 | sv = decode_sv (&dec); |
767 | *dec.end = 0; |
|
|
768 | |
880 | |
769 | if (!sv) |
881 | if (!sv) |
770 | { |
882 | { |
|
|
883 | IV offset = dec.flags & F_UTF8 |
|
|
884 | ? dec.cur - SvPVX (string) |
771 | IV offset = utf8_distance (dec.cur, SvPVX (string)); |
885 | : utf8_distance (dec.cur, SvPVX (string)); |
772 | SV *uni = sv_newmortal (); |
886 | SV *uni = sv_newmortal (); |
773 | |
887 | |
|
|
888 | // horrible hack to silence warning inside pv_uni_display |
|
|
889 | COP cop = *PL_curcop; |
|
|
890 | cop.cop_warnings = pWARN_NONE; |
|
|
891 | ENTER; |
|
|
892 | SAVEVPTR (PL_curcop); |
|
|
893 | PL_curcop = &cop; |
774 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
894 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
|
|
895 | LEAVE; |
|
|
896 | |
775 | croak ("%s, at character %d (%s)", |
897 | croak ("%s, at character offset %d (%s)", |
776 | dec.err, |
898 | dec.err, |
777 | (int)offset, |
899 | (int)offset, |
778 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
900 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
779 | } |
901 | } |
780 | |
902 | |
… | |
… | |
794 | |
916 | |
795 | memset (decode_hexdigit, 0xff, 256); |
917 | memset (decode_hexdigit, 0xff, 256); |
796 | for (i = 10; i--; ) |
918 | for (i = 10; i--; ) |
797 | decode_hexdigit ['0' + i] = i; |
919 | decode_hexdigit ['0' + i] = i; |
798 | |
920 | |
799 | for (i = 6; --i; ) |
921 | for (i = 7; i--; ) |
800 | { |
922 | { |
801 | decode_hexdigit ['a' + i] = 10 + i; |
923 | decode_hexdigit ['a' + i] = 10 + i; |
802 | decode_hexdigit ['A' + i] = 10 + i; |
924 | decode_hexdigit ['A' + i] = 10 + i; |
803 | } |
925 | } |
804 | |
926 | |
805 | json_stash = gv_stashpv ("JSON::XS", 1); |
927 | json_stash = gv_stashpv ("JSON::XS", 1); |
806 | } |
928 | } |
|
|
929 | |
|
|
930 | PROTOTYPES: DISABLE |
807 | |
931 | |
808 | SV *new (char *dummy) |
932 | SV *new (char *dummy) |
809 | CODE: |
933 | CODE: |
810 | RETVAL = sv_bless (newRV_noinc (newSVuv (F_DEFAULT)), json_stash); |
934 | RETVAL = sv_bless (newRV_noinc (newSVuv (F_DEFAULT)), json_stash); |
811 | OUTPUT: |
935 | OUTPUT: |
812 | RETVAL |
936 | RETVAL |
813 | |
937 | |
814 | SV *ascii (SV *self, int enable) |
938 | SV *ascii (SV *self, int enable = 1) |
815 | ALIAS: |
939 | ALIAS: |
816 | ascii = F_ASCII |
940 | ascii = F_ASCII |
817 | utf8 = F_UTF8 |
941 | utf8 = F_UTF8 |
818 | indent = F_INDENT |
942 | indent = F_INDENT |
819 | canonical = F_CANONICAL |
943 | canonical = F_CANONICAL |
820 | space_before = F_SPACE_BEFORE |
944 | space_before = F_SPACE_BEFORE |
821 | space_after = F_SPACE_AFTER |
945 | space_after = F_SPACE_AFTER |
822 | json_rpc = F_JSON_RPC |
946 | json_rpc = F_JSON_RPC |
823 | pretty = F_PRETTY |
947 | pretty = F_PRETTY |
824 | allow_nonref = F_ALLOW_NONREF |
948 | allow_nonref = F_ALLOW_NONREF |
|
|
949 | shrink = F_SHRINK |
825 | CODE: |
950 | CODE: |
826 | { |
951 | { |
827 | UV *uv = SvJSON (self); |
952 | UV *uv = SvJSON (self); |
828 | if (enable) |
953 | if (enable) |
829 | *uv |= ix; |
954 | *uv |= ix; |
… | |
… | |
841 | |
966 | |
842 | void decode (SV *self, SV *jsonstr) |
967 | void decode (SV *self, SV *jsonstr) |
843 | PPCODE: |
968 | PPCODE: |
844 | XPUSHs (decode_json (jsonstr, *SvJSON (self))); |
969 | XPUSHs (decode_json (jsonstr, *SvJSON (self))); |
845 | |
970 | |
|
|
971 | PROTOTYPES: ENABLE |
|
|
972 | |
846 | void to_json (SV *scalar) |
973 | void to_json (SV *scalar) |
847 | PPCODE: |
974 | PPCODE: |
848 | XPUSHs (encode_json (scalar, F_UTF8)); |
975 | XPUSHs (encode_json (scalar, F_UTF8)); |
849 | |
976 | |
850 | void from_json (SV *jsonstr) |
977 | void from_json (SV *jsonstr) |