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