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