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 | |
… | |
… | |
57 | #endif |
58 | #endif |
58 | |
59 | |
59 | #define expect_false(expr) expect ((expr) != 0, 0) |
60 | #define expect_false(expr) expect ((expr) != 0, 0) |
60 | #define expect_true(expr) expect ((expr) != 0, 1) |
61 | #define expect_true(expr) expect ((expr) != 0, 1) |
61 | |
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 | |
62 | static HV *json_stash, *json_boolean_stash; // JSON::XS:: |
71 | static HV *json_stash, *json_boolean_stash; // JSON::XS:: |
63 | static SV *json_true, *json_false; |
72 | static SV *json_true, *json_false; |
64 | |
73 | |
65 | typedef struct { |
74 | typedef struct { |
66 | U32 flags; |
75 | U32 flags; |
67 | SV *cb_object, *cb_sk_object; |
76 | SV *cb_object; |
|
|
77 | HV *cb_sk_object; |
68 | } JSON; |
78 | } JSON; |
69 | |
79 | |
70 | ///////////////////////////////////////////////////////////////////////////// |
80 | ///////////////////////////////////////////////////////////////////////////// |
71 | // utility functions |
81 | // utility functions |
72 | |
82 | |
… | |
… | |
305 | encode_ch (enc, '['); encode_nl (enc); |
315 | encode_ch (enc, '['); encode_nl (enc); |
306 | ++enc->indent; |
316 | ++enc->indent; |
307 | |
317 | |
308 | for (i = 0; i <= len; ++i) |
318 | for (i = 0; i <= len; ++i) |
309 | { |
319 | { |
|
|
320 | SV **svp = av_fetch (av, i, 0); |
|
|
321 | |
310 | encode_indent (enc); |
322 | encode_indent (enc); |
311 | 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); |
312 | |
328 | |
313 | if (i < len) |
329 | if (i < len) |
314 | encode_comma (enc); |
330 | encode_comma (enc); |
315 | } |
331 | } |
316 | |
332 | |
… | |
… | |
319 | --enc->indent; |
335 | --enc->indent; |
320 | encode_indent (enc); encode_ch (enc, ']'); |
336 | encode_indent (enc); encode_ch (enc, ']'); |
321 | } |
337 | } |
322 | |
338 | |
323 | static void |
339 | static void |
324 | encode_he (enc_t *enc, HE *he) |
340 | encode_hk (enc_t *enc, HE *he) |
325 | { |
341 | { |
326 | encode_ch (enc, '"'); |
342 | encode_ch (enc, '"'); |
327 | |
343 | |
328 | if (HeKLEN (he) == HEf_SVKEY) |
344 | if (HeKLEN (he) == HEf_SVKEY) |
329 | { |
345 | { |
… | |
… | |
342 | encode_ch (enc, '"'); |
358 | encode_ch (enc, '"'); |
343 | |
359 | |
344 | if (enc->json.flags & F_SPACE_BEFORE) encode_space (enc); |
360 | if (enc->json.flags & F_SPACE_BEFORE) encode_space (enc); |
345 | encode_ch (enc, ':'); |
361 | encode_ch (enc, ':'); |
346 | if (enc->json.flags & F_SPACE_AFTER ) encode_space (enc); |
362 | if (enc->json.flags & F_SPACE_AFTER ) encode_space (enc); |
347 | encode_sv (enc, HeVAL (he)); |
|
|
348 | } |
363 | } |
349 | |
364 | |
350 | // compare hash entries, used when all keys are bytestrings |
365 | // compare hash entries, used when all keys are bytestrings |
351 | static int |
366 | static int |
352 | he_cmp_fast (const void *a_, const void *b_) |
367 | he_cmp_fast (const void *a_, const void *b_) |
… | |
… | |
357 | HE *b = *(HE **)b_; |
372 | HE *b = *(HE **)b_; |
358 | |
373 | |
359 | STRLEN la = HeKLEN (a); |
374 | STRLEN la = HeKLEN (a); |
360 | STRLEN lb = HeKLEN (b); |
375 | STRLEN lb = HeKLEN (b); |
361 | |
376 | |
362 | if (!(cmp = memcmp (HeKEY (a), HeKEY (b), la < lb ? la : lb))) |
377 | if (!(cmp = memcmp (HeKEY (b), HeKEY (a), lb < la ? lb : la))) |
363 | cmp = la - lb; |
378 | cmp = lb - la; |
364 | |
379 | |
365 | return cmp; |
380 | return cmp; |
366 | } |
381 | } |
367 | |
382 | |
368 | // 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 |
369 | static int |
384 | static int |
370 | he_cmp_slow (const void *a, const void *b) |
385 | he_cmp_slow (const void *a, const void *b) |
371 | { |
386 | { |
372 | return sv_cmp (HeSVKEY_force (*(HE **)a), HeSVKEY_force (*(HE **)b)); |
387 | return sv_cmp (HeSVKEY_force (*(HE **)b), HeSVKEY_force (*(HE **)a)); |
373 | } |
388 | } |
374 | |
389 | |
375 | static void |
390 | static void |
376 | encode_hv (enc_t *enc, HV *hv) |
391 | encode_hv (enc_t *enc, HV *hv) |
377 | { |
392 | { |
|
|
393 | HE *he; |
378 | int count, i; |
394 | int count; |
379 | |
395 | |
380 | if (enc->indent >= enc->maxdepth) |
396 | if (enc->indent >= enc->maxdepth) |
381 | croak ("data structure too deep (hit recursion limit)"); |
397 | croak ("data structure too deep (hit recursion limit)"); |
382 | |
398 | |
383 | encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; |
399 | encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; |
384 | |
400 | |
385 | if ((count = hv_iterinit (hv))) |
|
|
386 | { |
|
|
387 | // for canonical output we have to sort by keys first |
401 | // for canonical output we have to sort by keys first |
388 | // actually, this is mostly due to the stupid so-called |
402 | // actually, this is mostly due to the stupid so-called |
389 | // security workaround added somewhere in 5.8.x. |
403 | // security workaround added somewhere in 5.8.x. |
390 | // that randomises hash orderings |
404 | // that randomises hash orderings |
391 | 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)) |
392 | { |
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 | { |
393 | int fast = 1; |
425 | int i, fast = 1; |
394 | HE *he; |
|
|
395 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
426 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
396 | HE **hes = _alloca (count * sizeof (HE)); |
427 | HE **hes = _alloca (count * sizeof (HE)); |
397 | #else |
428 | #else |
398 | 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 |
399 | #endif |
430 | #endif |
… | |
… | |
426 | |
457 | |
427 | FREETMPS; |
458 | FREETMPS; |
428 | LEAVE; |
459 | LEAVE; |
429 | } |
460 | } |
430 | |
461 | |
431 | for (i = 0; i < count; ++i) |
462 | while (count--) |
432 | { |
463 | { |
433 | encode_indent (enc); |
464 | encode_indent (enc); |
|
|
465 | he = hes [count]; |
434 | encode_he (enc, hes [i]); |
466 | encode_hk (enc, he); |
|
|
467 | encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he)); |
435 | |
468 | |
436 | if (i < count - 1) |
469 | if (count) |
437 | encode_comma (enc); |
470 | encode_comma (enc); |
438 | } |
471 | } |
439 | |
|
|
440 | encode_nl (enc); |
|
|
441 | } |
472 | } |
|
|
473 | } |
442 | else |
474 | else |
443 | { |
475 | { |
|
|
476 | if (hv_iterinit (hv) || SvMAGICAL (hv)) |
444 | HE *he = hv_iternext (hv); |
477 | if ((he = hv_iternext (hv))) |
445 | |
|
|
446 | for (;;) |
478 | for (;;) |
447 | { |
479 | { |
448 | encode_indent (enc); |
480 | encode_indent (enc); |
449 | encode_he (enc, he); |
481 | encode_hk (enc, he); |
|
|
482 | encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he)); |
450 | |
483 | |
451 | if (!(he = hv_iternext (hv))) |
484 | if (!(he = hv_iternext (hv))) |
452 | break; |
485 | break; |
453 | |
486 | |
454 | encode_comma (enc); |
487 | encode_comma (enc); |
455 | } |
488 | } |
|
|
489 | } |
456 | |
490 | |
457 | encode_nl (enc); |
491 | encode_nl (enc); |
458 | } |
|
|
459 | } |
|
|
460 | |
492 | |
461 | --enc->indent; encode_indent (enc); encode_ch (enc, '}'); |
493 | --enc->indent; encode_indent (enc); encode_ch (enc, '}'); |
462 | } |
494 | } |
463 | |
495 | |
464 | // encode objects, arrays and special \0=false and \1=true values. |
496 | // encode objects, arrays and special \0=false and \1=true values. |
… | |
… | |
470 | SvGETMAGIC (sv); |
502 | SvGETMAGIC (sv); |
471 | svt = SvTYPE (sv); |
503 | svt = SvTYPE (sv); |
472 | |
504 | |
473 | if (expect_false (SvOBJECT (sv))) |
505 | if (expect_false (SvOBJECT (sv))) |
474 | { |
506 | { |
|
|
507 | HV *stash = !JSON_SLOW || json_boolean_stash |
|
|
508 | ? json_boolean_stash |
|
|
509 | : gv_stashpv ("JSON::XS::Boolean", 1); |
|
|
510 | |
475 | if (SvSTASH (sv) == json_boolean_stash) |
511 | if (SvSTASH (sv) == stash) |
476 | { |
512 | { |
477 | if (SvIV (sv) == 0) |
513 | if (SvIV (sv)) |
|
|
514 | encode_str (enc, "true", 4, 0); |
|
|
515 | else |
478 | encode_str (enc, "false", 5, 0); |
516 | encode_str (enc, "false", 5, 0); |
479 | else |
|
|
480 | encode_str (enc, "true", 4, 0); |
|
|
481 | } |
517 | } |
482 | else |
518 | else |
483 | { |
519 | { |
484 | #if 0 |
520 | #if 0 |
485 | if (0 && sv_derived_from (rv, "JSON::Literal")) |
521 | if (0 && sv_derived_from (rv, "JSON::Literal")) |
… | |
… | |
488 | } |
524 | } |
489 | #endif |
525 | #endif |
490 | if (enc->json.flags & F_CONV_BLESSED) |
526 | if (enc->json.flags & F_CONV_BLESSED) |
491 | { |
527 | { |
492 | // 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 |
493 | GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 1); |
529 | GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 0); |
494 | |
530 | |
495 | if (to_json) |
531 | if (to_json) |
496 | { |
532 | { |
|
|
533 | dSP; |
|
|
534 | |
497 | dSP; ENTER; SAVETMPS; PUSHMARK (SP); |
535 | ENTER; SAVETMPS; PUSHMARK (SP); |
498 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); |
536 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); |
499 | |
537 | |
500 | // 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 |
501 | // check anyways. |
|
|
502 | PUTBACK; |
539 | PUTBACK; |
503 | assert (1 == call_sv ((SV *)GvCV (to_json), G_SCALAR)); |
540 | call_sv ((SV *)GvCV (to_json), G_SCALAR); |
504 | SPAGAIN; |
541 | SPAGAIN; |
505 | |
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 | |
506 | encode_sv (enc, POPs); |
550 | encode_sv (enc, sv); |
507 | |
551 | |
508 | FREETMPS; LEAVE; |
552 | FREETMPS; LEAVE; |
509 | } |
553 | } |
510 | else if (enc->json.flags & F_ALLOW_BLESSED) |
554 | else if (enc->json.flags & F_ALLOW_BLESSED) |
511 | encode_str (enc, "null", 4, 0); |
555 | encode_str (enc, "null", 4, 0); |
… | |
… | |
527 | else if (svt < SVt_PVAV) |
571 | else if (svt < SVt_PVAV) |
528 | { |
572 | { |
529 | STRLEN len = 0; |
573 | STRLEN len = 0; |
530 | char *pv = svt ? SvPV (sv, len) : 0; |
574 | char *pv = svt ? SvPV (sv, len) : 0; |
531 | |
575 | |
532 | if (len == 1 && *pv == '0') |
576 | if (len == 1 && *pv == '1') |
|
|
577 | encode_str (enc, "true", 4, 0); |
|
|
578 | else if (len == 1 && *pv == '0') |
533 | encode_str (enc, "false", 5, 0); |
579 | encode_str (enc, "false", 5, 0); |
534 | else if (len == 1 && *pv == '1') |
|
|
535 | encode_str (enc, "true", 4, 0); |
|
|
536 | else |
580 | else |
537 | 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", |
538 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
582 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
539 | } |
583 | } |
540 | else |
584 | else |
… | |
… | |
921 | is_nv = 1; |
965 | is_nv = 1; |
922 | } |
966 | } |
923 | |
967 | |
924 | if (!is_nv) |
968 | if (!is_nv) |
925 | { |
969 | { |
|
|
970 | int len = dec->cur - start; |
|
|
971 | |
926 | // 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 |
927 | if (*start == '-') |
973 | if (*start == '-') |
928 | switch (dec->cur - start) |
974 | switch (len) |
929 | { |
975 | { |
930 | case 2: return newSViv (-( start [1] - '0' * 1)); |
976 | case 2: return newSViv (-( start [1] - '0' * 1)); |
931 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
977 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
932 | 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)); |
933 | 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)); |
934 | } |
980 | } |
935 | else |
981 | else |
936 | switch (dec->cur - start) |
982 | switch (len) |
937 | { |
983 | { |
938 | case 1: return newSViv ( start [0] - '0' * 1); |
984 | case 1: return newSViv ( start [0] - '0' * 1); |
939 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
985 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
940 | 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); |
941 | 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); |
942 | } |
988 | } |
943 | |
989 | |
944 | { |
990 | { |
945 | UV uv; |
991 | UV uv; |
946 | int numtype = grok_number (start, dec->cur - start, &uv); |
992 | int numtype = grok_number (start, len, &uv); |
947 | if (numtype & IS_NUMBER_IN_UV) |
993 | if (numtype & IS_NUMBER_IN_UV) |
948 | if (numtype & IS_NUMBER_NEG) |
994 | if (numtype & IS_NUMBER_NEG) |
949 | { |
995 | { |
950 | if (uv < (UV)IV_MIN) |
996 | if (uv < (UV)IV_MIN) |
951 | return newSViv (-(IV)uv); |
997 | return newSViv (-(IV)uv); |
952 | } |
998 | } |
953 | else |
999 | else |
954 | return newSVuv (uv); |
1000 | return newSVuv (uv); |
955 | |
|
|
956 | // here would likely be the place for bigint support |
|
|
957 | } |
1001 | } |
958 | } |
|
|
959 | |
1002 | |
960 | // 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 |
961 | return newSVnv (Atof (start)); |
1019 | return newSVnv (Atof (start)); |
962 | |
1020 | |
963 | fail: |
1021 | fail: |
964 | return 0; |
1022 | return 0; |
965 | } |
1023 | } |
… | |
… | |
1098 | sv = newRV_noinc ((SV *)hv); |
1156 | sv = newRV_noinc ((SV *)hv); |
1099 | |
1157 | |
1100 | // check filter callbacks |
1158 | // check filter callbacks |
1101 | if (dec->json.flags & F_HOOK) |
1159 | if (dec->json.flags & F_HOOK) |
1102 | { |
1160 | { |
1103 | ENTER; SAVETMPS; |
1161 | if (dec->json.cb_sk_object && HvKEYS (hv) == 1) |
1104 | |
|
|
1105 | if (HvKEYS (hv) == 1 && dec->json.cb_sk_object) |
|
|
1106 | { |
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; |
1107 | int count; |
1176 | int count; |
1108 | |
1177 | |
1109 | dSP; PUSHMARK (SP); |
1178 | ENTER; SAVETMPS; PUSHMARK (SP); |
1110 | XPUSHs (sv_2mortal (sv)); |
1179 | XPUSHs (HeVAL (he)); |
1111 | |
1180 | |
1112 | PUTBACK; count = call_sv (dec->json.cb_sk_object, G_ARRAY); SPAGAIN; |
1181 | PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; |
1113 | |
1182 | |
1114 | if (count == 1) |
1183 | if (count == 1) |
|
|
1184 | { |
1115 | sv = newSVsv (POPs); |
1185 | sv = newSVsv (POPs); |
1116 | else |
1186 | FREETMPS; LEAVE; |
1117 | SvREFCNT_inc (sv); |
1187 | return sv; |
|
|
1188 | } |
|
|
1189 | |
|
|
1190 | FREETMPS; LEAVE; |
|
|
1191 | } |
1118 | } |
1192 | } |
1119 | |
1193 | |
1120 | if (dec->json.cb_object) |
1194 | if (dec->json.cb_object) |
1121 | { |
1195 | { |
|
|
1196 | dSP; |
1122 | int count; |
1197 | int count; |
1123 | |
1198 | |
1124 | dSP; ENTER; SAVETMPS; PUSHMARK (SP); |
1199 | ENTER; SAVETMPS; PUSHMARK (SP); |
1125 | XPUSHs (sv_2mortal (sv)); |
1200 | XPUSHs (sv_2mortal (sv)); |
1126 | |
1201 | |
1127 | PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN; |
1202 | PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN; |
1128 | |
1203 | |
1129 | if (count == 1) |
1204 | if (count == 1) |
|
|
1205 | { |
1130 | sv = newSVsv (POPs); |
1206 | sv = newSVsv (POPs); |
1131 | else |
1207 | FREETMPS; LEAVE; |
|
|
1208 | return sv; |
|
|
1209 | } |
|
|
1210 | |
1132 | SvREFCNT_inc (sv); |
1211 | SvREFCNT_inc (sv); |
|
|
1212 | FREETMPS; LEAVE; |
1133 | } |
1213 | } |
1134 | |
|
|
1135 | FREETMPS; LEAVE; |
|
|
1136 | } |
1214 | } |
1137 | |
1215 | |
1138 | return newRV_noinc ((SV *)hv); |
1216 | return sv; |
1139 | |
1217 | |
1140 | fail: |
1218 | fail: |
1141 | SvREFCNT_dec (hv); |
1219 | SvREFCNT_dec (hv); |
1142 | DEC_DEC_DEPTH; |
1220 | DEC_DEC_DEPTH; |
1143 | return 0; |
1221 | return 0; |
… | |
… | |
1163 | |
1241 | |
1164 | case 't': |
1242 | case 't': |
1165 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) |
1243 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) |
1166 | { |
1244 | { |
1167 | 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 |
1168 | return SvREFCNT_inc (json_true); |
1249 | return SvREFCNT_inc (json_true); |
1169 | } |
1250 | } |
1170 | else |
1251 | else |
1171 | ERR ("'true' expected"); |
1252 | ERR ("'true' expected"); |
1172 | |
1253 | |
… | |
… | |
1174 | |
1255 | |
1175 | case 'f': |
1256 | case 'f': |
1176 | if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) |
1257 | if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) |
1177 | { |
1258 | { |
1178 | 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 |
1179 | return SvREFCNT_inc (json_false); |
1263 | return SvREFCNT_inc (json_false); |
1180 | } |
1264 | } |
1181 | else |
1265 | else |
1182 | ERR ("'false' expected"); |
1266 | ERR ("'false' expected"); |
1183 | |
1267 | |
… | |
… | |
1310 | 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); |
1311 | } |
1395 | } |
1312 | |
1396 | |
1313 | PROTOTYPES: DISABLE |
1397 | PROTOTYPES: DISABLE |
1314 | |
1398 | |
|
|
1399 | void CLONE (...) |
|
|
1400 | CODE: |
|
|
1401 | json_stash = 0; |
|
|
1402 | json_boolean_stash = 0; |
|
|
1403 | |
1315 | void new (char *klass) |
1404 | void new (char *klass) |
1316 | PPCODE: |
1405 | PPCODE: |
1317 | { |
1406 | { |
1318 | SV *pv = NEWSV (0, sizeof (JSON)); |
1407 | SV *pv = NEWSV (0, sizeof (JSON)); |
1319 | SvPOK_only (pv); |
1408 | SvPOK_only (pv); |
1320 | Zero (SvPVX (pv), 1, JSON); |
1409 | Zero (SvPVX (pv), 1, JSON); |
1321 | ((JSON *)SvPVX (pv))->flags = F_DEFAULT; |
1410 | ((JSON *)SvPVX (pv))->flags = F_DEFAULT; |
1322 | XPUSHs (sv_2mortal (sv_bless (newRV_noinc (pv), json_stash))); |
1411 | XPUSHs (sv_2mortal (sv_bless (newRV_noinc (pv), JSON_STASH))); |
1323 | } |
1412 | } |
1324 | |
1413 | |
1325 | void ascii (JSON *self, int enable = 1) |
1414 | void ascii (JSON *self, int enable = 1) |
1326 | ALIAS: |
1415 | ALIAS: |
1327 | ascii = F_ASCII |
1416 | ascii = F_ASCII |
… | |
… | |
1375 | self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE); |
1464 | self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE); |
1376 | |
1465 | |
1377 | XPUSHs (ST (0)); |
1466 | XPUSHs (ST (0)); |
1378 | } |
1467 | } |
1379 | |
1468 | |
1380 | void filter_json_objects (JSON *self, SV *cb = &PL_sv_undef) |
1469 | void filter_json_object (JSON *self, SV *cb = &PL_sv_undef) |
1381 | ALIAS: |
|
|
1382 | filter_sk_json_objects = 1 |
|
|
1383 | PPCODE: |
1470 | PPCODE: |
1384 | { |
1471 | { |
|
|
1472 | SvREFCNT_dec (self->cb_object); |
|
|
1473 | self->cb_object = SvOK (cb) ? newSVsv (cb) : 0; |
|
|
1474 | |
|
|
1475 | XPUSHs (ST (0)); |
|
|
1476 | } |
|
|
1477 | |
|
|
1478 | void filter_json_single_key_object (JSON *self, SV *key, SV *cb = &PL_sv_undef) |
|
|
1479 | PPCODE: |
|
|
1480 | { |
|
|
1481 | if (!self->cb_sk_object) |
|
|
1482 | self->cb_sk_object = newHV (); |
|
|
1483 | |
1385 | if (!SvOK (cb)) |
1484 | if (SvOK (cb)) |
1386 | cb = 0; |
1485 | hv_store_ent (self->cb_sk_object, key, newSVsv (cb), 0); |
1387 | |
1486 | else |
1388 | switch (ix) |
|
|
1389 | { |
1487 | { |
1390 | case 0: self->cb_object = cb; break; |
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); |
1391 | case 1: self->cb_sk_object = cb; break; |
1493 | self->cb_sk_object = 0; |
|
|
1494 | } |
1392 | } |
1495 | } |
1393 | |
1496 | |
1394 | XPUSHs (ST (0)); |
1497 | XPUSHs (ST (0)); |
1395 | } |
1498 | } |
1396 | |
1499 | |
… | |
… | |
1409 | EXTEND (SP, 2); |
1512 | EXTEND (SP, 2); |
1410 | PUSHs (decode_json (jsonstr, self, &offset)); |
1513 | PUSHs (decode_json (jsonstr, self, &offset)); |
1411 | PUSHs (sv_2mortal (newSVuv (offset))); |
1514 | PUSHs (sv_2mortal (newSVuv (offset))); |
1412 | } |
1515 | } |
1413 | |
1516 | |
|
|
1517 | void DESTROY (JSON *self) |
|
|
1518 | CODE: |
|
|
1519 | SvREFCNT_dec (self->cb_sk_object); |
|
|
1520 | SvREFCNT_dec (self->cb_object); |
|
|
1521 | |
1414 | PROTOTYPES: ENABLE |
1522 | PROTOTYPES: ENABLE |
1415 | |
1523 | |
1416 | void to_json (SV *scalar) |
1524 | void to_json (SV *scalar) |
1417 | PPCODE: |
1525 | PPCODE: |
1418 | { |
1526 | { |