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