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 | |
… | |
… | |
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 |
31 | #define F_CONV_BLESSED 0x00000800UL |
|
|
32 | #define F_RELAXED 0x00001000UL |
|
|
33 | |
31 | #define F_MAXDEPTH 0xf8000000UL |
34 | #define F_MAXDEPTH 0xf8000000UL |
32 | #define S_MAXDEPTH 27 |
35 | #define S_MAXDEPTH 27 |
33 | #define F_MAXSIZE 0x01f00000UL |
36 | #define F_MAXSIZE 0x01f00000UL |
34 | #define S_MAXSIZE 20 |
37 | #define S_MAXSIZE 20 |
35 | #define F_HOOK 0x00080000UL // some hooks exist, so slow-path processing |
38 | #define F_HOOK 0x00080000UL // some hooks exist, so slow-path processing |
… | |
… | |
57 | #endif |
60 | #endif |
58 | |
61 | |
59 | #define expect_false(expr) expect ((expr) != 0, 0) |
62 | #define expect_false(expr) expect ((expr) != 0, 0) |
60 | #define expect_true(expr) expect ((expr) != 0, 1) |
63 | #define expect_true(expr) expect ((expr) != 0, 1) |
61 | |
64 | |
|
|
65 | #ifdef USE_ITHREADS |
|
|
66 | # define JSON_SLOW 1 |
|
|
67 | # define JSON_STASH (json_stash ? json_stash : gv_stashpv ("JSON::XS", 1)) |
|
|
68 | #else |
|
|
69 | # define JSON_SLOW 0 |
|
|
70 | # define JSON_STASH json_stash |
|
|
71 | #endif |
|
|
72 | |
62 | static HV *json_stash, *json_boolean_stash; // JSON::XS:: |
73 | static HV *json_stash, *json_boolean_stash; // JSON::XS:: |
63 | static SV *json_true, *json_false; |
74 | static SV *json_true, *json_false; |
64 | |
75 | |
65 | typedef struct { |
76 | typedef struct { |
66 | U32 flags; |
77 | U32 flags; |
67 | SV *cb_object, *cb_sk_object; |
78 | SV *cb_object; |
|
|
79 | HV *cb_sk_object; |
68 | } JSON; |
80 | } JSON; |
69 | |
81 | |
70 | ///////////////////////////////////////////////////////////////////////////// |
82 | ///////////////////////////////////////////////////////////////////////////// |
71 | // utility functions |
83 | // utility functions |
72 | |
84 | |
… | |
… | |
300 | int i, len = av_len (av); |
312 | int i, len = av_len (av); |
301 | |
313 | |
302 | if (enc->indent >= enc->maxdepth) |
314 | if (enc->indent >= enc->maxdepth) |
303 | croak ("data structure too deep (hit recursion limit)"); |
315 | croak ("data structure too deep (hit recursion limit)"); |
304 | |
316 | |
305 | encode_ch (enc, '['); encode_nl (enc); |
317 | encode_ch (enc, '['); |
306 | ++enc->indent; |
318 | |
|
|
319 | if (len >= 0) |
|
|
320 | { |
|
|
321 | encode_nl (enc); ++enc->indent; |
307 | |
322 | |
308 | for (i = 0; i <= len; ++i) |
323 | for (i = 0; i <= len; ++i) |
309 | { |
324 | { |
|
|
325 | SV **svp = av_fetch (av, i, 0); |
|
|
326 | |
310 | encode_indent (enc); |
327 | encode_indent (enc); |
311 | encode_sv (enc, *av_fetch (av, i, 0)); |
|
|
312 | |
328 | |
|
|
329 | if (svp) |
|
|
330 | encode_sv (enc, *svp); |
|
|
331 | else |
|
|
332 | encode_str (enc, "null", 4, 0); |
|
|
333 | |
313 | if (i < len) |
334 | if (i < len) |
314 | encode_comma (enc); |
335 | encode_comma (enc); |
315 | } |
336 | } |
316 | |
337 | |
|
|
338 | encode_nl (enc); --enc->indent; encode_indent (enc); |
|
|
339 | } |
|
|
340 | |
317 | encode_nl (enc); |
341 | encode_ch (enc, ']'); |
318 | |
|
|
319 | --enc->indent; |
|
|
320 | encode_indent (enc); encode_ch (enc, ']'); |
|
|
321 | } |
342 | } |
322 | |
343 | |
323 | static void |
344 | static void |
324 | encode_he (enc_t *enc, HE *he) |
345 | encode_hk (enc_t *enc, HE *he) |
325 | { |
346 | { |
326 | encode_ch (enc, '"'); |
347 | encode_ch (enc, '"'); |
327 | |
348 | |
328 | if (HeKLEN (he) == HEf_SVKEY) |
349 | if (HeKLEN (he) == HEf_SVKEY) |
329 | { |
350 | { |
… | |
… | |
342 | encode_ch (enc, '"'); |
363 | encode_ch (enc, '"'); |
343 | |
364 | |
344 | if (enc->json.flags & F_SPACE_BEFORE) encode_space (enc); |
365 | if (enc->json.flags & F_SPACE_BEFORE) encode_space (enc); |
345 | encode_ch (enc, ':'); |
366 | encode_ch (enc, ':'); |
346 | if (enc->json.flags & F_SPACE_AFTER ) encode_space (enc); |
367 | if (enc->json.flags & F_SPACE_AFTER ) encode_space (enc); |
347 | encode_sv (enc, HeVAL (he)); |
|
|
348 | } |
368 | } |
349 | |
369 | |
350 | // compare hash entries, used when all keys are bytestrings |
370 | // compare hash entries, used when all keys are bytestrings |
351 | static int |
371 | static int |
352 | he_cmp_fast (const void *a_, const void *b_) |
372 | he_cmp_fast (const void *a_, const void *b_) |
… | |
… | |
357 | HE *b = *(HE **)b_; |
377 | HE *b = *(HE **)b_; |
358 | |
378 | |
359 | STRLEN la = HeKLEN (a); |
379 | STRLEN la = HeKLEN (a); |
360 | STRLEN lb = HeKLEN (b); |
380 | STRLEN lb = HeKLEN (b); |
361 | |
381 | |
362 | if (!(cmp = memcmp (HeKEY (a), HeKEY (b), la < lb ? la : lb))) |
382 | if (!(cmp = memcmp (HeKEY (b), HeKEY (a), lb < la ? lb : la))) |
363 | cmp = la - lb; |
383 | cmp = lb - la; |
364 | |
384 | |
365 | return cmp; |
385 | return cmp; |
366 | } |
386 | } |
367 | |
387 | |
368 | // compare hash entries, used when some keys are sv's or utf-x |
388 | // compare hash entries, used when some keys are sv's or utf-x |
369 | static int |
389 | static int |
370 | he_cmp_slow (const void *a, const void *b) |
390 | he_cmp_slow (const void *a, const void *b) |
371 | { |
391 | { |
372 | return sv_cmp (HeSVKEY_force (*(HE **)a), HeSVKEY_force (*(HE **)b)); |
392 | return sv_cmp (HeSVKEY_force (*(HE **)b), HeSVKEY_force (*(HE **)a)); |
373 | } |
393 | } |
374 | |
394 | |
375 | static void |
395 | static void |
376 | encode_hv (enc_t *enc, HV *hv) |
396 | encode_hv (enc_t *enc, HV *hv) |
377 | { |
397 | { |
|
|
398 | HE *he; |
378 | int count, i; |
399 | int count; |
379 | |
400 | |
380 | if (enc->indent >= enc->maxdepth) |
401 | if (enc->indent >= enc->maxdepth) |
381 | croak ("data structure too deep (hit recursion limit)"); |
402 | croak ("data structure too deep (hit recursion limit)"); |
382 | |
403 | |
383 | encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; |
404 | encode_ch (enc, '{'); |
384 | |
405 | |
385 | if ((count = hv_iterinit (hv))) |
|
|
386 | { |
|
|
387 | // for canonical output we have to sort by keys first |
406 | // for canonical output we have to sort by keys first |
388 | // actually, this is mostly due to the stupid so-called |
407 | // actually, this is mostly due to the stupid so-called |
389 | // security workaround added somewhere in 5.8.x. |
408 | // security workaround added somewhere in 5.8.x. |
390 | // that randomises hash orderings |
409 | // that randomises hash orderings |
391 | if (enc->json.flags & F_CANONICAL) |
410 | if (enc->json.flags & F_CANONICAL) |
|
|
411 | { |
|
|
412 | int count = hv_iterinit (hv); |
|
|
413 | |
|
|
414 | if (SvMAGICAL (hv)) |
392 | { |
415 | { |
|
|
416 | // need to count by iterating. could improve by dynamically building the vector below |
|
|
417 | // but I don't care for the speed of this special case. |
|
|
418 | // note also that we will run into undefined behaviour when the two iterations |
|
|
419 | // do not result in the same count, something I might care for in some later release. |
|
|
420 | |
|
|
421 | count = 0; |
|
|
422 | while (hv_iternext (hv)) |
|
|
423 | ++count; |
|
|
424 | |
|
|
425 | hv_iterinit (hv); |
|
|
426 | } |
|
|
427 | |
|
|
428 | if (count) |
|
|
429 | { |
393 | int fast = 1; |
430 | int i, fast = 1; |
394 | HE *he; |
|
|
395 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
431 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
396 | HE **hes = _alloca (count * sizeof (HE)); |
432 | HE **hes = _alloca (count * sizeof (HE)); |
397 | #else |
433 | #else |
398 | HE *hes [count]; // if your compiler dies here, you need to enable C99 mode |
434 | HE *hes [count]; // if your compiler dies here, you need to enable C99 mode |
399 | #endif |
435 | #endif |
… | |
… | |
426 | |
462 | |
427 | FREETMPS; |
463 | FREETMPS; |
428 | LEAVE; |
464 | LEAVE; |
429 | } |
465 | } |
430 | |
466 | |
431 | for (i = 0; i < count; ++i) |
467 | encode_nl (enc); ++enc->indent; |
|
|
468 | |
|
|
469 | while (count--) |
432 | { |
470 | { |
433 | encode_indent (enc); |
471 | encode_indent (enc); |
|
|
472 | he = hes [count]; |
434 | encode_he (enc, hes [i]); |
473 | encode_hk (enc, he); |
|
|
474 | encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he)); |
435 | |
475 | |
436 | if (i < count - 1) |
476 | if (count) |
437 | encode_comma (enc); |
477 | encode_comma (enc); |
438 | } |
478 | } |
439 | |
479 | |
440 | encode_nl (enc); |
480 | encode_nl (enc); --enc->indent; encode_indent (enc); |
441 | } |
481 | } |
|
|
482 | } |
442 | else |
483 | else |
|
|
484 | { |
|
|
485 | if (hv_iterinit (hv) || SvMAGICAL (hv)) |
|
|
486 | if ((he = hv_iternext (hv))) |
443 | { |
487 | { |
444 | HE *he = hv_iternext (hv); |
488 | encode_nl (enc); ++enc->indent; |
445 | |
489 | |
446 | for (;;) |
490 | for (;;) |
447 | { |
491 | { |
448 | encode_indent (enc); |
492 | encode_indent (enc); |
449 | encode_he (enc, he); |
493 | encode_hk (enc, he); |
|
|
494 | encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he)); |
450 | |
495 | |
451 | if (!(he = hv_iternext (hv))) |
496 | if (!(he = hv_iternext (hv))) |
452 | break; |
497 | break; |
453 | |
498 | |
454 | encode_comma (enc); |
499 | encode_comma (enc); |
455 | } |
500 | } |
456 | |
501 | |
457 | encode_nl (enc); |
502 | encode_nl (enc); --enc->indent; encode_indent (enc); |
458 | } |
503 | } |
459 | } |
504 | } |
460 | |
505 | |
461 | --enc->indent; encode_indent (enc); encode_ch (enc, '}'); |
506 | encode_ch (enc, '}'); |
462 | } |
507 | } |
463 | |
508 | |
464 | // encode objects, arrays and special \0=false and \1=true values. |
509 | // encode objects, arrays and special \0=false and \1=true values. |
465 | static void |
510 | static void |
466 | encode_rv (enc_t *enc, SV *sv) |
511 | encode_rv (enc_t *enc, SV *sv) |
… | |
… | |
470 | SvGETMAGIC (sv); |
515 | SvGETMAGIC (sv); |
471 | svt = SvTYPE (sv); |
516 | svt = SvTYPE (sv); |
472 | |
517 | |
473 | if (expect_false (SvOBJECT (sv))) |
518 | if (expect_false (SvOBJECT (sv))) |
474 | { |
519 | { |
|
|
520 | HV *stash = !JSON_SLOW || json_boolean_stash |
|
|
521 | ? json_boolean_stash |
|
|
522 | : gv_stashpv ("JSON::XS::Boolean", 1); |
|
|
523 | |
475 | if (SvSTASH (sv) == json_boolean_stash) |
524 | if (SvSTASH (sv) == stash) |
476 | { |
525 | { |
477 | if (SvIV (sv) == 0) |
526 | if (SvIV (sv)) |
|
|
527 | encode_str (enc, "true", 4, 0); |
|
|
528 | else |
478 | encode_str (enc, "false", 5, 0); |
529 | encode_str (enc, "false", 5, 0); |
479 | else |
|
|
480 | encode_str (enc, "true", 4, 0); |
|
|
481 | } |
530 | } |
482 | else |
531 | else |
483 | { |
532 | { |
484 | #if 0 |
533 | #if 0 |
485 | if (0 && sv_derived_from (rv, "JSON::Literal")) |
534 | if (0 && sv_derived_from (rv, "JSON::Literal")) |
… | |
… | |
488 | } |
537 | } |
489 | #endif |
538 | #endif |
490 | if (enc->json.flags & F_CONV_BLESSED) |
539 | if (enc->json.flags & F_CONV_BLESSED) |
491 | { |
540 | { |
492 | // we re-bless the reference to get overload and other niceties right |
541 | // we re-bless the reference to get overload and other niceties right |
493 | GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 1); |
542 | GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 0); |
494 | |
543 | |
495 | if (to_json) |
544 | if (to_json) |
496 | { |
545 | { |
|
|
546 | dSP; |
|
|
547 | |
497 | dSP; ENTER; SAVETMPS; PUSHMARK (SP); |
548 | ENTER; SAVETMPS; PUSHMARK (SP); |
498 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); |
549 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); |
499 | |
550 | |
500 | // calling with G_SCALAR ensures that we always get a 1 reutrn value |
551 | // calling with G_SCALAR ensures that we always get a 1 return value |
501 | // check anyways. |
|
|
502 | PUTBACK; |
552 | PUTBACK; |
503 | assert (1 == call_sv ((SV *)GvCV (to_json), G_SCALAR)); |
553 | call_sv ((SV *)GvCV (to_json), G_SCALAR); |
504 | SPAGAIN; |
554 | SPAGAIN; |
505 | |
555 | |
|
|
556 | // catch this surprisingly common error |
|
|
557 | if (SvROK (TOPs) && SvRV (TOPs) == sv) |
|
|
558 | croak ("%s::TO_JSON method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); |
|
|
559 | |
|
|
560 | sv = POPs; |
|
|
561 | PUTBACK; |
|
|
562 | |
506 | encode_sv (enc, POPs); |
563 | encode_sv (enc, sv); |
507 | |
564 | |
508 | FREETMPS; LEAVE; |
565 | FREETMPS; LEAVE; |
509 | } |
566 | } |
510 | else if (enc->json.flags & F_ALLOW_BLESSED) |
567 | else if (enc->json.flags & F_ALLOW_BLESSED) |
511 | encode_str (enc, "null", 4, 0); |
568 | encode_str (enc, "null", 4, 0); |
… | |
… | |
524 | encode_hv (enc, (HV *)sv); |
581 | encode_hv (enc, (HV *)sv); |
525 | else if (svt == SVt_PVAV) |
582 | else if (svt == SVt_PVAV) |
526 | encode_av (enc, (AV *)sv); |
583 | encode_av (enc, (AV *)sv); |
527 | else if (svt < SVt_PVAV) |
584 | else if (svt < SVt_PVAV) |
528 | { |
585 | { |
529 | if (SvNIOK (sv) && SvIV (sv) == 0) |
586 | STRLEN len = 0; |
|
|
587 | char *pv = svt ? SvPV (sv, len) : 0; |
|
|
588 | |
|
|
589 | if (len == 1 && *pv == '1') |
|
|
590 | encode_str (enc, "true", 4, 0); |
|
|
591 | else if (len == 1 && *pv == '0') |
530 | encode_str (enc, "false", 5, 0); |
592 | encode_str (enc, "false", 5, 0); |
531 | else if (SvNIOK (sv) && SvIV (sv) == 1) |
|
|
532 | encode_str (enc, "true", 4, 0); |
|
|
533 | else |
593 | else |
534 | croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", |
594 | croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", |
535 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
595 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
536 | } |
596 | } |
537 | else |
597 | else |
… | |
… | |
651 | U32 depth; // recursion depth |
711 | U32 depth; // recursion depth |
652 | U32 maxdepth; // recursion depth limit |
712 | U32 maxdepth; // recursion depth limit |
653 | } dec_t; |
713 | } dec_t; |
654 | |
714 | |
655 | inline void |
715 | inline void |
|
|
716 | decode_comment (dec_t *dec) |
|
|
717 | { |
|
|
718 | // only '#'-style comments allowed a.t.m. |
|
|
719 | |
|
|
720 | while (*dec->cur && *dec->cur != 0x0a && *dec->cur != 0x0d) |
|
|
721 | ++dec->cur; |
|
|
722 | } |
|
|
723 | |
|
|
724 | inline void |
656 | decode_ws (dec_t *dec) |
725 | decode_ws (dec_t *dec) |
657 | { |
726 | { |
658 | for (;;) |
727 | for (;;) |
659 | { |
728 | { |
660 | char ch = *dec->cur; |
729 | char ch = *dec->cur; |
661 | |
730 | |
662 | if (ch > 0x20 |
731 | if (ch > 0x20) |
|
|
732 | { |
|
|
733 | if (expect_false (ch == '#')) |
|
|
734 | { |
|
|
735 | if (dec->json.flags & F_RELAXED) |
|
|
736 | decode_comment (dec); |
|
|
737 | else |
|
|
738 | break; |
|
|
739 | } |
|
|
740 | else |
|
|
741 | break; |
|
|
742 | } |
663 | || (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)) |
743 | else if (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09) |
664 | break; |
744 | break; // parse error, but let higher level handle it, gives better error messages |
665 | |
745 | |
666 | ++dec->cur; |
746 | ++dec->cur; |
667 | } |
747 | } |
668 | } |
748 | } |
669 | |
749 | |
… | |
… | |
918 | is_nv = 1; |
998 | is_nv = 1; |
919 | } |
999 | } |
920 | |
1000 | |
921 | if (!is_nv) |
1001 | if (!is_nv) |
922 | { |
1002 | { |
|
|
1003 | int len = dec->cur - start; |
|
|
1004 | |
923 | // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so |
1005 | // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so |
924 | if (*start == '-') |
1006 | if (*start == '-') |
925 | switch (dec->cur - start) |
1007 | switch (len) |
926 | { |
1008 | { |
927 | case 2: return newSViv (-( start [1] - '0' * 1)); |
1009 | case 2: return newSViv (-( start [1] - '0' * 1)); |
928 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
1010 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
929 | case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); |
1011 | case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); |
930 | case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
1012 | case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
931 | } |
1013 | } |
932 | else |
1014 | else |
933 | switch (dec->cur - start) |
1015 | switch (len) |
934 | { |
1016 | { |
935 | case 1: return newSViv ( start [0] - '0' * 1); |
1017 | case 1: return newSViv ( start [0] - '0' * 1); |
936 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
1018 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
937 | case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); |
1019 | case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); |
938 | case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
1020 | case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
939 | } |
1021 | } |
940 | |
1022 | |
941 | { |
1023 | { |
942 | UV uv; |
1024 | UV uv; |
943 | int numtype = grok_number (start, dec->cur - start, &uv); |
1025 | int numtype = grok_number (start, len, &uv); |
944 | if (numtype & IS_NUMBER_IN_UV) |
1026 | if (numtype & IS_NUMBER_IN_UV) |
945 | if (numtype & IS_NUMBER_NEG) |
1027 | if (numtype & IS_NUMBER_NEG) |
946 | { |
1028 | { |
947 | if (uv < (UV)IV_MIN) |
1029 | if (uv < (UV)IV_MIN) |
948 | return newSViv (-(IV)uv); |
1030 | return newSViv (-(IV)uv); |
949 | } |
1031 | } |
950 | else |
1032 | else |
951 | return newSVuv (uv); |
1033 | return newSVuv (uv); |
952 | |
|
|
953 | // here would likely be the place for bigint support |
|
|
954 | } |
1034 | } |
955 | } |
|
|
956 | |
1035 | |
957 | // if we ever support bigint or bigfloat, this is the place for bigfloat |
1036 | len -= *start == '-' ? 1 : 0; |
|
|
1037 | |
|
|
1038 | // does not fit into IV or UV, try NV |
|
|
1039 | if ((sizeof (NV) == sizeof (double) && DBL_DIG >= len) |
|
|
1040 | #if defined (LDBL_DIG) |
|
|
1041 | || (sizeof (NV) == sizeof (long double) && LDBL_DIG >= len) |
|
|
1042 | #endif |
|
|
1043 | ) |
|
|
1044 | // fits into NV without loss of precision |
|
|
1045 | return newSVnv (Atof (start)); |
|
|
1046 | |
|
|
1047 | // everything else fails, convert it to a string |
|
|
1048 | return newSVpvn (start, dec->cur - start); |
|
|
1049 | } |
|
|
1050 | |
|
|
1051 | // loss of precision here |
958 | return newSVnv (Atof (start)); |
1052 | return newSVnv (Atof (start)); |
959 | |
1053 | |
960 | fail: |
1054 | fail: |
961 | return 0; |
1055 | return 0; |
962 | } |
1056 | } |
… | |
… | |
992 | |
1086 | |
993 | if (*dec->cur != ',') |
1087 | if (*dec->cur != ',') |
994 | ERR (", or ] expected while parsing array"); |
1088 | ERR (", or ] expected while parsing array"); |
995 | |
1089 | |
996 | ++dec->cur; |
1090 | ++dec->cur; |
|
|
1091 | |
|
|
1092 | decode_ws (dec); |
|
|
1093 | |
|
|
1094 | if (*dec->cur == ']' && dec->json.flags & F_RELAXED) |
|
|
1095 | { |
|
|
1096 | ++dec->cur; |
|
|
1097 | break; |
|
|
1098 | } |
997 | } |
1099 | } |
998 | |
1100 | |
999 | DEC_DEC_DEPTH; |
1101 | DEC_DEC_DEPTH; |
1000 | return newRV_noinc ((SV *)av); |
1102 | return newRV_noinc ((SV *)av); |
1001 | |
1103 | |
… | |
… | |
1017 | if (*dec->cur == '}') |
1119 | if (*dec->cur == '}') |
1018 | ++dec->cur; |
1120 | ++dec->cur; |
1019 | else |
1121 | else |
1020 | for (;;) |
1122 | for (;;) |
1021 | { |
1123 | { |
1022 | decode_ws (dec); EXPECT_CH ('"'); |
1124 | EXPECT_CH ('"'); |
1023 | |
1125 | |
1024 | // heuristic: assume that |
1126 | // heuristic: assume that |
1025 | // a) decode_str + hv_store_ent are abysmally slow. |
1127 | // a) decode_str + hv_store_ent are abysmally slow. |
1026 | // b) most hash keys are short, simple ascii text. |
1128 | // b) most hash keys are short, simple ascii text. |
1027 | // => try to "fast-match" such strings to avoid |
1129 | // => try to "fast-match" such strings to avoid |
… | |
… | |
1041 | if (!key) |
1143 | if (!key) |
1042 | goto fail; |
1144 | goto fail; |
1043 | |
1145 | |
1044 | decode_ws (dec); EXPECT_CH (':'); |
1146 | decode_ws (dec); EXPECT_CH (':'); |
1045 | |
1147 | |
|
|
1148 | decode_ws (dec); |
1046 | value = decode_sv (dec); |
1149 | value = decode_sv (dec); |
1047 | if (!value) |
1150 | if (!value) |
1048 | { |
1151 | { |
1049 | SvREFCNT_dec (key); |
1152 | SvREFCNT_dec (key); |
1050 | goto fail; |
1153 | goto fail; |
… | |
… | |
1062 | int len = p - key; |
1165 | int len = p - key; |
1063 | dec->cur = p + 1; |
1166 | dec->cur = p + 1; |
1064 | |
1167 | |
1065 | decode_ws (dec); EXPECT_CH (':'); |
1168 | decode_ws (dec); EXPECT_CH (':'); |
1066 | |
1169 | |
|
|
1170 | decode_ws (dec); |
1067 | value = decode_sv (dec); |
1171 | value = decode_sv (dec); |
1068 | if (!value) |
1172 | if (!value) |
1069 | goto fail; |
1173 | goto fail; |
1070 | |
1174 | |
1071 | hv_store (hv, key, len, value, 0); |
1175 | hv_store (hv, key, len, value, 0); |
… | |
… | |
1087 | |
1191 | |
1088 | if (*dec->cur != ',') |
1192 | if (*dec->cur != ',') |
1089 | ERR (", or } expected while parsing object/hash"); |
1193 | ERR (", or } expected while parsing object/hash"); |
1090 | |
1194 | |
1091 | ++dec->cur; |
1195 | ++dec->cur; |
|
|
1196 | |
|
|
1197 | decode_ws (dec); |
|
|
1198 | |
|
|
1199 | if (*dec->cur == '}' && dec->json.flags & F_RELAXED) |
|
|
1200 | { |
|
|
1201 | ++dec->cur; |
|
|
1202 | break; |
|
|
1203 | } |
1092 | } |
1204 | } |
1093 | |
1205 | |
1094 | DEC_DEC_DEPTH; |
1206 | DEC_DEC_DEPTH; |
1095 | sv = newRV_noinc ((SV *)hv); |
1207 | sv = newRV_noinc ((SV *)hv); |
1096 | |
1208 | |
1097 | // check filter callbacks |
1209 | // check filter callbacks |
1098 | if (dec->json.flags & F_HOOK) |
1210 | if (dec->json.flags & F_HOOK) |
1099 | { |
1211 | { |
1100 | ENTER; SAVETMPS; |
1212 | if (dec->json.cb_sk_object && HvKEYS (hv) == 1) |
1101 | |
|
|
1102 | if (HvKEYS (hv) == 1 && dec->json.cb_sk_object) |
|
|
1103 | { |
1213 | { |
|
|
1214 | HE *cb, *he; |
|
|
1215 | |
|
|
1216 | hv_iterinit (hv); |
|
|
1217 | he = hv_iternext (hv); |
|
|
1218 | hv_iterinit (hv); |
|
|
1219 | |
|
|
1220 | // the next line creates a mortal sv each time its called. |
|
|
1221 | // might want to optimise this for common cases. |
|
|
1222 | cb = hv_fetch_ent (dec->json.cb_sk_object, hv_iterkeysv (he), 0, 0); |
|
|
1223 | |
|
|
1224 | if (cb) |
|
|
1225 | { |
|
|
1226 | dSP; |
1104 | int count; |
1227 | int count; |
1105 | |
1228 | |
1106 | dSP; PUSHMARK (SP); |
1229 | ENTER; SAVETMPS; PUSHMARK (SP); |
1107 | XPUSHs (sv_2mortal (sv)); |
1230 | XPUSHs (HeVAL (he)); |
1108 | |
1231 | |
1109 | PUTBACK; count = call_sv (dec->json.cb_sk_object, G_ARRAY); SPAGAIN; |
1232 | PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; |
1110 | |
1233 | |
1111 | if (count == 1) |
1234 | if (count == 1) |
|
|
1235 | { |
1112 | sv = newSVsv (POPs); |
1236 | sv = newSVsv (POPs); |
1113 | else |
1237 | FREETMPS; LEAVE; |
1114 | SvREFCNT_inc (sv); |
1238 | return sv; |
|
|
1239 | } |
|
|
1240 | |
|
|
1241 | FREETMPS; LEAVE; |
|
|
1242 | } |
1115 | } |
1243 | } |
1116 | |
1244 | |
1117 | if (dec->json.cb_object) |
1245 | if (dec->json.cb_object) |
1118 | { |
1246 | { |
|
|
1247 | dSP; |
1119 | int count; |
1248 | int count; |
1120 | |
1249 | |
1121 | dSP; ENTER; SAVETMPS; PUSHMARK (SP); |
1250 | ENTER; SAVETMPS; PUSHMARK (SP); |
1122 | XPUSHs (sv_2mortal (sv)); |
1251 | XPUSHs (sv_2mortal (sv)); |
1123 | |
1252 | |
1124 | PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN; |
1253 | PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN; |
1125 | |
1254 | |
1126 | if (count == 1) |
1255 | if (count == 1) |
|
|
1256 | { |
1127 | sv = newSVsv (POPs); |
1257 | sv = newSVsv (POPs); |
1128 | else |
1258 | FREETMPS; LEAVE; |
|
|
1259 | return sv; |
|
|
1260 | } |
|
|
1261 | |
1129 | SvREFCNT_inc (sv); |
1262 | SvREFCNT_inc (sv); |
|
|
1263 | FREETMPS; LEAVE; |
1130 | } |
1264 | } |
1131 | |
|
|
1132 | FREETMPS; LEAVE; |
|
|
1133 | } |
1265 | } |
1134 | |
1266 | |
1135 | return newRV_noinc ((SV *)hv); |
1267 | return sv; |
1136 | |
1268 | |
1137 | fail: |
1269 | fail: |
1138 | SvREFCNT_dec (hv); |
1270 | SvREFCNT_dec (hv); |
1139 | DEC_DEC_DEPTH; |
1271 | DEC_DEC_DEPTH; |
1140 | return 0; |
1272 | return 0; |
1141 | } |
1273 | } |
1142 | |
1274 | |
1143 | static SV * |
1275 | static SV * |
1144 | decode_sv (dec_t *dec) |
1276 | decode_sv (dec_t *dec) |
1145 | { |
1277 | { |
1146 | decode_ws (dec); |
|
|
1147 | |
|
|
1148 | // the beauty of JSON: you need exactly one character lookahead |
1278 | // the beauty of JSON: you need exactly one character lookahead |
1149 | // to parse anything. |
1279 | // to parse anything. |
1150 | switch (*dec->cur) |
1280 | switch (*dec->cur) |
1151 | { |
1281 | { |
1152 | case '"': ++dec->cur; return decode_str (dec); |
1282 | case '"': ++dec->cur; return decode_str (dec); |
… | |
… | |
1160 | |
1290 | |
1161 | case 't': |
1291 | case 't': |
1162 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) |
1292 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) |
1163 | { |
1293 | { |
1164 | dec->cur += 4; |
1294 | dec->cur += 4; |
|
|
1295 | #if JSON_SLOW |
|
|
1296 | json_true = get_sv ("JSON::XS::true", 1); SvREADONLY_on (json_true); |
|
|
1297 | #endif |
1165 | return SvREFCNT_inc (json_true); |
1298 | return SvREFCNT_inc (json_true); |
1166 | } |
1299 | } |
1167 | else |
1300 | else |
1168 | ERR ("'true' expected"); |
1301 | ERR ("'true' expected"); |
1169 | |
1302 | |
… | |
… | |
1171 | |
1304 | |
1172 | case 'f': |
1305 | case 'f': |
1173 | if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) |
1306 | if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) |
1174 | { |
1307 | { |
1175 | dec->cur += 5; |
1308 | dec->cur += 5; |
|
|
1309 | #if JSON_SLOW |
|
|
1310 | json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false); |
|
|
1311 | #endif |
1176 | return SvREFCNT_inc (json_false); |
1312 | return SvREFCNT_inc (json_false); |
1177 | } |
1313 | } |
1178 | else |
1314 | else |
1179 | ERR ("'false' expected"); |
1315 | ERR ("'false' expected"); |
1180 | |
1316 | |
… | |
… | |
1230 | |
1366 | |
1231 | if (dec.json.cb_object || dec.json.cb_sk_object) |
1367 | if (dec.json.cb_object || dec.json.cb_sk_object) |
1232 | dec.json.flags |= F_HOOK; |
1368 | dec.json.flags |= F_HOOK; |
1233 | |
1369 | |
1234 | *dec.end = 0; // this should basically be a nop, too, but make sure it's there |
1370 | *dec.end = 0; // this should basically be a nop, too, but make sure it's there |
|
|
1371 | |
|
|
1372 | decode_ws (&dec); |
1235 | sv = decode_sv (&dec); |
1373 | sv = decode_sv (&dec); |
1236 | |
1374 | |
1237 | if (!(offset_return || !sv)) |
1375 | if (!(offset_return || !sv)) |
1238 | { |
1376 | { |
1239 | // check for trailing garbage |
1377 | // check for trailing garbage |
… | |
… | |
1307 | json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false); |
1445 | json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false); |
1308 | } |
1446 | } |
1309 | |
1447 | |
1310 | PROTOTYPES: DISABLE |
1448 | PROTOTYPES: DISABLE |
1311 | |
1449 | |
|
|
1450 | void CLONE (...) |
|
|
1451 | CODE: |
|
|
1452 | json_stash = 0; |
|
|
1453 | json_boolean_stash = 0; |
|
|
1454 | |
1312 | void new (char *klass) |
1455 | void new (char *klass) |
1313 | PPCODE: |
1456 | PPCODE: |
1314 | { |
1457 | { |
1315 | SV *pv = NEWSV (0, sizeof (JSON)); |
1458 | SV *pv = NEWSV (0, sizeof (JSON)); |
1316 | SvPOK_only (pv); |
1459 | SvPOK_only (pv); |
1317 | Zero (SvPVX (pv), 1, JSON); |
1460 | Zero (SvPVX (pv), 1, JSON); |
1318 | ((JSON *)SvPVX (pv))->flags = F_DEFAULT; |
1461 | ((JSON *)SvPVX (pv))->flags = F_DEFAULT; |
1319 | XPUSHs (sv_2mortal (sv_bless (newRV_noinc (pv), json_stash))); |
1462 | XPUSHs (sv_2mortal (sv_bless (newRV_noinc (pv), JSON_STASH))); |
1320 | } |
1463 | } |
1321 | |
1464 | |
1322 | void ascii (JSON *self, int enable = 1) |
1465 | void ascii (JSON *self, int enable = 1) |
1323 | ALIAS: |
1466 | ALIAS: |
1324 | ascii = F_ASCII |
1467 | ascii = F_ASCII |
… | |
… | |
1331 | pretty = F_PRETTY |
1474 | pretty = F_PRETTY |
1332 | allow_nonref = F_ALLOW_NONREF |
1475 | allow_nonref = F_ALLOW_NONREF |
1333 | shrink = F_SHRINK |
1476 | shrink = F_SHRINK |
1334 | allow_blessed = F_ALLOW_BLESSED |
1477 | allow_blessed = F_ALLOW_BLESSED |
1335 | convert_blessed = F_CONV_BLESSED |
1478 | convert_blessed = F_CONV_BLESSED |
|
|
1479 | relaxed = F_RELAXED |
1336 | PPCODE: |
1480 | PPCODE: |
1337 | { |
1481 | { |
1338 | if (enable) |
1482 | if (enable) |
1339 | self->flags |= ix; |
1483 | self->flags |= ix; |
1340 | else |
1484 | else |
1341 | self->flags &= ~ix; |
1485 | self->flags &= ~ix; |
1342 | |
1486 | |
1343 | XPUSHs (ST (0)); |
1487 | XPUSHs (ST (0)); |
1344 | } |
1488 | } |
1345 | |
1489 | |
|
|
1490 | void get_ascii (JSON *self) |
|
|
1491 | ALIAS: |
|
|
1492 | get_ascii = F_ASCII |
|
|
1493 | get_latin1 = F_LATIN1 |
|
|
1494 | get_utf8 = F_UTF8 |
|
|
1495 | get_indent = F_INDENT |
|
|
1496 | get_canonical = F_CANONICAL |
|
|
1497 | get_space_before = F_SPACE_BEFORE |
|
|
1498 | get_space_after = F_SPACE_AFTER |
|
|
1499 | get_pretty = F_PRETTY |
|
|
1500 | get_allow_nonref = F_ALLOW_NONREF |
|
|
1501 | get_shrink = F_SHRINK |
|
|
1502 | get_allow_blessed = F_ALLOW_BLESSED |
|
|
1503 | get_convert_blessed = F_CONV_BLESSED |
|
|
1504 | get_relaxed = F_RELAXED |
|
|
1505 | PPCODE: |
|
|
1506 | XPUSHs (boolSV (self->flags & ix)); |
|
|
1507 | |
1346 | void max_depth (JSON *self, UV max_depth = 0x80000000UL) |
1508 | void max_depth (JSON *self, UV max_depth = 0x80000000UL) |
1347 | PPCODE: |
1509 | PPCODE: |
1348 | { |
1510 | { |
1349 | UV log2 = 0; |
1511 | UV log2 = 0; |
1350 | |
1512 | |
… | |
… | |
1356 | self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); |
1518 | self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); |
1357 | |
1519 | |
1358 | XPUSHs (ST (0)); |
1520 | XPUSHs (ST (0)); |
1359 | } |
1521 | } |
1360 | |
1522 | |
|
|
1523 | U32 get_max_depth (JSON *self) |
|
|
1524 | CODE: |
|
|
1525 | RETVAL = DEC_DEPTH (self->flags); |
|
|
1526 | OUTPUT: |
|
|
1527 | RETVAL |
|
|
1528 | |
1361 | void max_size (JSON *self, UV max_size = 0) |
1529 | void max_size (JSON *self, UV max_size = 0) |
1362 | PPCODE: |
1530 | PPCODE: |
1363 | { |
1531 | { |
1364 | UV log2 = 0; |
1532 | UV log2 = 0; |
1365 | |
1533 | |
… | |
… | |
1372 | self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE); |
1540 | self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE); |
1373 | |
1541 | |
1374 | XPUSHs (ST (0)); |
1542 | XPUSHs (ST (0)); |
1375 | } |
1543 | } |
1376 | |
1544 | |
|
|
1545 | int get_max_size (JSON *self) |
|
|
1546 | CODE: |
|
|
1547 | RETVAL = DEC_SIZE (self->flags); |
|
|
1548 | OUTPUT: |
|
|
1549 | RETVAL |
|
|
1550 | |
1377 | void filter_json_objects (JSON *self, SV *cb = &PL_sv_undef) |
1551 | void filter_json_object (JSON *self, SV *cb = &PL_sv_undef) |
1378 | ALIAS: |
|
|
1379 | filter_sk_json_objects = 1 |
|
|
1380 | PPCODE: |
1552 | PPCODE: |
1381 | { |
1553 | { |
|
|
1554 | SvREFCNT_dec (self->cb_object); |
|
|
1555 | self->cb_object = SvOK (cb) ? newSVsv (cb) : 0; |
|
|
1556 | |
|
|
1557 | XPUSHs (ST (0)); |
|
|
1558 | } |
|
|
1559 | |
|
|
1560 | void filter_json_single_key_object (JSON *self, SV *key, SV *cb = &PL_sv_undef) |
|
|
1561 | PPCODE: |
|
|
1562 | { |
|
|
1563 | if (!self->cb_sk_object) |
|
|
1564 | self->cb_sk_object = newHV (); |
|
|
1565 | |
1382 | if (!SvOK (cb)) |
1566 | if (SvOK (cb)) |
1383 | cb = 0; |
1567 | hv_store_ent (self->cb_sk_object, key, newSVsv (cb), 0); |
1384 | |
1568 | else |
1385 | switch (ix) |
|
|
1386 | { |
1569 | { |
1387 | case 0: self->cb_object = cb; break; |
1570 | hv_delete_ent (self->cb_sk_object, key, G_DISCARD, 0); |
|
|
1571 | |
|
|
1572 | if (!HvKEYS (self->cb_sk_object)) |
|
|
1573 | { |
|
|
1574 | SvREFCNT_dec (self->cb_sk_object); |
1388 | case 1: self->cb_sk_object = cb; break; |
1575 | self->cb_sk_object = 0; |
|
|
1576 | } |
1389 | } |
1577 | } |
1390 | |
1578 | |
1391 | XPUSHs (ST (0)); |
1579 | XPUSHs (ST (0)); |
1392 | } |
1580 | } |
1393 | |
1581 | |
… | |
… | |
1406 | EXTEND (SP, 2); |
1594 | EXTEND (SP, 2); |
1407 | PUSHs (decode_json (jsonstr, self, &offset)); |
1595 | PUSHs (decode_json (jsonstr, self, &offset)); |
1408 | PUSHs (sv_2mortal (newSVuv (offset))); |
1596 | PUSHs (sv_2mortal (newSVuv (offset))); |
1409 | } |
1597 | } |
1410 | |
1598 | |
|
|
1599 | void DESTROY (JSON *self) |
|
|
1600 | CODE: |
|
|
1601 | SvREFCNT_dec (self->cb_sk_object); |
|
|
1602 | SvREFCNT_dec (self->cb_object); |
|
|
1603 | |
1411 | PROTOTYPES: ENABLE |
1604 | PROTOTYPES: ENABLE |
1412 | |
1605 | |
1413 | void to_json (SV *scalar) |
1606 | void to_json (SV *scalar) |
1414 | PPCODE: |
1607 | PPCODE: |
1415 | { |
1608 | { |