1 | #include "EXTERN.h" |
1 | #include "EXTERN.h" |
2 | #include "perl.h" |
2 | #include "perl.h" |
3 | #include "XSUB.h" |
3 | #include "XSUB.h" |
4 | |
4 | |
5 | #include "assert.h" |
5 | #include <assert.h> |
6 | #include "string.h" |
6 | #include <string.h> |
7 | #include "stdlib.h" |
7 | #include <stdlib.h> |
8 | #include "stdio.h" |
8 | #include <stdio.h> |
|
|
9 | #include <float.h> |
9 | |
10 | |
10 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
11 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
11 | # define snprintf _snprintf // C compilers have this in stdio.h |
12 | # define snprintf _snprintf // C compilers have this in stdio.h |
12 | #endif |
13 | #endif |
13 | |
14 | |
… | |
… | |
25 | #define F_SPACE_BEFORE 0x00000020UL |
26 | #define F_SPACE_BEFORE 0x00000020UL |
26 | #define F_SPACE_AFTER 0x00000040UL |
27 | #define F_SPACE_AFTER 0x00000040UL |
27 | #define F_ALLOW_NONREF 0x00000100UL |
28 | #define F_ALLOW_NONREF 0x00000100UL |
28 | #define F_SHRINK 0x00000200UL |
29 | #define F_SHRINK 0x00000200UL |
29 | #define F_ALLOW_BLESSED 0x00000400UL |
30 | #define F_ALLOW_BLESSED 0x00000400UL |
30 | #define F_CONV_BLESSED 0x00000800UL // NYI |
31 | #define F_CONV_BLESSED 0x00000800UL |
31 | #define F_MAXDEPTH 0xf8000000UL |
32 | #define F_MAXDEPTH 0xf8000000UL |
32 | #define S_MAXDEPTH 27 |
33 | #define S_MAXDEPTH 27 |
33 | #define F_MAXSIZE 0x01f00000UL |
34 | #define F_MAXSIZE 0x01f00000UL |
34 | #define S_MAXSIZE 20 |
35 | #define S_MAXSIZE 20 |
|
|
36 | #define F_HOOK 0x00080000UL // some hooks exist, so slow-path processing |
35 | |
37 | |
36 | #define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) |
38 | #define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) |
37 | #define DEC_SIZE(flags) (1UL << ((flags & F_MAXSIZE ) >> S_MAXSIZE )) |
39 | #define DEC_SIZE(flags) (1UL << ((flags & F_MAXSIZE ) >> S_MAXSIZE )) |
38 | |
40 | |
39 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
41 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
… | |
… | |
56 | #endif |
58 | #endif |
57 | |
59 | |
58 | #define expect_false(expr) expect ((expr) != 0, 0) |
60 | #define expect_false(expr) expect ((expr) != 0, 0) |
59 | #define expect_true(expr) expect ((expr) != 0, 1) |
61 | #define expect_true(expr) expect ((expr) != 0, 1) |
60 | |
62 | |
|
|
63 | #ifdef USE_ITHREADS |
|
|
64 | # define JSON_SLOW 1 |
|
|
65 | # define JSON_STASH (json_stash ? json_stash : gv_stashpv ("JSON::XS", 1)) |
|
|
66 | #else |
|
|
67 | # define JSON_SLOW 0 |
|
|
68 | # define JSON_STASH json_stash |
|
|
69 | #endif |
|
|
70 | |
61 | static HV *json_stash, *json_boolean_stash; // JSON::XS:: |
71 | static HV *json_stash, *json_boolean_stash; // JSON::XS:: |
62 | static SV *json_true, *json_false; |
72 | static SV *json_true, *json_false; |
63 | |
73 | |
|
|
74 | typedef struct { |
|
|
75 | U32 flags; |
|
|
76 | SV *cb_object; |
|
|
77 | HV *cb_sk_object; |
|
|
78 | } JSON; |
|
|
79 | |
64 | ///////////////////////////////////////////////////////////////////////////// |
80 | ///////////////////////////////////////////////////////////////////////////// |
65 | // utility functions |
81 | // utility functions |
66 | |
82 | |
67 | static UV * |
83 | inline void |
68 | SvJSON (SV *sv) |
|
|
69 | { |
|
|
70 | if (!(SvROK (sv) && SvOBJECT (SvRV (sv)) && SvSTASH (SvRV (sv)) == json_stash)) |
|
|
71 | croak ("object is not of type JSON::XS"); |
|
|
72 | |
|
|
73 | return &SvUVX (SvRV (sv)); |
|
|
74 | } |
|
|
75 | |
|
|
76 | static void |
|
|
77 | shrink (SV *sv) |
84 | shrink (SV *sv) |
78 | { |
85 | { |
79 | sv_utf8_downgrade (sv, 1); |
86 | sv_utf8_downgrade (sv, 1); |
80 | if (SvLEN (sv) > SvCUR (sv) + 1) |
87 | if (SvLEN (sv) > SvCUR (sv) + 1) |
81 | { |
88 | { |
… | |
… | |
116 | typedef struct |
123 | typedef struct |
117 | { |
124 | { |
118 | char *cur; // SvPVX (sv) + current output position |
125 | char *cur; // SvPVX (sv) + current output position |
119 | char *end; // SvEND (sv) |
126 | char *end; // SvEND (sv) |
120 | SV *sv; // result scalar |
127 | SV *sv; // result scalar |
121 | U32 flags; // F_* |
128 | JSON json; |
122 | U32 indent; // indentation level |
129 | U32 indent; // indentation level |
123 | U32 maxdepth; // max. indentation/recursion level |
130 | U32 maxdepth; // max. indentation/recursion level |
124 | } enc_t; |
131 | } enc_t; |
125 | |
132 | |
126 | inline void |
133 | inline void |
… | |
… | |
200 | } |
207 | } |
201 | |
208 | |
202 | if (uch > 0x10FFFFUL) |
209 | if (uch > 0x10FFFFUL) |
203 | croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
210 | croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
204 | |
211 | |
205 | if (uch < 0x80 || enc->flags & F_ASCII || (enc->flags & F_LATIN1 && uch > 0xFF)) |
212 | if (uch < 0x80 || enc->json.flags & F_ASCII || (enc->json.flags & F_LATIN1 && uch > 0xFF)) |
206 | { |
213 | { |
207 | if (uch > 0xFFFFUL) |
214 | if (uch > 0xFFFFUL) |
208 | { |
215 | { |
209 | need (enc, len += 11); |
216 | need (enc, len += 11); |
210 | sprintf (enc->cur, "\\u%04x\\u%04x", |
217 | sprintf (enc->cur, "\\u%04x\\u%04x", |
… | |
… | |
224 | *enc->cur++ = hexdigit [(uch >> 0) & 15]; |
231 | *enc->cur++ = hexdigit [(uch >> 0) & 15]; |
225 | } |
232 | } |
226 | |
233 | |
227 | str += clen; |
234 | str += clen; |
228 | } |
235 | } |
229 | else if (enc->flags & F_LATIN1) |
236 | else if (enc->json.flags & F_LATIN1) |
230 | { |
237 | { |
231 | *enc->cur++ = uch; |
238 | *enc->cur++ = uch; |
232 | str += clen; |
239 | str += clen; |
233 | } |
240 | } |
234 | else if (is_utf8) |
241 | else if (is_utf8) |
… | |
… | |
255 | } |
262 | } |
256 | |
263 | |
257 | inline void |
264 | inline void |
258 | encode_indent (enc_t *enc) |
265 | encode_indent (enc_t *enc) |
259 | { |
266 | { |
260 | if (enc->flags & F_INDENT) |
267 | if (enc->json.flags & F_INDENT) |
261 | { |
268 | { |
262 | int spaces = enc->indent * INDENT_STEP; |
269 | int spaces = enc->indent * INDENT_STEP; |
263 | |
270 | |
264 | need (enc, spaces); |
271 | need (enc, spaces); |
265 | memset (enc->cur, ' ', spaces); |
272 | memset (enc->cur, ' ', spaces); |
… | |
… | |
275 | } |
282 | } |
276 | |
283 | |
277 | inline void |
284 | inline void |
278 | encode_nl (enc_t *enc) |
285 | encode_nl (enc_t *enc) |
279 | { |
286 | { |
280 | if (enc->flags & F_INDENT) |
287 | if (enc->json.flags & F_INDENT) |
281 | { |
288 | { |
282 | need (enc, 1); |
289 | need (enc, 1); |
283 | encode_ch (enc, '\n'); |
290 | encode_ch (enc, '\n'); |
284 | } |
291 | } |
285 | } |
292 | } |
… | |
… | |
287 | inline void |
294 | inline void |
288 | encode_comma (enc_t *enc) |
295 | encode_comma (enc_t *enc) |
289 | { |
296 | { |
290 | encode_ch (enc, ','); |
297 | encode_ch (enc, ','); |
291 | |
298 | |
292 | if (enc->flags & F_INDENT) |
299 | if (enc->json.flags & F_INDENT) |
293 | encode_nl (enc); |
300 | encode_nl (enc); |
294 | else if (enc->flags & F_SPACE_AFTER) |
301 | else if (enc->json.flags & F_SPACE_AFTER) |
295 | encode_space (enc); |
302 | encode_space (enc); |
296 | } |
303 | } |
297 | |
304 | |
298 | static void encode_sv (enc_t *enc, SV *sv); |
305 | static void encode_sv (enc_t *enc, SV *sv); |
299 | |
306 | |
… | |
… | |
308 | encode_ch (enc, '['); encode_nl (enc); |
315 | encode_ch (enc, '['); encode_nl (enc); |
309 | ++enc->indent; |
316 | ++enc->indent; |
310 | |
317 | |
311 | for (i = 0; i <= len; ++i) |
318 | for (i = 0; i <= len; ++i) |
312 | { |
319 | { |
|
|
320 | SV **svp = av_fetch (av, i, 0); |
|
|
321 | |
313 | encode_indent (enc); |
322 | encode_indent (enc); |
314 | encode_sv (enc, *av_fetch (av, i, 0)); |
323 | |
|
|
324 | if (svp) |
|
|
325 | encode_sv (enc, *svp); |
|
|
326 | else |
|
|
327 | encode_str (enc, "null", 4, 0); |
315 | |
328 | |
316 | if (i < len) |
329 | if (i < len) |
317 | encode_comma (enc); |
330 | encode_comma (enc); |
318 | } |
331 | } |
319 | |
332 | |
… | |
… | |
342 | else |
355 | else |
343 | encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); |
356 | encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); |
344 | |
357 | |
345 | encode_ch (enc, '"'); |
358 | encode_ch (enc, '"'); |
346 | |
359 | |
347 | if (enc->flags & F_SPACE_BEFORE) encode_space (enc); |
360 | if (enc->json.flags & F_SPACE_BEFORE) encode_space (enc); |
348 | encode_ch (enc, ':'); |
361 | encode_ch (enc, ':'); |
349 | if (enc->flags & F_SPACE_AFTER ) encode_space (enc); |
362 | if (enc->json.flags & F_SPACE_AFTER ) encode_space (enc); |
350 | encode_sv (enc, HeVAL (he)); |
363 | encode_sv (enc, HeVAL (he)); |
351 | } |
364 | } |
352 | |
365 | |
353 | // compare hash entries, used when all keys are bytestrings |
366 | // compare hash entries, used when all keys are bytestrings |
354 | static int |
367 | static int |
… | |
… | |
389 | { |
402 | { |
390 | // for canonical output we have to sort by keys first |
403 | // for canonical output we have to sort by keys first |
391 | // actually, this is mostly due to the stupid so-called |
404 | // actually, this is mostly due to the stupid so-called |
392 | // security workaround added somewhere in 5.8.x. |
405 | // security workaround added somewhere in 5.8.x. |
393 | // that randomises hash orderings |
406 | // that randomises hash orderings |
394 | if (enc->flags & F_CANONICAL) |
407 | if (enc->json.flags & F_CANONICAL) |
395 | { |
408 | { |
396 | int fast = 1; |
409 | int fast = 1; |
397 | HE *he; |
410 | HE *he; |
398 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
411 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
399 | HE **hes = _alloca (count * sizeof (HE)); |
412 | HE **hes = _alloca (count * sizeof (HE)); |
… | |
… | |
473 | SvGETMAGIC (sv); |
486 | SvGETMAGIC (sv); |
474 | svt = SvTYPE (sv); |
487 | svt = SvTYPE (sv); |
475 | |
488 | |
476 | if (expect_false (SvOBJECT (sv))) |
489 | if (expect_false (SvOBJECT (sv))) |
477 | { |
490 | { |
|
|
491 | HV *stash = !JSON_SLOW || json_boolean_stash |
|
|
492 | ? json_boolean_stash |
|
|
493 | : gv_stashpv ("JSON::XS::Boolean", 1); |
|
|
494 | |
478 | if (SvSTASH (sv) == json_boolean_stash) |
495 | if (SvSTASH (sv) == stash) |
479 | { |
496 | { |
480 | if (SvIV (sv) == 0) |
497 | if (SvIV (sv)) |
|
|
498 | encode_str (enc, "true", 4, 0); |
|
|
499 | else |
481 | encode_str (enc, "false", 5, 0); |
500 | encode_str (enc, "false", 5, 0); |
482 | else |
|
|
483 | encode_str (enc, "true", 4, 0); |
|
|
484 | } |
501 | } |
485 | else |
502 | else |
486 | { |
503 | { |
487 | #if 0 |
504 | #if 0 |
488 | if (0 && sv_derived_from (rv, "JSON::Literal")) |
505 | if (0 && sv_derived_from (rv, "JSON::Literal")) |
489 | { |
506 | { |
490 | // not yet |
507 | // not yet |
491 | } |
508 | } |
492 | #endif |
509 | #endif |
493 | if (enc->flags & F_CONV_BLESSED) |
510 | if (enc->json.flags & F_CONV_BLESSED) |
494 | { |
511 | { |
495 | // we re-bless the reference to get overload and other niceties right |
512 | // we re-bless the reference to get overload and other niceties right |
496 | GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 1); |
513 | GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 0); |
497 | |
514 | |
498 | if (to_json) |
515 | if (to_json) |
499 | { |
516 | { |
500 | dSP; |
517 | dSP; |
501 | ENTER; |
518 | |
502 | SAVETMPS; |
|
|
503 | PUSHMARK (SP); |
519 | ENTER; SAVETMPS; PUSHMARK (SP); |
504 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); |
520 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); |
505 | |
521 | |
506 | // calling with G_SCALAR ensures that we always get a 1 reutrn value |
522 | // calling with G_SCALAR ensures that we always get a 1 return value |
507 | // check anyways. |
|
|
508 | PUTBACK; |
523 | PUTBACK; |
509 | assert (1 == call_sv ((SV *)GvCV (to_json), G_SCALAR)); |
524 | call_sv ((SV *)GvCV (to_json), G_SCALAR); |
510 | SPAGAIN; |
525 | SPAGAIN; |
511 | |
526 | |
|
|
527 | // catch this surprisingly common error |
|
|
528 | if (SvROK (TOPs) && SvRV (TOPs) == sv) |
|
|
529 | croak ("%s::TO_JSON method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); |
|
|
530 | |
|
|
531 | sv = POPs; |
|
|
532 | PUTBACK; |
|
|
533 | |
512 | encode_sv (enc, POPs); |
534 | encode_sv (enc, sv); |
513 | |
535 | |
514 | FREETMPS; |
536 | FREETMPS; LEAVE; |
515 | LEAVE; |
|
|
516 | } |
537 | } |
517 | else if (enc->flags & F_ALLOW_BLESSED) |
538 | else if (enc->json.flags & F_ALLOW_BLESSED) |
518 | encode_str (enc, "null", 4, 0); |
539 | encode_str (enc, "null", 4, 0); |
519 | else |
540 | else |
520 | croak ("encountered object '%s', but neither allow_blessed enabled nor TO_JSON method available on it", |
541 | croak ("encountered object '%s', but neither allow_blessed enabled nor TO_JSON method available on it", |
521 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
542 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
522 | } |
543 | } |
523 | else if (enc->flags & F_ALLOW_BLESSED) |
544 | else if (enc->json.flags & F_ALLOW_BLESSED) |
524 | encode_str (enc, "null", 4, 0); |
545 | encode_str (enc, "null", 4, 0); |
525 | else |
546 | else |
526 | croak ("encountered object '%s', but neither allow_blessed nor convert_blessed settings are enabled", |
547 | croak ("encountered object '%s', but neither allow_blessed nor convert_blessed settings are enabled", |
527 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
548 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
528 | } |
549 | } |
… | |
… | |
531 | encode_hv (enc, (HV *)sv); |
552 | encode_hv (enc, (HV *)sv); |
532 | else if (svt == SVt_PVAV) |
553 | else if (svt == SVt_PVAV) |
533 | encode_av (enc, (AV *)sv); |
554 | encode_av (enc, (AV *)sv); |
534 | else if (svt < SVt_PVAV) |
555 | else if (svt < SVt_PVAV) |
535 | { |
556 | { |
536 | if (SvNIOK (sv) && SvIV (sv) == 0) |
557 | STRLEN len = 0; |
|
|
558 | char *pv = svt ? SvPV (sv, len) : 0; |
|
|
559 | |
|
|
560 | if (len == 1 && *pv == '1') |
|
|
561 | encode_str (enc, "true", 4, 0); |
|
|
562 | else if (len == 1 && *pv == '0') |
537 | encode_str (enc, "false", 5, 0); |
563 | encode_str (enc, "false", 5, 0); |
538 | else if (SvNIOK (sv) && SvIV (sv) == 1) |
|
|
539 | encode_str (enc, "true", 4, 0); |
|
|
540 | else |
564 | else |
541 | croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", |
565 | croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", |
542 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
566 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
543 | } |
567 | } |
544 | else |
568 | else |
… | |
… | |
614 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
638 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
615 | SvPV_nolen (sv), SvFLAGS (sv)); |
639 | SvPV_nolen (sv), SvFLAGS (sv)); |
616 | } |
640 | } |
617 | |
641 | |
618 | static SV * |
642 | static SV * |
619 | encode_json (SV *scalar, U32 flags) |
643 | encode_json (SV *scalar, JSON *json) |
620 | { |
644 | { |
621 | enc_t enc; |
645 | enc_t enc; |
622 | |
646 | |
623 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
647 | if (!(json->flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
624 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
648 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
625 | |
649 | |
626 | enc.flags = flags; |
650 | enc.json = *json; |
627 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
651 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
628 | enc.cur = SvPVX (enc.sv); |
652 | enc.cur = SvPVX (enc.sv); |
629 | enc.end = SvEND (enc.sv); |
653 | enc.end = SvEND (enc.sv); |
630 | enc.indent = 0; |
654 | enc.indent = 0; |
631 | enc.maxdepth = DEC_DEPTH (flags); |
655 | enc.maxdepth = DEC_DEPTH (enc.json.flags); |
632 | |
656 | |
633 | SvPOK_only (enc.sv); |
657 | SvPOK_only (enc.sv); |
634 | encode_sv (&enc, scalar); |
658 | encode_sv (&enc, scalar); |
635 | |
659 | |
636 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
660 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
637 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
661 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
638 | |
662 | |
639 | if (!(flags & (F_ASCII | F_LATIN1 | F_UTF8))) |
663 | if (!(enc.json.flags & (F_ASCII | F_LATIN1 | F_UTF8))) |
640 | SvUTF8_on (enc.sv); |
664 | SvUTF8_on (enc.sv); |
641 | |
665 | |
642 | if (enc.flags & F_SHRINK) |
666 | if (enc.json.flags & F_SHRINK) |
643 | shrink (enc.sv); |
667 | shrink (enc.sv); |
644 | |
668 | |
645 | return enc.sv; |
669 | return enc.sv; |
646 | } |
670 | } |
647 | |
671 | |
… | |
… | |
652 | typedef struct |
676 | typedef struct |
653 | { |
677 | { |
654 | char *cur; // current parser pointer |
678 | char *cur; // current parser pointer |
655 | char *end; // end of input string |
679 | char *end; // end of input string |
656 | const char *err; // parse error, if != 0 |
680 | const char *err; // parse error, if != 0 |
657 | U32 flags; // F_* |
681 | JSON json; |
658 | U32 depth; // recursion depth |
682 | U32 depth; // recursion depth |
659 | U32 maxdepth; // recursion depth limit |
683 | U32 maxdepth; // recursion depth limit |
660 | } dec_t; |
684 | } dec_t; |
661 | |
685 | |
662 | inline void |
686 | inline void |
… | |
… | |
925 | is_nv = 1; |
949 | is_nv = 1; |
926 | } |
950 | } |
927 | |
951 | |
928 | if (!is_nv) |
952 | if (!is_nv) |
929 | { |
953 | { |
|
|
954 | int len = dec->cur - start; |
|
|
955 | |
930 | // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so |
956 | // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so |
931 | if (*start == '-') |
957 | if (*start == '-') |
932 | switch (dec->cur - start) |
958 | switch (len) |
933 | { |
959 | { |
934 | case 2: return newSViv (-( start [1] - '0' * 1)); |
960 | case 2: return newSViv (-( start [1] - '0' * 1)); |
935 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
961 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
936 | case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); |
962 | case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); |
937 | case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
963 | case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
938 | } |
964 | } |
939 | else |
965 | else |
940 | switch (dec->cur - start) |
966 | switch (len) |
941 | { |
967 | { |
942 | case 1: return newSViv ( start [0] - '0' * 1); |
968 | case 1: return newSViv ( start [0] - '0' * 1); |
943 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
969 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
944 | case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); |
970 | case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); |
945 | case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
971 | case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
946 | } |
972 | } |
947 | |
973 | |
948 | { |
974 | { |
949 | UV uv; |
975 | UV uv; |
950 | int numtype = grok_number (start, dec->cur - start, &uv); |
976 | int numtype = grok_number (start, len, &uv); |
951 | if (numtype & IS_NUMBER_IN_UV) |
977 | if (numtype & IS_NUMBER_IN_UV) |
952 | if (numtype & IS_NUMBER_NEG) |
978 | if (numtype & IS_NUMBER_NEG) |
953 | { |
979 | { |
954 | if (uv < (UV)IV_MIN) |
980 | if (uv < (UV)IV_MIN) |
955 | return newSViv (-(IV)uv); |
981 | return newSViv (-(IV)uv); |
956 | } |
982 | } |
957 | else |
983 | else |
958 | return newSVuv (uv); |
984 | return newSVuv (uv); |
959 | |
|
|
960 | // here would likely be the place for bigint support |
|
|
961 | } |
985 | } |
962 | } |
|
|
963 | |
986 | |
964 | // if we ever support bigint or bigfloat, this is the place for bigfloat |
987 | len -= *start == '-' ? 1 : 0; |
|
|
988 | |
|
|
989 | // does not fit into IV or UV, try NV |
|
|
990 | if ((sizeof (NV) == sizeof (double) && DBL_DIG >= len) |
|
|
991 | #if defined (LDBL_DIG) |
|
|
992 | || (sizeof (NV) == sizeof (long double) && LDBL_DIG >= len) |
|
|
993 | #endif |
|
|
994 | ) |
|
|
995 | // fits into NV without loss of precision |
|
|
996 | return newSVnv (Atof (start)); |
|
|
997 | |
|
|
998 | // everything else fails, convert it to a string |
|
|
999 | return newSVpvn (start, dec->cur - start); |
|
|
1000 | } |
|
|
1001 | |
|
|
1002 | // loss of precision here |
965 | return newSVnv (Atof (start)); |
1003 | return newSVnv (Atof (start)); |
966 | |
1004 | |
967 | fail: |
1005 | fail: |
968 | return 0; |
1006 | return 0; |
969 | } |
1007 | } |
… | |
… | |
1013 | } |
1051 | } |
1014 | |
1052 | |
1015 | static SV * |
1053 | static SV * |
1016 | decode_hv (dec_t *dec) |
1054 | decode_hv (dec_t *dec) |
1017 | { |
1055 | { |
|
|
1056 | SV *sv; |
1018 | HV *hv = newHV (); |
1057 | HV *hv = newHV (); |
1019 | |
1058 | |
1020 | DEC_INC_DEPTH; |
1059 | DEC_INC_DEPTH; |
1021 | decode_ws (dec); |
1060 | decode_ws (dec); |
1022 | |
1061 | |
… | |
… | |
1026 | for (;;) |
1065 | for (;;) |
1027 | { |
1066 | { |
1028 | decode_ws (dec); EXPECT_CH ('"'); |
1067 | decode_ws (dec); EXPECT_CH ('"'); |
1029 | |
1068 | |
1030 | // heuristic: assume that |
1069 | // heuristic: assume that |
1031 | // a) decode_str + hv_store_ent are abysmally slow |
1070 | // a) decode_str + hv_store_ent are abysmally slow. |
1032 | // b) most hash keys are short, simple ascii text |
1071 | // b) most hash keys are short, simple ascii text. |
1033 | // so try to "fast-match" such strings to avoid |
1072 | // => try to "fast-match" such strings to avoid |
1034 | // the overhead of hv_store_ent. |
1073 | // the overhead of decode_str + hv_store_ent. |
1035 | { |
1074 | { |
1036 | SV *value; |
1075 | SV *value; |
1037 | char *p = dec->cur; |
1076 | char *p = dec->cur; |
1038 | char *e = p + 24; // only try up to 24 bytes |
1077 | char *e = p + 24; // only try up to 24 bytes |
1039 | |
1078 | |
1040 | for (;;) |
1079 | for (;;) |
1041 | { |
1080 | { |
|
|
1081 | // the >= 0x80 is true on most architectures |
1042 | if (p == e || *p < 0x20 || *p >= 0x80 || *p == '\\') |
1082 | if (p == e || *p < 0x20 || *p >= 0x80 || *p == '\\') |
1043 | { |
1083 | { |
1044 | // slow path, back up and use decode_str |
1084 | // slow path, back up and use decode_str |
1045 | SV *key = decode_str (dec); |
1085 | SV *key = decode_str (dec); |
1046 | if (!key) |
1086 | if (!key) |
… | |
… | |
1095 | |
1135 | |
1096 | ++dec->cur; |
1136 | ++dec->cur; |
1097 | } |
1137 | } |
1098 | |
1138 | |
1099 | DEC_DEC_DEPTH; |
1139 | DEC_DEC_DEPTH; |
1100 | return newRV_noinc ((SV *)hv); |
1140 | sv = newRV_noinc ((SV *)hv); |
|
|
1141 | |
|
|
1142 | // check filter callbacks |
|
|
1143 | if (dec->json.flags & F_HOOK) |
|
|
1144 | { |
|
|
1145 | if (dec->json.cb_sk_object && HvKEYS (hv) == 1) |
|
|
1146 | { |
|
|
1147 | HE *cb, *he; |
|
|
1148 | |
|
|
1149 | hv_iterinit (hv); |
|
|
1150 | he = hv_iternext (hv); |
|
|
1151 | hv_iterinit (hv); |
|
|
1152 | |
|
|
1153 | // the next line creates a mortal sv each time its called. |
|
|
1154 | // might want to optimise this for common cases. |
|
|
1155 | cb = hv_fetch_ent (dec->json.cb_sk_object, hv_iterkeysv (he), 0, 0); |
|
|
1156 | |
|
|
1157 | if (cb) |
|
|
1158 | { |
|
|
1159 | dSP; |
|
|
1160 | int count; |
|
|
1161 | |
|
|
1162 | ENTER; SAVETMPS; PUSHMARK (SP); |
|
|
1163 | XPUSHs (HeVAL (he)); |
|
|
1164 | |
|
|
1165 | PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; |
|
|
1166 | |
|
|
1167 | if (count == 1) |
|
|
1168 | { |
|
|
1169 | sv = newSVsv (POPs); |
|
|
1170 | FREETMPS; LEAVE; |
|
|
1171 | return sv; |
|
|
1172 | } |
|
|
1173 | |
|
|
1174 | FREETMPS; LEAVE; |
|
|
1175 | } |
|
|
1176 | } |
|
|
1177 | |
|
|
1178 | if (dec->json.cb_object) |
|
|
1179 | { |
|
|
1180 | dSP; |
|
|
1181 | int count; |
|
|
1182 | |
|
|
1183 | ENTER; SAVETMPS; PUSHMARK (SP); |
|
|
1184 | XPUSHs (sv_2mortal (sv)); |
|
|
1185 | |
|
|
1186 | PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN; |
|
|
1187 | |
|
|
1188 | if (count == 1) |
|
|
1189 | { |
|
|
1190 | sv = newSVsv (POPs); |
|
|
1191 | FREETMPS; LEAVE; |
|
|
1192 | return sv; |
|
|
1193 | } |
|
|
1194 | |
|
|
1195 | SvREFCNT_inc (sv); |
|
|
1196 | FREETMPS; LEAVE; |
|
|
1197 | } |
|
|
1198 | } |
|
|
1199 | |
|
|
1200 | return sv; |
1101 | |
1201 | |
1102 | fail: |
1202 | fail: |
1103 | SvREFCNT_dec (hv); |
1203 | SvREFCNT_dec (hv); |
1104 | DEC_DEC_DEPTH; |
1204 | DEC_DEC_DEPTH; |
1105 | return 0; |
1205 | return 0; |
… | |
… | |
1164 | fail: |
1264 | fail: |
1165 | return 0; |
1265 | return 0; |
1166 | } |
1266 | } |
1167 | |
1267 | |
1168 | static SV * |
1268 | static SV * |
1169 | decode_json (SV *string, U32 flags, UV *offset_return) |
1269 | decode_json (SV *string, JSON *json, UV *offset_return) |
1170 | { |
1270 | { |
1171 | dec_t dec; |
1271 | dec_t dec; |
1172 | UV offset; |
1272 | UV offset; |
1173 | SV *sv; |
1273 | SV *sv; |
1174 | |
1274 | |
1175 | SvGETMAGIC (string); |
1275 | SvGETMAGIC (string); |
1176 | SvUPGRADE (string, SVt_PV); |
1276 | SvUPGRADE (string, SVt_PV); |
1177 | |
1277 | |
1178 | if (flags & F_MAXSIZE && SvCUR (string) > DEC_SIZE (flags)) |
1278 | if (json->flags & F_MAXSIZE && SvCUR (string) > DEC_SIZE (json->flags)) |
1179 | croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", |
1279 | croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", |
1180 | (unsigned long)SvCUR (string), (unsigned long)DEC_SIZE (flags)); |
1280 | (unsigned long)SvCUR (string), (unsigned long)DEC_SIZE (json->flags)); |
1181 | |
1281 | |
1182 | if (flags & F_UTF8) |
1282 | if (json->flags & F_UTF8) |
1183 | sv_utf8_downgrade (string, 0); |
1283 | sv_utf8_downgrade (string, 0); |
1184 | else |
1284 | else |
1185 | sv_utf8_upgrade (string); |
1285 | sv_utf8_upgrade (string); |
1186 | |
1286 | |
1187 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
1287 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
1188 | |
1288 | |
1189 | dec.flags = flags; |
1289 | dec.json = *json; |
1190 | dec.cur = SvPVX (string); |
1290 | dec.cur = SvPVX (string); |
1191 | dec.end = SvEND (string); |
1291 | dec.end = SvEND (string); |
1192 | dec.err = 0; |
1292 | dec.err = 0; |
1193 | dec.depth = 0; |
1293 | dec.depth = 0; |
1194 | dec.maxdepth = DEC_DEPTH (dec.flags); |
1294 | dec.maxdepth = DEC_DEPTH (dec.json.flags); |
|
|
1295 | |
|
|
1296 | if (dec.json.cb_object || dec.json.cb_sk_object) |
|
|
1297 | dec.json.flags |= F_HOOK; |
1195 | |
1298 | |
1196 | *dec.end = 0; // this should basically be a nop, too, but make sure it's there |
1299 | *dec.end = 0; // this should basically be a nop, too, but make sure it's there |
1197 | sv = decode_sv (&dec); |
1300 | sv = decode_sv (&dec); |
1198 | |
1301 | |
1199 | if (!(offset_return || !sv)) |
1302 | if (!(offset_return || !sv)) |
… | |
… | |
1209 | } |
1312 | } |
1210 | } |
1313 | } |
1211 | |
1314 | |
1212 | if (offset_return || !sv) |
1315 | if (offset_return || !sv) |
1213 | { |
1316 | { |
1214 | offset = dec.flags & F_UTF8 |
1317 | offset = dec.json.flags & F_UTF8 |
1215 | ? dec.cur - SvPVX (string) |
1318 | ? dec.cur - SvPVX (string) |
1216 | : utf8_distance (dec.cur, SvPVX (string)); |
1319 | : utf8_distance (dec.cur, SvPVX (string)); |
1217 | |
1320 | |
1218 | if (offset_return) |
1321 | if (offset_return) |
1219 | *offset_return = offset; |
1322 | *offset_return = offset; |
… | |
… | |
1238 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1341 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1239 | } |
1342 | } |
1240 | |
1343 | |
1241 | sv = sv_2mortal (sv); |
1344 | sv = sv_2mortal (sv); |
1242 | |
1345 | |
1243 | if (!(dec.flags & F_ALLOW_NONREF) && !SvROK (sv)) |
1346 | if (!(dec.json.flags & F_ALLOW_NONREF) && !SvROK (sv)) |
1244 | croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)"); |
1347 | croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)"); |
1245 | |
1348 | |
1246 | return sv; |
1349 | return sv; |
1247 | } |
1350 | } |
1248 | |
1351 | |
… | |
… | |
1269 | json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false); |
1372 | json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false); |
1270 | } |
1373 | } |
1271 | |
1374 | |
1272 | PROTOTYPES: DISABLE |
1375 | PROTOTYPES: DISABLE |
1273 | |
1376 | |
1274 | SV *new (char *dummy) |
1377 | void CLONE (...) |
1275 | CODE: |
1378 | CODE: |
1276 | RETVAL = sv_bless (newRV_noinc (newSVuv (F_DEFAULT)), json_stash); |
1379 | json_stash = 0; |
1277 | OUTPUT: |
1380 | json_boolean_stash = 0; |
1278 | RETVAL |
|
|
1279 | |
1381 | |
|
|
1382 | void new (char *klass) |
|
|
1383 | PPCODE: |
|
|
1384 | { |
|
|
1385 | SV *pv = NEWSV (0, sizeof (JSON)); |
|
|
1386 | SvPOK_only (pv); |
|
|
1387 | Zero (SvPVX (pv), 1, JSON); |
|
|
1388 | ((JSON *)SvPVX (pv))->flags = F_DEFAULT; |
|
|
1389 | XPUSHs (sv_2mortal (sv_bless (newRV_noinc (pv), JSON_STASH))); |
|
|
1390 | } |
|
|
1391 | |
1280 | SV *ascii (SV *self, int enable = 1) |
1392 | void ascii (JSON *self, int enable = 1) |
1281 | ALIAS: |
1393 | ALIAS: |
1282 | ascii = F_ASCII |
1394 | ascii = F_ASCII |
1283 | latin1 = F_LATIN1 |
1395 | latin1 = F_LATIN1 |
1284 | utf8 = F_UTF8 |
1396 | utf8 = F_UTF8 |
1285 | indent = F_INDENT |
1397 | indent = F_INDENT |
… | |
… | |
1289 | pretty = F_PRETTY |
1401 | pretty = F_PRETTY |
1290 | allow_nonref = F_ALLOW_NONREF |
1402 | allow_nonref = F_ALLOW_NONREF |
1291 | shrink = F_SHRINK |
1403 | shrink = F_SHRINK |
1292 | allow_blessed = F_ALLOW_BLESSED |
1404 | allow_blessed = F_ALLOW_BLESSED |
1293 | convert_blessed = F_CONV_BLESSED |
1405 | convert_blessed = F_CONV_BLESSED |
1294 | CODE: |
1406 | PPCODE: |
1295 | { |
1407 | { |
1296 | UV *uv = SvJSON (self); |
|
|
1297 | if (enable) |
1408 | if (enable) |
1298 | *uv |= ix; |
1409 | self->flags |= ix; |
1299 | else |
1410 | else |
1300 | *uv &= ~ix; |
1411 | self->flags &= ~ix; |
1301 | |
1412 | |
1302 | RETVAL = newSVsv (self); |
1413 | XPUSHs (ST (0)); |
1303 | } |
1414 | } |
1304 | OUTPUT: |
|
|
1305 | RETVAL |
|
|
1306 | |
1415 | |
1307 | SV *max_depth (SV *self, UV max_depth = 0x80000000UL) |
1416 | void max_depth (JSON *self, UV max_depth = 0x80000000UL) |
1308 | CODE: |
1417 | PPCODE: |
1309 | { |
1418 | { |
1310 | UV *uv = SvJSON (self); |
|
|
1311 | UV log2 = 0; |
1419 | UV log2 = 0; |
1312 | |
1420 | |
1313 | if (max_depth > 0x80000000UL) max_depth = 0x80000000UL; |
1421 | if (max_depth > 0x80000000UL) max_depth = 0x80000000UL; |
1314 | |
1422 | |
1315 | while ((1UL << log2) < max_depth) |
1423 | while ((1UL << log2) < max_depth) |
1316 | ++log2; |
1424 | ++log2; |
1317 | |
1425 | |
1318 | *uv = *uv & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); |
1426 | self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); |
1319 | |
1427 | |
1320 | RETVAL = newSVsv (self); |
1428 | XPUSHs (ST (0)); |
1321 | } |
1429 | } |
1322 | OUTPUT: |
|
|
1323 | RETVAL |
|
|
1324 | |
1430 | |
1325 | SV *max_size (SV *self, UV max_size = 0) |
1431 | void max_size (JSON *self, UV max_size = 0) |
1326 | CODE: |
1432 | PPCODE: |
1327 | { |
1433 | { |
1328 | UV *uv = SvJSON (self); |
|
|
1329 | UV log2 = 0; |
1434 | UV log2 = 0; |
1330 | |
1435 | |
1331 | if (max_size > 0x80000000UL) max_size = 0x80000000UL; |
1436 | if (max_size > 0x80000000UL) max_size = 0x80000000UL; |
1332 | if (max_size == 1) max_size = 2; |
1437 | if (max_size == 1) max_size = 2; |
1333 | |
1438 | |
1334 | while ((1UL << log2) < max_size) |
1439 | while ((1UL << log2) < max_size) |
1335 | ++log2; |
1440 | ++log2; |
1336 | |
1441 | |
1337 | *uv = *uv & ~F_MAXSIZE | (log2 << S_MAXSIZE); |
1442 | self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE); |
1338 | |
1443 | |
1339 | RETVAL = newSVsv (self); |
1444 | XPUSHs (ST (0)); |
1340 | } |
1445 | } |
1341 | OUTPUT: |
|
|
1342 | RETVAL |
|
|
1343 | |
1446 | |
1344 | void encode (SV *self, SV *scalar) |
1447 | void filter_json_object (JSON *self, SV *cb = &PL_sv_undef) |
1345 | PPCODE: |
1448 | PPCODE: |
1346 | XPUSHs (encode_json (scalar, *SvJSON (self))); |
1449 | { |
|
|
1450 | SvREFCNT_dec (self->cb_object); |
|
|
1451 | self->cb_object = SvOK (cb) ? newSVsv (cb) : 0; |
1347 | |
1452 | |
1348 | void decode (SV *self, SV *jsonstr) |
1453 | XPUSHs (ST (0)); |
|
|
1454 | } |
|
|
1455 | |
|
|
1456 | void filter_json_single_key_object (JSON *self, SV *key, SV *cb = &PL_sv_undef) |
1349 | PPCODE: |
1457 | PPCODE: |
|
|
1458 | { |
|
|
1459 | if (!self->cb_sk_object) |
|
|
1460 | self->cb_sk_object = newHV (); |
|
|
1461 | |
|
|
1462 | if (SvOK (cb)) |
|
|
1463 | hv_store_ent (self->cb_sk_object, key, newSVsv (cb), 0); |
|
|
1464 | else |
|
|
1465 | { |
|
|
1466 | hv_delete_ent (self->cb_sk_object, key, G_DISCARD, 0); |
|
|
1467 | |
|
|
1468 | if (!HvKEYS (self->cb_sk_object)) |
|
|
1469 | { |
|
|
1470 | SvREFCNT_dec (self->cb_sk_object); |
|
|
1471 | self->cb_sk_object = 0; |
|
|
1472 | } |
|
|
1473 | } |
|
|
1474 | |
|
|
1475 | XPUSHs (ST (0)); |
|
|
1476 | } |
|
|
1477 | |
|
|
1478 | void encode (JSON *self, SV *scalar) |
|
|
1479 | PPCODE: |
|
|
1480 | XPUSHs (encode_json (scalar, self)); |
|
|
1481 | |
|
|
1482 | void decode (JSON *self, SV *jsonstr) |
|
|
1483 | PPCODE: |
1350 | XPUSHs (decode_json (jsonstr, *SvJSON (self), 0)); |
1484 | XPUSHs (decode_json (jsonstr, self, 0)); |
1351 | |
1485 | |
1352 | void decode_prefix (SV *self, SV *jsonstr) |
1486 | void decode_prefix (JSON *self, SV *jsonstr) |
1353 | PPCODE: |
1487 | PPCODE: |
1354 | { |
1488 | { |
1355 | UV offset; |
1489 | UV offset; |
1356 | EXTEND (SP, 2); |
1490 | EXTEND (SP, 2); |
1357 | PUSHs (decode_json (jsonstr, *SvJSON (self), &offset)); |
1491 | PUSHs (decode_json (jsonstr, self, &offset)); |
1358 | PUSHs (sv_2mortal (newSVuv (offset))); |
1492 | PUSHs (sv_2mortal (newSVuv (offset))); |
1359 | } |
1493 | } |
1360 | |
1494 | |
|
|
1495 | void DESTROY (JSON *self) |
|
|
1496 | CODE: |
|
|
1497 | SvREFCNT_dec (self->cb_sk_object); |
|
|
1498 | SvREFCNT_dec (self->cb_object); |
|
|
1499 | |
1361 | PROTOTYPES: ENABLE |
1500 | PROTOTYPES: ENABLE |
1362 | |
1501 | |
1363 | void to_json (SV *scalar) |
1502 | void to_json (SV *scalar) |
1364 | ALIAS: |
|
|
1365 | objToJson = 0 |
|
|
1366 | PPCODE: |
1503 | PPCODE: |
|
|
1504 | { |
|
|
1505 | JSON json = { F_DEFAULT | F_UTF8 }; |
1367 | XPUSHs (encode_json (scalar, F_DEFAULT | F_UTF8)); |
1506 | XPUSHs (encode_json (scalar, &json)); |
|
|
1507 | } |
1368 | |
1508 | |
1369 | void from_json (SV *jsonstr) |
1509 | void from_json (SV *jsonstr) |
1370 | ALIAS: |
|
|
1371 | jsonToObj = 0 |
|
|
1372 | PPCODE: |
1510 | PPCODE: |
|
|
1511 | { |
|
|
1512 | JSON json = { F_DEFAULT | F_UTF8 }; |
1373 | XPUSHs (decode_json (jsonstr, F_DEFAULT | F_UTF8, 0)); |
1513 | XPUSHs (decode_json (jsonstr, &json, 0)); |
|
|
1514 | } |
1374 | |
1515 | |