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