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