… | |
… | |
9 | |
9 | |
10 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
10 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
11 | # define snprintf _snprintf // C compilers have this in stdio.h |
11 | # define snprintf _snprintf // C compilers have this in stdio.h |
12 | #endif |
12 | #endif |
13 | |
13 | |
|
|
14 | // some old perls do not have this, try to make it work, no |
|
|
15 | // guarentees, though. if it breaks, you get to keep the pieces. |
|
|
16 | #ifndef UTF8_MAXBYTES |
|
|
17 | # define UTF8_MAXBYTES 13 |
|
|
18 | #endif |
|
|
19 | |
14 | #define F_ASCII 0x00000001UL |
20 | #define F_ASCII 0x00000001UL |
15 | #define F_LATIN1 0x00000002UL |
21 | #define F_LATIN1 0x00000002UL |
16 | #define F_UTF8 0x00000004UL |
22 | #define F_UTF8 0x00000004UL |
17 | #define F_INDENT 0x00000008UL |
23 | #define F_INDENT 0x00000008UL |
18 | #define F_CANONICAL 0x00000010UL |
24 | #define F_CANONICAL 0x00000010UL |
19 | #define F_SPACE_BEFORE 0x00000020UL |
25 | #define F_SPACE_BEFORE 0x00000020UL |
20 | #define F_SPACE_AFTER 0x00000040UL |
26 | #define F_SPACE_AFTER 0x00000040UL |
21 | #define F_ALLOW_NONREF 0x00000100UL |
27 | #define F_ALLOW_NONREF 0x00000100UL |
22 | #define F_SHRINK 0x00000200UL |
28 | #define F_SHRINK 0x00000200UL |
|
|
29 | #define F_ALLOW_BLESSED 0x00000400UL |
|
|
30 | #define F_CONV_BLESSED 0x00000800UL |
23 | #define F_MAXDEPTH 0xf8000000UL |
31 | #define F_MAXDEPTH 0xf8000000UL |
24 | #define S_MAXDEPTH 27 |
32 | #define S_MAXDEPTH 27 |
|
|
33 | #define F_MAXSIZE 0x01f00000UL |
|
|
34 | #define S_MAXSIZE 20 |
|
|
35 | #define F_HOOK 0x00080000UL // some hooks exist, so slow-path processing |
25 | |
36 | |
26 | #define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) |
37 | #define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) |
27 | |
38 | #define DEC_SIZE(flags) (1UL << ((flags & F_MAXSIZE ) >> S_MAXSIZE )) |
28 | // F_SELFCONVERT? <=> to_json/toJson |
|
|
29 | // F_BLESSED? <=> { $__class__$ => } |
|
|
30 | |
39 | |
31 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
40 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
32 | #define F_DEFAULT (9UL << S_MAXDEPTH) |
41 | #define F_DEFAULT (9UL << S_MAXDEPTH) |
33 | |
42 | |
34 | #define INIT_SIZE 32 // initial scalar size to be allocated |
43 | #define INIT_SIZE 32 // initial scalar size to be allocated |
… | |
… | |
48 | #endif |
57 | #endif |
49 | |
58 | |
50 | #define expect_false(expr) expect ((expr) != 0, 0) |
59 | #define expect_false(expr) expect ((expr) != 0, 0) |
51 | #define expect_true(expr) expect ((expr) != 0, 1) |
60 | #define expect_true(expr) expect ((expr) != 0, 1) |
52 | |
61 | |
53 | static HV *json_stash; // JSON::XS:: |
62 | static HV *json_stash, *json_boolean_stash; // JSON::XS:: |
|
|
63 | static SV *json_true, *json_false; |
|
|
64 | |
|
|
65 | typedef struct { |
|
|
66 | U32 flags; |
|
|
67 | SV *cb_object; |
|
|
68 | HV *cb_sk_object; |
|
|
69 | } JSON; |
54 | |
70 | |
55 | ///////////////////////////////////////////////////////////////////////////// |
71 | ///////////////////////////////////////////////////////////////////////////// |
56 | // utility functions |
72 | // utility functions |
57 | |
73 | |
58 | static UV * |
74 | inline void |
59 | SvJSON (SV *sv) |
|
|
60 | { |
|
|
61 | if (!(SvROK (sv) && SvOBJECT (SvRV (sv)) && SvSTASH (SvRV (sv)) == json_stash)) |
|
|
62 | croak ("object is not of type JSON::XS"); |
|
|
63 | |
|
|
64 | return &SvUVX (SvRV (sv)); |
|
|
65 | } |
|
|
66 | |
|
|
67 | static void |
|
|
68 | shrink (SV *sv) |
75 | shrink (SV *sv) |
69 | { |
76 | { |
70 | sv_utf8_downgrade (sv, 1); |
77 | sv_utf8_downgrade (sv, 1); |
71 | if (SvLEN (sv) > SvCUR (sv) + 1) |
78 | if (SvLEN (sv) > SvCUR (sv) + 1) |
72 | { |
79 | { |
… | |
… | |
107 | typedef struct |
114 | typedef struct |
108 | { |
115 | { |
109 | char *cur; // SvPVX (sv) + current output position |
116 | char *cur; // SvPVX (sv) + current output position |
110 | char *end; // SvEND (sv) |
117 | char *end; // SvEND (sv) |
111 | SV *sv; // result scalar |
118 | SV *sv; // result scalar |
112 | U32 flags; // F_* |
119 | JSON json; |
113 | U32 indent; // indentation level |
120 | U32 indent; // indentation level |
114 | U32 maxdepth; // max. indentation/recursion level |
121 | U32 maxdepth; // max. indentation/recursion level |
115 | } enc_t; |
122 | } enc_t; |
116 | |
123 | |
117 | inline void |
124 | inline void |
… | |
… | |
178 | STRLEN clen; |
185 | STRLEN clen; |
179 | UV uch; |
186 | UV uch; |
180 | |
187 | |
181 | if (is_utf8) |
188 | if (is_utf8) |
182 | { |
189 | { |
183 | //uch = utf8n_to_uvuni (str, end - str, &clen, UTF8_CHECK_ONLY); |
|
|
184 | uch = decode_utf8 (str, end - str, &clen); |
190 | uch = decode_utf8 (str, end - str, &clen); |
185 | if (clen == (STRLEN)-1) |
191 | if (clen == (STRLEN)-1) |
186 | croak ("malformed or illegal unicode character in string [%.11s], cannot convert to JSON", str); |
192 | croak ("malformed or illegal unicode character in string [%.11s], cannot convert to JSON", str); |
187 | } |
193 | } |
188 | else |
194 | else |
… | |
… | |
192 | } |
198 | } |
193 | |
199 | |
194 | if (uch > 0x10FFFFUL) |
200 | if (uch > 0x10FFFFUL) |
195 | croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
201 | croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
196 | |
202 | |
197 | if (uch < 0x80 || enc->flags & F_ASCII || (enc->flags & F_LATIN1 && uch > 0xFF)) |
203 | if (uch < 0x80 || enc->json.flags & F_ASCII || (enc->json.flags & F_LATIN1 && uch > 0xFF)) |
198 | { |
204 | { |
199 | if (uch > 0xFFFFUL) |
205 | if (uch > 0xFFFFUL) |
200 | { |
206 | { |
201 | need (enc, len += 11); |
207 | need (enc, len += 11); |
202 | sprintf (enc->cur, "\\u%04x\\u%04x", |
208 | sprintf (enc->cur, "\\u%04x\\u%04x", |
… | |
… | |
216 | *enc->cur++ = hexdigit [(uch >> 0) & 15]; |
222 | *enc->cur++ = hexdigit [(uch >> 0) & 15]; |
217 | } |
223 | } |
218 | |
224 | |
219 | str += clen; |
225 | str += clen; |
220 | } |
226 | } |
221 | else if (enc->flags & F_LATIN1) |
227 | else if (enc->json.flags & F_LATIN1) |
222 | { |
228 | { |
223 | *enc->cur++ = uch; |
229 | *enc->cur++ = uch; |
224 | str += clen; |
230 | str += clen; |
225 | } |
231 | } |
226 | else if (is_utf8) |
232 | else if (is_utf8) |
… | |
… | |
247 | } |
253 | } |
248 | |
254 | |
249 | inline void |
255 | inline void |
250 | encode_indent (enc_t *enc) |
256 | encode_indent (enc_t *enc) |
251 | { |
257 | { |
252 | if (enc->flags & F_INDENT) |
258 | if (enc->json.flags & F_INDENT) |
253 | { |
259 | { |
254 | int spaces = enc->indent * INDENT_STEP; |
260 | int spaces = enc->indent * INDENT_STEP; |
255 | |
261 | |
256 | need (enc, spaces); |
262 | need (enc, spaces); |
257 | memset (enc->cur, ' ', spaces); |
263 | memset (enc->cur, ' ', spaces); |
… | |
… | |
267 | } |
273 | } |
268 | |
274 | |
269 | inline void |
275 | inline void |
270 | encode_nl (enc_t *enc) |
276 | encode_nl (enc_t *enc) |
271 | { |
277 | { |
272 | if (enc->flags & F_INDENT) |
278 | if (enc->json.flags & F_INDENT) |
273 | { |
279 | { |
274 | need (enc, 1); |
280 | need (enc, 1); |
275 | encode_ch (enc, '\n'); |
281 | encode_ch (enc, '\n'); |
276 | } |
282 | } |
277 | } |
283 | } |
… | |
… | |
279 | inline void |
285 | inline void |
280 | encode_comma (enc_t *enc) |
286 | encode_comma (enc_t *enc) |
281 | { |
287 | { |
282 | encode_ch (enc, ','); |
288 | encode_ch (enc, ','); |
283 | |
289 | |
284 | if (enc->flags & F_INDENT) |
290 | if (enc->json.flags & F_INDENT) |
285 | encode_nl (enc); |
291 | encode_nl (enc); |
286 | else if (enc->flags & F_SPACE_AFTER) |
292 | else if (enc->json.flags & F_SPACE_AFTER) |
287 | encode_space (enc); |
293 | encode_space (enc); |
288 | } |
294 | } |
289 | |
295 | |
290 | static void encode_sv (enc_t *enc, SV *sv); |
296 | static void encode_sv (enc_t *enc, SV *sv); |
291 | |
297 | |
… | |
… | |
300 | encode_ch (enc, '['); encode_nl (enc); |
306 | encode_ch (enc, '['); encode_nl (enc); |
301 | ++enc->indent; |
307 | ++enc->indent; |
302 | |
308 | |
303 | for (i = 0; i <= len; ++i) |
309 | for (i = 0; i <= len; ++i) |
304 | { |
310 | { |
|
|
311 | SV **svp = av_fetch (av, i, 0); |
|
|
312 | |
305 | encode_indent (enc); |
313 | encode_indent (enc); |
306 | encode_sv (enc, *av_fetch (av, i, 0)); |
314 | |
|
|
315 | if (svp) |
|
|
316 | encode_sv (enc, *svp); |
|
|
317 | else |
|
|
318 | encode_str (enc, "null", 4, 0); |
307 | |
319 | |
308 | if (i < len) |
320 | if (i < len) |
309 | encode_comma (enc); |
321 | encode_comma (enc); |
310 | } |
322 | } |
311 | |
323 | |
… | |
… | |
334 | else |
346 | else |
335 | encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); |
347 | encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); |
336 | |
348 | |
337 | encode_ch (enc, '"'); |
349 | encode_ch (enc, '"'); |
338 | |
350 | |
339 | if (enc->flags & F_SPACE_BEFORE) encode_space (enc); |
351 | if (enc->json.flags & F_SPACE_BEFORE) encode_space (enc); |
340 | encode_ch (enc, ':'); |
352 | encode_ch (enc, ':'); |
341 | if (enc->flags & F_SPACE_AFTER ) encode_space (enc); |
353 | if (enc->json.flags & F_SPACE_AFTER ) encode_space (enc); |
342 | encode_sv (enc, HeVAL (he)); |
354 | encode_sv (enc, HeVAL (he)); |
343 | } |
355 | } |
344 | |
356 | |
345 | // compare hash entries, used when all keys are bytestrings |
357 | // compare hash entries, used when all keys are bytestrings |
346 | static int |
358 | static int |
… | |
… | |
381 | { |
393 | { |
382 | // for canonical output we have to sort by keys first |
394 | // for canonical output we have to sort by keys first |
383 | // actually, this is mostly due to the stupid so-called |
395 | // actually, this is mostly due to the stupid so-called |
384 | // security workaround added somewhere in 5.8.x. |
396 | // security workaround added somewhere in 5.8.x. |
385 | // that randomises hash orderings |
397 | // that randomises hash orderings |
386 | if (enc->flags & F_CANONICAL) |
398 | if (enc->json.flags & F_CANONICAL) |
387 | { |
399 | { |
388 | int fast = 1; |
400 | int fast = 1; |
389 | HE *he; |
401 | HE *he; |
390 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
402 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
391 | HE **hes = _alloca (count * sizeof (HE)); |
403 | HE **hes = _alloca (count * sizeof (HE)); |
… | |
… | |
463 | svtype svt; |
475 | svtype svt; |
464 | |
476 | |
465 | SvGETMAGIC (sv); |
477 | SvGETMAGIC (sv); |
466 | svt = SvTYPE (sv); |
478 | svt = SvTYPE (sv); |
467 | |
479 | |
|
|
480 | if (expect_false (SvOBJECT (sv))) |
|
|
481 | { |
|
|
482 | if (SvSTASH (sv) == json_boolean_stash) |
|
|
483 | { |
|
|
484 | if (SvIV (sv)) |
|
|
485 | encode_str (enc, "true", 4, 0); |
|
|
486 | else |
|
|
487 | encode_str (enc, "false", 5, 0); |
|
|
488 | } |
|
|
489 | else |
|
|
490 | { |
|
|
491 | #if 0 |
|
|
492 | if (0 && sv_derived_from (rv, "JSON::Literal")) |
|
|
493 | { |
|
|
494 | // not yet |
|
|
495 | } |
|
|
496 | #endif |
|
|
497 | if (enc->json.flags & F_CONV_BLESSED) |
|
|
498 | { |
|
|
499 | // we re-bless the reference to get overload and other niceties right |
|
|
500 | GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 0); |
|
|
501 | |
|
|
502 | if (to_json) |
|
|
503 | { |
|
|
504 | int count; |
|
|
505 | dSP; |
|
|
506 | |
|
|
507 | ENTER; SAVETMPS; PUSHMARK (SP); |
|
|
508 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); |
|
|
509 | |
|
|
510 | // calling with G_SCALAR ensures that we always get a 1 return value |
|
|
511 | PUTBACK; |
|
|
512 | call_sv ((SV *)GvCV (to_json), G_SCALAR); |
|
|
513 | SPAGAIN; |
|
|
514 | |
|
|
515 | // catch this surprisingly common error |
|
|
516 | if (SvROK (TOPs) && SvRV (TOPs) == sv) |
|
|
517 | croak ("%s::TO_JSON method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); |
|
|
518 | |
|
|
519 | sv = POPs; |
|
|
520 | PUTBACK; |
|
|
521 | |
|
|
522 | encode_sv (enc, sv); |
|
|
523 | |
|
|
524 | FREETMPS; LEAVE; |
|
|
525 | } |
|
|
526 | else if (enc->json.flags & F_ALLOW_BLESSED) |
|
|
527 | encode_str (enc, "null", 4, 0); |
|
|
528 | else |
|
|
529 | croak ("encountered object '%s', but neither allow_blessed enabled nor TO_JSON method available on it", |
|
|
530 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
|
|
531 | } |
|
|
532 | else if (enc->json.flags & F_ALLOW_BLESSED) |
|
|
533 | encode_str (enc, "null", 4, 0); |
|
|
534 | else |
|
|
535 | croak ("encountered object '%s', but neither allow_blessed nor convert_blessed settings are enabled", |
|
|
536 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
|
|
537 | } |
|
|
538 | } |
468 | if (svt == SVt_PVHV) |
539 | else if (svt == SVt_PVHV) |
469 | encode_hv (enc, (HV *)sv); |
540 | encode_hv (enc, (HV *)sv); |
470 | else if (svt == SVt_PVAV) |
541 | else if (svt == SVt_PVAV) |
471 | encode_av (enc, (AV *)sv); |
542 | encode_av (enc, (AV *)sv); |
472 | else if (svt < SVt_PVAV) |
543 | else if (svt < SVt_PVAV) |
473 | { |
544 | { |
474 | if (SvNIOK (sv) && SvIV (sv) == 0) |
545 | STRLEN len = 0; |
|
|
546 | char *pv = svt ? SvPV (sv, len) : 0; |
|
|
547 | |
|
|
548 | if (len == 1 && *pv == '1') |
|
|
549 | encode_str (enc, "true", 4, 0); |
|
|
550 | else if (len == 1 && *pv == '0') |
475 | encode_str (enc, "false", 5, 0); |
551 | encode_str (enc, "false", 5, 0); |
476 | else if (SvNIOK (sv) && SvIV (sv) == 1) |
|
|
477 | encode_str (enc, "true", 4, 0); |
|
|
478 | else |
552 | else |
479 | croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", |
553 | croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", |
480 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
554 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
481 | } |
555 | } |
482 | else |
556 | else |
… | |
… | |
552 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
626 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
553 | SvPV_nolen (sv), SvFLAGS (sv)); |
627 | SvPV_nolen (sv), SvFLAGS (sv)); |
554 | } |
628 | } |
555 | |
629 | |
556 | static SV * |
630 | static SV * |
557 | encode_json (SV *scalar, U32 flags) |
631 | encode_json (SV *scalar, JSON *json) |
558 | { |
632 | { |
559 | enc_t enc; |
633 | enc_t enc; |
560 | |
634 | |
561 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
635 | if (!(json->flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
562 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
636 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
563 | |
637 | |
564 | enc.flags = flags; |
638 | enc.json = *json; |
565 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
639 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
566 | enc.cur = SvPVX (enc.sv); |
640 | enc.cur = SvPVX (enc.sv); |
567 | enc.end = SvEND (enc.sv); |
641 | enc.end = SvEND (enc.sv); |
568 | enc.indent = 0; |
642 | enc.indent = 0; |
569 | enc.maxdepth = DEC_DEPTH (flags); |
643 | enc.maxdepth = DEC_DEPTH (enc.json.flags); |
570 | |
644 | |
571 | SvPOK_only (enc.sv); |
645 | SvPOK_only (enc.sv); |
572 | encode_sv (&enc, scalar); |
646 | encode_sv (&enc, scalar); |
573 | |
647 | |
574 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
648 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
575 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
649 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
576 | |
650 | |
577 | if (!(flags & (F_ASCII | F_LATIN1 | F_UTF8))) |
651 | if (!(enc.json.flags & (F_ASCII | F_LATIN1 | F_UTF8))) |
578 | SvUTF8_on (enc.sv); |
652 | SvUTF8_on (enc.sv); |
579 | |
653 | |
580 | if (enc.flags & F_SHRINK) |
654 | if (enc.json.flags & F_SHRINK) |
581 | shrink (enc.sv); |
655 | shrink (enc.sv); |
582 | |
656 | |
583 | return enc.sv; |
657 | return enc.sv; |
584 | } |
658 | } |
585 | |
659 | |
… | |
… | |
590 | typedef struct |
664 | typedef struct |
591 | { |
665 | { |
592 | char *cur; // current parser pointer |
666 | char *cur; // current parser pointer |
593 | char *end; // end of input string |
667 | char *end; // end of input string |
594 | const char *err; // parse error, if != 0 |
668 | const char *err; // parse error, if != 0 |
595 | U32 flags; // F_* |
669 | JSON json; |
596 | U32 depth; // recursion depth |
670 | U32 depth; // recursion depth |
597 | U32 maxdepth; // recursion depth limit |
671 | U32 maxdepth; // recursion depth limit |
598 | } dec_t; |
672 | } dec_t; |
599 | |
673 | |
600 | inline void |
674 | inline void |
… | |
… | |
631 | decode_4hex (dec_t *dec) |
705 | decode_4hex (dec_t *dec) |
632 | { |
706 | { |
633 | signed char d1, d2, d3, d4; |
707 | signed char d1, d2, d3, d4; |
634 | unsigned char *cur = (unsigned char *)dec->cur; |
708 | unsigned char *cur = (unsigned char *)dec->cur; |
635 | |
709 | |
636 | d1 = decode_hexdigit [cur [0]]; if (expect_false (d1 < 0)) ERR ("four hexadecimal digits expected"); |
710 | d1 = decode_hexdigit [cur [0]]; if (expect_false (d1 < 0)) ERR ("exactly four hexadecimal digits expected"); |
637 | d2 = decode_hexdigit [cur [1]]; if (expect_false (d2 < 0)) ERR ("four hexadecimal digits expected"); |
711 | d2 = decode_hexdigit [cur [1]]; if (expect_false (d2 < 0)) ERR ("exactly four hexadecimal digits expected"); |
638 | d3 = decode_hexdigit [cur [2]]; if (expect_false (d3 < 0)) ERR ("four hexadecimal digits expected"); |
712 | d3 = decode_hexdigit [cur [2]]; if (expect_false (d3 < 0)) ERR ("exactly four hexadecimal digits expected"); |
639 | d4 = decode_hexdigit [cur [3]]; if (expect_false (d4 < 0)) ERR ("four hexadecimal digits expected"); |
713 | d4 = decode_hexdigit [cur [3]]; if (expect_false (d4 < 0)) ERR ("exactly four hexadecimal digits expected"); |
640 | |
714 | |
641 | dec->cur += 4; |
715 | dec->cur += 4; |
642 | |
716 | |
643 | return ((UV)d1) << 12 |
717 | return ((UV)d1) << 12 |
644 | | ((UV)d2) << 8 |
718 | | ((UV)d2) << 8 |
… | |
… | |
867 | { |
941 | { |
868 | // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so |
942 | // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so |
869 | if (*start == '-') |
943 | if (*start == '-') |
870 | switch (dec->cur - start) |
944 | switch (dec->cur - start) |
871 | { |
945 | { |
872 | case 2: return newSViv (-( start [1] - '0' )); |
946 | case 2: return newSViv (-( start [1] - '0' * 1)); |
873 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
947 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
874 | case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); |
948 | case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); |
875 | case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
949 | case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
876 | } |
950 | } |
877 | else |
951 | else |
878 | switch (dec->cur - start) |
952 | switch (dec->cur - start) |
879 | { |
953 | { |
880 | case 1: return newSViv ( start [0] - '0' ); |
954 | case 1: return newSViv ( start [0] - '0' * 1); |
881 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
955 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
882 | case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); |
956 | case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); |
883 | case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
957 | case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
884 | } |
958 | } |
885 | |
959 | |
… | |
… | |
892 | if (uv < (UV)IV_MIN) |
966 | if (uv < (UV)IV_MIN) |
893 | return newSViv (-(IV)uv); |
967 | return newSViv (-(IV)uv); |
894 | } |
968 | } |
895 | else |
969 | else |
896 | return newSVuv (uv); |
970 | return newSVuv (uv); |
|
|
971 | |
|
|
972 | // here would likely be the place for bigint support |
897 | } |
973 | } |
898 | } |
974 | } |
899 | |
975 | |
|
|
976 | // if we ever support bigint or bigfloat, this is the place for bigfloat |
900 | return newSVnv (Atof (start)); |
977 | return newSVnv (Atof (start)); |
901 | |
978 | |
902 | fail: |
979 | fail: |
903 | return 0; |
980 | return 0; |
904 | } |
981 | } |
… | |
… | |
948 | } |
1025 | } |
949 | |
1026 | |
950 | static SV * |
1027 | static SV * |
951 | decode_hv (dec_t *dec) |
1028 | decode_hv (dec_t *dec) |
952 | { |
1029 | { |
|
|
1030 | SV *sv; |
953 | HV *hv = newHV (); |
1031 | HV *hv = newHV (); |
954 | |
1032 | |
955 | DEC_INC_DEPTH; |
1033 | DEC_INC_DEPTH; |
956 | decode_ws (dec); |
1034 | decode_ws (dec); |
957 | |
1035 | |
958 | if (*dec->cur == '}') |
1036 | if (*dec->cur == '}') |
959 | ++dec->cur; |
1037 | ++dec->cur; |
960 | else |
1038 | else |
961 | for (;;) |
1039 | for (;;) |
962 | { |
1040 | { |
963 | SV *key, *value; |
|
|
964 | |
|
|
965 | decode_ws (dec); EXPECT_CH ('"'); |
1041 | decode_ws (dec); EXPECT_CH ('"'); |
966 | |
1042 | |
967 | key = decode_str (dec); |
1043 | // heuristic: assume that |
968 | if (!key) |
1044 | // a) decode_str + hv_store_ent are abysmally slow. |
969 | goto fail; |
1045 | // b) most hash keys are short, simple ascii text. |
|
|
1046 | // => try to "fast-match" such strings to avoid |
|
|
1047 | // the overhead of decode_str + hv_store_ent. |
|
|
1048 | { |
|
|
1049 | SV *value; |
|
|
1050 | char *p = dec->cur; |
|
|
1051 | char *e = p + 24; // only try up to 24 bytes |
970 | |
1052 | |
971 | decode_ws (dec); EXPECT_CH (':'); |
1053 | for (;;) |
972 | |
|
|
973 | value = decode_sv (dec); |
|
|
974 | if (!value) |
|
|
975 | { |
1054 | { |
|
|
1055 | // the >= 0x80 is true on most architectures |
|
|
1056 | if (p == e || *p < 0x20 || *p >= 0x80 || *p == '\\') |
|
|
1057 | { |
|
|
1058 | // slow path, back up and use decode_str |
|
|
1059 | SV *key = decode_str (dec); |
|
|
1060 | if (!key) |
|
|
1061 | goto fail; |
|
|
1062 | |
|
|
1063 | decode_ws (dec); EXPECT_CH (':'); |
|
|
1064 | |
|
|
1065 | value = decode_sv (dec); |
|
|
1066 | if (!value) |
|
|
1067 | { |
|
|
1068 | SvREFCNT_dec (key); |
|
|
1069 | goto fail; |
|
|
1070 | } |
|
|
1071 | |
|
|
1072 | hv_store_ent (hv, key, value, 0); |
976 | SvREFCNT_dec (key); |
1073 | SvREFCNT_dec (key); |
|
|
1074 | |
|
|
1075 | break; |
|
|
1076 | } |
|
|
1077 | else if (*p == '"') |
|
|
1078 | { |
|
|
1079 | // fast path, got a simple key |
|
|
1080 | char *key = dec->cur; |
|
|
1081 | int len = p - key; |
|
|
1082 | dec->cur = p + 1; |
|
|
1083 | |
|
|
1084 | decode_ws (dec); EXPECT_CH (':'); |
|
|
1085 | |
|
|
1086 | value = decode_sv (dec); |
|
|
1087 | if (!value) |
977 | goto fail; |
1088 | goto fail; |
|
|
1089 | |
|
|
1090 | hv_store (hv, key, len, value, 0); |
|
|
1091 | |
|
|
1092 | break; |
|
|
1093 | } |
|
|
1094 | |
|
|
1095 | ++p; |
978 | } |
1096 | } |
979 | |
1097 | } |
980 | hv_store_ent (hv, key, value, 0); |
|
|
981 | SvREFCNT_dec (key); |
|
|
982 | |
1098 | |
983 | decode_ws (dec); |
1099 | decode_ws (dec); |
984 | |
1100 | |
985 | if (*dec->cur == '}') |
1101 | if (*dec->cur == '}') |
986 | { |
1102 | { |
… | |
… | |
993 | |
1109 | |
994 | ++dec->cur; |
1110 | ++dec->cur; |
995 | } |
1111 | } |
996 | |
1112 | |
997 | DEC_DEC_DEPTH; |
1113 | DEC_DEC_DEPTH; |
998 | return newRV_noinc ((SV *)hv); |
1114 | sv = newRV_noinc ((SV *)hv); |
|
|
1115 | |
|
|
1116 | // check filter callbacks |
|
|
1117 | if (dec->json.flags & F_HOOK) |
|
|
1118 | { |
|
|
1119 | if (dec->json.cb_sk_object && HvKEYS (hv) == 1) |
|
|
1120 | { |
|
|
1121 | HE *cb, *he; |
|
|
1122 | |
|
|
1123 | hv_iterinit (hv); |
|
|
1124 | he = hv_iternext (hv); |
|
|
1125 | hv_iterinit (hv); |
|
|
1126 | |
|
|
1127 | // the next line creates a mortal sv each time its called. |
|
|
1128 | // might want to optimise this for common cases. |
|
|
1129 | cb = hv_fetch_ent (dec->json.cb_sk_object, hv_iterkeysv (he), 0, 0); |
|
|
1130 | |
|
|
1131 | if (cb) |
|
|
1132 | { |
|
|
1133 | dSP; |
|
|
1134 | int count; |
|
|
1135 | |
|
|
1136 | ENTER; SAVETMPS; PUSHMARK (SP); |
|
|
1137 | XPUSHs (HeVAL (he)); |
|
|
1138 | |
|
|
1139 | PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; |
|
|
1140 | |
|
|
1141 | if (count == 1) |
|
|
1142 | { |
|
|
1143 | sv = newSVsv (POPs); |
|
|
1144 | FREETMPS; LEAVE; |
|
|
1145 | return sv; |
|
|
1146 | } |
|
|
1147 | |
|
|
1148 | FREETMPS; LEAVE; |
|
|
1149 | } |
|
|
1150 | } |
|
|
1151 | |
|
|
1152 | if (dec->json.cb_object) |
|
|
1153 | { |
|
|
1154 | dSP; |
|
|
1155 | int count; |
|
|
1156 | |
|
|
1157 | ENTER; SAVETMPS; PUSHMARK (SP); |
|
|
1158 | XPUSHs (sv_2mortal (sv)); |
|
|
1159 | |
|
|
1160 | PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN; |
|
|
1161 | |
|
|
1162 | if (count == 1) |
|
|
1163 | { |
|
|
1164 | sv = newSVsv (POPs); |
|
|
1165 | FREETMPS; LEAVE; |
|
|
1166 | return sv; |
|
|
1167 | } |
|
|
1168 | |
|
|
1169 | SvREFCNT_inc (sv); |
|
|
1170 | FREETMPS; LEAVE; |
|
|
1171 | } |
|
|
1172 | } |
|
|
1173 | |
|
|
1174 | return sv; |
999 | |
1175 | |
1000 | fail: |
1176 | fail: |
1001 | SvREFCNT_dec (hv); |
1177 | SvREFCNT_dec (hv); |
1002 | DEC_DEC_DEPTH; |
1178 | DEC_DEC_DEPTH; |
1003 | return 0; |
1179 | return 0; |
… | |
… | |
1005 | |
1181 | |
1006 | static SV * |
1182 | static SV * |
1007 | decode_sv (dec_t *dec) |
1183 | decode_sv (dec_t *dec) |
1008 | { |
1184 | { |
1009 | decode_ws (dec); |
1185 | decode_ws (dec); |
|
|
1186 | |
|
|
1187 | // the beauty of JSON: you need exactly one character lookahead |
|
|
1188 | // to parse anything. |
1010 | switch (*dec->cur) |
1189 | switch (*dec->cur) |
1011 | { |
1190 | { |
1012 | case '"': ++dec->cur; return decode_str (dec); |
1191 | case '"': ++dec->cur; return decode_str (dec); |
1013 | case '[': ++dec->cur; return decode_av (dec); |
1192 | case '[': ++dec->cur; return decode_av (dec); |
1014 | case '{': ++dec->cur; return decode_hv (dec); |
1193 | case '{': ++dec->cur; return decode_hv (dec); |
… | |
… | |
1020 | |
1199 | |
1021 | case 't': |
1200 | case 't': |
1022 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) |
1201 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) |
1023 | { |
1202 | { |
1024 | dec->cur += 4; |
1203 | dec->cur += 4; |
1025 | return newSViv (1); |
1204 | return SvREFCNT_inc (json_true); |
1026 | } |
1205 | } |
1027 | else |
1206 | else |
1028 | ERR ("'true' expected"); |
1207 | ERR ("'true' expected"); |
1029 | |
1208 | |
1030 | break; |
1209 | break; |
1031 | |
1210 | |
1032 | case 'f': |
1211 | case 'f': |
1033 | if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) |
1212 | if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) |
1034 | { |
1213 | { |
1035 | dec->cur += 5; |
1214 | dec->cur += 5; |
1036 | return newSViv (0); |
1215 | return SvREFCNT_inc (json_false); |
1037 | } |
1216 | } |
1038 | else |
1217 | else |
1039 | ERR ("'false' expected"); |
1218 | ERR ("'false' expected"); |
1040 | |
1219 | |
1041 | break; |
1220 | break; |
… | |
… | |
1059 | fail: |
1238 | fail: |
1060 | return 0; |
1239 | return 0; |
1061 | } |
1240 | } |
1062 | |
1241 | |
1063 | static SV * |
1242 | static SV * |
1064 | decode_json (SV *string, U32 flags, UV *offset_return) |
1243 | decode_json (SV *string, JSON *json, UV *offset_return) |
1065 | { |
1244 | { |
1066 | dec_t dec; |
1245 | dec_t dec; |
1067 | UV offset; |
1246 | UV offset; |
1068 | SV *sv; |
1247 | SV *sv; |
1069 | |
1248 | |
1070 | SvGETMAGIC (string); |
1249 | SvGETMAGIC (string); |
1071 | SvUPGRADE (string, SVt_PV); |
1250 | SvUPGRADE (string, SVt_PV); |
1072 | |
1251 | |
|
|
1252 | if (json->flags & F_MAXSIZE && SvCUR (string) > DEC_SIZE (json->flags)) |
|
|
1253 | croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", |
|
|
1254 | (unsigned long)SvCUR (string), (unsigned long)DEC_SIZE (json->flags)); |
|
|
1255 | |
1073 | if (flags & F_UTF8) |
1256 | if (json->flags & F_UTF8) |
1074 | sv_utf8_downgrade (string, 0); |
1257 | sv_utf8_downgrade (string, 0); |
1075 | else |
1258 | else |
1076 | sv_utf8_upgrade (string); |
1259 | sv_utf8_upgrade (string); |
1077 | |
1260 | |
1078 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
1261 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
1079 | |
1262 | |
1080 | dec.flags = flags; |
1263 | dec.json = *json; |
1081 | dec.cur = SvPVX (string); |
1264 | dec.cur = SvPVX (string); |
1082 | dec.end = SvEND (string); |
1265 | dec.end = SvEND (string); |
1083 | dec.err = 0; |
1266 | dec.err = 0; |
1084 | dec.depth = 0; |
1267 | dec.depth = 0; |
1085 | dec.maxdepth = DEC_DEPTH (dec.flags); |
1268 | dec.maxdepth = DEC_DEPTH (dec.json.flags); |
|
|
1269 | |
|
|
1270 | if (dec.json.cb_object || dec.json.cb_sk_object) |
|
|
1271 | dec.json.flags |= F_HOOK; |
1086 | |
1272 | |
1087 | *dec.end = 0; // this should basically be a nop, too, but make sure it's there |
1273 | *dec.end = 0; // this should basically be a nop, too, but make sure it's there |
1088 | sv = decode_sv (&dec); |
1274 | sv = decode_sv (&dec); |
1089 | |
1275 | |
1090 | if (!(offset_return || !sv)) |
1276 | if (!(offset_return || !sv)) |
… | |
… | |
1100 | } |
1286 | } |
1101 | } |
1287 | } |
1102 | |
1288 | |
1103 | if (offset_return || !sv) |
1289 | if (offset_return || !sv) |
1104 | { |
1290 | { |
1105 | offset = dec.flags & F_UTF8 |
1291 | offset = dec.json.flags & F_UTF8 |
1106 | ? dec.cur - SvPVX (string) |
1292 | ? dec.cur - SvPVX (string) |
1107 | : utf8_distance (dec.cur, SvPVX (string)); |
1293 | : utf8_distance (dec.cur, SvPVX (string)); |
1108 | |
1294 | |
1109 | if (offset_return) |
1295 | if (offset_return) |
1110 | *offset_return = offset; |
1296 | *offset_return = offset; |
… | |
… | |
1129 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1315 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1130 | } |
1316 | } |
1131 | |
1317 | |
1132 | sv = sv_2mortal (sv); |
1318 | sv = sv_2mortal (sv); |
1133 | |
1319 | |
1134 | if (!(dec.flags & F_ALLOW_NONREF) && !SvROK (sv)) |
1320 | if (!(dec.json.flags & F_ALLOW_NONREF) && !SvROK (sv)) |
1135 | croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)"); |
1321 | croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)"); |
1136 | |
1322 | |
1137 | return sv; |
1323 | return sv; |
1138 | } |
1324 | } |
1139 | |
1325 | |
… | |
… | |
1143 | MODULE = JSON::XS PACKAGE = JSON::XS |
1329 | MODULE = JSON::XS PACKAGE = JSON::XS |
1144 | |
1330 | |
1145 | BOOT: |
1331 | BOOT: |
1146 | { |
1332 | { |
1147 | int i; |
1333 | int i; |
1148 | |
|
|
1149 | memset (decode_hexdigit, 0xff, 256); |
|
|
1150 | |
1334 | |
1151 | for (i = 0; i < 256; ++i) |
1335 | for (i = 0; i < 256; ++i) |
1152 | decode_hexdigit [i] = |
1336 | decode_hexdigit [i] = |
1153 | i >= '0' && i <= '9' ? i - '0' |
1337 | i >= '0' && i <= '9' ? i - '0' |
1154 | : i >= 'a' && i <= 'f' ? i - 'a' + 10 |
1338 | : i >= 'a' && i <= 'f' ? i - 'a' + 10 |
1155 | : i >= 'A' && i <= 'F' ? i - 'A' + 10 |
1339 | : i >= 'A' && i <= 'F' ? i - 'A' + 10 |
1156 | : -1; |
1340 | : -1; |
1157 | |
1341 | |
1158 | json_stash = gv_stashpv ("JSON::XS", 1); |
1342 | json_stash = gv_stashpv ("JSON::XS" , 1); |
|
|
1343 | json_boolean_stash = gv_stashpv ("JSON::XS::Boolean", 1); |
|
|
1344 | |
|
|
1345 | json_true = get_sv ("JSON::XS::true" , 1); SvREADONLY_on (json_true ); |
|
|
1346 | json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false); |
1159 | } |
1347 | } |
1160 | |
1348 | |
1161 | PROTOTYPES: DISABLE |
1349 | PROTOTYPES: DISABLE |
1162 | |
1350 | |
1163 | SV *new (char *dummy) |
1351 | void new (char *klass) |
1164 | CODE: |
1352 | PPCODE: |
1165 | RETVAL = sv_bless (newRV_noinc (newSVuv (F_DEFAULT)), json_stash); |
1353 | { |
1166 | OUTPUT: |
1354 | SV *pv = NEWSV (0, sizeof (JSON)); |
1167 | RETVAL |
1355 | SvPOK_only (pv); |
|
|
1356 | Zero (SvPVX (pv), 1, JSON); |
|
|
1357 | ((JSON *)SvPVX (pv))->flags = F_DEFAULT; |
|
|
1358 | XPUSHs (sv_2mortal (sv_bless (newRV_noinc (pv), json_stash))); |
|
|
1359 | } |
1168 | |
1360 | |
1169 | SV *ascii (SV *self, int enable = 1) |
1361 | void ascii (JSON *self, int enable = 1) |
1170 | ALIAS: |
1362 | ALIAS: |
1171 | ascii = F_ASCII |
1363 | ascii = F_ASCII |
1172 | latin1 = F_LATIN1 |
1364 | latin1 = F_LATIN1 |
1173 | utf8 = F_UTF8 |
1365 | utf8 = F_UTF8 |
1174 | indent = F_INDENT |
1366 | indent = F_INDENT |
1175 | canonical = F_CANONICAL |
1367 | canonical = F_CANONICAL |
1176 | space_before = F_SPACE_BEFORE |
1368 | space_before = F_SPACE_BEFORE |
1177 | space_after = F_SPACE_AFTER |
1369 | space_after = F_SPACE_AFTER |
1178 | pretty = F_PRETTY |
1370 | pretty = F_PRETTY |
1179 | allow_nonref = F_ALLOW_NONREF |
1371 | allow_nonref = F_ALLOW_NONREF |
1180 | shrink = F_SHRINK |
1372 | shrink = F_SHRINK |
|
|
1373 | allow_blessed = F_ALLOW_BLESSED |
|
|
1374 | convert_blessed = F_CONV_BLESSED |
1181 | CODE: |
1375 | PPCODE: |
1182 | { |
1376 | { |
1183 | UV *uv = SvJSON (self); |
|
|
1184 | if (enable) |
1377 | if (enable) |
1185 | *uv |= ix; |
1378 | self->flags |= ix; |
1186 | else |
1379 | else |
1187 | *uv &= ~ix; |
1380 | self->flags &= ~ix; |
1188 | |
1381 | |
1189 | RETVAL = newSVsv (self); |
1382 | XPUSHs (ST (0)); |
1190 | } |
1383 | } |
1191 | OUTPUT: |
|
|
1192 | RETVAL |
|
|
1193 | |
1384 | |
1194 | SV *max_depth (SV *self, UV max_depth = 0x80000000UL) |
1385 | void max_depth (JSON *self, UV max_depth = 0x80000000UL) |
1195 | CODE: |
1386 | PPCODE: |
1196 | { |
1387 | { |
1197 | UV *uv = SvJSON (self); |
|
|
1198 | UV log2 = 0; |
1388 | UV log2 = 0; |
1199 | |
1389 | |
1200 | if (max_depth > 0x80000000UL) max_depth = 0x80000000UL; |
1390 | if (max_depth > 0x80000000UL) max_depth = 0x80000000UL; |
1201 | |
1391 | |
1202 | while ((1UL << log2) < max_depth) |
1392 | while ((1UL << log2) < max_depth) |
1203 | ++log2; |
1393 | ++log2; |
1204 | |
1394 | |
1205 | *uv = *uv & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); |
1395 | self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); |
1206 | |
1396 | |
1207 | RETVAL = newSVsv (self); |
1397 | XPUSHs (ST (0)); |
1208 | } |
1398 | } |
1209 | OUTPUT: |
|
|
1210 | RETVAL |
|
|
1211 | |
1399 | |
1212 | void encode (SV *self, SV *scalar) |
1400 | void max_size (JSON *self, UV max_size = 0) |
1213 | PPCODE: |
1401 | PPCODE: |
1214 | XPUSHs (encode_json (scalar, *SvJSON (self))); |
1402 | { |
|
|
1403 | UV log2 = 0; |
1215 | |
1404 | |
1216 | void decode (SV *self, SV *jsonstr) |
1405 | if (max_size > 0x80000000UL) max_size = 0x80000000UL; |
|
|
1406 | if (max_size == 1) max_size = 2; |
|
|
1407 | |
|
|
1408 | while ((1UL << log2) < max_size) |
|
|
1409 | ++log2; |
|
|
1410 | |
|
|
1411 | self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE); |
|
|
1412 | |
|
|
1413 | XPUSHs (ST (0)); |
|
|
1414 | } |
|
|
1415 | |
|
|
1416 | void filter_json_object (JSON *self, SV *cb = &PL_sv_undef) |
1217 | PPCODE: |
1417 | PPCODE: |
|
|
1418 | { |
|
|
1419 | SvREFCNT_dec (self->cb_object); |
|
|
1420 | self->cb_object = SvOK (cb) ? newSVsv (cb) : 0; |
|
|
1421 | |
|
|
1422 | XPUSHs (ST (0)); |
|
|
1423 | } |
|
|
1424 | |
|
|
1425 | void filter_json_single_key_object (JSON *self, SV *key, SV *cb = &PL_sv_undef) |
|
|
1426 | PPCODE: |
|
|
1427 | { |
|
|
1428 | if (!self->cb_sk_object) |
|
|
1429 | self->cb_sk_object = newHV (); |
|
|
1430 | |
|
|
1431 | if (SvOK (cb)) |
|
|
1432 | hv_store_ent (self->cb_sk_object, key, newSVsv (cb), 0); |
|
|
1433 | else |
|
|
1434 | { |
|
|
1435 | hv_delete_ent (self->cb_sk_object, key, G_DISCARD, 0); |
|
|
1436 | |
|
|
1437 | if (!HvKEYS (self->cb_sk_object)) |
|
|
1438 | { |
|
|
1439 | SvREFCNT_dec (self->cb_sk_object); |
|
|
1440 | self->cb_sk_object = 0; |
|
|
1441 | } |
|
|
1442 | } |
|
|
1443 | |
|
|
1444 | XPUSHs (ST (0)); |
|
|
1445 | } |
|
|
1446 | |
|
|
1447 | void encode (JSON *self, SV *scalar) |
|
|
1448 | PPCODE: |
|
|
1449 | XPUSHs (encode_json (scalar, self)); |
|
|
1450 | |
|
|
1451 | void decode (JSON *self, SV *jsonstr) |
|
|
1452 | PPCODE: |
1218 | XPUSHs (decode_json (jsonstr, *SvJSON (self), 0)); |
1453 | XPUSHs (decode_json (jsonstr, self, 0)); |
1219 | |
1454 | |
1220 | void decode_prefix (SV *self, SV *jsonstr) |
1455 | void decode_prefix (JSON *self, SV *jsonstr) |
1221 | PPCODE: |
1456 | PPCODE: |
1222 | { |
1457 | { |
1223 | UV offset; |
1458 | UV offset; |
1224 | EXTEND (SP, 2); |
1459 | EXTEND (SP, 2); |
1225 | PUSHs (decode_json (jsonstr, *SvJSON (self), &offset)); |
1460 | PUSHs (decode_json (jsonstr, self, &offset)); |
1226 | PUSHs (sv_2mortal (newSVuv (offset))); |
1461 | PUSHs (sv_2mortal (newSVuv (offset))); |
1227 | } |
1462 | } |
1228 | |
1463 | |
|
|
1464 | void DESTROY (JSON *self) |
|
|
1465 | CODE: |
|
|
1466 | SvREFCNT_dec (self->cb_sk_object); |
|
|
1467 | SvREFCNT_dec (self->cb_object); |
|
|
1468 | |
1229 | PROTOTYPES: ENABLE |
1469 | PROTOTYPES: ENABLE |
1230 | |
1470 | |
1231 | void to_json (SV *scalar) |
1471 | void to_json (SV *scalar) |
1232 | ALIAS: |
|
|
1233 | objToJson = 0 |
|
|
1234 | PPCODE: |
1472 | PPCODE: |
|
|
1473 | { |
|
|
1474 | JSON json = { F_DEFAULT | F_UTF8 }; |
1235 | XPUSHs (encode_json (scalar, F_DEFAULT | F_UTF8)); |
1475 | XPUSHs (encode_json (scalar, &json)); |
|
|
1476 | } |
1236 | |
1477 | |
1237 | void from_json (SV *jsonstr) |
1478 | void from_json (SV *jsonstr) |
1238 | ALIAS: |
|
|
1239 | jsonToObj = 0 |
|
|
1240 | PPCODE: |
1479 | PPCODE: |
|
|
1480 | { |
|
|
1481 | JSON json = { F_DEFAULT | F_UTF8 }; |
1241 | XPUSHs (decode_json (jsonstr, F_DEFAULT | F_UTF8, 0)); |
1482 | XPUSHs (decode_json (jsonstr, &json, 0)); |
|
|
1483 | } |
1242 | |
1484 | |