ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/JSON-XS/XS.xs
(Generate patch)

Comparing JSON-XS/XS.xs (file contents):
Revision 1.50 by root, Mon Jul 2 00:29:38 2007 UTC vs.
Revision 1.70 by root, Wed Mar 19 00:44:54 2008 UTC

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
62static HV *json_stash, *json_boolean_stash; // JSON::XS:: 73static HV *json_stash, *json_boolean_stash; // JSON::XS::
63static SV *json_true, *json_false; 74static SV *json_true, *json_false;
64 75
65typedef struct { 76typedef 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
323static void 344static void
324encode_he (enc_t *enc, HE *he) 345encode_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
351static int 371static int
352he_cmp_fast (const void *a_, const void *b_) 372he_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
369static int 389static int
370he_cmp_slow (const void *a, const void *b) 390he_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
375static void 395static void
376encode_hv (enc_t *enc, HV *hv) 396encode_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.
465static void 510static void
466encode_rv (enc_t *enc, SV *sv) 511encode_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);
527 else if (svt < SVt_PVAV) 584 else if (svt < SVt_PVAV)
528 { 585 {
529 STRLEN len = 0; 586 STRLEN len = 0;
530 char *pv = svt ? SvPV (sv, len) : 0; 587 char *pv = svt ? SvPV (sv, len) : 0;
531 588
532 if (len == 1 && *pv == '0') 589 if (len == 1 && *pv == '1')
590 encode_str (enc, "true", 4, 0);
591 else if (len == 1 && *pv == '0')
533 encode_str (enc, "false", 5, 0); 592 encode_str (enc, "false", 5, 0);
534 else if (len == 1 && *pv == '1')
535 encode_str (enc, "true", 4, 0);
536 else 593 else
537 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",
538 SvPV_nolen (sv_2mortal (newRV_inc (sv)))); 595 SvPV_nolen (sv_2mortal (newRV_inc (sv))));
539 } 596 }
540 else 597 else
654 U32 depth; // recursion depth 711 U32 depth; // recursion depth
655 U32 maxdepth; // recursion depth limit 712 U32 maxdepth; // recursion depth limit
656} dec_t; 713} dec_t;
657 714
658inline void 715inline void
716decode_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
724inline void
659decode_ws (dec_t *dec) 725decode_ws (dec_t *dec)
660{ 726{
661 for (;;) 727 for (;;)
662 { 728 {
663 char ch = *dec->cur; 729 char ch = *dec->cur;
664 730
665 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 }
666 || (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)) 743 else if (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)
667 break; 744 break; // parse error, but let higher level handle it, gives better error messages
668 745
669 ++dec->cur; 746 ++dec->cur;
670 } 747 }
671} 748}
672 749
921 is_nv = 1; 998 is_nv = 1;
922 } 999 }
923 1000
924 if (!is_nv) 1001 if (!is_nv)
925 { 1002 {
1003 int len = dec->cur - start;
1004
926 // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so 1005 // special case the rather common 1..5-digit-int case
927 if (*start == '-') 1006 if (*start == '-')
928 switch (dec->cur - start) 1007 switch (len)
929 { 1008 {
930 case 2: return newSViv (-( start [1] - '0' * 1)); 1009 case 2: return newSViv (-( start [1] - '0' * 1));
931 case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); 1010 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)); 1011 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)); 1012 case 5: return newSViv (-( start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111));
1013 case 6: return newSViv (-(start [1] * 10000 + start [2] * 1000 + start [3] * 100 + start [4] * 10 + start [5] - '0' * 11111));
934 } 1014 }
935 else 1015 else
936 switch (dec->cur - start) 1016 switch (len)
937 { 1017 {
938 case 1: return newSViv ( start [0] - '0' * 1); 1018 case 1: return newSViv ( start [0] - '0' * 1);
939 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); 1019 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); 1020 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); 1021 case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111);
1022 case 5: return newSViv ( start [0] * 10000 + start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 11111);
942 } 1023 }
943 1024
944 { 1025 {
945 UV uv; 1026 UV uv;
946 int numtype = grok_number (start, dec->cur - start, &uv); 1027 int numtype = grok_number (start, len, &uv);
947 if (numtype & IS_NUMBER_IN_UV) 1028 if (numtype & IS_NUMBER_IN_UV)
948 if (numtype & IS_NUMBER_NEG) 1029 if (numtype & IS_NUMBER_NEG)
949 { 1030 {
950 if (uv < (UV)IV_MIN) 1031 if (uv < (UV)IV_MIN)
951 return newSViv (-(IV)uv); 1032 return newSViv (-(IV)uv);
952 } 1033 }
953 else 1034 else
954 return newSVuv (uv); 1035 return newSVuv (uv);
955
956 // here would likely be the place for bigint support
957 } 1036 }
958 }
959 1037
960 // if we ever support bigint or bigfloat, this is the place for bigfloat 1038 len -= *start == '-' ? 1 : 0;
1039
1040 // does not fit into IV or UV, try NV
1041 if ((sizeof (NV) == sizeof (double) && DBL_DIG >= len)
1042 #if defined (LDBL_DIG)
1043 || (sizeof (NV) == sizeof (long double) && LDBL_DIG >= len)
1044 #endif
1045 )
1046 // fits into NV without loss of precision
1047 return newSVnv (Atof (start));
1048
1049 // everything else fails, convert it to a string
1050 return newSVpvn (start, dec->cur - start);
1051 }
1052
1053 // loss of precision here
961 return newSVnv (Atof (start)); 1054 return newSVnv (Atof (start));
962 1055
963fail: 1056fail:
964 return 0; 1057 return 0;
965} 1058}
995 1088
996 if (*dec->cur != ',') 1089 if (*dec->cur != ',')
997 ERR (", or ] expected while parsing array"); 1090 ERR (", or ] expected while parsing array");
998 1091
999 ++dec->cur; 1092 ++dec->cur;
1093
1094 decode_ws (dec);
1095
1096 if (*dec->cur == ']' && dec->json.flags & F_RELAXED)
1097 {
1098 ++dec->cur;
1099 break;
1100 }
1000 } 1101 }
1001 1102
1002 DEC_DEC_DEPTH; 1103 DEC_DEC_DEPTH;
1003 return newRV_noinc ((SV *)av); 1104 return newRV_noinc ((SV *)av);
1004 1105
1020 if (*dec->cur == '}') 1121 if (*dec->cur == '}')
1021 ++dec->cur; 1122 ++dec->cur;
1022 else 1123 else
1023 for (;;) 1124 for (;;)
1024 { 1125 {
1025 decode_ws (dec); EXPECT_CH ('"'); 1126 EXPECT_CH ('"');
1026 1127
1027 // heuristic: assume that 1128 // heuristic: assume that
1028 // a) decode_str + hv_store_ent are abysmally slow. 1129 // a) decode_str + hv_store_ent are abysmally slow.
1029 // b) most hash keys are short, simple ascii text. 1130 // b) most hash keys are short, simple ascii text.
1030 // => try to "fast-match" such strings to avoid 1131 // => try to "fast-match" such strings to avoid
1044 if (!key) 1145 if (!key)
1045 goto fail; 1146 goto fail;
1046 1147
1047 decode_ws (dec); EXPECT_CH (':'); 1148 decode_ws (dec); EXPECT_CH (':');
1048 1149
1150 decode_ws (dec);
1049 value = decode_sv (dec); 1151 value = decode_sv (dec);
1050 if (!value) 1152 if (!value)
1051 { 1153 {
1052 SvREFCNT_dec (key); 1154 SvREFCNT_dec (key);
1053 goto fail; 1155 goto fail;
1065 int len = p - key; 1167 int len = p - key;
1066 dec->cur = p + 1; 1168 dec->cur = p + 1;
1067 1169
1068 decode_ws (dec); EXPECT_CH (':'); 1170 decode_ws (dec); EXPECT_CH (':');
1069 1171
1172 decode_ws (dec);
1070 value = decode_sv (dec); 1173 value = decode_sv (dec);
1071 if (!value) 1174 if (!value)
1072 goto fail; 1175 goto fail;
1073 1176
1074 hv_store (hv, key, len, value, 0); 1177 hv_store (hv, key, len, value, 0);
1090 1193
1091 if (*dec->cur != ',') 1194 if (*dec->cur != ',')
1092 ERR (", or } expected while parsing object/hash"); 1195 ERR (", or } expected while parsing object/hash");
1093 1196
1094 ++dec->cur; 1197 ++dec->cur;
1198
1199 decode_ws (dec);
1200
1201 if (*dec->cur == '}' && dec->json.flags & F_RELAXED)
1202 {
1203 ++dec->cur;
1204 break;
1205 }
1095 } 1206 }
1096 1207
1097 DEC_DEC_DEPTH; 1208 DEC_DEC_DEPTH;
1098 sv = newRV_noinc ((SV *)hv); 1209 sv = newRV_noinc ((SV *)hv);
1099 1210
1100 // check filter callbacks 1211 // check filter callbacks
1101 if (dec->json.flags & F_HOOK) 1212 if (dec->json.flags & F_HOOK)
1102 { 1213 {
1103 ENTER; SAVETMPS; 1214 if (dec->json.cb_sk_object && HvKEYS (hv) == 1)
1104
1105 if (HvKEYS (hv) == 1 && dec->json.cb_sk_object)
1106 { 1215 {
1216 HE *cb, *he;
1217
1218 hv_iterinit (hv);
1219 he = hv_iternext (hv);
1220 hv_iterinit (hv);
1221
1222 // the next line creates a mortal sv each time its called.
1223 // might want to optimise this for common cases.
1224 cb = hv_fetch_ent (dec->json.cb_sk_object, hv_iterkeysv (he), 0, 0);
1225
1226 if (cb)
1227 {
1228 dSP;
1107 int count; 1229 int count;
1108 1230
1109 dSP; PUSHMARK (SP); 1231 ENTER; SAVETMPS; PUSHMARK (SP);
1110 XPUSHs (sv_2mortal (sv)); 1232 XPUSHs (HeVAL (he));
1111 1233
1112 PUTBACK; count = call_sv (dec->json.cb_sk_object, G_ARRAY); SPAGAIN; 1234 PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN;
1113 1235
1114 if (count == 1) 1236 if (count == 1)
1237 {
1115 sv = newSVsv (POPs); 1238 sv = newSVsv (POPs);
1116 else 1239 FREETMPS; LEAVE;
1117 SvREFCNT_inc (sv); 1240 return sv;
1241 }
1242
1243 FREETMPS; LEAVE;
1244 }
1118 } 1245 }
1119 1246
1120 if (dec->json.cb_object) 1247 if (dec->json.cb_object)
1121 { 1248 {
1249 dSP;
1122 int count; 1250 int count;
1123 1251
1124 dSP; ENTER; SAVETMPS; PUSHMARK (SP); 1252 ENTER; SAVETMPS; PUSHMARK (SP);
1125 XPUSHs (sv_2mortal (sv)); 1253 XPUSHs (sv_2mortal (sv));
1126 1254
1127 PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN; 1255 PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN;
1128 1256
1129 if (count == 1) 1257 if (count == 1)
1258 {
1130 sv = newSVsv (POPs); 1259 sv = newSVsv (POPs);
1131 else 1260 FREETMPS; LEAVE;
1261 return sv;
1262 }
1263
1132 SvREFCNT_inc (sv); 1264 SvREFCNT_inc (sv);
1265 FREETMPS; LEAVE;
1133 } 1266 }
1134
1135 FREETMPS; LEAVE;
1136 } 1267 }
1137 1268
1138 return newRV_noinc ((SV *)hv); 1269 return sv;
1139 1270
1140fail: 1271fail:
1141 SvREFCNT_dec (hv); 1272 SvREFCNT_dec (hv);
1142 DEC_DEC_DEPTH; 1273 DEC_DEC_DEPTH;
1143 return 0; 1274 return 0;
1144} 1275}
1145 1276
1146static SV * 1277static SV *
1147decode_sv (dec_t *dec) 1278decode_sv (dec_t *dec)
1148{ 1279{
1149 decode_ws (dec);
1150
1151 // the beauty of JSON: you need exactly one character lookahead 1280 // the beauty of JSON: you need exactly one character lookahead
1152 // to parse anything. 1281 // to parse anything.
1153 switch (*dec->cur) 1282 switch (*dec->cur)
1154 { 1283 {
1155 case '"': ++dec->cur; return decode_str (dec); 1284 case '"': ++dec->cur; return decode_str (dec);
1163 1292
1164 case 't': 1293 case 't':
1165 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) 1294 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4))
1166 { 1295 {
1167 dec->cur += 4; 1296 dec->cur += 4;
1297#if JSON_SLOW
1298 json_true = get_sv ("JSON::XS::true", 1); SvREADONLY_on (json_true);
1299#endif
1168 return SvREFCNT_inc (json_true); 1300 return SvREFCNT_inc (json_true);
1169 } 1301 }
1170 else 1302 else
1171 ERR ("'true' expected"); 1303 ERR ("'true' expected");
1172 1304
1174 1306
1175 case 'f': 1307 case 'f':
1176 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) 1308 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5))
1177 { 1309 {
1178 dec->cur += 5; 1310 dec->cur += 5;
1311#if JSON_SLOW
1312 json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false);
1313#endif
1179 return SvREFCNT_inc (json_false); 1314 return SvREFCNT_inc (json_false);
1180 } 1315 }
1181 else 1316 else
1182 ERR ("'false' expected"); 1317 ERR ("'false' expected");
1183 1318
1233 1368
1234 if (dec.json.cb_object || dec.json.cb_sk_object) 1369 if (dec.json.cb_object || dec.json.cb_sk_object)
1235 dec.json.flags |= F_HOOK; 1370 dec.json.flags |= F_HOOK;
1236 1371
1237 *dec.end = 0; // this should basically be a nop, too, but make sure it's there 1372 *dec.end = 0; // this should basically be a nop, too, but make sure it's there
1373
1374 decode_ws (&dec);
1238 sv = decode_sv (&dec); 1375 sv = decode_sv (&dec);
1239 1376
1240 if (!(offset_return || !sv)) 1377 if (!(offset_return || !sv))
1241 { 1378 {
1242 // check for trailing garbage 1379 // check for trailing garbage
1310 json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false); 1447 json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false);
1311} 1448}
1312 1449
1313PROTOTYPES: DISABLE 1450PROTOTYPES: DISABLE
1314 1451
1452void CLONE (...)
1453 CODE:
1454 json_stash = 0;
1455 json_boolean_stash = 0;
1456
1315void new (char *klass) 1457void new (char *klass)
1316 PPCODE: 1458 PPCODE:
1317{ 1459{
1318 SV *pv = NEWSV (0, sizeof (JSON)); 1460 SV *pv = NEWSV (0, sizeof (JSON));
1319 SvPOK_only (pv); 1461 SvPOK_only (pv);
1320 Zero (SvPVX (pv), 1, JSON); 1462 Zero (SvPVX (pv), 1, JSON);
1321 ((JSON *)SvPVX (pv))->flags = F_DEFAULT; 1463 ((JSON *)SvPVX (pv))->flags = F_DEFAULT;
1322 XPUSHs (sv_2mortal (sv_bless (newRV_noinc (pv), json_stash))); 1464 XPUSHs (sv_2mortal (sv_bless (
1465 newRV_noinc (pv),
1466 strEQ (klass, "JSON::XS") ? JSON_STASH : gv_stashpv (klass, 1)
1467 )));
1323} 1468}
1324 1469
1325void ascii (JSON *self, int enable = 1) 1470void ascii (JSON *self, int enable = 1)
1326 ALIAS: 1471 ALIAS:
1327 ascii = F_ASCII 1472 ascii = F_ASCII
1334 pretty = F_PRETTY 1479 pretty = F_PRETTY
1335 allow_nonref = F_ALLOW_NONREF 1480 allow_nonref = F_ALLOW_NONREF
1336 shrink = F_SHRINK 1481 shrink = F_SHRINK
1337 allow_blessed = F_ALLOW_BLESSED 1482 allow_blessed = F_ALLOW_BLESSED
1338 convert_blessed = F_CONV_BLESSED 1483 convert_blessed = F_CONV_BLESSED
1484 relaxed = F_RELAXED
1339 PPCODE: 1485 PPCODE:
1340{ 1486{
1341 if (enable) 1487 if (enable)
1342 self->flags |= ix; 1488 self->flags |= ix;
1343 else 1489 else
1344 self->flags &= ~ix; 1490 self->flags &= ~ix;
1345 1491
1346 XPUSHs (ST (0)); 1492 XPUSHs (ST (0));
1347} 1493}
1348 1494
1495void get_ascii (JSON *self)
1496 ALIAS:
1497 get_ascii = F_ASCII
1498 get_latin1 = F_LATIN1
1499 get_utf8 = F_UTF8
1500 get_indent = F_INDENT
1501 get_canonical = F_CANONICAL
1502 get_space_before = F_SPACE_BEFORE
1503 get_space_after = F_SPACE_AFTER
1504 get_allow_nonref = F_ALLOW_NONREF
1505 get_shrink = F_SHRINK
1506 get_allow_blessed = F_ALLOW_BLESSED
1507 get_convert_blessed = F_CONV_BLESSED
1508 get_relaxed = F_RELAXED
1509 PPCODE:
1510 XPUSHs (boolSV (self->flags & ix));
1511
1349void max_depth (JSON *self, UV max_depth = 0x80000000UL) 1512void max_depth (JSON *self, UV max_depth = 0x80000000UL)
1350 PPCODE: 1513 PPCODE:
1351{ 1514{
1352 UV log2 = 0; 1515 UV log2 = 0;
1353 1516
1359 self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); 1522 self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH);
1360 1523
1361 XPUSHs (ST (0)); 1524 XPUSHs (ST (0));
1362} 1525}
1363 1526
1527U32 get_max_depth (JSON *self)
1528 CODE:
1529 RETVAL = DEC_DEPTH (self->flags);
1530 OUTPUT:
1531 RETVAL
1532
1364void max_size (JSON *self, UV max_size = 0) 1533void max_size (JSON *self, UV max_size = 0)
1365 PPCODE: 1534 PPCODE:
1366{ 1535{
1367 UV log2 = 0; 1536 UV log2 = 0;
1368 1537
1375 self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE); 1544 self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE);
1376 1545
1377 XPUSHs (ST (0)); 1546 XPUSHs (ST (0));
1378} 1547}
1379 1548
1549int get_max_size (JSON *self)
1550 CODE:
1551 RETVAL = DEC_SIZE (self->flags);
1552 OUTPUT:
1553 RETVAL
1554
1380void filter_json_objects (JSON *self, SV *cb = &PL_sv_undef) 1555void filter_json_object (JSON *self, SV *cb = &PL_sv_undef)
1381 ALIAS:
1382 filter_sk_json_objects = 1
1383 PPCODE: 1556 PPCODE:
1384{ 1557{
1558 SvREFCNT_dec (self->cb_object);
1559 self->cb_object = SvOK (cb) ? newSVsv (cb) : 0;
1560
1561 XPUSHs (ST (0));
1562}
1563
1564void filter_json_single_key_object (JSON *self, SV *key, SV *cb = &PL_sv_undef)
1565 PPCODE:
1566{
1567 if (!self->cb_sk_object)
1568 self->cb_sk_object = newHV ();
1569
1385 if (!SvOK (cb)) 1570 if (SvOK (cb))
1386 cb = 0; 1571 hv_store_ent (self->cb_sk_object, key, newSVsv (cb), 0);
1387 1572 else
1388 switch (ix)
1389 { 1573 {
1390 case 0: self->cb_object = cb; break; 1574 hv_delete_ent (self->cb_sk_object, key, G_DISCARD, 0);
1575
1576 if (!HvKEYS (self->cb_sk_object))
1577 {
1578 SvREFCNT_dec (self->cb_sk_object);
1391 case 1: self->cb_sk_object = cb; break; 1579 self->cb_sk_object = 0;
1580 }
1392 } 1581 }
1393 1582
1394 XPUSHs (ST (0)); 1583 XPUSHs (ST (0));
1395} 1584}
1396 1585
1409 EXTEND (SP, 2); 1598 EXTEND (SP, 2);
1410 PUSHs (decode_json (jsonstr, self, &offset)); 1599 PUSHs (decode_json (jsonstr, self, &offset));
1411 PUSHs (sv_2mortal (newSVuv (offset))); 1600 PUSHs (sv_2mortal (newSVuv (offset)));
1412} 1601}
1413 1602
1603void DESTROY (JSON *self)
1604 CODE:
1605 SvREFCNT_dec (self->cb_sk_object);
1606 SvREFCNT_dec (self->cb_object);
1607
1414PROTOTYPES: ENABLE 1608PROTOTYPES: ENABLE
1415 1609
1416void to_json (SV *scalar) 1610void encode_json (SV *scalar)
1417 PPCODE: 1611 PPCODE:
1418{ 1612{
1419 JSON json = { F_DEFAULT | F_UTF8 }; 1613 JSON json = { F_DEFAULT | F_UTF8 };
1420 XPUSHs (encode_json (scalar, &json)); 1614 XPUSHs (encode_json (scalar, &json));
1421} 1615}
1422 1616
1423void from_json (SV *jsonstr) 1617void decode_json (SV *jsonstr)
1424 PPCODE: 1618 PPCODE:
1425{ 1619{
1426 JSON json = { F_DEFAULT | F_UTF8 }; 1620 JSON json = { F_DEFAULT | F_UTF8 };
1427 XPUSHs (decode_json (jsonstr, &json, 0)); 1621 XPUSHs (decode_json (jsonstr, &json, 0));
1428} 1622}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines