1 | #include "EXTERN.h" |
1 | #include "EXTERN.h" |
2 | #include "perl.h" |
2 | #include "perl.h" |
3 | #include "XSUB.h" |
3 | #include "XSUB.h" |
4 | |
4 | |
5 | #include "assert.h" |
5 | #include <assert.h> |
6 | #include "string.h" |
6 | #include <string.h> |
7 | #include "stdlib.h" |
7 | #include <stdlib.h> |
8 | #include "stdio.h" |
8 | #include <stdio.h> |
|
|
9 | #include <float.h> |
9 | |
10 | |
10 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
11 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
11 | # define snprintf _snprintf // C compilers have this in stdio.h |
12 | # define snprintf _snprintf // C compilers have this in stdio.h |
12 | #endif |
13 | #endif |
13 | |
14 | |
… | |
… | |
25 | #define F_SPACE_BEFORE 0x00000020UL |
26 | #define F_SPACE_BEFORE 0x00000020UL |
26 | #define F_SPACE_AFTER 0x00000040UL |
27 | #define F_SPACE_AFTER 0x00000040UL |
27 | #define F_ALLOW_NONREF 0x00000100UL |
28 | #define F_ALLOW_NONREF 0x00000100UL |
28 | #define F_SHRINK 0x00000200UL |
29 | #define F_SHRINK 0x00000200UL |
29 | #define F_ALLOW_BLESSED 0x00000400UL |
30 | #define F_ALLOW_BLESSED 0x00000400UL |
30 | #define F_CONV_BLESSED 0x00000800UL // NYI |
31 | #define F_CONV_BLESSED 0x00000800UL |
31 | #define F_MAXDEPTH 0xf8000000UL |
32 | #define F_MAXDEPTH 0xf8000000UL |
32 | #define S_MAXDEPTH 27 |
33 | #define S_MAXDEPTH 27 |
33 | #define F_MAXSIZE 0x01f00000UL |
34 | #define F_MAXSIZE 0x01f00000UL |
34 | #define S_MAXSIZE 20 |
35 | #define S_MAXSIZE 20 |
|
|
36 | #define F_HOOK 0x00080000UL // some hooks exist, so slow-path processing |
35 | |
37 | |
36 | #define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) |
38 | #define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) |
37 | #define DEC_SIZE(flags) (1UL << ((flags & F_MAXSIZE ) >> S_MAXSIZE )) |
39 | #define DEC_SIZE(flags) (1UL << ((flags & F_MAXSIZE ) >> S_MAXSIZE )) |
38 | |
40 | |
39 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
41 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
… | |
… | |
56 | #endif |
58 | #endif |
57 | |
59 | |
58 | #define expect_false(expr) expect ((expr) != 0, 0) |
60 | #define expect_false(expr) expect ((expr) != 0, 0) |
59 | #define expect_true(expr) expect ((expr) != 0, 1) |
61 | #define expect_true(expr) expect ((expr) != 0, 1) |
60 | |
62 | |
|
|
63 | #ifdef USE_ITHREADS |
|
|
64 | # define JSON_SLOW 1 |
|
|
65 | # define JSON_STASH (json_stash ? json_stash : gv_stashpv ("JSON::XS", 1)) |
|
|
66 | #else |
|
|
67 | # define JSON_SLOW 0 |
|
|
68 | # define JSON_STASH json_stash |
|
|
69 | #endif |
|
|
70 | |
61 | static HV *json_stash, *json_boolean_stash; // JSON::XS:: |
71 | static HV *json_stash, *json_boolean_stash; // JSON::XS:: |
62 | static SV *json_true, *json_false; |
72 | static SV *json_true, *json_false; |
63 | |
73 | |
64 | typedef struct json { |
74 | typedef struct { |
65 | U32 flags; |
75 | U32 flags; |
|
|
76 | SV *cb_object; |
|
|
77 | HV *cb_sk_object; |
66 | } JSON__XS; |
78 | } JSON; |
67 | |
79 | |
68 | ///////////////////////////////////////////////////////////////////////////// |
80 | ///////////////////////////////////////////////////////////////////////////// |
69 | // utility functions |
81 | // utility functions |
70 | |
82 | |
71 | static UV * |
83 | inline void |
72 | SvJSON (SV *sv) |
|
|
73 | { |
|
|
74 | if (!(SvROK (sv) && SvOBJECT (SvRV (sv)) && SvSTASH (SvRV (sv)) == json_stash)) |
|
|
75 | croak ("object is not of type JSON::XS"); |
|
|
76 | |
|
|
77 | return &SvUVX (SvRV (sv)); |
|
|
78 | } |
|
|
79 | |
|
|
80 | static void |
|
|
81 | shrink (SV *sv) |
84 | shrink (SV *sv) |
82 | { |
85 | { |
83 | sv_utf8_downgrade (sv, 1); |
86 | sv_utf8_downgrade (sv, 1); |
84 | if (SvLEN (sv) > SvCUR (sv) + 1) |
87 | if (SvLEN (sv) > SvCUR (sv) + 1) |
85 | { |
88 | { |
… | |
… | |
120 | typedef struct |
123 | typedef struct |
121 | { |
124 | { |
122 | char *cur; // SvPVX (sv) + current output position |
125 | char *cur; // SvPVX (sv) + current output position |
123 | char *end; // SvEND (sv) |
126 | char *end; // SvEND (sv) |
124 | SV *sv; // result scalar |
127 | SV *sv; // result scalar |
125 | struct json json; |
128 | JSON json; |
126 | U32 indent; // indentation level |
129 | U32 indent; // indentation level |
127 | U32 maxdepth; // max. indentation/recursion level |
130 | U32 maxdepth; // max. indentation/recursion level |
128 | } enc_t; |
131 | } enc_t; |
129 | |
132 | |
130 | inline void |
133 | inline void |
… | |
… | |
312 | encode_ch (enc, '['); encode_nl (enc); |
315 | encode_ch (enc, '['); encode_nl (enc); |
313 | ++enc->indent; |
316 | ++enc->indent; |
314 | |
317 | |
315 | for (i = 0; i <= len; ++i) |
318 | for (i = 0; i <= len; ++i) |
316 | { |
319 | { |
|
|
320 | SV **svp = av_fetch (av, i, 0); |
|
|
321 | |
317 | encode_indent (enc); |
322 | encode_indent (enc); |
318 | encode_sv (enc, *av_fetch (av, i, 0)); |
323 | |
|
|
324 | if (svp) |
|
|
325 | encode_sv (enc, *svp); |
|
|
326 | else |
|
|
327 | encode_str (enc, "null", 4, 0); |
319 | |
328 | |
320 | if (i < len) |
329 | if (i < len) |
321 | encode_comma (enc); |
330 | encode_comma (enc); |
322 | } |
331 | } |
323 | |
332 | |
… | |
… | |
326 | --enc->indent; |
335 | --enc->indent; |
327 | encode_indent (enc); encode_ch (enc, ']'); |
336 | encode_indent (enc); encode_ch (enc, ']'); |
328 | } |
337 | } |
329 | |
338 | |
330 | static void |
339 | static void |
331 | encode_he (enc_t *enc, HE *he) |
340 | encode_hk (enc_t *enc, HE *he) |
332 | { |
341 | { |
333 | encode_ch (enc, '"'); |
342 | encode_ch (enc, '"'); |
334 | |
343 | |
335 | if (HeKLEN (he) == HEf_SVKEY) |
344 | if (HeKLEN (he) == HEf_SVKEY) |
336 | { |
345 | { |
… | |
… | |
349 | encode_ch (enc, '"'); |
358 | encode_ch (enc, '"'); |
350 | |
359 | |
351 | if (enc->json.flags & F_SPACE_BEFORE) encode_space (enc); |
360 | if (enc->json.flags & F_SPACE_BEFORE) encode_space (enc); |
352 | encode_ch (enc, ':'); |
361 | encode_ch (enc, ':'); |
353 | if (enc->json.flags & F_SPACE_AFTER ) encode_space (enc); |
362 | if (enc->json.flags & F_SPACE_AFTER ) encode_space (enc); |
354 | encode_sv (enc, HeVAL (he)); |
|
|
355 | } |
363 | } |
356 | |
364 | |
357 | // compare hash entries, used when all keys are bytestrings |
365 | // compare hash entries, used when all keys are bytestrings |
358 | static int |
366 | static int |
359 | he_cmp_fast (const void *a_, const void *b_) |
367 | he_cmp_fast (const void *a_, const void *b_) |
… | |
… | |
364 | HE *b = *(HE **)b_; |
372 | HE *b = *(HE **)b_; |
365 | |
373 | |
366 | STRLEN la = HeKLEN (a); |
374 | STRLEN la = HeKLEN (a); |
367 | STRLEN lb = HeKLEN (b); |
375 | STRLEN lb = HeKLEN (b); |
368 | |
376 | |
369 | if (!(cmp = memcmp (HeKEY (a), HeKEY (b), la < lb ? la : lb))) |
377 | if (!(cmp = memcmp (HeKEY (b), HeKEY (a), lb < la ? lb : la))) |
370 | cmp = la - lb; |
378 | cmp = lb - la; |
371 | |
379 | |
372 | return cmp; |
380 | return cmp; |
373 | } |
381 | } |
374 | |
382 | |
375 | // compare hash entries, used when some keys are sv's or utf-x |
383 | // compare hash entries, used when some keys are sv's or utf-x |
376 | static int |
384 | static int |
377 | he_cmp_slow (const void *a, const void *b) |
385 | he_cmp_slow (const void *a, const void *b) |
378 | { |
386 | { |
379 | return sv_cmp (HeSVKEY_force (*(HE **)a), HeSVKEY_force (*(HE **)b)); |
387 | return sv_cmp (HeSVKEY_force (*(HE **)b), HeSVKEY_force (*(HE **)a)); |
380 | } |
388 | } |
381 | |
389 | |
382 | static void |
390 | static void |
383 | encode_hv (enc_t *enc, HV *hv) |
391 | encode_hv (enc_t *enc, HV *hv) |
384 | { |
392 | { |
|
|
393 | HE *he; |
385 | int count, i; |
394 | int count; |
386 | |
395 | |
387 | if (enc->indent >= enc->maxdepth) |
396 | if (enc->indent >= enc->maxdepth) |
388 | croak ("data structure too deep (hit recursion limit)"); |
397 | croak ("data structure too deep (hit recursion limit)"); |
389 | |
398 | |
390 | encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; |
399 | encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; |
391 | |
400 | |
392 | if ((count = hv_iterinit (hv))) |
|
|
393 | { |
|
|
394 | // for canonical output we have to sort by keys first |
401 | // for canonical output we have to sort by keys first |
395 | // actually, this is mostly due to the stupid so-called |
402 | // actually, this is mostly due to the stupid so-called |
396 | // security workaround added somewhere in 5.8.x. |
403 | // security workaround added somewhere in 5.8.x. |
397 | // that randomises hash orderings |
404 | // that randomises hash orderings |
398 | if (enc->json.flags & F_CANONICAL) |
405 | if (enc->json.flags & F_CANONICAL) |
|
|
406 | { |
|
|
407 | int count = hv_iterinit (hv); |
|
|
408 | |
|
|
409 | if (SvMAGICAL (hv)) |
399 | { |
410 | { |
|
|
411 | // need to count by iterating. could improve by dynamically building the vector below |
|
|
412 | // but I don't care for the speed of this special case. |
|
|
413 | // note also that we will run into undefined behaviour when the two iterations |
|
|
414 | // do not result in the same count, something I might care for in some later release. |
|
|
415 | |
|
|
416 | count = 0; |
|
|
417 | while (hv_iternext (hv)) |
|
|
418 | ++count; |
|
|
419 | |
|
|
420 | hv_iterinit (hv); |
|
|
421 | } |
|
|
422 | |
|
|
423 | if (count) |
|
|
424 | { |
400 | int fast = 1; |
425 | int i, fast = 1; |
401 | HE *he; |
|
|
402 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
426 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
403 | HE **hes = _alloca (count * sizeof (HE)); |
427 | HE **hes = _alloca (count * sizeof (HE)); |
404 | #else |
428 | #else |
405 | HE *hes [count]; // if your compiler dies here, you need to enable C99 mode |
429 | HE *hes [count]; // if your compiler dies here, you need to enable C99 mode |
406 | #endif |
430 | #endif |
… | |
… | |
433 | |
457 | |
434 | FREETMPS; |
458 | FREETMPS; |
435 | LEAVE; |
459 | LEAVE; |
436 | } |
460 | } |
437 | |
461 | |
438 | for (i = 0; i < count; ++i) |
462 | while (count--) |
439 | { |
463 | { |
440 | encode_indent (enc); |
464 | encode_indent (enc); |
|
|
465 | he = hes [count]; |
441 | encode_he (enc, hes [i]); |
466 | encode_hk (enc, he); |
|
|
467 | encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he)); |
442 | |
468 | |
443 | if (i < count - 1) |
469 | if (count) |
444 | encode_comma (enc); |
470 | encode_comma (enc); |
445 | } |
471 | } |
446 | |
|
|
447 | encode_nl (enc); |
|
|
448 | } |
472 | } |
|
|
473 | } |
449 | else |
474 | else |
450 | { |
475 | { |
|
|
476 | if (hv_iterinit (hv) || SvMAGICAL (hv)) |
451 | HE *he = hv_iternext (hv); |
477 | if ((he = hv_iternext (hv))) |
452 | |
|
|
453 | for (;;) |
478 | for (;;) |
454 | { |
479 | { |
455 | encode_indent (enc); |
480 | encode_indent (enc); |
456 | encode_he (enc, he); |
481 | encode_hk (enc, he); |
|
|
482 | encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he)); |
457 | |
483 | |
458 | if (!(he = hv_iternext (hv))) |
484 | if (!(he = hv_iternext (hv))) |
459 | break; |
485 | break; |
460 | |
486 | |
461 | encode_comma (enc); |
487 | encode_comma (enc); |
462 | } |
488 | } |
|
|
489 | } |
463 | |
490 | |
464 | encode_nl (enc); |
491 | encode_nl (enc); |
465 | } |
|
|
466 | } |
|
|
467 | |
492 | |
468 | --enc->indent; encode_indent (enc); encode_ch (enc, '}'); |
493 | --enc->indent; encode_indent (enc); encode_ch (enc, '}'); |
469 | } |
494 | } |
470 | |
495 | |
471 | // encode objects, arrays and special \0=false and \1=true values. |
496 | // encode objects, arrays and special \0=false and \1=true values. |
… | |
… | |
477 | SvGETMAGIC (sv); |
502 | SvGETMAGIC (sv); |
478 | svt = SvTYPE (sv); |
503 | svt = SvTYPE (sv); |
479 | |
504 | |
480 | if (expect_false (SvOBJECT (sv))) |
505 | if (expect_false (SvOBJECT (sv))) |
481 | { |
506 | { |
|
|
507 | HV *stash = !JSON_SLOW || json_boolean_stash |
|
|
508 | ? json_boolean_stash |
|
|
509 | : gv_stashpv ("JSON::XS::Boolean", 1); |
|
|
510 | |
482 | if (SvSTASH (sv) == json_boolean_stash) |
511 | if (SvSTASH (sv) == stash) |
483 | { |
512 | { |
484 | if (SvIV (sv) == 0) |
513 | if (SvIV (sv)) |
|
|
514 | encode_str (enc, "true", 4, 0); |
|
|
515 | else |
485 | encode_str (enc, "false", 5, 0); |
516 | encode_str (enc, "false", 5, 0); |
486 | else |
|
|
487 | encode_str (enc, "true", 4, 0); |
|
|
488 | } |
517 | } |
489 | else |
518 | else |
490 | { |
519 | { |
491 | #if 0 |
520 | #if 0 |
492 | if (0 && sv_derived_from (rv, "JSON::Literal")) |
521 | if (0 && sv_derived_from (rv, "JSON::Literal")) |
… | |
… | |
495 | } |
524 | } |
496 | #endif |
525 | #endif |
497 | if (enc->json.flags & F_CONV_BLESSED) |
526 | if (enc->json.flags & F_CONV_BLESSED) |
498 | { |
527 | { |
499 | // we re-bless the reference to get overload and other niceties right |
528 | // we re-bless the reference to get overload and other niceties right |
500 | GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 1); |
529 | GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 0); |
501 | |
530 | |
502 | if (to_json) |
531 | if (to_json) |
503 | { |
532 | { |
504 | dSP; |
533 | dSP; |
505 | ENTER; |
534 | |
506 | SAVETMPS; |
|
|
507 | PUSHMARK (SP); |
535 | ENTER; SAVETMPS; PUSHMARK (SP); |
508 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); |
536 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); |
509 | |
537 | |
510 | // calling with G_SCALAR ensures that we always get a 1 reutrn value |
538 | // calling with G_SCALAR ensures that we always get a 1 return value |
511 | // check anyways. |
|
|
512 | PUTBACK; |
539 | PUTBACK; |
513 | assert (1 == call_sv ((SV *)GvCV (to_json), G_SCALAR)); |
540 | call_sv ((SV *)GvCV (to_json), G_SCALAR); |
514 | SPAGAIN; |
541 | SPAGAIN; |
515 | |
542 | |
|
|
543 | // catch this surprisingly common error |
|
|
544 | if (SvROK (TOPs) && SvRV (TOPs) == sv) |
|
|
545 | croak ("%s::TO_JSON method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); |
|
|
546 | |
|
|
547 | sv = POPs; |
|
|
548 | PUTBACK; |
|
|
549 | |
516 | encode_sv (enc, POPs); |
550 | encode_sv (enc, sv); |
517 | |
551 | |
518 | FREETMPS; |
552 | FREETMPS; LEAVE; |
519 | LEAVE; |
|
|
520 | } |
553 | } |
521 | else if (enc->json.flags & F_ALLOW_BLESSED) |
554 | else if (enc->json.flags & F_ALLOW_BLESSED) |
522 | encode_str (enc, "null", 4, 0); |
555 | encode_str (enc, "null", 4, 0); |
523 | else |
556 | else |
524 | croak ("encountered object '%s', but neither allow_blessed enabled nor TO_JSON method available on it", |
557 | croak ("encountered object '%s', but neither allow_blessed enabled nor TO_JSON method available on it", |
… | |
… | |
535 | encode_hv (enc, (HV *)sv); |
568 | encode_hv (enc, (HV *)sv); |
536 | else if (svt == SVt_PVAV) |
569 | else if (svt == SVt_PVAV) |
537 | encode_av (enc, (AV *)sv); |
570 | encode_av (enc, (AV *)sv); |
538 | else if (svt < SVt_PVAV) |
571 | else if (svt < SVt_PVAV) |
539 | { |
572 | { |
540 | if (SvNIOK (sv) && SvIV (sv) == 0) |
573 | STRLEN len = 0; |
|
|
574 | char *pv = svt ? SvPV (sv, len) : 0; |
|
|
575 | |
|
|
576 | if (len == 1 && *pv == '1') |
|
|
577 | encode_str (enc, "true", 4, 0); |
|
|
578 | else if (len == 1 && *pv == '0') |
541 | encode_str (enc, "false", 5, 0); |
579 | encode_str (enc, "false", 5, 0); |
542 | else if (SvNIOK (sv) && SvIV (sv) == 1) |
|
|
543 | encode_str (enc, "true", 4, 0); |
|
|
544 | else |
580 | else |
545 | croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", |
581 | croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", |
546 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
582 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
547 | } |
583 | } |
548 | else |
584 | else |
… | |
… | |
618 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
654 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
619 | SvPV_nolen (sv), SvFLAGS (sv)); |
655 | SvPV_nolen (sv), SvFLAGS (sv)); |
620 | } |
656 | } |
621 | |
657 | |
622 | static SV * |
658 | static SV * |
623 | encode_json (SV *scalar, struct json *json) |
659 | encode_json (SV *scalar, JSON *json) |
624 | { |
660 | { |
625 | enc_t enc; |
661 | enc_t enc; |
626 | |
662 | |
627 | if (!(json->flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
663 | if (!(json->flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
628 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
664 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
… | |
… | |
656 | typedef struct |
692 | typedef struct |
657 | { |
693 | { |
658 | char *cur; // current parser pointer |
694 | char *cur; // current parser pointer |
659 | char *end; // end of input string |
695 | char *end; // end of input string |
660 | const char *err; // parse error, if != 0 |
696 | const char *err; // parse error, if != 0 |
661 | struct json json; |
697 | JSON json; |
662 | U32 depth; // recursion depth |
698 | U32 depth; // recursion depth |
663 | U32 maxdepth; // recursion depth limit |
699 | U32 maxdepth; // recursion depth limit |
664 | } dec_t; |
700 | } dec_t; |
665 | |
701 | |
666 | inline void |
702 | inline void |
… | |
… | |
929 | is_nv = 1; |
965 | is_nv = 1; |
930 | } |
966 | } |
931 | |
967 | |
932 | if (!is_nv) |
968 | if (!is_nv) |
933 | { |
969 | { |
|
|
970 | int len = dec->cur - start; |
|
|
971 | |
934 | // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so |
972 | // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so |
935 | if (*start == '-') |
973 | if (*start == '-') |
936 | switch (dec->cur - start) |
974 | switch (len) |
937 | { |
975 | { |
938 | case 2: return newSViv (-( start [1] - '0' * 1)); |
976 | case 2: return newSViv (-( start [1] - '0' * 1)); |
939 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
977 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
940 | case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); |
978 | case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); |
941 | case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
979 | case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
942 | } |
980 | } |
943 | else |
981 | else |
944 | switch (dec->cur - start) |
982 | switch (len) |
945 | { |
983 | { |
946 | case 1: return newSViv ( start [0] - '0' * 1); |
984 | case 1: return newSViv ( start [0] - '0' * 1); |
947 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
985 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
948 | case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); |
986 | case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); |
949 | case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
987 | case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
950 | } |
988 | } |
951 | |
989 | |
952 | { |
990 | { |
953 | UV uv; |
991 | UV uv; |
954 | int numtype = grok_number (start, dec->cur - start, &uv); |
992 | int numtype = grok_number (start, len, &uv); |
955 | if (numtype & IS_NUMBER_IN_UV) |
993 | if (numtype & IS_NUMBER_IN_UV) |
956 | if (numtype & IS_NUMBER_NEG) |
994 | if (numtype & IS_NUMBER_NEG) |
957 | { |
995 | { |
958 | if (uv < (UV)IV_MIN) |
996 | if (uv < (UV)IV_MIN) |
959 | return newSViv (-(IV)uv); |
997 | return newSViv (-(IV)uv); |
960 | } |
998 | } |
961 | else |
999 | else |
962 | return newSVuv (uv); |
1000 | return newSVuv (uv); |
963 | |
|
|
964 | // here would likely be the place for bigint support |
|
|
965 | } |
1001 | } |
966 | } |
|
|
967 | |
1002 | |
968 | // if we ever support bigint or bigfloat, this is the place for bigfloat |
1003 | len -= *start == '-' ? 1 : 0; |
|
|
1004 | |
|
|
1005 | // does not fit into IV or UV, try NV |
|
|
1006 | if ((sizeof (NV) == sizeof (double) && DBL_DIG >= len) |
|
|
1007 | #if defined (LDBL_DIG) |
|
|
1008 | || (sizeof (NV) == sizeof (long double) && LDBL_DIG >= len) |
|
|
1009 | #endif |
|
|
1010 | ) |
|
|
1011 | // fits into NV without loss of precision |
|
|
1012 | return newSVnv (Atof (start)); |
|
|
1013 | |
|
|
1014 | // everything else fails, convert it to a string |
|
|
1015 | return newSVpvn (start, dec->cur - start); |
|
|
1016 | } |
|
|
1017 | |
|
|
1018 | // loss of precision here |
969 | return newSVnv (Atof (start)); |
1019 | return newSVnv (Atof (start)); |
970 | |
1020 | |
971 | fail: |
1021 | fail: |
972 | return 0; |
1022 | return 0; |
973 | } |
1023 | } |
… | |
… | |
1017 | } |
1067 | } |
1018 | |
1068 | |
1019 | static SV * |
1069 | static SV * |
1020 | decode_hv (dec_t *dec) |
1070 | decode_hv (dec_t *dec) |
1021 | { |
1071 | { |
|
|
1072 | SV *sv; |
1022 | HV *hv = newHV (); |
1073 | HV *hv = newHV (); |
1023 | |
1074 | |
1024 | DEC_INC_DEPTH; |
1075 | DEC_INC_DEPTH; |
1025 | decode_ws (dec); |
1076 | decode_ws (dec); |
1026 | |
1077 | |
… | |
… | |
1100 | |
1151 | |
1101 | ++dec->cur; |
1152 | ++dec->cur; |
1102 | } |
1153 | } |
1103 | |
1154 | |
1104 | DEC_DEC_DEPTH; |
1155 | DEC_DEC_DEPTH; |
1105 | return newRV_noinc ((SV *)hv); |
1156 | sv = newRV_noinc ((SV *)hv); |
|
|
1157 | |
|
|
1158 | // check filter callbacks |
|
|
1159 | if (dec->json.flags & F_HOOK) |
|
|
1160 | { |
|
|
1161 | if (dec->json.cb_sk_object && HvKEYS (hv) == 1) |
|
|
1162 | { |
|
|
1163 | HE *cb, *he; |
|
|
1164 | |
|
|
1165 | hv_iterinit (hv); |
|
|
1166 | he = hv_iternext (hv); |
|
|
1167 | hv_iterinit (hv); |
|
|
1168 | |
|
|
1169 | // the next line creates a mortal sv each time its called. |
|
|
1170 | // might want to optimise this for common cases. |
|
|
1171 | cb = hv_fetch_ent (dec->json.cb_sk_object, hv_iterkeysv (he), 0, 0); |
|
|
1172 | |
|
|
1173 | if (cb) |
|
|
1174 | { |
|
|
1175 | dSP; |
|
|
1176 | int count; |
|
|
1177 | |
|
|
1178 | ENTER; SAVETMPS; PUSHMARK (SP); |
|
|
1179 | XPUSHs (HeVAL (he)); |
|
|
1180 | |
|
|
1181 | PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; |
|
|
1182 | |
|
|
1183 | if (count == 1) |
|
|
1184 | { |
|
|
1185 | sv = newSVsv (POPs); |
|
|
1186 | FREETMPS; LEAVE; |
|
|
1187 | return sv; |
|
|
1188 | } |
|
|
1189 | |
|
|
1190 | FREETMPS; LEAVE; |
|
|
1191 | } |
|
|
1192 | } |
|
|
1193 | |
|
|
1194 | if (dec->json.cb_object) |
|
|
1195 | { |
|
|
1196 | dSP; |
|
|
1197 | int count; |
|
|
1198 | |
|
|
1199 | ENTER; SAVETMPS; PUSHMARK (SP); |
|
|
1200 | XPUSHs (sv_2mortal (sv)); |
|
|
1201 | |
|
|
1202 | PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN; |
|
|
1203 | |
|
|
1204 | if (count == 1) |
|
|
1205 | { |
|
|
1206 | sv = newSVsv (POPs); |
|
|
1207 | FREETMPS; LEAVE; |
|
|
1208 | return sv; |
|
|
1209 | } |
|
|
1210 | |
|
|
1211 | SvREFCNT_inc (sv); |
|
|
1212 | FREETMPS; LEAVE; |
|
|
1213 | } |
|
|
1214 | } |
|
|
1215 | |
|
|
1216 | return sv; |
1106 | |
1217 | |
1107 | fail: |
1218 | fail: |
1108 | SvREFCNT_dec (hv); |
1219 | SvREFCNT_dec (hv); |
1109 | DEC_DEC_DEPTH; |
1220 | DEC_DEC_DEPTH; |
1110 | return 0; |
1221 | return 0; |
… | |
… | |
1130 | |
1241 | |
1131 | case 't': |
1242 | case 't': |
1132 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) |
1243 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) |
1133 | { |
1244 | { |
1134 | dec->cur += 4; |
1245 | dec->cur += 4; |
|
|
1246 | #if JSON_SLOW |
|
|
1247 | json_true = get_sv ("JSON::XS::true", 1); SvREADONLY_on (json_true); |
|
|
1248 | #endif |
1135 | return SvREFCNT_inc (json_true); |
1249 | return SvREFCNT_inc (json_true); |
1136 | } |
1250 | } |
1137 | else |
1251 | else |
1138 | ERR ("'true' expected"); |
1252 | ERR ("'true' expected"); |
1139 | |
1253 | |
… | |
… | |
1141 | |
1255 | |
1142 | case 'f': |
1256 | case 'f': |
1143 | if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) |
1257 | if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) |
1144 | { |
1258 | { |
1145 | dec->cur += 5; |
1259 | dec->cur += 5; |
|
|
1260 | #if JSON_SLOW |
|
|
1261 | json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false); |
|
|
1262 | #endif |
1146 | return SvREFCNT_inc (json_false); |
1263 | return SvREFCNT_inc (json_false); |
1147 | } |
1264 | } |
1148 | else |
1265 | else |
1149 | ERR ("'false' expected"); |
1266 | ERR ("'false' expected"); |
1150 | |
1267 | |
… | |
… | |
1169 | fail: |
1286 | fail: |
1170 | return 0; |
1287 | return 0; |
1171 | } |
1288 | } |
1172 | |
1289 | |
1173 | static SV * |
1290 | static SV * |
1174 | decode_json (SV *string, struct json *json, UV *offset_return) |
1291 | decode_json (SV *string, JSON *json, UV *offset_return) |
1175 | { |
1292 | { |
1176 | dec_t dec; |
1293 | dec_t dec; |
1177 | UV offset; |
1294 | UV offset; |
1178 | SV *sv; |
1295 | SV *sv; |
1179 | |
1296 | |
… | |
… | |
1195 | dec.cur = SvPVX (string); |
1312 | dec.cur = SvPVX (string); |
1196 | dec.end = SvEND (string); |
1313 | dec.end = SvEND (string); |
1197 | dec.err = 0; |
1314 | dec.err = 0; |
1198 | dec.depth = 0; |
1315 | dec.depth = 0; |
1199 | dec.maxdepth = DEC_DEPTH (dec.json.flags); |
1316 | dec.maxdepth = DEC_DEPTH (dec.json.flags); |
|
|
1317 | |
|
|
1318 | if (dec.json.cb_object || dec.json.cb_sk_object) |
|
|
1319 | dec.json.flags |= F_HOOK; |
1200 | |
1320 | |
1201 | *dec.end = 0; // this should basically be a nop, too, but make sure it's there |
1321 | *dec.end = 0; // this should basically be a nop, too, but make sure it's there |
1202 | sv = decode_sv (&dec); |
1322 | sv = decode_sv (&dec); |
1203 | |
1323 | |
1204 | if (!(offset_return || !sv)) |
1324 | if (!(offset_return || !sv)) |
… | |
… | |
1274 | json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false); |
1394 | json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false); |
1275 | } |
1395 | } |
1276 | |
1396 | |
1277 | PROTOTYPES: DISABLE |
1397 | PROTOTYPES: DISABLE |
1278 | |
1398 | |
1279 | SV *new (char *dummy) |
1399 | void CLONE (...) |
1280 | CODE: |
1400 | CODE: |
1281 | RETVAL = sv_bless (newRV_noinc (newSVuv (F_DEFAULT)), json_stash); |
1401 | json_stash = 0; |
1282 | OUTPUT: |
1402 | json_boolean_stash = 0; |
1283 | RETVAL |
|
|
1284 | |
1403 | |
|
|
1404 | void new (char *klass) |
|
|
1405 | PPCODE: |
|
|
1406 | { |
|
|
1407 | SV *pv = NEWSV (0, sizeof (JSON)); |
|
|
1408 | SvPOK_only (pv); |
|
|
1409 | Zero (SvPVX (pv), 1, JSON); |
|
|
1410 | ((JSON *)SvPVX (pv))->flags = F_DEFAULT; |
|
|
1411 | XPUSHs (sv_2mortal (sv_bless (newRV_noinc (pv), JSON_STASH))); |
|
|
1412 | } |
|
|
1413 | |
1285 | SV *ascii (SV *self, int enable = 1) |
1414 | void ascii (JSON *self, int enable = 1) |
1286 | ALIAS: |
1415 | ALIAS: |
1287 | ascii = F_ASCII |
1416 | ascii = F_ASCII |
1288 | latin1 = F_LATIN1 |
1417 | latin1 = F_LATIN1 |
1289 | utf8 = F_UTF8 |
1418 | utf8 = F_UTF8 |
1290 | indent = F_INDENT |
1419 | indent = F_INDENT |
… | |
… | |
1294 | pretty = F_PRETTY |
1423 | pretty = F_PRETTY |
1295 | allow_nonref = F_ALLOW_NONREF |
1424 | allow_nonref = F_ALLOW_NONREF |
1296 | shrink = F_SHRINK |
1425 | shrink = F_SHRINK |
1297 | allow_blessed = F_ALLOW_BLESSED |
1426 | allow_blessed = F_ALLOW_BLESSED |
1298 | convert_blessed = F_CONV_BLESSED |
1427 | convert_blessed = F_CONV_BLESSED |
1299 | CODE: |
1428 | PPCODE: |
1300 | { |
1429 | { |
1301 | UV *uv = SvJSON (self); |
|
|
1302 | if (enable) |
1430 | if (enable) |
1303 | *uv |= ix; |
1431 | self->flags |= ix; |
1304 | else |
1432 | else |
1305 | *uv &= ~ix; |
1433 | self->flags &= ~ix; |
1306 | |
1434 | |
1307 | RETVAL = newSVsv (self); |
1435 | XPUSHs (ST (0)); |
1308 | } |
1436 | } |
1309 | OUTPUT: |
|
|
1310 | RETVAL |
|
|
1311 | |
1437 | |
1312 | SV *max_depth (SV *self, UV max_depth = 0x80000000UL) |
1438 | void max_depth (JSON *self, UV max_depth = 0x80000000UL) |
1313 | CODE: |
1439 | PPCODE: |
1314 | { |
1440 | { |
1315 | UV *uv = SvJSON (self); |
|
|
1316 | UV log2 = 0; |
1441 | UV log2 = 0; |
1317 | |
1442 | |
1318 | if (max_depth > 0x80000000UL) max_depth = 0x80000000UL; |
1443 | if (max_depth > 0x80000000UL) max_depth = 0x80000000UL; |
1319 | |
1444 | |
1320 | while ((1UL << log2) < max_depth) |
1445 | while ((1UL << log2) < max_depth) |
1321 | ++log2; |
1446 | ++log2; |
1322 | |
1447 | |
1323 | *uv = *uv & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); |
1448 | self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); |
1324 | |
1449 | |
1325 | RETVAL = newSVsv (self); |
1450 | XPUSHs (ST (0)); |
1326 | } |
1451 | } |
1327 | OUTPUT: |
|
|
1328 | RETVAL |
|
|
1329 | |
1452 | |
1330 | SV *max_size (SV *self, UV max_size = 0) |
1453 | void max_size (JSON *self, UV max_size = 0) |
1331 | CODE: |
1454 | PPCODE: |
1332 | { |
1455 | { |
1333 | UV *uv = SvJSON (self); |
|
|
1334 | UV log2 = 0; |
1456 | UV log2 = 0; |
1335 | |
1457 | |
1336 | if (max_size > 0x80000000UL) max_size = 0x80000000UL; |
1458 | if (max_size > 0x80000000UL) max_size = 0x80000000UL; |
1337 | if (max_size == 1) max_size = 2; |
1459 | if (max_size == 1) max_size = 2; |
1338 | |
1460 | |
1339 | while ((1UL << log2) < max_size) |
1461 | while ((1UL << log2) < max_size) |
1340 | ++log2; |
1462 | ++log2; |
1341 | |
1463 | |
1342 | *uv = *uv & ~F_MAXSIZE | (log2 << S_MAXSIZE); |
1464 | self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE); |
1343 | |
1465 | |
1344 | RETVAL = newSVsv (self); |
1466 | XPUSHs (ST (0)); |
1345 | } |
1467 | } |
1346 | OUTPUT: |
|
|
1347 | RETVAL |
|
|
1348 | |
1468 | |
1349 | void encode (SV *self, SV *scalar) |
1469 | void filter_json_object (JSON *self, SV *cb = &PL_sv_undef) |
1350 | PPCODE: |
1470 | PPCODE: |
1351 | { |
1471 | { |
1352 | struct json json = { *SvJSON (self) }; |
1472 | SvREFCNT_dec (self->cb_object); |
1353 | XPUSHs (encode_json (scalar, &json)); |
1473 | self->cb_object = SvOK (cb) ? newSVsv (cb) : 0; |
1354 | } |
|
|
1355 | |
1474 | |
1356 | void decode (SV *self, SV *jsonstr) |
1475 | XPUSHs (ST (0)); |
|
|
1476 | } |
|
|
1477 | |
|
|
1478 | void filter_json_single_key_object (JSON *self, SV *key, SV *cb = &PL_sv_undef) |
1357 | PPCODE: |
1479 | PPCODE: |
1358 | { |
1480 | { |
1359 | struct json json = { *SvJSON (self) }; |
1481 | if (!self->cb_sk_object) |
1360 | XPUSHs (decode_json (jsonstr, &json, 0)); |
1482 | self->cb_sk_object = newHV (); |
1361 | } |
|
|
1362 | |
1483 | |
1363 | void decode_prefix (SV *self, SV *jsonstr) |
1484 | if (SvOK (cb)) |
|
|
1485 | hv_store_ent (self->cb_sk_object, key, newSVsv (cb), 0); |
|
|
1486 | else |
|
|
1487 | { |
|
|
1488 | hv_delete_ent (self->cb_sk_object, key, G_DISCARD, 0); |
|
|
1489 | |
|
|
1490 | if (!HvKEYS (self->cb_sk_object)) |
|
|
1491 | { |
|
|
1492 | SvREFCNT_dec (self->cb_sk_object); |
|
|
1493 | self->cb_sk_object = 0; |
|
|
1494 | } |
|
|
1495 | } |
|
|
1496 | |
|
|
1497 | XPUSHs (ST (0)); |
|
|
1498 | } |
|
|
1499 | |
|
|
1500 | void encode (JSON *self, SV *scalar) |
1364 | PPCODE: |
1501 | PPCODE: |
|
|
1502 | XPUSHs (encode_json (scalar, self)); |
|
|
1503 | |
|
|
1504 | void decode (JSON *self, SV *jsonstr) |
|
|
1505 | PPCODE: |
|
|
1506 | XPUSHs (decode_json (jsonstr, self, 0)); |
|
|
1507 | |
|
|
1508 | void decode_prefix (JSON *self, SV *jsonstr) |
|
|
1509 | PPCODE: |
1365 | { |
1510 | { |
1366 | UV offset; |
1511 | UV offset; |
1367 | struct json json = { *SvJSON (self) }; |
|
|
1368 | EXTEND (SP, 2); |
1512 | EXTEND (SP, 2); |
1369 | PUSHs (decode_json (jsonstr, &json, &offset)); |
1513 | PUSHs (decode_json (jsonstr, self, &offset)); |
1370 | PUSHs (sv_2mortal (newSVuv (offset))); |
1514 | PUSHs (sv_2mortal (newSVuv (offset))); |
1371 | } |
1515 | } |
|
|
1516 | |
|
|
1517 | void DESTROY (JSON *self) |
|
|
1518 | CODE: |
|
|
1519 | SvREFCNT_dec (self->cb_sk_object); |
|
|
1520 | SvREFCNT_dec (self->cb_object); |
1372 | |
1521 | |
1373 | PROTOTYPES: ENABLE |
1522 | PROTOTYPES: ENABLE |
1374 | |
1523 | |
1375 | void to_json (SV *scalar) |
1524 | void to_json (SV *scalar) |
1376 | PPCODE: |
1525 | PPCODE: |
1377 | { |
1526 | { |
1378 | struct json json = { F_DEFAULT | F_UTF8 }; |
1527 | JSON json = { F_DEFAULT | F_UTF8 }; |
1379 | XPUSHs (encode_json (scalar, &json)); |
1528 | XPUSHs (encode_json (scalar, &json)); |
1380 | } |
1529 | } |
1381 | |
1530 | |
1382 | void from_json (SV *jsonstr) |
1531 | void from_json (SV *jsonstr) |
1383 | PPCODE: |
1532 | PPCODE: |
1384 | { |
1533 | { |
1385 | struct json json = { F_DEFAULT | F_UTF8 }; |
1534 | JSON json = { F_DEFAULT | F_UTF8 }; |
1386 | XPUSHs (decode_json (jsonstr, &json, 0)); |
1535 | XPUSHs (decode_json (jsonstr, &json, 0)); |
1387 | } |
1536 | } |
1388 | |
1537 | |