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