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