… | |
… | |
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 |
17 | #define F_SHRINK 0x00000100 |
16 | #define F_SHRINK 0x00000100 |
18 | |
17 | |
|
|
18 | // F_SKIPINVALID? |
|
|
19 | // F_EXECCODEREF? |
|
|
20 | // F_SELFCONVERT? |
|
|
21 | |
19 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
22 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
20 | #define F_DEFAULT 0 |
23 | #define F_DEFAULT 0 |
21 | |
24 | |
22 | #define INIT_SIZE 32 // initial scalar size to be allocated |
25 | #define INIT_SIZE 32 // initial scalar size to be allocated |
|
|
26 | #define INDENT_STEP 3 // spaces per indentation level |
|
|
27 | |
|
|
28 | #define UTF8_MAX_LEN 11 // for perls UTF-X: max. number of octets per character |
|
|
29 | #define SHORT_STRING_LEN 512 // special-case strings of up to this size |
23 | |
30 | |
24 | #define SB do { |
31 | #define SB do { |
25 | #define SE } while (0) |
32 | #define SE } while (0) |
26 | |
33 | |
27 | static HV *json_stash; |
34 | static HV *json_stash; // JSON::XS:: |
|
|
35 | |
|
|
36 | ///////////////////////////////////////////////////////////////////////////// |
|
|
37 | // utility functions |
|
|
38 | |
|
|
39 | static UV * |
|
|
40 | SvJSON (SV *sv) |
|
|
41 | { |
|
|
42 | if (!(SvROK (sv) && SvOBJECT (SvRV (sv)) && SvSTASH (SvRV (sv)) == json_stash)) |
|
|
43 | croak ("object is not of type JSON::XS"); |
|
|
44 | |
|
|
45 | return &SvUVX (SvRV (sv)); |
|
|
46 | } |
|
|
47 | |
|
|
48 | static void |
|
|
49 | shrink (SV *sv) |
|
|
50 | { |
|
|
51 | sv_utf8_downgrade (sv, 1); |
|
|
52 | if (SvLEN (sv) > SvCUR (sv) + 1) |
|
|
53 | { |
|
|
54 | #ifdef SvPV_shrink_to_cur |
|
|
55 | SvPV_shrink_to_cur (sv); |
|
|
56 | #elif defined (SvPV_renew) |
|
|
57 | SvPV_renew (sv, SvCUR (sv) + 1); |
|
|
58 | #endif |
|
|
59 | } |
|
|
60 | } |
|
|
61 | |
|
|
62 | // decode an utf-8 character and return it, or (UV)-1 in |
|
|
63 | // case of an error. |
|
|
64 | // we special-case "safe" characters from U+80 .. U+7FF, |
|
|
65 | // but use the very good perl function to parse anything else. |
|
|
66 | // note that we never call this function for a ascii codepoints |
|
|
67 | static UV |
|
|
68 | decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen) |
|
|
69 | { |
|
|
70 | if (s[0] > 0xdf || s[0] < 0xc2) |
|
|
71 | return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY); |
|
|
72 | else if (len > 1 && s[1] >= 0x80 && s[1] <= 0xbf) |
|
|
73 | { |
|
|
74 | *clen = 2; |
|
|
75 | return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); |
|
|
76 | } |
|
|
77 | else |
|
|
78 | return (UV)-1; |
|
|
79 | } |
|
|
80 | |
|
|
81 | ///////////////////////////////////////////////////////////////////////////// |
|
|
82 | // encoder |
28 | |
83 | |
29 | // structure used for encoding JSON |
84 | // structure used for encoding JSON |
30 | typedef struct |
85 | typedef struct |
31 | { |
86 | { |
32 | char *cur; |
87 | char *cur; // SvPVX (sv) + current output position |
33 | STRLEN len; // SvLEN (sv) |
|
|
34 | char *end; // SvEND (sv) |
88 | char *end; // SvEND (sv) |
35 | SV *sv; |
89 | SV *sv; // result scalar |
36 | UV flags; |
90 | UV flags; // F_* |
37 | int max_recurse; |
91 | int indent; // indentation level |
38 | int indent; |
92 | int max_depth; // max. recursion level |
39 | } enc_t; |
93 | } enc_t; |
40 | |
|
|
41 | // structure used for decoding JSON |
|
|
42 | typedef struct |
|
|
43 | { |
|
|
44 | char *cur; |
|
|
45 | char *end; |
|
|
46 | const char *err; |
|
|
47 | UV flags; |
|
|
48 | } dec_t; |
|
|
49 | |
|
|
50 | static UV * |
|
|
51 | SvJSON (SV *sv) |
|
|
52 | { |
|
|
53 | if (!(SvROK (sv) && SvOBJECT (SvRV (sv)) && SvSTASH (SvRV (sv)) == json_stash)) |
|
|
54 | croak ("object is not of type JSON::XS"); |
|
|
55 | |
|
|
56 | return &SvUVX (SvRV (sv)); |
|
|
57 | } |
|
|
58 | |
|
|
59 | ///////////////////////////////////////////////////////////////////////////// |
|
|
60 | |
94 | |
61 | static void |
95 | static void |
62 | need (enc_t *enc, STRLEN len) |
96 | need (enc_t *enc, STRLEN len) |
63 | { |
97 | { |
64 | if (enc->cur + len >= enc->end) |
98 | if (enc->cur + len >= enc->end) |
… | |
… | |
122 | STRLEN clen; |
156 | STRLEN clen; |
123 | UV uch; |
157 | UV uch; |
124 | |
158 | |
125 | if (is_utf8) |
159 | if (is_utf8) |
126 | { |
160 | { |
127 | uch = utf8n_to_uvuni (str, end - str, &clen, UTF8_CHECK_ONLY); |
161 | //uch = utf8n_to_uvuni (str, end - str, &clen, UTF8_CHECK_ONLY); |
|
|
162 | uch = decode_utf8 (str, end - str, &clen); |
128 | if (clen == (STRLEN)-1) |
163 | if (clen == (STRLEN)-1) |
129 | croak ("malformed UTF-8 character in string, cannot convert to JSON"); |
164 | croak ("malformed or illegal unicode character in string [%.11s], cannot convert to JSON", str); |
130 | } |
165 | } |
131 | else |
166 | else |
132 | { |
167 | { |
133 | uch = ch; |
168 | uch = ch; |
134 | clen = 1; |
169 | clen = 1; |
135 | } |
170 | } |
136 | |
171 | |
|
|
172 | if (uch > 0x10FFFFUL) |
|
|
173 | croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
|
|
174 | |
137 | if (uch < 0x80 || enc->flags & F_ASCII) |
175 | if (uch < 0x80 || enc->flags & F_ASCII) |
138 | { |
176 | { |
139 | if (uch > 0xFFFFUL) |
177 | if (uch > 0xFFFFUL) |
140 | { |
178 | { |
141 | need (enc, len += 11); |
179 | need (enc, len += 11); |
142 | sprintf (enc->cur, "\\u%04x\\u%04x", |
180 | sprintf (enc->cur, "\\u%04x\\u%04x", |
143 | (uch - 0x10000) / 0x400 + 0xD800, |
181 | (int)((uch - 0x10000) / 0x400 + 0xD800), |
144 | (uch - 0x10000) % 0x400 + 0xDC00); |
182 | (int)((uch - 0x10000) % 0x400 + 0xDC00)); |
145 | enc->cur += 12; |
183 | enc->cur += 12; |
146 | } |
184 | } |
147 | else |
185 | else |
148 | { |
186 | { |
149 | static char hexdigit [16] = "0123456789abcdef"; |
187 | static char hexdigit [16] = "0123456789abcdef"; |
… | |
… | |
167 | } |
205 | } |
168 | while (--clen); |
206 | while (--clen); |
169 | } |
207 | } |
170 | else |
208 | else |
171 | { |
209 | { |
172 | need (enc, 10); // never more than 11 bytes needed |
210 | need (enc, len += UTF8_MAX_LEN - 1); // never more than 11 bytes needed |
173 | enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
211 | enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
174 | ++str; |
212 | ++str; |
175 | } |
213 | } |
176 | } |
214 | } |
177 | } |
215 | } |
… | |
… | |
179 | |
217 | |
180 | --len; |
218 | --len; |
181 | } |
219 | } |
182 | } |
220 | } |
183 | |
221 | |
184 | #define INDENT SB \ |
222 | static void |
|
|
223 | encode_indent (enc_t *enc) |
|
|
224 | { |
185 | if (enc->flags & F_INDENT) \ |
225 | if (enc->flags & F_INDENT) |
186 | { \ |
226 | { |
187 | int i_; \ |
227 | int spaces = enc->indent * INDENT_STEP; |
188 | need (enc, enc->indent); \ |
|
|
189 | for (i_ = enc->indent * 3; i_--; )\ |
|
|
190 | encode_ch (enc, ' '); \ |
|
|
191 | } \ |
|
|
192 | SE |
|
|
193 | |
228 | |
194 | #define SPACE SB need (enc, 1); encode_ch (enc, ' '); SE |
229 | need (enc, spaces); |
195 | #define NL SB if (enc->flags & F_INDENT) { need (enc, 1); encode_ch (enc, '\n'); } SE |
230 | memset (enc->cur, ' ', spaces); |
196 | #define COMMA SB \ |
231 | enc->cur += spaces; |
|
|
232 | } |
|
|
233 | } |
|
|
234 | |
|
|
235 | static void |
|
|
236 | encode_space (enc_t *enc) |
|
|
237 | { |
|
|
238 | need (enc, 1); |
197 | encode_ch (enc, ','); \ |
239 | encode_ch (enc, ' '); |
|
|
240 | } |
|
|
241 | |
|
|
242 | static void |
|
|
243 | encode_nl (enc_t *enc) |
|
|
244 | { |
198 | if (enc->flags & F_INDENT) \ |
245 | if (enc->flags & F_INDENT) |
199 | NL; \ |
246 | { |
|
|
247 | need (enc, 1); |
|
|
248 | encode_ch (enc, '\n'); |
|
|
249 | } |
|
|
250 | } |
|
|
251 | |
|
|
252 | static void |
|
|
253 | encode_comma (enc_t *enc) |
|
|
254 | { |
|
|
255 | encode_ch (enc, ','); |
|
|
256 | |
|
|
257 | if (enc->flags & F_INDENT) |
|
|
258 | encode_nl (enc); |
200 | else if (enc->flags & F_SPACE_AFTER) \ |
259 | else if (enc->flags & F_SPACE_AFTER) |
201 | SPACE; \ |
260 | encode_space (enc); |
202 | SE |
261 | } |
203 | |
262 | |
204 | static void encode_sv (enc_t *enc, SV *sv); |
263 | static void encode_sv (enc_t *enc, SV *sv); |
205 | |
264 | |
206 | static void |
265 | static void |
207 | encode_av (enc_t *enc, AV *av) |
266 | encode_av (enc_t *enc, AV *av) |
208 | { |
267 | { |
209 | int i, len = av_len (av); |
268 | int i, len = av_len (av); |
210 | |
269 | |
211 | encode_ch (enc, '['); NL; |
270 | encode_ch (enc, '['); encode_nl (enc); |
212 | ++enc->indent; |
271 | ++enc->indent; |
213 | |
272 | |
214 | for (i = 0; i <= len; ++i) |
273 | for (i = 0; i <= len; ++i) |
215 | { |
274 | { |
216 | INDENT; |
275 | encode_indent (enc); |
217 | encode_sv (enc, *av_fetch (av, i, 0)); |
276 | encode_sv (enc, *av_fetch (av, i, 0)); |
218 | |
277 | |
219 | if (i < len) |
278 | if (i < len) |
220 | COMMA; |
279 | encode_comma (enc); |
221 | } |
280 | } |
222 | |
281 | |
223 | NL; |
282 | encode_nl (enc); |
224 | |
283 | |
225 | --enc->indent; |
284 | --enc->indent; |
226 | INDENT; encode_ch (enc, ']'); |
285 | encode_indent (enc); encode_ch (enc, ']'); |
227 | } |
286 | } |
228 | |
287 | |
229 | static void |
288 | static void |
230 | encode_he (enc_t *enc, HE *he) |
289 | encode_he (enc_t *enc, HE *he) |
231 | { |
290 | { |
… | |
… | |
245 | else |
304 | else |
246 | encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); |
305 | encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); |
247 | |
306 | |
248 | encode_ch (enc, '"'); |
307 | encode_ch (enc, '"'); |
249 | |
308 | |
250 | if (enc->flags & F_SPACE_BEFORE) SPACE; |
309 | if (enc->flags & F_SPACE_BEFORE) encode_space (enc); |
251 | encode_ch (enc, ':'); |
310 | encode_ch (enc, ':'); |
252 | if (enc->flags & F_SPACE_AFTER ) SPACE; |
311 | if (enc->flags & F_SPACE_AFTER ) encode_space (enc); |
253 | encode_sv (enc, HeVAL (he)); |
312 | encode_sv (enc, HeVAL (he)); |
254 | } |
313 | } |
255 | |
314 | |
256 | // compare hash entries, used when all keys are bytestrings |
315 | // compare hash entries, used when all keys are bytestrings |
257 | static int |
316 | static int |
… | |
… | |
263 | HE *b = *(HE **)b_; |
322 | HE *b = *(HE **)b_; |
264 | |
323 | |
265 | STRLEN la = HeKLEN (a); |
324 | STRLEN la = HeKLEN (a); |
266 | STRLEN lb = HeKLEN (b); |
325 | STRLEN lb = HeKLEN (b); |
267 | |
326 | |
268 | if (!(cmp == memcmp (HeKEY (a), HeKEY (b), la < lb ? la : lb))) |
327 | if (!(cmp = memcmp (HeKEY (a), HeKEY (b), la < lb ? la : lb))) |
269 | cmp = la < lb ? -1 : la == lb ? 0 : 1; |
328 | cmp = la - lb; |
270 | |
329 | |
271 | return cmp; |
330 | return cmp; |
272 | } |
331 | } |
273 | |
332 | |
274 | // compare hash entries, used when some keys are sv's or utf-x |
333 | // compare hash entries, used when some keys are sv's or utf-x |
… | |
… | |
281 | static void |
340 | static void |
282 | encode_hv (enc_t *enc, HV *hv) |
341 | encode_hv (enc_t *enc, HV *hv) |
283 | { |
342 | { |
284 | int count, i; |
343 | int count, i; |
285 | |
344 | |
286 | encode_ch (enc, '{'); NL; ++enc->indent; |
345 | encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; |
287 | |
346 | |
288 | if ((count = hv_iterinit (hv))) |
347 | if ((count = hv_iterinit (hv))) |
289 | { |
348 | { |
290 | // for canonical output we have to sort by keys first |
349 | // for canonical output we have to sort by keys first |
291 | // actually, this is mostly due to the stupid so-called |
350 | // actually, this is mostly due to the stupid so-called |
292 | // security workaround added somewhere in 5.8.x. |
351 | // security workaround added somewhere in 5.8.x. |
293 | // that randomises hash orderings |
352 | // that randomises hash orderings |
294 | if (enc->flags & F_CANONICAL) |
353 | if (enc->flags & F_CANONICAL) |
295 | { |
354 | { |
296 | HE *he, *hes [count]; |
355 | HE *he, *hes [count]; // if your compiler dies here, you need to enable C99 mode |
297 | int fast = 1; |
356 | int fast = 1; |
298 | |
357 | |
299 | i = 0; |
358 | i = 0; |
300 | while ((he = hv_iternext (hv))) |
359 | while ((he = hv_iternext (hv))) |
301 | { |
360 | { |
… | |
… | |
308 | |
367 | |
309 | if (fast) |
368 | if (fast) |
310 | qsort (hes, count, sizeof (HE *), he_cmp_fast); |
369 | qsort (hes, count, sizeof (HE *), he_cmp_fast); |
311 | else |
370 | else |
312 | { |
371 | { |
313 | // hack to disable "use bytes" |
372 | // hack to forcefully disable "use bytes" |
314 | COP *oldcop = PL_curcop, cop; |
373 | COP cop = *PL_curcop; |
315 | cop.op_private = 0; |
374 | cop.op_private = 0; |
|
|
375 | |
|
|
376 | ENTER; |
|
|
377 | SAVETMPS; |
|
|
378 | |
|
|
379 | SAVEVPTR (PL_curcop); |
316 | PL_curcop = &cop; |
380 | PL_curcop = &cop; |
317 | |
381 | |
318 | SAVETMPS; |
|
|
319 | qsort (hes, count, sizeof (HE *), he_cmp_slow); |
382 | qsort (hes, count, sizeof (HE *), he_cmp_slow); |
|
|
383 | |
320 | FREETMPS; |
384 | FREETMPS; |
321 | |
385 | LEAVE; |
322 | PL_curcop = oldcop; |
|
|
323 | } |
386 | } |
324 | |
387 | |
325 | for (i = 0; i < count; ++i) |
388 | for (i = 0; i < count; ++i) |
326 | { |
389 | { |
327 | INDENT; |
390 | encode_indent (enc); |
328 | encode_he (enc, hes [i]); |
391 | encode_he (enc, hes [i]); |
329 | |
392 | |
330 | if (i < count - 1) |
393 | if (i < count - 1) |
331 | COMMA; |
394 | encode_comma (enc); |
332 | } |
395 | } |
333 | |
396 | |
334 | NL; |
397 | encode_nl (enc); |
335 | } |
398 | } |
336 | else |
399 | else |
337 | { |
400 | { |
338 | SV *sv; |
401 | SV *sv; |
339 | HE *he = hv_iternext (hv); |
402 | HE *he = hv_iternext (hv); |
340 | |
403 | |
341 | for (;;) |
404 | for (;;) |
342 | { |
405 | { |
343 | INDENT; |
406 | encode_indent (enc); |
344 | encode_he (enc, he); |
407 | encode_he (enc, he); |
345 | |
408 | |
346 | if (!(he = hv_iternext (hv))) |
409 | if (!(he = hv_iternext (hv))) |
347 | break; |
410 | break; |
348 | |
411 | |
349 | COMMA; |
412 | encode_comma (enc); |
350 | } |
413 | } |
351 | |
414 | |
352 | NL; |
415 | encode_nl (enc); |
353 | } |
416 | } |
354 | } |
417 | } |
355 | |
418 | |
356 | --enc->indent; INDENT; encode_ch (enc, '}'); |
419 | --enc->indent; encode_indent (enc); encode_ch (enc, '}'); |
357 | } |
420 | } |
358 | |
421 | |
359 | static void |
422 | static void |
360 | encode_sv (enc_t *enc, SV *sv) |
423 | encode_sv (enc_t *enc, SV *sv) |
361 | { |
424 | { |
… | |
… | |
383 | ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv)) |
446 | ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv)) |
384 | : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv)); |
447 | : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv)); |
385 | } |
448 | } |
386 | else if (SvROK (sv)) |
449 | else if (SvROK (sv)) |
387 | { |
450 | { |
388 | if (!--enc->max_recurse) |
451 | SV *rv = SvRV (sv); |
|
|
452 | |
|
|
453 | if (enc->indent >= enc->max_depth) |
389 | croak ("data structure too deep (hit recursion limit)"); |
454 | croak ("data structure too deep (hit recursion limit)"); |
390 | |
455 | |
391 | sv = SvRV (sv); |
|
|
392 | |
|
|
393 | switch (SvTYPE (sv)) |
456 | switch (SvTYPE (rv)) |
394 | { |
457 | { |
395 | case SVt_PVAV: encode_av (enc, (AV *)sv); break; |
458 | case SVt_PVAV: encode_av (enc, (AV *)rv); break; |
396 | case SVt_PVHV: encode_hv (enc, (HV *)sv); break; |
459 | case SVt_PVHV: encode_hv (enc, (HV *)rv); break; |
397 | |
460 | |
398 | default: |
461 | default: |
399 | croak ("JSON can only represent references to arrays or hashes"); |
462 | croak ("encountered %s, but JSON can only represent references to arrays or hashes", |
|
|
463 | SvPV_nolen (sv)); |
400 | } |
464 | } |
401 | } |
465 | } |
402 | else if (!SvOK (sv)) |
466 | else if (!SvOK (sv)) |
403 | encode_str (enc, "null", 4, 0); |
467 | encode_str (enc, "null", 4, 0); |
404 | else |
468 | else |
405 | croak ("encountered perl type that JSON cannot handle"); |
469 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
|
|
470 | SvPV_nolen (sv), SvFLAGS (sv)); |
406 | } |
471 | } |
407 | |
472 | |
408 | static SV * |
473 | static SV * |
409 | encode_json (SV *scalar, UV flags) |
474 | encode_json (SV *scalar, UV flags) |
410 | { |
475 | { |
411 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
476 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
412 | croak ("hash- or arraref required (not a simple scalar, use allow_nonref to allow this)"); |
477 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
413 | |
478 | |
414 | enc_t enc; |
479 | enc_t enc; |
415 | enc.flags = flags; |
480 | enc.flags = flags; |
416 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
481 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
417 | enc.cur = SvPVX (enc.sv); |
482 | enc.cur = SvPVX (enc.sv); |
418 | enc.end = SvEND (enc.sv); |
483 | enc.end = SvEND (enc.sv); |
419 | enc.max_recurse = 0; |
|
|
420 | enc.indent = 0; |
484 | enc.indent = 0; |
|
|
485 | enc.max_depth = 0x7fffffffUL; |
421 | |
486 | |
422 | SvPOK_only (enc.sv); |
487 | SvPOK_only (enc.sv); |
423 | encode_sv (&enc, scalar); |
488 | encode_sv (&enc, scalar); |
424 | |
489 | |
425 | if (!(flags & (F_ASCII | F_UTF8))) |
490 | if (!(flags & (F_ASCII | F_UTF8))) |
426 | SvUTF8_on (enc.sv); |
491 | SvUTF8_on (enc.sv); |
427 | |
492 | |
428 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
493 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
429 | |
494 | |
430 | #ifdef SvPV_shrink_to_cur |
|
|
431 | if (enc.flags & F_SHRINK) |
495 | if (enc.flags & F_SHRINK) |
432 | SvPV_shrink_to_cur (enc.sv); |
496 | shrink (enc.sv); |
433 | #endif |
497 | |
434 | return enc.sv; |
498 | return enc.sv; |
435 | } |
499 | } |
436 | |
500 | |
437 | ///////////////////////////////////////////////////////////////////////////// |
501 | ///////////////////////////////////////////////////////////////////////////// |
|
|
502 | // decoder |
438 | |
503 | |
439 | #define WS \ |
504 | // structure used for decoding JSON |
|
|
505 | typedef struct |
|
|
506 | { |
|
|
507 | char *cur; // current parser pointer |
|
|
508 | char *end; // end of input string |
|
|
509 | const char *err; // parse error, if != 0 |
|
|
510 | UV flags; // F_* |
|
|
511 | } dec_t; |
|
|
512 | |
|
|
513 | static void |
|
|
514 | decode_ws (dec_t *dec) |
|
|
515 | { |
440 | for (;;) \ |
516 | for (;;) |
441 | { \ |
517 | { |
442 | char ch = *dec->cur; \ |
518 | char ch = *dec->cur; |
|
|
519 | |
443 | if (ch > 0x20 \ |
520 | if (ch > 0x20 |
444 | || (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)) \ |
521 | || (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)) |
445 | break; \ |
522 | break; |
|
|
523 | |
446 | ++dec->cur; \ |
524 | ++dec->cur; |
447 | } |
525 | } |
|
|
526 | } |
448 | |
527 | |
449 | #define ERR(reason) SB dec->err = reason; goto fail; SE |
528 | #define ERR(reason) SB dec->err = reason; goto fail; SE |
450 | #define EXPECT_CH(ch) SB \ |
529 | #define EXPECT_CH(ch) SB \ |
451 | if (*dec->cur != ch) \ |
530 | if (*dec->cur != ch) \ |
452 | ERR (# ch " expected"); \ |
531 | ERR (# ch " expected"); \ |
… | |
… | |
459 | |
538 | |
460 | static UV |
539 | static UV |
461 | decode_4hex (dec_t *dec) |
540 | decode_4hex (dec_t *dec) |
462 | { |
541 | { |
463 | signed char d1, d2, d3, d4; |
542 | signed char d1, d2, d3, d4; |
|
|
543 | unsigned char *cur = (unsigned char *)dec->cur; |
464 | |
544 | |
465 | d1 = decode_hexdigit [((unsigned char *)dec->cur) [0]]; |
|
|
466 | if (d1 < 0) ERR ("four hexadecimal digits expected"); |
545 | d1 = decode_hexdigit [cur [0]]; if (d1 < 0) ERR ("four hexadecimal digits expected"); |
467 | d2 = decode_hexdigit [((unsigned char *)dec->cur) [1]]; |
|
|
468 | if (d2 < 0) ERR ("four hexadecimal digits expected"); |
546 | d2 = decode_hexdigit [cur [1]]; if (d2 < 0) ERR ("four hexadecimal digits expected"); |
469 | d3 = decode_hexdigit [((unsigned char *)dec->cur) [2]]; |
|
|
470 | if (d3 < 0) ERR ("four hexadecimal digits expected"); |
547 | d3 = decode_hexdigit [cur [2]]; if (d3 < 0) ERR ("four hexadecimal digits expected"); |
471 | d4 = decode_hexdigit [((unsigned char *)dec->cur) [3]]; |
|
|
472 | if (d4 < 0) ERR ("four hexadecimal digits expected"); |
548 | d4 = decode_hexdigit [cur [3]]; if (d4 < 0) ERR ("four hexadecimal digits expected"); |
473 | |
549 | |
474 | dec->cur += 4; |
550 | dec->cur += 4; |
475 | |
551 | |
476 | return ((UV)d1) << 12 |
552 | return ((UV)d1) << 12 |
477 | | ((UV)d2) << 8 |
553 | | ((UV)d2) << 8 |
… | |
… | |
480 | |
556 | |
481 | fail: |
557 | fail: |
482 | return (UV)-1; |
558 | return (UV)-1; |
483 | } |
559 | } |
484 | |
560 | |
485 | #define APPEND_GROW(n) SB \ |
|
|
486 | if (cur + (n) >= end) \ |
|
|
487 | { \ |
|
|
488 | STRLEN ofs = cur - SvPVX (sv); \ |
|
|
489 | SvGROW (sv, ofs + (n) + 1); \ |
|
|
490 | cur = SvPVX (sv) + ofs; \ |
|
|
491 | end = SvEND (sv); \ |
|
|
492 | } \ |
|
|
493 | SE |
|
|
494 | |
|
|
495 | #define APPEND_CH(ch) SB \ |
|
|
496 | APPEND_GROW (1); \ |
|
|
497 | *cur++ = (ch); \ |
|
|
498 | SE |
|
|
499 | |
|
|
500 | static SV * |
561 | static SV * |
501 | decode_str (dec_t *dec) |
562 | decode_str (dec_t *dec) |
502 | { |
563 | { |
503 | SV *sv = NEWSV (0,2); |
564 | SV *sv = 0; |
504 | int utf8 = 0; |
565 | int utf8 = 0; |
505 | char *cur = SvPVX (sv); |
|
|
506 | char *end = SvEND (sv); |
|
|
507 | |
566 | |
508 | for (;;) |
567 | do |
509 | { |
568 | { |
510 | unsigned char ch = *(unsigned char *)dec->cur; |
569 | char buf [SHORT_STRING_LEN + UTF8_MAX_LEN]; |
|
|
570 | char *cur = buf; |
511 | |
571 | |
512 | if (ch == '"') |
572 | do |
513 | break; |
|
|
514 | else if (ch == '\\') |
|
|
515 | { |
573 | { |
516 | switch (*++dec->cur) |
574 | unsigned char ch = *(unsigned char *)dec->cur++; |
|
|
575 | |
|
|
576 | if (ch == '"') |
517 | { |
577 | { |
518 | case '\\': |
578 | --dec->cur; |
519 | case '/': |
579 | break; |
520 | case '"': APPEND_CH (*dec->cur++); break; |
580 | } |
521 | |
581 | else if (ch == '\\') |
522 | case 'b': APPEND_CH ('\010'); ++dec->cur; break; |
582 | { |
523 | case 't': APPEND_CH ('\011'); ++dec->cur; break; |
583 | switch (*dec->cur) |
524 | case 'n': APPEND_CH ('\012'); ++dec->cur; break; |
|
|
525 | case 'f': APPEND_CH ('\014'); ++dec->cur; break; |
|
|
526 | case 'r': APPEND_CH ('\015'); ++dec->cur; break; |
|
|
527 | |
|
|
528 | case 'u': |
|
|
529 | { |
584 | { |
530 | UV lo, hi; |
585 | case '\\': |
531 | ++dec->cur; |
586 | case '/': |
|
|
587 | case '"': *cur++ = *dec->cur++; break; |
532 | |
588 | |
533 | hi = decode_4hex (dec); |
589 | case 'b': ++dec->cur; *cur++ = '\010'; break; |
534 | if (hi == (UV)-1) |
590 | case 't': ++dec->cur; *cur++ = '\011'; break; |
535 | goto fail; |
591 | case 'n': ++dec->cur; *cur++ = '\012'; break; |
|
|
592 | case 'f': ++dec->cur; *cur++ = '\014'; break; |
|
|
593 | case 'r': ++dec->cur; *cur++ = '\015'; break; |
536 | |
594 | |
537 | // possibly a surrogate pair |
595 | case 'u': |
538 | if (hi >= 0xd800 && hi < 0xdc00) |
|
|
539 | { |
596 | { |
540 | if (dec->cur [0] != '\\' || dec->cur [1] != 'u') |
597 | UV lo, hi; |
541 | ERR ("missing low surrogate character in surrogate pair"); |
|
|
542 | |
|
|
543 | dec->cur += 2; |
598 | ++dec->cur; |
544 | |
599 | |
545 | lo = decode_4hex (dec); |
600 | hi = decode_4hex (dec); |
546 | if (lo == (UV)-1) |
601 | if (hi == (UV)-1) |
547 | goto fail; |
602 | goto fail; |
548 | |
603 | |
|
|
604 | // possibly a surrogate pair |
|
|
605 | if (hi >= 0xd800) |
|
|
606 | if (hi < 0xdc00) |
|
|
607 | { |
|
|
608 | if (dec->cur [0] != '\\' || dec->cur [1] != 'u') |
|
|
609 | ERR ("missing low surrogate character in surrogate pair"); |
|
|
610 | |
|
|
611 | dec->cur += 2; |
|
|
612 | |
|
|
613 | lo = decode_4hex (dec); |
|
|
614 | if (lo == (UV)-1) |
|
|
615 | goto fail; |
|
|
616 | |
549 | if (lo < 0xdc00 || lo >= 0xe000) |
617 | if (lo < 0xdc00 || lo >= 0xe000) |
550 | ERR ("surrogate pair expected"); |
618 | ERR ("surrogate pair expected"); |
551 | |
619 | |
552 | hi = (hi - 0xD800) * 0x400 + (lo - 0xDC00) + 0x10000; |
620 | hi = (hi - 0xD800) * 0x400 + (lo - 0xDC00) + 0x10000; |
|
|
621 | } |
|
|
622 | else if (hi < 0xe000) |
|
|
623 | ERR ("missing high surrogate character in surrogate pair"); |
|
|
624 | |
|
|
625 | if (hi >= 0x80) |
|
|
626 | { |
|
|
627 | utf8 = 1; |
|
|
628 | |
|
|
629 | cur = (char *)uvuni_to_utf8_flags (cur, hi, 0); |
|
|
630 | } |
|
|
631 | else |
|
|
632 | *cur++ = hi; |
553 | } |
633 | } |
554 | else if (hi >= 0xdc00 && hi < 0xe000) |
|
|
555 | ERR ("missing high surrogate character in surrogate pair"); |
|
|
556 | |
|
|
557 | if (hi >= 0x80) |
|
|
558 | { |
634 | break; |
559 | utf8 = 1; |
|
|
560 | |
635 | |
561 | APPEND_GROW (4); // at most 4 bytes for 21 bits |
|
|
562 | cur = (char *)uvuni_to_utf8_flags (cur, hi, 0); |
|
|
563 | } |
|
|
564 | else |
636 | default: |
565 | APPEND_CH (hi); |
637 | --dec->cur; |
|
|
638 | ERR ("illegal backslash escape sequence in string"); |
566 | } |
639 | } |
567 | break; |
|
|
568 | |
|
|
569 | default: |
|
|
570 | --dec->cur; |
|
|
571 | ERR ("illegal backslash escape sequence in string"); |
|
|
572 | } |
640 | } |
|
|
641 | else if (ch >= 0x20 && ch <= 0x7f) |
|
|
642 | *cur++ = ch; |
|
|
643 | else if (ch >= 0x80) |
|
|
644 | { |
|
|
645 | --dec->cur; |
|
|
646 | |
|
|
647 | STRLEN clen; |
|
|
648 | UV uch = decode_utf8 (dec->cur, dec->end - dec->cur, &clen); |
|
|
649 | if (clen == (STRLEN)-1) |
|
|
650 | ERR ("malformed UTF-8 character in JSON string"); |
|
|
651 | |
|
|
652 | do |
|
|
653 | { |
|
|
654 | *cur++ = *dec->cur++; |
|
|
655 | } |
|
|
656 | while (--clen); |
|
|
657 | |
|
|
658 | utf8 = 1; |
|
|
659 | } |
|
|
660 | else if (!ch) |
|
|
661 | ERR ("unexpected end of string while parsing json string"); |
|
|
662 | else |
|
|
663 | ERR ("invalid character encountered"); |
|
|
664 | |
573 | } |
665 | } |
574 | else if (ch >= 0x20 && ch <= 0x7f) |
666 | while (cur < buf + SHORT_STRING_LEN); |
575 | APPEND_CH (*dec->cur++); |
667 | |
576 | else if (ch >= 0x80) |
668 | STRLEN len = cur - buf; |
|
|
669 | |
|
|
670 | if (sv) |
577 | { |
671 | { |
578 | STRLEN clen; |
672 | SvGROW (sv, SvCUR (sv) + len + 1); |
579 | UV uch = utf8n_to_uvuni (dec->cur, dec->end - dec->cur, &clen, UTF8_CHECK_ONLY); |
673 | memcpy (SvPVX (sv) + SvCUR (sv), buf, len); |
580 | if (clen == (STRLEN)-1) |
674 | SvCUR_set (sv, SvCUR (sv) + len); |
581 | ERR ("malformed UTF-8 character in string, cannot convert to JSON"); |
|
|
582 | |
|
|
583 | APPEND_GROW (clen); |
|
|
584 | do |
|
|
585 | { |
|
|
586 | *cur++ = *dec->cur++; |
|
|
587 | } |
|
|
588 | while (--clen); |
|
|
589 | |
|
|
590 | utf8 = 1; |
|
|
591 | } |
675 | } |
592 | else if (dec->cur == dec->end) |
|
|
593 | ERR ("unexpected end of string while parsing json string"); |
|
|
594 | else |
676 | else |
595 | ERR ("invalid character encountered"); |
677 | sv = newSVpvn (buf, len); |
596 | } |
678 | } |
|
|
679 | while (*dec->cur != '"'); |
597 | |
680 | |
598 | ++dec->cur; |
681 | ++dec->cur; |
599 | |
682 | |
600 | SvCUR_set (sv, cur - SvPVX (sv)); |
683 | if (sv) |
601 | |
684 | { |
602 | SvPOK_only (sv); |
685 | SvPOK_only (sv); |
603 | *SvEND (sv) = 0; |
686 | *SvEND (sv) = 0; |
604 | |
687 | |
605 | if (utf8) |
688 | if (utf8) |
606 | SvUTF8_on (sv); |
689 | SvUTF8_on (sv); |
607 | |
690 | } |
608 | #ifdef SvPV_shrink_to_cur |
691 | else |
609 | if (dec->flags & F_SHRINK) |
692 | sv = newSVpvn ("", 0); |
610 | SvPV_shrink_to_cur (sv); |
|
|
611 | #endif |
|
|
612 | |
693 | |
613 | return sv; |
694 | return sv; |
614 | |
695 | |
615 | fail: |
696 | fail: |
616 | SvREFCNT_dec (sv); |
|
|
617 | return 0; |
697 | return 0; |
618 | } |
698 | } |
619 | |
699 | |
620 | static SV * |
700 | static SV * |
621 | decode_num (dec_t *dec) |
701 | decode_num (dec_t *dec) |
… | |
… | |
702 | static SV * |
782 | static SV * |
703 | decode_av (dec_t *dec) |
783 | decode_av (dec_t *dec) |
704 | { |
784 | { |
705 | AV *av = newAV (); |
785 | AV *av = newAV (); |
706 | |
786 | |
707 | WS; |
787 | decode_ws (dec); |
708 | if (*dec->cur == ']') |
788 | if (*dec->cur == ']') |
709 | ++dec->cur; |
789 | ++dec->cur; |
710 | else |
790 | else |
711 | for (;;) |
791 | for (;;) |
712 | { |
792 | { |
… | |
… | |
716 | if (!value) |
796 | if (!value) |
717 | goto fail; |
797 | goto fail; |
718 | |
798 | |
719 | av_push (av, value); |
799 | av_push (av, value); |
720 | |
800 | |
721 | WS; |
801 | decode_ws (dec); |
722 | |
802 | |
723 | if (*dec->cur == ']') |
803 | if (*dec->cur == ']') |
724 | { |
804 | { |
725 | ++dec->cur; |
805 | ++dec->cur; |
726 | break; |
806 | break; |
… | |
… | |
742 | static SV * |
822 | static SV * |
743 | decode_hv (dec_t *dec) |
823 | decode_hv (dec_t *dec) |
744 | { |
824 | { |
745 | HV *hv = newHV (); |
825 | HV *hv = newHV (); |
746 | |
826 | |
747 | WS; |
827 | decode_ws (dec); |
748 | if (*dec->cur == '}') |
828 | if (*dec->cur == '}') |
749 | ++dec->cur; |
829 | ++dec->cur; |
750 | else |
830 | else |
751 | for (;;) |
831 | for (;;) |
752 | { |
832 | { |
753 | SV *key, *value; |
833 | SV *key, *value; |
754 | |
834 | |
755 | WS; EXPECT_CH ('"'); |
835 | decode_ws (dec); EXPECT_CH ('"'); |
756 | |
836 | |
757 | key = decode_str (dec); |
837 | key = decode_str (dec); |
758 | if (!key) |
838 | if (!key) |
759 | goto fail; |
839 | goto fail; |
760 | |
840 | |
761 | WS; EXPECT_CH (':'); |
841 | decode_ws (dec); EXPECT_CH (':'); |
762 | |
842 | |
763 | value = decode_sv (dec); |
843 | value = decode_sv (dec); |
764 | if (!value) |
844 | if (!value) |
765 | { |
845 | { |
766 | SvREFCNT_dec (key); |
846 | SvREFCNT_dec (key); |
… | |
… | |
768 | } |
848 | } |
769 | |
849 | |
770 | //TODO: optimise |
850 | //TODO: optimise |
771 | hv_store_ent (hv, key, value, 0); |
851 | hv_store_ent (hv, key, value, 0); |
772 | |
852 | |
773 | WS; |
853 | decode_ws (dec); |
774 | |
854 | |
775 | if (*dec->cur == '}') |
855 | if (*dec->cur == '}') |
776 | { |
856 | { |
777 | ++dec->cur; |
857 | ++dec->cur; |
778 | break; |
858 | break; |
… | |
… | |
792 | } |
872 | } |
793 | |
873 | |
794 | static SV * |
874 | static SV * |
795 | decode_sv (dec_t *dec) |
875 | decode_sv (dec_t *dec) |
796 | { |
876 | { |
797 | WS; |
877 | decode_ws (dec); |
798 | switch (*dec->cur) |
878 | switch (*dec->cur) |
799 | { |
879 | { |
800 | case '"': ++dec->cur; return decode_str (dec); |
880 | case '"': ++dec->cur; return decode_str (dec); |
801 | case '[': ++dec->cur; return decode_av (dec); |
881 | case '[': ++dec->cur; return decode_av (dec); |
802 | case '{': ++dec->cur; return decode_hv (dec); |
882 | case '{': ++dec->cur; return decode_hv (dec); |
… | |
… | |
838 | ERR ("'null' expected"); |
918 | ERR ("'null' expected"); |
839 | |
919 | |
840 | break; |
920 | break; |
841 | |
921 | |
842 | default: |
922 | default: |
843 | ERR ("malformed json string"); |
923 | ERR ("malformed json string, neither array, object, number, string or atom"); |
844 | break; |
924 | break; |
845 | } |
925 | } |
846 | |
926 | |
847 | fail: |
927 | fail: |
848 | return 0; |
928 | return 0; |
… | |
… | |
868 | |
948 | |
869 | sv = decode_sv (&dec); |
949 | sv = decode_sv (&dec); |
870 | |
950 | |
871 | if (!sv) |
951 | if (!sv) |
872 | { |
952 | { |
|
|
953 | IV offset = dec.flags & F_UTF8 |
|
|
954 | ? dec.cur - SvPVX (string) |
873 | IV offset = utf8_distance (dec.cur, SvPVX (string)); |
955 | : utf8_distance (dec.cur, SvPVX (string)); |
874 | SV *uni = sv_newmortal (); |
956 | SV *uni = sv_newmortal (); |
|
|
957 | |
875 | // horrible hack to silence warning inside pv_uni_display |
958 | // horrible hack to silence warning inside pv_uni_display |
876 | COP cop; |
959 | COP cop = *PL_curcop; |
877 | memset (&cop, 0, sizeof (cop)); |
|
|
878 | cop.cop_warnings = pWARN_NONE; |
960 | cop.cop_warnings = pWARN_NONE; |
|
|
961 | ENTER; |
879 | SAVEVPTR (PL_curcop); |
962 | SAVEVPTR (PL_curcop); |
880 | PL_curcop = &cop; |
963 | PL_curcop = &cop; |
881 | |
|
|
882 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
964 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
|
|
965 | LEAVE; |
|
|
966 | |
883 | croak ("%s, at character offset %d (%s)", |
967 | croak ("%s, at character offset %d (%s)", |
884 | dec.err, |
968 | dec.err, |
885 | (int)offset, |
969 | (int)offset, |
886 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
970 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
887 | } |
971 | } |
888 | |
972 | |
889 | sv = sv_2mortal (sv); |
973 | sv = sv_2mortal (sv); |
890 | |
974 | |
891 | if (!(dec.flags & F_ALLOW_NONREF) && !SvROK (sv)) |
975 | if (!(dec.flags & F_ALLOW_NONREF) && !SvROK (sv)) |
892 | croak ("JSON object or array expected (but number, string, true, false or null found, use allow_nonref to allow this)"); |
976 | croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)"); |
893 | |
977 | |
894 | return sv; |
978 | return sv; |
895 | } |
979 | } |
|
|
980 | |
|
|
981 | ///////////////////////////////////////////////////////////////////////////// |
|
|
982 | // XS interface functions |
896 | |
983 | |
897 | MODULE = JSON::XS PACKAGE = JSON::XS |
984 | MODULE = JSON::XS PACKAGE = JSON::XS |
898 | |
985 | |
899 | BOOT: |
986 | BOOT: |
900 | { |
987 | { |
… | |
… | |
927 | utf8 = F_UTF8 |
1014 | utf8 = F_UTF8 |
928 | indent = F_INDENT |
1015 | indent = F_INDENT |
929 | canonical = F_CANONICAL |
1016 | canonical = F_CANONICAL |
930 | space_before = F_SPACE_BEFORE |
1017 | space_before = F_SPACE_BEFORE |
931 | space_after = F_SPACE_AFTER |
1018 | space_after = F_SPACE_AFTER |
932 | json_rpc = F_JSON_RPC |
|
|
933 | pretty = F_PRETTY |
1019 | pretty = F_PRETTY |
934 | allow_nonref = F_ALLOW_NONREF |
1020 | allow_nonref = F_ALLOW_NONREF |
935 | shrink = F_SHRINK |
1021 | shrink = F_SHRINK |
936 | CODE: |
1022 | CODE: |
937 | { |
1023 | { |
… | |
… | |
955 | XPUSHs (decode_json (jsonstr, *SvJSON (self))); |
1041 | XPUSHs (decode_json (jsonstr, *SvJSON (self))); |
956 | |
1042 | |
957 | PROTOTYPES: ENABLE |
1043 | PROTOTYPES: ENABLE |
958 | |
1044 | |
959 | void to_json (SV *scalar) |
1045 | void to_json (SV *scalar) |
|
|
1046 | ALIAS: |
|
|
1047 | objToJson = 0 |
960 | PPCODE: |
1048 | PPCODE: |
961 | XPUSHs (encode_json (scalar, F_UTF8)); |
1049 | XPUSHs (encode_json (scalar, F_UTF8)); |
962 | |
1050 | |
963 | void from_json (SV *jsonstr) |
1051 | void from_json (SV *jsonstr) |
|
|
1052 | ALIAS: |
|
|
1053 | jsonToObj = 0 |
964 | PPCODE: |
1054 | PPCODE: |
965 | XPUSHs (decode_json (jsonstr, F_UTF8)); |
1055 | XPUSHs (decode_json (jsonstr, F_UTF8)); |
966 | |
1056 | |