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