… | |
… | |
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 256 // 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 | ///////////////////////////////////////////////////////////////////////////// |
|
|
59 | // encoder |
28 | |
60 | |
29 | // structure used for encoding JSON |
61 | // structure used for encoding JSON |
30 | typedef struct |
62 | typedef struct |
31 | { |
63 | { |
32 | char *cur; |
64 | char *cur; // SvPVX (sv) + current output position |
33 | STRLEN len; // SvLEN (sv) |
|
|
34 | char *end; // SvEND (sv) |
65 | char *end; // SvEND (sv) |
35 | SV *sv; |
66 | SV *sv; // result scalar |
36 | UV flags; |
67 | UV flags; // F_* |
37 | int max_recurse; |
68 | int indent; // indentation level |
38 | int indent; |
69 | int max_depth; // max. recursion level |
39 | } enc_t; |
70 | } 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 | static void |
|
|
60 | shrink (SV *sv) |
|
|
61 | { |
|
|
62 | sv_utf8_downgrade (sv, 1); |
|
|
63 | #ifdef SvPV_shrink_to_cur |
|
|
64 | SvPV_shrink_to_cur (sv); |
|
|
65 | #endif |
|
|
66 | } |
|
|
67 | |
|
|
68 | ///////////////////////////////////////////////////////////////////////////// |
|
|
69 | |
71 | |
70 | static void |
72 | static void |
71 | need (enc_t *enc, STRLEN len) |
73 | need (enc_t *enc, STRLEN len) |
72 | { |
74 | { |
73 | if (enc->cur + len >= enc->end) |
75 | if (enc->cur + len >= enc->end) |
… | |
… | |
150 | { |
152 | { |
151 | if (uch > 0xFFFFUL) |
153 | if (uch > 0xFFFFUL) |
152 | { |
154 | { |
153 | need (enc, len += 11); |
155 | need (enc, len += 11); |
154 | sprintf (enc->cur, "\\u%04x\\u%04x", |
156 | sprintf (enc->cur, "\\u%04x\\u%04x", |
155 | (uch - 0x10000) / 0x400 + 0xD800, |
157 | (int)((uch - 0x10000) / 0x400 + 0xD800), |
156 | (uch - 0x10000) % 0x400 + 0xDC00); |
158 | (int)((uch - 0x10000) % 0x400 + 0xDC00)); |
157 | enc->cur += 12; |
159 | enc->cur += 12; |
158 | } |
160 | } |
159 | else |
161 | else |
160 | { |
162 | { |
161 | static char hexdigit [16] = "0123456789abcdef"; |
163 | static char hexdigit [16] = "0123456789abcdef"; |
… | |
… | |
179 | } |
181 | } |
180 | while (--clen); |
182 | while (--clen); |
181 | } |
183 | } |
182 | else |
184 | else |
183 | { |
185 | { |
184 | need (enc, len += 10); // never more than 11 bytes needed |
186 | need (enc, len += UTF8_MAX_LEN - 1); // never more than 11 bytes needed |
185 | enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
187 | enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
186 | ++str; |
188 | ++str; |
187 | } |
189 | } |
188 | } |
190 | } |
189 | } |
191 | } |
… | |
… | |
191 | |
193 | |
192 | --len; |
194 | --len; |
193 | } |
195 | } |
194 | } |
196 | } |
195 | |
197 | |
196 | #define INDENT SB \ |
198 | static void |
|
|
199 | encode_indent (enc_t *enc) |
|
|
200 | { |
197 | if (enc->flags & F_INDENT) \ |
201 | if (enc->flags & F_INDENT) |
198 | { \ |
202 | { |
199 | int i_; \ |
203 | int spaces = enc->indent * INDENT_STEP; |
200 | need (enc, enc->indent); \ |
|
|
201 | for (i_ = enc->indent * 3; i_--; )\ |
|
|
202 | encode_ch (enc, ' '); \ |
|
|
203 | } \ |
|
|
204 | SE |
|
|
205 | |
204 | |
206 | #define SPACE SB need (enc, 1); encode_ch (enc, ' '); SE |
205 | need (enc, spaces); |
207 | #define NL SB if (enc->flags & F_INDENT) { need (enc, 1); encode_ch (enc, '\n'); } SE |
206 | memset (enc->cur, ' ', spaces); |
208 | #define COMMA SB \ |
207 | enc->cur += spaces; |
|
|
208 | } |
|
|
209 | } |
|
|
210 | |
|
|
211 | static void |
|
|
212 | encode_space (enc_t *enc) |
|
|
213 | { |
|
|
214 | need (enc, 1); |
209 | encode_ch (enc, ','); \ |
215 | encode_ch (enc, ' '); |
|
|
216 | } |
|
|
217 | |
|
|
218 | static void |
|
|
219 | encode_nl (enc_t *enc) |
|
|
220 | { |
210 | if (enc->flags & F_INDENT) \ |
221 | if (enc->flags & F_INDENT) |
211 | NL; \ |
222 | { |
|
|
223 | need (enc, 1); |
|
|
224 | encode_ch (enc, '\n'); |
|
|
225 | } |
|
|
226 | } |
|
|
227 | |
|
|
228 | static void |
|
|
229 | encode_comma (enc_t *enc) |
|
|
230 | { |
|
|
231 | encode_ch (enc, ','); |
|
|
232 | |
|
|
233 | if (enc->flags & F_INDENT) |
|
|
234 | encode_nl (enc); |
212 | else if (enc->flags & F_SPACE_AFTER) \ |
235 | else if (enc->flags & F_SPACE_AFTER) |
213 | SPACE; \ |
236 | encode_space (enc); |
214 | SE |
237 | } |
215 | |
238 | |
216 | static void encode_sv (enc_t *enc, SV *sv); |
239 | static void encode_sv (enc_t *enc, SV *sv); |
217 | |
240 | |
218 | static void |
241 | static void |
219 | encode_av (enc_t *enc, AV *av) |
242 | encode_av (enc_t *enc, AV *av) |
220 | { |
243 | { |
221 | int i, len = av_len (av); |
244 | int i, len = av_len (av); |
222 | |
245 | |
223 | encode_ch (enc, '['); NL; |
246 | encode_ch (enc, '['); encode_nl (enc); |
224 | ++enc->indent; |
247 | ++enc->indent; |
225 | |
248 | |
226 | for (i = 0; i <= len; ++i) |
249 | for (i = 0; i <= len; ++i) |
227 | { |
250 | { |
228 | INDENT; |
251 | encode_indent (enc); |
229 | encode_sv (enc, *av_fetch (av, i, 0)); |
252 | encode_sv (enc, *av_fetch (av, i, 0)); |
230 | |
253 | |
231 | if (i < len) |
254 | if (i < len) |
232 | COMMA; |
255 | encode_comma (enc); |
233 | } |
256 | } |
234 | |
257 | |
235 | NL; |
258 | encode_nl (enc); |
236 | |
259 | |
237 | --enc->indent; |
260 | --enc->indent; |
238 | INDENT; encode_ch (enc, ']'); |
261 | encode_indent (enc); encode_ch (enc, ']'); |
239 | } |
262 | } |
240 | |
263 | |
241 | static void |
264 | static void |
242 | encode_he (enc_t *enc, HE *he) |
265 | encode_he (enc_t *enc, HE *he) |
243 | { |
266 | { |
… | |
… | |
257 | else |
280 | else |
258 | encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); |
281 | encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); |
259 | |
282 | |
260 | encode_ch (enc, '"'); |
283 | encode_ch (enc, '"'); |
261 | |
284 | |
262 | if (enc->flags & F_SPACE_BEFORE) SPACE; |
285 | if (enc->flags & F_SPACE_BEFORE) encode_space (enc); |
263 | encode_ch (enc, ':'); |
286 | encode_ch (enc, ':'); |
264 | if (enc->flags & F_SPACE_AFTER ) SPACE; |
287 | if (enc->flags & F_SPACE_AFTER ) encode_space (enc); |
265 | encode_sv (enc, HeVAL (he)); |
288 | encode_sv (enc, HeVAL (he)); |
266 | } |
289 | } |
267 | |
290 | |
268 | // compare hash entries, used when all keys are bytestrings |
291 | // compare hash entries, used when all keys are bytestrings |
269 | static int |
292 | static int |
… | |
… | |
275 | HE *b = *(HE **)b_; |
298 | HE *b = *(HE **)b_; |
276 | |
299 | |
277 | STRLEN la = HeKLEN (a); |
300 | STRLEN la = HeKLEN (a); |
278 | STRLEN lb = HeKLEN (b); |
301 | STRLEN lb = HeKLEN (b); |
279 | |
302 | |
280 | if (!(cmp == memcmp (HeKEY (a), HeKEY (b), la < lb ? la : lb))) |
303 | if (!(cmp = memcmp (HeKEY (a), HeKEY (b), la < lb ? la : lb))) |
281 | cmp = la < lb ? -1 : la == lb ? 0 : 1; |
304 | cmp = la - lb; |
282 | |
305 | |
283 | return cmp; |
306 | return cmp; |
284 | } |
307 | } |
285 | |
308 | |
286 | // compare hash entries, used when some keys are sv's or utf-x |
309 | // compare hash entries, used when some keys are sv's or utf-x |
… | |
… | |
293 | static void |
316 | static void |
294 | encode_hv (enc_t *enc, HV *hv) |
317 | encode_hv (enc_t *enc, HV *hv) |
295 | { |
318 | { |
296 | int count, i; |
319 | int count, i; |
297 | |
320 | |
298 | encode_ch (enc, '{'); NL; ++enc->indent; |
321 | encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; |
299 | |
322 | |
300 | if ((count = hv_iterinit (hv))) |
323 | if ((count = hv_iterinit (hv))) |
301 | { |
324 | { |
302 | // for canonical output we have to sort by keys first |
325 | // for canonical output we have to sort by keys first |
303 | // actually, this is mostly due to the stupid so-called |
326 | // actually, this is mostly due to the stupid so-called |
304 | // security workaround added somewhere in 5.8.x. |
327 | // security workaround added somewhere in 5.8.x. |
305 | // that randomises hash orderings |
328 | // that randomises hash orderings |
306 | if (enc->flags & F_CANONICAL) |
329 | if (enc->flags & F_CANONICAL) |
307 | { |
330 | { |
308 | HE *he, *hes [count]; |
331 | HE *he, *hes [count]; // if your compiler dies here, you need to enable C99 mode |
309 | int fast = 1; |
332 | int fast = 1; |
310 | |
333 | |
311 | i = 0; |
334 | i = 0; |
312 | while ((he = hv_iternext (hv))) |
335 | while ((he = hv_iternext (hv))) |
313 | { |
336 | { |
… | |
… | |
338 | LEAVE; |
361 | LEAVE; |
339 | } |
362 | } |
340 | |
363 | |
341 | for (i = 0; i < count; ++i) |
364 | for (i = 0; i < count; ++i) |
342 | { |
365 | { |
343 | INDENT; |
366 | encode_indent (enc); |
344 | encode_he (enc, hes [i]); |
367 | encode_he (enc, hes [i]); |
345 | |
368 | |
346 | if (i < count - 1) |
369 | if (i < count - 1) |
347 | COMMA; |
370 | encode_comma (enc); |
348 | } |
371 | } |
349 | |
372 | |
350 | NL; |
373 | encode_nl (enc); |
351 | } |
374 | } |
352 | else |
375 | else |
353 | { |
376 | { |
354 | SV *sv; |
377 | SV *sv; |
355 | HE *he = hv_iternext (hv); |
378 | HE *he = hv_iternext (hv); |
356 | |
379 | |
357 | for (;;) |
380 | for (;;) |
358 | { |
381 | { |
359 | INDENT; |
382 | encode_indent (enc); |
360 | encode_he (enc, he); |
383 | encode_he (enc, he); |
361 | |
384 | |
362 | if (!(he = hv_iternext (hv))) |
385 | if (!(he = hv_iternext (hv))) |
363 | break; |
386 | break; |
364 | |
387 | |
365 | COMMA; |
388 | encode_comma (enc); |
366 | } |
389 | } |
367 | |
390 | |
368 | NL; |
391 | encode_nl (enc); |
369 | } |
392 | } |
370 | } |
393 | } |
371 | |
394 | |
372 | --enc->indent; INDENT; encode_ch (enc, '}'); |
395 | --enc->indent; encode_indent (enc); encode_ch (enc, '}'); |
373 | } |
396 | } |
374 | |
397 | |
375 | static void |
398 | static void |
376 | encode_sv (enc_t *enc, SV *sv) |
399 | encode_sv (enc_t *enc, SV *sv) |
377 | { |
400 | { |
… | |
… | |
401 | } |
424 | } |
402 | else if (SvROK (sv)) |
425 | else if (SvROK (sv)) |
403 | { |
426 | { |
404 | SV *rv = SvRV (sv); |
427 | SV *rv = SvRV (sv); |
405 | |
428 | |
406 | if (!--enc->max_recurse) |
429 | if (enc->indent >= enc->max_depth) |
407 | croak ("data structure too deep (hit recursion limit)"); |
430 | croak ("data structure too deep (hit recursion limit)"); |
408 | |
431 | |
409 | switch (SvTYPE (rv)) |
432 | switch (SvTYPE (rv)) |
410 | { |
433 | { |
411 | case SVt_PVAV: encode_av (enc, (AV *)rv); break; |
434 | case SVt_PVAV: encode_av (enc, (AV *)rv); break; |
… | |
… | |
428 | { |
451 | { |
429 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
452 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
430 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
453 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
431 | |
454 | |
432 | enc_t enc; |
455 | enc_t enc; |
433 | enc.flags = flags; |
456 | enc.flags = flags; |
434 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
457 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
435 | enc.cur = SvPVX (enc.sv); |
458 | enc.cur = SvPVX (enc.sv); |
436 | enc.end = SvEND (enc.sv); |
459 | enc.end = SvEND (enc.sv); |
437 | enc.max_recurse = 0; |
|
|
438 | enc.indent = 0; |
460 | enc.indent = 0; |
|
|
461 | enc.max_depth = 0x7fffffffUL; |
439 | |
462 | |
440 | SvPOK_only (enc.sv); |
463 | SvPOK_only (enc.sv); |
441 | encode_sv (&enc, scalar); |
464 | encode_sv (&enc, scalar); |
442 | |
465 | |
443 | if (!(flags & (F_ASCII | F_UTF8))) |
466 | if (!(flags & (F_ASCII | F_UTF8))) |
… | |
… | |
450 | |
473 | |
451 | return enc.sv; |
474 | return enc.sv; |
452 | } |
475 | } |
453 | |
476 | |
454 | ///////////////////////////////////////////////////////////////////////////// |
477 | ///////////////////////////////////////////////////////////////////////////// |
|
|
478 | // decoder |
455 | |
479 | |
456 | #define WS \ |
480 | // structure used for decoding JSON |
|
|
481 | typedef struct |
|
|
482 | { |
|
|
483 | char *cur; // current parser pointer |
|
|
484 | char *end; // end of input string |
|
|
485 | const char *err; // parse error, if != 0 |
|
|
486 | UV flags; // F_* |
|
|
487 | } dec_t; |
|
|
488 | |
|
|
489 | static void |
|
|
490 | decode_ws (dec_t *dec) |
|
|
491 | { |
457 | for (;;) \ |
492 | for (;;) |
458 | { \ |
493 | { |
459 | char ch = *dec->cur; \ |
494 | char ch = *dec->cur; |
|
|
495 | |
460 | if (ch > 0x20 \ |
496 | if (ch > 0x20 |
461 | || (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)) \ |
497 | || (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)) |
462 | break; \ |
498 | break; |
|
|
499 | |
463 | ++dec->cur; \ |
500 | ++dec->cur; |
464 | } |
501 | } |
|
|
502 | } |
465 | |
503 | |
466 | #define ERR(reason) SB dec->err = reason; goto fail; SE |
504 | #define ERR(reason) SB dec->err = reason; goto fail; SE |
467 | #define EXPECT_CH(ch) SB \ |
505 | #define EXPECT_CH(ch) SB \ |
468 | if (*dec->cur != ch) \ |
506 | if (*dec->cur != ch) \ |
469 | ERR (# ch " expected"); \ |
507 | ERR (# ch " expected"); \ |
… | |
… | |
476 | |
514 | |
477 | static UV |
515 | static UV |
478 | decode_4hex (dec_t *dec) |
516 | decode_4hex (dec_t *dec) |
479 | { |
517 | { |
480 | signed char d1, d2, d3, d4; |
518 | signed char d1, d2, d3, d4; |
|
|
519 | unsigned char *cur = (unsigned char *)dec->cur; |
481 | |
520 | |
482 | d1 = decode_hexdigit [((unsigned char *)dec->cur) [0]]; |
|
|
483 | if (d1 < 0) ERR ("four hexadecimal digits expected"); |
521 | d1 = decode_hexdigit [cur [0]]; if (d1 < 0) ERR ("four hexadecimal digits expected"); |
484 | d2 = decode_hexdigit [((unsigned char *)dec->cur) [1]]; |
|
|
485 | if (d2 < 0) ERR ("four hexadecimal digits expected"); |
522 | d2 = decode_hexdigit [cur [1]]; if (d2 < 0) ERR ("four hexadecimal digits expected"); |
486 | d3 = decode_hexdigit [((unsigned char *)dec->cur) [2]]; |
|
|
487 | if (d3 < 0) ERR ("four hexadecimal digits expected"); |
523 | d3 = decode_hexdigit [cur [2]]; if (d3 < 0) ERR ("four hexadecimal digits expected"); |
488 | d4 = decode_hexdigit [((unsigned char *)dec->cur) [3]]; |
|
|
489 | if (d4 < 0) ERR ("four hexadecimal digits expected"); |
524 | d4 = decode_hexdigit [cur [3]]; if (d4 < 0) ERR ("four hexadecimal digits expected"); |
490 | |
525 | |
491 | dec->cur += 4; |
526 | dec->cur += 4; |
492 | |
527 | |
493 | return ((UV)d1) << 12 |
528 | return ((UV)d1) << 12 |
494 | | ((UV)d2) << 8 |
529 | | ((UV)d2) << 8 |
… | |
… | |
497 | |
532 | |
498 | fail: |
533 | fail: |
499 | return (UV)-1; |
534 | return (UV)-1; |
500 | } |
535 | } |
501 | |
536 | |
502 | #define APPEND_GROW(n) SB \ |
|
|
503 | if (cur + (n) >= end) \ |
|
|
504 | { \ |
|
|
505 | STRLEN ofs = cur - SvPVX (sv); \ |
|
|
506 | SvGROW (sv, ofs + (n) + 1); \ |
|
|
507 | cur = SvPVX (sv) + ofs; \ |
|
|
508 | end = SvEND (sv); \ |
|
|
509 | } \ |
|
|
510 | SE |
|
|
511 | |
|
|
512 | #define APPEND_CH(ch) SB \ |
|
|
513 | APPEND_GROW (1); \ |
|
|
514 | *cur++ = (ch); \ |
|
|
515 | SE |
|
|
516 | |
|
|
517 | static SV * |
537 | static SV * |
518 | decode_str (dec_t *dec) |
538 | decode_str (dec_t *dec) |
519 | { |
539 | { |
520 | SV *sv = NEWSV (0,2); |
540 | SV *sv = 0; |
521 | int utf8 = 0; |
541 | int utf8 = 0; |
522 | char *cur = SvPVX (sv); |
|
|
523 | char *end = SvEND (sv); |
|
|
524 | |
542 | |
525 | for (;;) |
543 | do |
526 | { |
544 | { |
527 | unsigned char ch = *(unsigned char *)dec->cur; |
545 | char buf [SHORT_STRING_LEN + UTF8_MAX_LEN]; |
|
|
546 | char *cur = buf; |
528 | |
547 | |
529 | if (ch == '"') |
548 | do |
530 | break; |
|
|
531 | else if (ch == '\\') |
|
|
532 | { |
549 | { |
533 | switch (*++dec->cur) |
550 | unsigned char ch = *(unsigned char *)dec->cur++; |
|
|
551 | |
|
|
552 | if (ch == '"') |
534 | { |
553 | { |
535 | case '\\': |
554 | --dec->cur; |
536 | case '/': |
555 | break; |
537 | case '"': APPEND_CH (*dec->cur++); break; |
556 | } |
538 | |
557 | else if (ch == '\\') |
539 | case 'b': APPEND_CH ('\010'); ++dec->cur; break; |
558 | { |
540 | case 't': APPEND_CH ('\011'); ++dec->cur; break; |
559 | switch (*dec->cur) |
541 | case 'n': APPEND_CH ('\012'); ++dec->cur; break; |
|
|
542 | case 'f': APPEND_CH ('\014'); ++dec->cur; break; |
|
|
543 | case 'r': APPEND_CH ('\015'); ++dec->cur; break; |
|
|
544 | |
|
|
545 | case 'u': |
|
|
546 | { |
560 | { |
547 | UV lo, hi; |
561 | case '\\': |
548 | ++dec->cur; |
562 | case '/': |
|
|
563 | case '"': *cur++ = *dec->cur++; break; |
549 | |
564 | |
550 | hi = decode_4hex (dec); |
565 | case 'b': ++dec->cur; *cur++ = '\010'; break; |
551 | if (hi == (UV)-1) |
566 | case 't': ++dec->cur; *cur++ = '\011'; break; |
552 | goto fail; |
567 | case 'n': ++dec->cur; *cur++ = '\012'; break; |
|
|
568 | case 'f': ++dec->cur; *cur++ = '\014'; break; |
|
|
569 | case 'r': ++dec->cur; *cur++ = '\015'; break; |
553 | |
570 | |
554 | // possibly a surrogate pair |
571 | case 'u': |
555 | if (hi >= 0xd800 && hi < 0xdc00) |
|
|
556 | { |
572 | { |
557 | if (dec->cur [0] != '\\' || dec->cur [1] != 'u') |
573 | UV lo, hi; |
558 | ERR ("missing low surrogate character in surrogate pair"); |
|
|
559 | |
|
|
560 | dec->cur += 2; |
574 | ++dec->cur; |
561 | |
575 | |
562 | lo = decode_4hex (dec); |
576 | hi = decode_4hex (dec); |
563 | if (lo == (UV)-1) |
577 | if (hi == (UV)-1) |
564 | goto fail; |
578 | goto fail; |
565 | |
579 | |
|
|
580 | // possibly a surrogate pair |
|
|
581 | if (hi >= 0xd800) |
|
|
582 | if (hi < 0xdc00) |
|
|
583 | { |
|
|
584 | if (dec->cur [0] != '\\' || dec->cur [1] != 'u') |
|
|
585 | ERR ("missing low surrogate character in surrogate pair"); |
|
|
586 | |
|
|
587 | dec->cur += 2; |
|
|
588 | |
|
|
589 | lo = decode_4hex (dec); |
|
|
590 | if (lo == (UV)-1) |
|
|
591 | goto fail; |
|
|
592 | |
566 | if (lo < 0xdc00 || lo >= 0xe000) |
593 | if (lo < 0xdc00 || lo >= 0xe000) |
567 | ERR ("surrogate pair expected"); |
594 | ERR ("surrogate pair expected"); |
568 | |
595 | |
569 | hi = (hi - 0xD800) * 0x400 + (lo - 0xDC00) + 0x10000; |
596 | hi = (hi - 0xD800) * 0x400 + (lo - 0xDC00) + 0x10000; |
|
|
597 | } |
|
|
598 | else if (hi < 0xe000) |
|
|
599 | ERR ("missing high surrogate character in surrogate pair"); |
|
|
600 | |
|
|
601 | if (hi >= 0x80) |
|
|
602 | { |
|
|
603 | utf8 = 1; |
|
|
604 | |
|
|
605 | cur = (char *)uvuni_to_utf8_flags (cur, hi, 0); |
|
|
606 | } |
|
|
607 | else |
|
|
608 | *cur++ = hi; |
570 | } |
609 | } |
571 | else if (hi >= 0xdc00 && hi < 0xe000) |
|
|
572 | ERR ("missing high surrogate character in surrogate pair"); |
|
|
573 | |
|
|
574 | if (hi >= 0x80) |
|
|
575 | { |
610 | break; |
576 | utf8 = 1; |
|
|
577 | |
611 | |
578 | APPEND_GROW (4); // at most 4 bytes for 21 bits |
|
|
579 | cur = (char *)uvuni_to_utf8_flags (cur, hi, 0); |
|
|
580 | } |
|
|
581 | else |
612 | default: |
582 | APPEND_CH (hi); |
613 | --dec->cur; |
|
|
614 | ERR ("illegal backslash escape sequence in string"); |
583 | } |
615 | } |
584 | break; |
|
|
585 | |
|
|
586 | default: |
|
|
587 | --dec->cur; |
|
|
588 | ERR ("illegal backslash escape sequence in string"); |
|
|
589 | } |
616 | } |
|
|
617 | else if (ch >= 0x20 && ch <= 0x7f) |
|
|
618 | *cur++ = ch; |
|
|
619 | else if (ch >= 0x80) |
|
|
620 | { |
|
|
621 | --dec->cur; |
|
|
622 | |
|
|
623 | STRLEN clen; |
|
|
624 | UV uch = utf8n_to_uvuni (dec->cur, dec->end - dec->cur, &clen, UTF8_CHECK_ONLY); |
|
|
625 | if (clen == (STRLEN)-1) |
|
|
626 | ERR ("malformed UTF-8 character in JSON string"); |
|
|
627 | |
|
|
628 | do |
|
|
629 | { |
|
|
630 | *cur++ = *dec->cur++; |
|
|
631 | } |
|
|
632 | while (--clen); |
|
|
633 | |
|
|
634 | utf8 = 1; |
|
|
635 | } |
|
|
636 | else if (!ch) |
|
|
637 | ERR ("unexpected end of string while parsing json string"); |
|
|
638 | else |
|
|
639 | ERR ("invalid character encountered"); |
|
|
640 | |
590 | } |
641 | } |
591 | else if (ch >= 0x20 && ch <= 0x7f) |
642 | while (cur < buf + SHORT_STRING_LEN); |
592 | APPEND_CH (*dec->cur++); |
643 | |
593 | else if (ch >= 0x80) |
644 | STRLEN len = cur - buf; |
|
|
645 | |
|
|
646 | if (sv) |
594 | { |
647 | { |
595 | STRLEN clen; |
648 | SvGROW (sv, SvCUR (sv) + len + 1); |
596 | UV uch = utf8n_to_uvuni (dec->cur, dec->end - dec->cur, &clen, UTF8_CHECK_ONLY); |
649 | memcpy (SvPVX (sv) + SvCUR (sv), buf, len); |
597 | if (clen == (STRLEN)-1) |
650 | SvCUR_set (sv, SvCUR (sv) + len); |
598 | ERR ("malformed UTF-8 character in JSON string"); |
|
|
599 | |
|
|
600 | APPEND_GROW (clen); |
|
|
601 | do |
|
|
602 | { |
|
|
603 | *cur++ = *dec->cur++; |
|
|
604 | } |
|
|
605 | while (--clen); |
|
|
606 | |
|
|
607 | utf8 = 1; |
|
|
608 | } |
651 | } |
609 | else if (dec->cur == dec->end) |
|
|
610 | ERR ("unexpected end of string while parsing json string"); |
|
|
611 | else |
652 | else |
612 | ERR ("invalid character encountered"); |
653 | sv = newSVpvn (buf, len); |
613 | } |
654 | } |
|
|
655 | while (*dec->cur != '"'); |
614 | |
656 | |
615 | ++dec->cur; |
657 | ++dec->cur; |
616 | |
658 | |
617 | SvCUR_set (sv, cur - SvPVX (sv)); |
659 | if (sv) |
618 | |
660 | { |
619 | SvPOK_only (sv); |
661 | SvPOK_only (sv); |
620 | *SvEND (sv) = 0; |
662 | *SvEND (sv) = 0; |
621 | |
663 | |
622 | if (utf8) |
664 | if (utf8) |
623 | SvUTF8_on (sv); |
665 | SvUTF8_on (sv); |
624 | |
666 | } |
625 | if (dec->flags & F_SHRINK) |
667 | else |
626 | shrink (sv); |
668 | sv = newSVpvn ("", 0); |
627 | |
669 | |
628 | return sv; |
670 | return sv; |
629 | |
671 | |
630 | fail: |
672 | fail: |
631 | SvREFCNT_dec (sv); |
|
|
632 | return 0; |
673 | return 0; |
633 | } |
674 | } |
634 | |
675 | |
635 | static SV * |
676 | static SV * |
636 | decode_num (dec_t *dec) |
677 | decode_num (dec_t *dec) |
… | |
… | |
717 | static SV * |
758 | static SV * |
718 | decode_av (dec_t *dec) |
759 | decode_av (dec_t *dec) |
719 | { |
760 | { |
720 | AV *av = newAV (); |
761 | AV *av = newAV (); |
721 | |
762 | |
722 | WS; |
763 | decode_ws (dec); |
723 | if (*dec->cur == ']') |
764 | if (*dec->cur == ']') |
724 | ++dec->cur; |
765 | ++dec->cur; |
725 | else |
766 | else |
726 | for (;;) |
767 | for (;;) |
727 | { |
768 | { |
… | |
… | |
731 | if (!value) |
772 | if (!value) |
732 | goto fail; |
773 | goto fail; |
733 | |
774 | |
734 | av_push (av, value); |
775 | av_push (av, value); |
735 | |
776 | |
736 | WS; |
777 | decode_ws (dec); |
737 | |
778 | |
738 | if (*dec->cur == ']') |
779 | if (*dec->cur == ']') |
739 | { |
780 | { |
740 | ++dec->cur; |
781 | ++dec->cur; |
741 | break; |
782 | break; |
… | |
… | |
757 | static SV * |
798 | static SV * |
758 | decode_hv (dec_t *dec) |
799 | decode_hv (dec_t *dec) |
759 | { |
800 | { |
760 | HV *hv = newHV (); |
801 | HV *hv = newHV (); |
761 | |
802 | |
762 | WS; |
803 | decode_ws (dec); |
763 | if (*dec->cur == '}') |
804 | if (*dec->cur == '}') |
764 | ++dec->cur; |
805 | ++dec->cur; |
765 | else |
806 | else |
766 | for (;;) |
807 | for (;;) |
767 | { |
808 | { |
768 | SV *key, *value; |
809 | SV *key, *value; |
769 | |
810 | |
770 | WS; EXPECT_CH ('"'); |
811 | decode_ws (dec); EXPECT_CH ('"'); |
771 | |
812 | |
772 | key = decode_str (dec); |
813 | key = decode_str (dec); |
773 | if (!key) |
814 | if (!key) |
774 | goto fail; |
815 | goto fail; |
775 | |
816 | |
776 | WS; EXPECT_CH (':'); |
817 | decode_ws (dec); EXPECT_CH (':'); |
777 | |
818 | |
778 | value = decode_sv (dec); |
819 | value = decode_sv (dec); |
779 | if (!value) |
820 | if (!value) |
780 | { |
821 | { |
781 | SvREFCNT_dec (key); |
822 | SvREFCNT_dec (key); |
… | |
… | |
783 | } |
824 | } |
784 | |
825 | |
785 | //TODO: optimise |
826 | //TODO: optimise |
786 | hv_store_ent (hv, key, value, 0); |
827 | hv_store_ent (hv, key, value, 0); |
787 | |
828 | |
788 | WS; |
829 | decode_ws (dec); |
789 | |
830 | |
790 | if (*dec->cur == '}') |
831 | if (*dec->cur == '}') |
791 | { |
832 | { |
792 | ++dec->cur; |
833 | ++dec->cur; |
793 | break; |
834 | break; |
… | |
… | |
807 | } |
848 | } |
808 | |
849 | |
809 | static SV * |
850 | static SV * |
810 | decode_sv (dec_t *dec) |
851 | decode_sv (dec_t *dec) |
811 | { |
852 | { |
812 | WS; |
853 | decode_ws (dec); |
813 | switch (*dec->cur) |
854 | switch (*dec->cur) |
814 | { |
855 | { |
815 | case '"': ++dec->cur; return decode_str (dec); |
856 | case '"': ++dec->cur; return decode_str (dec); |
816 | case '[': ++dec->cur; return decode_av (dec); |
857 | case '[': ++dec->cur; return decode_av (dec); |
817 | case '{': ++dec->cur; return decode_hv (dec); |
858 | case '{': ++dec->cur; return decode_hv (dec); |
… | |
… | |
911 | croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)"); |
952 | croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)"); |
912 | |
953 | |
913 | return sv; |
954 | return sv; |
914 | } |
955 | } |
915 | |
956 | |
|
|
957 | ///////////////////////////////////////////////////////////////////////////// |
|
|
958 | // XS interface functions |
|
|
959 | |
916 | MODULE = JSON::XS PACKAGE = JSON::XS |
960 | MODULE = JSON::XS PACKAGE = JSON::XS |
917 | |
961 | |
918 | BOOT: |
962 | BOOT: |
919 | { |
963 | { |
920 | int i; |
964 | int i; |
… | |
… | |
946 | utf8 = F_UTF8 |
990 | utf8 = F_UTF8 |
947 | indent = F_INDENT |
991 | indent = F_INDENT |
948 | canonical = F_CANONICAL |
992 | canonical = F_CANONICAL |
949 | space_before = F_SPACE_BEFORE |
993 | space_before = F_SPACE_BEFORE |
950 | space_after = F_SPACE_AFTER |
994 | space_after = F_SPACE_AFTER |
951 | json_rpc = F_JSON_RPC |
|
|
952 | pretty = F_PRETTY |
995 | pretty = F_PRETTY |
953 | allow_nonref = F_ALLOW_NONREF |
996 | allow_nonref = F_ALLOW_NONREF |
954 | shrink = F_SHRINK |
997 | shrink = F_SHRINK |
955 | CODE: |
998 | CODE: |
956 | { |
999 | { |