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 |
|
|
34 | #define F_MAXSIZE 0x01f00000UL |
|
|
35 | #define S_MAXSIZE 20 |
|
|
36 | #define F_HOOK 0x00080000UL // some hooks exist, so slow-path processing |
33 | |
37 | |
34 | #define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) |
38 | #define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) |
|
|
39 | #define DEC_SIZE(flags) (1UL << ((flags & F_MAXSIZE ) >> S_MAXSIZE )) |
35 | |
40 | |
36 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
41 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
37 | #define F_DEFAULT (9UL << S_MAXDEPTH) |
42 | #define F_DEFAULT (9UL << S_MAXDEPTH) |
38 | |
43 | |
39 | #define INIT_SIZE 32 // initial scalar size to be allocated |
44 | #define INIT_SIZE 32 // initial scalar size to be allocated |
… | |
… | |
56 | #define expect_true(expr) expect ((expr) != 0, 1) |
61 | #define expect_true(expr) expect ((expr) != 0, 1) |
57 | |
62 | |
58 | static HV *json_stash, *json_boolean_stash; // JSON::XS:: |
63 | static HV *json_stash, *json_boolean_stash; // JSON::XS:: |
59 | static SV *json_true, *json_false; |
64 | static SV *json_true, *json_false; |
60 | |
65 | |
|
|
66 | typedef struct { |
|
|
67 | U32 flags; |
|
|
68 | SV *cb_object; |
|
|
69 | HV *cb_sk_object; |
|
|
70 | } JSON; |
|
|
71 | |
61 | ///////////////////////////////////////////////////////////////////////////// |
72 | ///////////////////////////////////////////////////////////////////////////// |
62 | // utility functions |
73 | // utility functions |
63 | |
74 | |
64 | static UV * |
75 | inline void |
65 | SvJSON (SV *sv) |
|
|
66 | { |
|
|
67 | if (!(SvROK (sv) && SvOBJECT (SvRV (sv)) && SvSTASH (SvRV (sv)) == json_stash)) |
|
|
68 | croak ("object is not of type JSON::XS"); |
|
|
69 | |
|
|
70 | return &SvUVX (SvRV (sv)); |
|
|
71 | } |
|
|
72 | |
|
|
73 | static void |
|
|
74 | shrink (SV *sv) |
76 | shrink (SV *sv) |
75 | { |
77 | { |
76 | sv_utf8_downgrade (sv, 1); |
78 | sv_utf8_downgrade (sv, 1); |
77 | if (SvLEN (sv) > SvCUR (sv) + 1) |
79 | if (SvLEN (sv) > SvCUR (sv) + 1) |
78 | { |
80 | { |
… | |
… | |
113 | typedef struct |
115 | typedef struct |
114 | { |
116 | { |
115 | char *cur; // SvPVX (sv) + current output position |
117 | char *cur; // SvPVX (sv) + current output position |
116 | char *end; // SvEND (sv) |
118 | char *end; // SvEND (sv) |
117 | SV *sv; // result scalar |
119 | SV *sv; // result scalar |
118 | U32 flags; // F_* |
120 | JSON json; |
119 | U32 indent; // indentation level |
121 | U32 indent; // indentation level |
120 | U32 maxdepth; // max. indentation/recursion level |
122 | U32 maxdepth; // max. indentation/recursion level |
121 | } enc_t; |
123 | } enc_t; |
122 | |
124 | |
123 | inline void |
125 | inline void |
… | |
… | |
197 | } |
199 | } |
198 | |
200 | |
199 | if (uch > 0x10FFFFUL) |
201 | if (uch > 0x10FFFFUL) |
200 | croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
202 | croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
201 | |
203 | |
202 | if (uch < 0x80 || enc->flags & F_ASCII || (enc->flags & F_LATIN1 && uch > 0xFF)) |
204 | if (uch < 0x80 || enc->json.flags & F_ASCII || (enc->json.flags & F_LATIN1 && uch > 0xFF)) |
203 | { |
205 | { |
204 | if (uch > 0xFFFFUL) |
206 | if (uch > 0xFFFFUL) |
205 | { |
207 | { |
206 | need (enc, len += 11); |
208 | need (enc, len += 11); |
207 | sprintf (enc->cur, "\\u%04x\\u%04x", |
209 | sprintf (enc->cur, "\\u%04x\\u%04x", |
… | |
… | |
221 | *enc->cur++ = hexdigit [(uch >> 0) & 15]; |
223 | *enc->cur++ = hexdigit [(uch >> 0) & 15]; |
222 | } |
224 | } |
223 | |
225 | |
224 | str += clen; |
226 | str += clen; |
225 | } |
227 | } |
226 | else if (enc->flags & F_LATIN1) |
228 | else if (enc->json.flags & F_LATIN1) |
227 | { |
229 | { |
228 | *enc->cur++ = uch; |
230 | *enc->cur++ = uch; |
229 | str += clen; |
231 | str += clen; |
230 | } |
232 | } |
231 | else if (is_utf8) |
233 | else if (is_utf8) |
… | |
… | |
252 | } |
254 | } |
253 | |
255 | |
254 | inline void |
256 | inline void |
255 | encode_indent (enc_t *enc) |
257 | encode_indent (enc_t *enc) |
256 | { |
258 | { |
257 | if (enc->flags & F_INDENT) |
259 | if (enc->json.flags & F_INDENT) |
258 | { |
260 | { |
259 | int spaces = enc->indent * INDENT_STEP; |
261 | int spaces = enc->indent * INDENT_STEP; |
260 | |
262 | |
261 | need (enc, spaces); |
263 | need (enc, spaces); |
262 | memset (enc->cur, ' ', spaces); |
264 | memset (enc->cur, ' ', spaces); |
… | |
… | |
272 | } |
274 | } |
273 | |
275 | |
274 | inline void |
276 | inline void |
275 | encode_nl (enc_t *enc) |
277 | encode_nl (enc_t *enc) |
276 | { |
278 | { |
277 | if (enc->flags & F_INDENT) |
279 | if (enc->json.flags & F_INDENT) |
278 | { |
280 | { |
279 | need (enc, 1); |
281 | need (enc, 1); |
280 | encode_ch (enc, '\n'); |
282 | encode_ch (enc, '\n'); |
281 | } |
283 | } |
282 | } |
284 | } |
… | |
… | |
284 | inline void |
286 | inline void |
285 | encode_comma (enc_t *enc) |
287 | encode_comma (enc_t *enc) |
286 | { |
288 | { |
287 | encode_ch (enc, ','); |
289 | encode_ch (enc, ','); |
288 | |
290 | |
289 | if (enc->flags & F_INDENT) |
291 | if (enc->json.flags & F_INDENT) |
290 | encode_nl (enc); |
292 | encode_nl (enc); |
291 | else if (enc->flags & F_SPACE_AFTER) |
293 | else if (enc->json.flags & F_SPACE_AFTER) |
292 | encode_space (enc); |
294 | encode_space (enc); |
293 | } |
295 | } |
294 | |
296 | |
295 | static void encode_sv (enc_t *enc, SV *sv); |
297 | static void encode_sv (enc_t *enc, SV *sv); |
296 | |
298 | |
… | |
… | |
305 | encode_ch (enc, '['); encode_nl (enc); |
307 | encode_ch (enc, '['); encode_nl (enc); |
306 | ++enc->indent; |
308 | ++enc->indent; |
307 | |
309 | |
308 | for (i = 0; i <= len; ++i) |
310 | for (i = 0; i <= len; ++i) |
309 | { |
311 | { |
|
|
312 | SV **svp = av_fetch (av, i, 0); |
|
|
313 | |
310 | encode_indent (enc); |
314 | encode_indent (enc); |
311 | encode_sv (enc, *av_fetch (av, i, 0)); |
315 | |
|
|
316 | if (svp) |
|
|
317 | encode_sv (enc, *svp); |
|
|
318 | else |
|
|
319 | encode_str (enc, "null", 4, 0); |
312 | |
320 | |
313 | if (i < len) |
321 | if (i < len) |
314 | encode_comma (enc); |
322 | encode_comma (enc); |
315 | } |
323 | } |
316 | |
324 | |
… | |
… | |
339 | else |
347 | else |
340 | encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); |
348 | encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); |
341 | |
349 | |
342 | encode_ch (enc, '"'); |
350 | encode_ch (enc, '"'); |
343 | |
351 | |
344 | if (enc->flags & F_SPACE_BEFORE) encode_space (enc); |
352 | if (enc->json.flags & F_SPACE_BEFORE) encode_space (enc); |
345 | encode_ch (enc, ':'); |
353 | encode_ch (enc, ':'); |
346 | if (enc->flags & F_SPACE_AFTER ) encode_space (enc); |
354 | if (enc->json.flags & F_SPACE_AFTER ) encode_space (enc); |
347 | encode_sv (enc, HeVAL (he)); |
355 | encode_sv (enc, HeVAL (he)); |
348 | } |
356 | } |
349 | |
357 | |
350 | // compare hash entries, used when all keys are bytestrings |
358 | // compare hash entries, used when all keys are bytestrings |
351 | static int |
359 | static int |
… | |
… | |
386 | { |
394 | { |
387 | // for canonical output we have to sort by keys first |
395 | // for canonical output we have to sort by keys first |
388 | // actually, this is mostly due to the stupid so-called |
396 | // actually, this is mostly due to the stupid so-called |
389 | // security workaround added somewhere in 5.8.x. |
397 | // security workaround added somewhere in 5.8.x. |
390 | // that randomises hash orderings |
398 | // that randomises hash orderings |
391 | if (enc->flags & F_CANONICAL) |
399 | if (enc->json.flags & F_CANONICAL) |
392 | { |
400 | { |
393 | int fast = 1; |
401 | int fast = 1; |
394 | HE *he; |
402 | HE *he; |
395 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
403 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
396 | HE **hes = _alloca (count * sizeof (HE)); |
404 | HE **hes = _alloca (count * sizeof (HE)); |
… | |
… | |
472 | |
480 | |
473 | if (expect_false (SvOBJECT (sv))) |
481 | if (expect_false (SvOBJECT (sv))) |
474 | { |
482 | { |
475 | if (SvSTASH (sv) == json_boolean_stash) |
483 | if (SvSTASH (sv) == json_boolean_stash) |
476 | { |
484 | { |
477 | if (SvIV (sv) == 0) |
485 | if (SvIV (sv)) |
|
|
486 | encode_str (enc, "true", 4, 0); |
|
|
487 | else |
478 | encode_str (enc, "false", 5, 0); |
488 | encode_str (enc, "false", 5, 0); |
479 | else |
|
|
480 | encode_str (enc, "true", 4, 0); |
|
|
481 | } |
489 | } |
482 | else |
490 | else |
483 | { |
491 | { |
484 | #if 0 |
492 | #if 0 |
485 | if (0 && sv_derived_from (rv, "JSON::Literal")) |
493 | if (0 && sv_derived_from (rv, "JSON::Literal")) |
486 | { |
494 | { |
487 | // not yet |
495 | // not yet |
488 | } |
496 | } |
489 | #endif |
497 | #endif |
490 | if (enc->flags & F_CONV_BLESSED) |
498 | if (enc->json.flags & F_CONV_BLESSED) |
491 | { |
499 | { |
492 | // we re-bless the reference to get overload and other niceties right |
500 | // we re-bless the reference to get overload and other niceties right |
493 | GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 1); |
501 | GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 0); |
494 | |
502 | |
495 | if (to_json) |
503 | if (to_json) |
496 | { |
504 | { |
|
|
505 | int count; |
497 | dSP; |
506 | dSP; |
498 | ENTER; |
507 | |
499 | SAVETMPS; |
|
|
500 | PUSHMARK (SP); |
508 | ENTER; SAVETMPS; PUSHMARK (SP); |
501 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); |
509 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); |
502 | |
510 | |
503 | // calling with G_SCALAR ensures that we always get a 1 reutrn value |
511 | // calling with G_SCALAR ensures that we always get a 1 return value |
504 | // check anyways. |
|
|
505 | PUTBACK; |
512 | PUTBACK; |
506 | assert (1 == call_sv ((SV *)GvCV (to_json), G_SCALAR)); |
513 | call_sv ((SV *)GvCV (to_json), G_SCALAR); |
507 | SPAGAIN; |
514 | SPAGAIN; |
508 | |
515 | |
|
|
516 | // catch this surprisingly common error |
|
|
517 | if (SvROK (TOPs) && SvRV (TOPs) == sv) |
|
|
518 | croak ("%s::TO_JSON method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); |
|
|
519 | |
|
|
520 | sv = POPs; |
|
|
521 | PUTBACK; |
|
|
522 | |
509 | encode_sv (enc, POPs); |
523 | encode_sv (enc, sv); |
510 | |
524 | |
511 | FREETMPS; |
525 | FREETMPS; LEAVE; |
512 | LEAVE; |
|
|
513 | } |
526 | } |
514 | else if (enc->flags & F_ALLOW_BLESSED) |
527 | else if (enc->json.flags & F_ALLOW_BLESSED) |
515 | encode_str (enc, "null", 4, 0); |
528 | encode_str (enc, "null", 4, 0); |
516 | else |
529 | else |
517 | croak ("encountered object '%s', but neither allow_blessed enabled nor TO_JSON method available on it", |
530 | croak ("encountered object '%s', but neither allow_blessed enabled nor TO_JSON method available on it", |
518 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
531 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
519 | } |
532 | } |
520 | else if (enc->flags & F_ALLOW_BLESSED) |
533 | else if (enc->json.flags & F_ALLOW_BLESSED) |
521 | encode_str (enc, "null", 4, 0); |
534 | encode_str (enc, "null", 4, 0); |
522 | else |
535 | else |
523 | croak ("encountered object '%s', but neither allow_blessed nor convert_blessed settings are enabled", |
536 | croak ("encountered object '%s', but neither allow_blessed nor convert_blessed settings are enabled", |
524 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
537 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
525 | } |
538 | } |
… | |
… | |
528 | encode_hv (enc, (HV *)sv); |
541 | encode_hv (enc, (HV *)sv); |
529 | else if (svt == SVt_PVAV) |
542 | else if (svt == SVt_PVAV) |
530 | encode_av (enc, (AV *)sv); |
543 | encode_av (enc, (AV *)sv); |
531 | else if (svt < SVt_PVAV) |
544 | else if (svt < SVt_PVAV) |
532 | { |
545 | { |
533 | if (SvNIOK (sv) && SvIV (sv) == 0) |
546 | STRLEN len = 0; |
|
|
547 | char *pv = svt ? SvPV (sv, len) : 0; |
|
|
548 | |
|
|
549 | if (len == 1 && *pv == '1') |
|
|
550 | encode_str (enc, "true", 4, 0); |
|
|
551 | else if (len == 1 && *pv == '0') |
534 | encode_str (enc, "false", 5, 0); |
552 | encode_str (enc, "false", 5, 0); |
535 | else if (SvNIOK (sv) && SvIV (sv) == 1) |
|
|
536 | encode_str (enc, "true", 4, 0); |
|
|
537 | else |
553 | else |
538 | croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", |
554 | croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", |
539 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
555 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
540 | } |
556 | } |
541 | else |
557 | else |
… | |
… | |
611 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
627 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
612 | SvPV_nolen (sv), SvFLAGS (sv)); |
628 | SvPV_nolen (sv), SvFLAGS (sv)); |
613 | } |
629 | } |
614 | |
630 | |
615 | static SV * |
631 | static SV * |
616 | encode_json (SV *scalar, U32 flags) |
632 | encode_json (SV *scalar, JSON *json) |
617 | { |
633 | { |
618 | enc_t enc; |
634 | enc_t enc; |
619 | |
635 | |
620 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
636 | if (!(json->flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
621 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
637 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
622 | |
638 | |
623 | enc.flags = flags; |
639 | enc.json = *json; |
624 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
640 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
625 | enc.cur = SvPVX (enc.sv); |
641 | enc.cur = SvPVX (enc.sv); |
626 | enc.end = SvEND (enc.sv); |
642 | enc.end = SvEND (enc.sv); |
627 | enc.indent = 0; |
643 | enc.indent = 0; |
628 | enc.maxdepth = DEC_DEPTH (flags); |
644 | enc.maxdepth = DEC_DEPTH (enc.json.flags); |
629 | |
645 | |
630 | SvPOK_only (enc.sv); |
646 | SvPOK_only (enc.sv); |
631 | encode_sv (&enc, scalar); |
647 | encode_sv (&enc, scalar); |
632 | |
648 | |
633 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
649 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
634 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
650 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
635 | |
651 | |
636 | if (!(flags & (F_ASCII | F_LATIN1 | F_UTF8))) |
652 | if (!(enc.json.flags & (F_ASCII | F_LATIN1 | F_UTF8))) |
637 | SvUTF8_on (enc.sv); |
653 | SvUTF8_on (enc.sv); |
638 | |
654 | |
639 | if (enc.flags & F_SHRINK) |
655 | if (enc.json.flags & F_SHRINK) |
640 | shrink (enc.sv); |
656 | shrink (enc.sv); |
641 | |
657 | |
642 | return enc.sv; |
658 | return enc.sv; |
643 | } |
659 | } |
644 | |
660 | |
… | |
… | |
649 | typedef struct |
665 | typedef struct |
650 | { |
666 | { |
651 | char *cur; // current parser pointer |
667 | char *cur; // current parser pointer |
652 | char *end; // end of input string |
668 | char *end; // end of input string |
653 | const char *err; // parse error, if != 0 |
669 | const char *err; // parse error, if != 0 |
654 | U32 flags; // F_* |
670 | JSON json; |
655 | U32 depth; // recursion depth |
671 | U32 depth; // recursion depth |
656 | U32 maxdepth; // recursion depth limit |
672 | U32 maxdepth; // recursion depth limit |
657 | } dec_t; |
673 | } dec_t; |
658 | |
674 | |
659 | inline void |
675 | inline void |
… | |
… | |
922 | is_nv = 1; |
938 | is_nv = 1; |
923 | } |
939 | } |
924 | |
940 | |
925 | if (!is_nv) |
941 | if (!is_nv) |
926 | { |
942 | { |
|
|
943 | int len = dec->cur - start; |
|
|
944 | |
927 | // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so |
945 | // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so |
928 | if (*start == '-') |
946 | if (*start == '-') |
929 | switch (dec->cur - start) |
947 | switch (len) |
930 | { |
948 | { |
931 | case 2: return newSViv (-( start [1] - '0' * 1)); |
949 | case 2: return newSViv (-( start [1] - '0' * 1)); |
932 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
950 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
933 | case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); |
951 | case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); |
934 | case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
952 | case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
935 | } |
953 | } |
936 | else |
954 | else |
937 | switch (dec->cur - start) |
955 | switch (len) |
938 | { |
956 | { |
939 | case 1: return newSViv ( start [0] - '0' * 1); |
957 | case 1: return newSViv ( start [0] - '0' * 1); |
940 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
958 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
941 | case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); |
959 | case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); |
942 | case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
960 | case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
943 | } |
961 | } |
944 | |
962 | |
945 | { |
963 | { |
946 | UV uv; |
964 | UV uv; |
947 | int numtype = grok_number (start, dec->cur - start, &uv); |
965 | int numtype = grok_number (start, len, &uv); |
948 | if (numtype & IS_NUMBER_IN_UV) |
966 | if (numtype & IS_NUMBER_IN_UV) |
949 | if (numtype & IS_NUMBER_NEG) |
967 | if (numtype & IS_NUMBER_NEG) |
950 | { |
968 | { |
951 | if (uv < (UV)IV_MIN) |
969 | if (uv < (UV)IV_MIN) |
952 | return newSViv (-(IV)uv); |
970 | return newSViv (-(IV)uv); |
953 | } |
971 | } |
954 | else |
972 | else |
955 | return newSVuv (uv); |
973 | return newSVuv (uv); |
956 | |
|
|
957 | // here would likely be the place for bigint support |
|
|
958 | } |
974 | } |
959 | } |
|
|
960 | |
975 | |
961 | // if we ever support bigint or bigfloat, this is the place for bigfloat |
976 | len -= *start == '-' ? 1 : 0; |
|
|
977 | |
|
|
978 | // does not fit into IV or UV, try NV |
|
|
979 | if ((sizeof (NV) == sizeof (double) && DBL_DIG >= len) |
|
|
980 | #if defined (LDBL_DIG) |
|
|
981 | || (sizeof (NV) == sizeof (long double) && LDBL_DIG >= len) |
|
|
982 | #endif |
|
|
983 | ) |
|
|
984 | // fits into NV without loss of precision |
|
|
985 | return newSVnv (Atof (start)); |
|
|
986 | |
|
|
987 | // everything else fails, convert it to a string |
|
|
988 | return newSVpvn (start, dec->cur - start); |
|
|
989 | } |
|
|
990 | |
|
|
991 | // loss of precision here |
962 | return newSVnv (Atof (start)); |
992 | return newSVnv (Atof (start)); |
963 | |
993 | |
964 | fail: |
994 | fail: |
965 | return 0; |
995 | return 0; |
966 | } |
996 | } |
… | |
… | |
1010 | } |
1040 | } |
1011 | |
1041 | |
1012 | static SV * |
1042 | static SV * |
1013 | decode_hv (dec_t *dec) |
1043 | decode_hv (dec_t *dec) |
1014 | { |
1044 | { |
|
|
1045 | SV *sv; |
1015 | HV *hv = newHV (); |
1046 | HV *hv = newHV (); |
1016 | |
1047 | |
1017 | DEC_INC_DEPTH; |
1048 | DEC_INC_DEPTH; |
1018 | decode_ws (dec); |
1049 | decode_ws (dec); |
1019 | |
1050 | |
1020 | if (*dec->cur == '}') |
1051 | if (*dec->cur == '}') |
1021 | ++dec->cur; |
1052 | ++dec->cur; |
1022 | else |
1053 | else |
1023 | for (;;) |
1054 | for (;;) |
1024 | { |
1055 | { |
1025 | SV *key, *value; |
|
|
1026 | |
|
|
1027 | decode_ws (dec); EXPECT_CH ('"'); |
1056 | decode_ws (dec); EXPECT_CH ('"'); |
1028 | |
1057 | |
1029 | key = decode_str (dec); |
1058 | // heuristic: assume that |
1030 | if (!key) |
1059 | // a) decode_str + hv_store_ent are abysmally slow. |
1031 | goto fail; |
1060 | // b) most hash keys are short, simple ascii text. |
|
|
1061 | // => try to "fast-match" such strings to avoid |
|
|
1062 | // the overhead of decode_str + hv_store_ent. |
|
|
1063 | { |
|
|
1064 | SV *value; |
|
|
1065 | char *p = dec->cur; |
|
|
1066 | char *e = p + 24; // only try up to 24 bytes |
1032 | |
1067 | |
1033 | decode_ws (dec); EXPECT_CH (':'); |
1068 | for (;;) |
1034 | |
|
|
1035 | value = decode_sv (dec); |
|
|
1036 | if (!value) |
|
|
1037 | { |
1069 | { |
|
|
1070 | // the >= 0x80 is true on most architectures |
|
|
1071 | if (p == e || *p < 0x20 || *p >= 0x80 || *p == '\\') |
|
|
1072 | { |
|
|
1073 | // slow path, back up and use decode_str |
|
|
1074 | SV *key = decode_str (dec); |
|
|
1075 | if (!key) |
|
|
1076 | goto fail; |
|
|
1077 | |
|
|
1078 | decode_ws (dec); EXPECT_CH (':'); |
|
|
1079 | |
|
|
1080 | value = decode_sv (dec); |
|
|
1081 | if (!value) |
|
|
1082 | { |
|
|
1083 | SvREFCNT_dec (key); |
|
|
1084 | goto fail; |
|
|
1085 | } |
|
|
1086 | |
|
|
1087 | hv_store_ent (hv, key, value, 0); |
1038 | SvREFCNT_dec (key); |
1088 | SvREFCNT_dec (key); |
|
|
1089 | |
|
|
1090 | break; |
|
|
1091 | } |
|
|
1092 | else if (*p == '"') |
|
|
1093 | { |
|
|
1094 | // fast path, got a simple key |
|
|
1095 | char *key = dec->cur; |
|
|
1096 | int len = p - key; |
|
|
1097 | dec->cur = p + 1; |
|
|
1098 | |
|
|
1099 | decode_ws (dec); EXPECT_CH (':'); |
|
|
1100 | |
|
|
1101 | value = decode_sv (dec); |
|
|
1102 | if (!value) |
1039 | goto fail; |
1103 | goto fail; |
|
|
1104 | |
|
|
1105 | hv_store (hv, key, len, value, 0); |
|
|
1106 | |
|
|
1107 | break; |
|
|
1108 | } |
|
|
1109 | |
|
|
1110 | ++p; |
1040 | } |
1111 | } |
1041 | |
1112 | } |
1042 | hv_store_ent (hv, key, value, 0); |
|
|
1043 | SvREFCNT_dec (key); |
|
|
1044 | |
1113 | |
1045 | decode_ws (dec); |
1114 | decode_ws (dec); |
1046 | |
1115 | |
1047 | if (*dec->cur == '}') |
1116 | if (*dec->cur == '}') |
1048 | { |
1117 | { |
… | |
… | |
1055 | |
1124 | |
1056 | ++dec->cur; |
1125 | ++dec->cur; |
1057 | } |
1126 | } |
1058 | |
1127 | |
1059 | DEC_DEC_DEPTH; |
1128 | DEC_DEC_DEPTH; |
1060 | return newRV_noinc ((SV *)hv); |
1129 | sv = newRV_noinc ((SV *)hv); |
|
|
1130 | |
|
|
1131 | // check filter callbacks |
|
|
1132 | if (dec->json.flags & F_HOOK) |
|
|
1133 | { |
|
|
1134 | if (dec->json.cb_sk_object && HvKEYS (hv) == 1) |
|
|
1135 | { |
|
|
1136 | HE *cb, *he; |
|
|
1137 | |
|
|
1138 | hv_iterinit (hv); |
|
|
1139 | he = hv_iternext (hv); |
|
|
1140 | hv_iterinit (hv); |
|
|
1141 | |
|
|
1142 | // the next line creates a mortal sv each time its called. |
|
|
1143 | // might want to optimise this for common cases. |
|
|
1144 | cb = hv_fetch_ent (dec->json.cb_sk_object, hv_iterkeysv (he), 0, 0); |
|
|
1145 | |
|
|
1146 | if (cb) |
|
|
1147 | { |
|
|
1148 | dSP; |
|
|
1149 | int count; |
|
|
1150 | |
|
|
1151 | ENTER; SAVETMPS; PUSHMARK (SP); |
|
|
1152 | XPUSHs (HeVAL (he)); |
|
|
1153 | |
|
|
1154 | PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; |
|
|
1155 | |
|
|
1156 | if (count == 1) |
|
|
1157 | { |
|
|
1158 | sv = newSVsv (POPs); |
|
|
1159 | FREETMPS; LEAVE; |
|
|
1160 | return sv; |
|
|
1161 | } |
|
|
1162 | |
|
|
1163 | FREETMPS; LEAVE; |
|
|
1164 | } |
|
|
1165 | } |
|
|
1166 | |
|
|
1167 | if (dec->json.cb_object) |
|
|
1168 | { |
|
|
1169 | dSP; |
|
|
1170 | int count; |
|
|
1171 | |
|
|
1172 | ENTER; SAVETMPS; PUSHMARK (SP); |
|
|
1173 | XPUSHs (sv_2mortal (sv)); |
|
|
1174 | |
|
|
1175 | PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN; |
|
|
1176 | |
|
|
1177 | if (count == 1) |
|
|
1178 | { |
|
|
1179 | sv = newSVsv (POPs); |
|
|
1180 | FREETMPS; LEAVE; |
|
|
1181 | return sv; |
|
|
1182 | } |
|
|
1183 | |
|
|
1184 | SvREFCNT_inc (sv); |
|
|
1185 | FREETMPS; LEAVE; |
|
|
1186 | } |
|
|
1187 | } |
|
|
1188 | |
|
|
1189 | return sv; |
1061 | |
1190 | |
1062 | fail: |
1191 | fail: |
1063 | SvREFCNT_dec (hv); |
1192 | SvREFCNT_dec (hv); |
1064 | DEC_DEC_DEPTH; |
1193 | DEC_DEC_DEPTH; |
1065 | return 0; |
1194 | return 0; |
… | |
… | |
1124 | fail: |
1253 | fail: |
1125 | return 0; |
1254 | return 0; |
1126 | } |
1255 | } |
1127 | |
1256 | |
1128 | static SV * |
1257 | static SV * |
1129 | decode_json (SV *string, U32 flags, UV *offset_return) |
1258 | decode_json (SV *string, JSON *json, UV *offset_return) |
1130 | { |
1259 | { |
1131 | dec_t dec; |
1260 | dec_t dec; |
1132 | UV offset; |
1261 | UV offset; |
1133 | SV *sv; |
1262 | SV *sv; |
1134 | |
1263 | |
1135 | SvGETMAGIC (string); |
1264 | SvGETMAGIC (string); |
1136 | SvUPGRADE (string, SVt_PV); |
1265 | SvUPGRADE (string, SVt_PV); |
1137 | |
1266 | |
|
|
1267 | if (json->flags & F_MAXSIZE && SvCUR (string) > DEC_SIZE (json->flags)) |
|
|
1268 | croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", |
|
|
1269 | (unsigned long)SvCUR (string), (unsigned long)DEC_SIZE (json->flags)); |
|
|
1270 | |
1138 | if (flags & F_UTF8) |
1271 | if (json->flags & F_UTF8) |
1139 | sv_utf8_downgrade (string, 0); |
1272 | sv_utf8_downgrade (string, 0); |
1140 | else |
1273 | else |
1141 | sv_utf8_upgrade (string); |
1274 | sv_utf8_upgrade (string); |
1142 | |
1275 | |
1143 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
1276 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
1144 | |
1277 | |
1145 | dec.flags = flags; |
1278 | dec.json = *json; |
1146 | dec.cur = SvPVX (string); |
1279 | dec.cur = SvPVX (string); |
1147 | dec.end = SvEND (string); |
1280 | dec.end = SvEND (string); |
1148 | dec.err = 0; |
1281 | dec.err = 0; |
1149 | dec.depth = 0; |
1282 | dec.depth = 0; |
1150 | dec.maxdepth = DEC_DEPTH (dec.flags); |
1283 | dec.maxdepth = DEC_DEPTH (dec.json.flags); |
|
|
1284 | |
|
|
1285 | if (dec.json.cb_object || dec.json.cb_sk_object) |
|
|
1286 | dec.json.flags |= F_HOOK; |
1151 | |
1287 | |
1152 | *dec.end = 0; // this should basically be a nop, too, but make sure it's there |
1288 | *dec.end = 0; // this should basically be a nop, too, but make sure it's there |
1153 | sv = decode_sv (&dec); |
1289 | sv = decode_sv (&dec); |
1154 | |
1290 | |
1155 | if (!(offset_return || !sv)) |
1291 | if (!(offset_return || !sv)) |
… | |
… | |
1165 | } |
1301 | } |
1166 | } |
1302 | } |
1167 | |
1303 | |
1168 | if (offset_return || !sv) |
1304 | if (offset_return || !sv) |
1169 | { |
1305 | { |
1170 | offset = dec.flags & F_UTF8 |
1306 | offset = dec.json.flags & F_UTF8 |
1171 | ? dec.cur - SvPVX (string) |
1307 | ? dec.cur - SvPVX (string) |
1172 | : utf8_distance (dec.cur, SvPVX (string)); |
1308 | : utf8_distance (dec.cur, SvPVX (string)); |
1173 | |
1309 | |
1174 | if (offset_return) |
1310 | if (offset_return) |
1175 | *offset_return = offset; |
1311 | *offset_return = offset; |
… | |
… | |
1194 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1330 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1195 | } |
1331 | } |
1196 | |
1332 | |
1197 | sv = sv_2mortal (sv); |
1333 | sv = sv_2mortal (sv); |
1198 | |
1334 | |
1199 | if (!(dec.flags & F_ALLOW_NONREF) && !SvROK (sv)) |
1335 | if (!(dec.json.flags & F_ALLOW_NONREF) && !SvROK (sv)) |
1200 | croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)"); |
1336 | croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)"); |
1201 | |
1337 | |
1202 | return sv; |
1338 | return sv; |
1203 | } |
1339 | } |
1204 | |
1340 | |
… | |
… | |
1225 | json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false); |
1361 | json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false); |
1226 | } |
1362 | } |
1227 | |
1363 | |
1228 | PROTOTYPES: DISABLE |
1364 | PROTOTYPES: DISABLE |
1229 | |
1365 | |
1230 | SV *new (char *dummy) |
1366 | void new (char *klass) |
1231 | CODE: |
1367 | PPCODE: |
1232 | RETVAL = sv_bless (newRV_noinc (newSVuv (F_DEFAULT)), json_stash); |
1368 | { |
1233 | OUTPUT: |
1369 | SV *pv = NEWSV (0, sizeof (JSON)); |
1234 | RETVAL |
1370 | SvPOK_only (pv); |
|
|
1371 | Zero (SvPVX (pv), 1, JSON); |
|
|
1372 | ((JSON *)SvPVX (pv))->flags = F_DEFAULT; |
|
|
1373 | XPUSHs (sv_2mortal (sv_bless (newRV_noinc (pv), json_stash))); |
|
|
1374 | } |
1235 | |
1375 | |
1236 | SV *ascii (SV *self, int enable = 1) |
1376 | void ascii (JSON *self, int enable = 1) |
1237 | ALIAS: |
1377 | ALIAS: |
1238 | ascii = F_ASCII |
1378 | ascii = F_ASCII |
1239 | latin1 = F_LATIN1 |
1379 | latin1 = F_LATIN1 |
1240 | utf8 = F_UTF8 |
1380 | utf8 = F_UTF8 |
1241 | indent = F_INDENT |
1381 | indent = F_INDENT |
… | |
… | |
1245 | pretty = F_PRETTY |
1385 | pretty = F_PRETTY |
1246 | allow_nonref = F_ALLOW_NONREF |
1386 | allow_nonref = F_ALLOW_NONREF |
1247 | shrink = F_SHRINK |
1387 | shrink = F_SHRINK |
1248 | allow_blessed = F_ALLOW_BLESSED |
1388 | allow_blessed = F_ALLOW_BLESSED |
1249 | convert_blessed = F_CONV_BLESSED |
1389 | convert_blessed = F_CONV_BLESSED |
1250 | CODE: |
1390 | PPCODE: |
1251 | { |
1391 | { |
1252 | UV *uv = SvJSON (self); |
|
|
1253 | if (enable) |
1392 | if (enable) |
1254 | *uv |= ix; |
1393 | self->flags |= ix; |
1255 | else |
1394 | else |
1256 | *uv &= ~ix; |
1395 | self->flags &= ~ix; |
1257 | |
1396 | |
1258 | RETVAL = newSVsv (self); |
1397 | XPUSHs (ST (0)); |
1259 | } |
1398 | } |
1260 | OUTPUT: |
|
|
1261 | RETVAL |
|
|
1262 | |
1399 | |
1263 | SV *max_depth (SV *self, UV max_depth = 0x80000000UL) |
1400 | void max_depth (JSON *self, UV max_depth = 0x80000000UL) |
1264 | CODE: |
1401 | PPCODE: |
1265 | { |
1402 | { |
1266 | UV *uv = SvJSON (self); |
|
|
1267 | UV log2 = 0; |
1403 | UV log2 = 0; |
1268 | |
1404 | |
1269 | if (max_depth > 0x80000000UL) max_depth = 0x80000000UL; |
1405 | if (max_depth > 0x80000000UL) max_depth = 0x80000000UL; |
1270 | |
1406 | |
1271 | while ((1UL << log2) < max_depth) |
1407 | while ((1UL << log2) < max_depth) |
1272 | ++log2; |
1408 | ++log2; |
1273 | |
1409 | |
1274 | *uv = *uv & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); |
1410 | self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); |
1275 | |
1411 | |
1276 | RETVAL = newSVsv (self); |
1412 | XPUSHs (ST (0)); |
1277 | } |
1413 | } |
1278 | OUTPUT: |
|
|
1279 | RETVAL |
|
|
1280 | |
1414 | |
1281 | void encode (SV *self, SV *scalar) |
1415 | void max_size (JSON *self, UV max_size = 0) |
1282 | PPCODE: |
1416 | PPCODE: |
1283 | XPUSHs (encode_json (scalar, *SvJSON (self))); |
1417 | { |
|
|
1418 | UV log2 = 0; |
1284 | |
1419 | |
1285 | void decode (SV *self, SV *jsonstr) |
1420 | if (max_size > 0x80000000UL) max_size = 0x80000000UL; |
|
|
1421 | if (max_size == 1) max_size = 2; |
|
|
1422 | |
|
|
1423 | while ((1UL << log2) < max_size) |
|
|
1424 | ++log2; |
|
|
1425 | |
|
|
1426 | self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE); |
|
|
1427 | |
|
|
1428 | XPUSHs (ST (0)); |
|
|
1429 | } |
|
|
1430 | |
|
|
1431 | void filter_json_object (JSON *self, SV *cb = &PL_sv_undef) |
1286 | PPCODE: |
1432 | PPCODE: |
|
|
1433 | { |
|
|
1434 | SvREFCNT_dec (self->cb_object); |
|
|
1435 | self->cb_object = SvOK (cb) ? newSVsv (cb) : 0; |
|
|
1436 | |
|
|
1437 | XPUSHs (ST (0)); |
|
|
1438 | } |
|
|
1439 | |
|
|
1440 | void filter_json_single_key_object (JSON *self, SV *key, SV *cb = &PL_sv_undef) |
|
|
1441 | PPCODE: |
|
|
1442 | { |
|
|
1443 | if (!self->cb_sk_object) |
|
|
1444 | self->cb_sk_object = newHV (); |
|
|
1445 | |
|
|
1446 | if (SvOK (cb)) |
|
|
1447 | hv_store_ent (self->cb_sk_object, key, newSVsv (cb), 0); |
|
|
1448 | else |
|
|
1449 | { |
|
|
1450 | hv_delete_ent (self->cb_sk_object, key, G_DISCARD, 0); |
|
|
1451 | |
|
|
1452 | if (!HvKEYS (self->cb_sk_object)) |
|
|
1453 | { |
|
|
1454 | SvREFCNT_dec (self->cb_sk_object); |
|
|
1455 | self->cb_sk_object = 0; |
|
|
1456 | } |
|
|
1457 | } |
|
|
1458 | |
|
|
1459 | XPUSHs (ST (0)); |
|
|
1460 | } |
|
|
1461 | |
|
|
1462 | void encode (JSON *self, SV *scalar) |
|
|
1463 | PPCODE: |
|
|
1464 | XPUSHs (encode_json (scalar, self)); |
|
|
1465 | |
|
|
1466 | void decode (JSON *self, SV *jsonstr) |
|
|
1467 | PPCODE: |
1287 | XPUSHs (decode_json (jsonstr, *SvJSON (self), 0)); |
1468 | XPUSHs (decode_json (jsonstr, self, 0)); |
1288 | |
1469 | |
1289 | void decode_prefix (SV *self, SV *jsonstr) |
1470 | void decode_prefix (JSON *self, SV *jsonstr) |
1290 | PPCODE: |
1471 | PPCODE: |
1291 | { |
1472 | { |
1292 | UV offset; |
1473 | UV offset; |
1293 | EXTEND (SP, 2); |
1474 | EXTEND (SP, 2); |
1294 | PUSHs (decode_json (jsonstr, *SvJSON (self), &offset)); |
1475 | PUSHs (decode_json (jsonstr, self, &offset)); |
1295 | PUSHs (sv_2mortal (newSVuv (offset))); |
1476 | PUSHs (sv_2mortal (newSVuv (offset))); |
1296 | } |
1477 | } |
1297 | |
1478 | |
|
|
1479 | void DESTROY (JSON *self) |
|
|
1480 | CODE: |
|
|
1481 | SvREFCNT_dec (self->cb_sk_object); |
|
|
1482 | SvREFCNT_dec (self->cb_object); |
|
|
1483 | |
1298 | PROTOTYPES: ENABLE |
1484 | PROTOTYPES: ENABLE |
1299 | |
1485 | |
1300 | void to_json (SV *scalar) |
1486 | void to_json (SV *scalar) |
1301 | ALIAS: |
|
|
1302 | objToJson = 0 |
|
|
1303 | PPCODE: |
1487 | PPCODE: |
|
|
1488 | { |
|
|
1489 | JSON json = { F_DEFAULT | F_UTF8 }; |
1304 | XPUSHs (encode_json (scalar, F_DEFAULT | F_UTF8)); |
1490 | XPUSHs (encode_json (scalar, &json)); |
|
|
1491 | } |
1305 | |
1492 | |
1306 | void from_json (SV *jsonstr) |
1493 | void from_json (SV *jsonstr) |
1307 | ALIAS: |
|
|
1308 | jsonToObj = 0 |
|
|
1309 | PPCODE: |
1494 | PPCODE: |
|
|
1495 | { |
|
|
1496 | JSON json = { F_DEFAULT | F_UTF8 }; |
1310 | XPUSHs (decode_json (jsonstr, F_DEFAULT | F_UTF8, 0)); |
1497 | XPUSHs (decode_json (jsonstr, &json, 0)); |
|
|
1498 | } |
1311 | |
1499 | |