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.48 by root, Sun Jul 1 22:20:00 2007 UTC vs.
Revision 1.62 by root, Sun Aug 26 22:27:32 2007 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
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
61static HV *json_stash, *json_boolean_stash; // JSON::XS:: 71static HV *json_stash, *json_boolean_stash; // JSON::XS::
62static SV *json_true, *json_false; 72static SV *json_true, *json_false;
63 73
64typedef struct { 74typedef struct {
65 U32 flags; 75 U32 flags;
76 SV *cb_object;
77 HV *cb_sk_object;
66} JSON; 78} JSON;
67 79
68///////////////////////////////////////////////////////////////////////////// 80/////////////////////////////////////////////////////////////////////////////
69// utility functions 81// utility functions
70 82
303 encode_ch (enc, '['); encode_nl (enc); 315 encode_ch (enc, '['); encode_nl (enc);
304 ++enc->indent; 316 ++enc->indent;
305 317
306 for (i = 0; i <= len; ++i) 318 for (i = 0; i <= len; ++i)
307 { 319 {
320 SV **svp = av_fetch (av, i, 0);
321
308 encode_indent (enc); 322 encode_indent (enc);
309 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);
310 328
311 if (i < len) 329 if (i < len)
312 encode_comma (enc); 330 encode_comma (enc);
313 } 331 }
314 332
317 --enc->indent; 335 --enc->indent;
318 encode_indent (enc); encode_ch (enc, ']'); 336 encode_indent (enc); encode_ch (enc, ']');
319} 337}
320 338
321static void 339static void
322encode_he (enc_t *enc, HE *he) 340encode_hk (enc_t *enc, HE *he)
323{ 341{
324 encode_ch (enc, '"'); 342 encode_ch (enc, '"');
325 343
326 if (HeKLEN (he) == HEf_SVKEY) 344 if (HeKLEN (he) == HEf_SVKEY)
327 { 345 {
340 encode_ch (enc, '"'); 358 encode_ch (enc, '"');
341 359
342 if (enc->json.flags & F_SPACE_BEFORE) encode_space (enc); 360 if (enc->json.flags & F_SPACE_BEFORE) encode_space (enc);
343 encode_ch (enc, ':'); 361 encode_ch (enc, ':');
344 if (enc->json.flags & F_SPACE_AFTER ) encode_space (enc); 362 if (enc->json.flags & F_SPACE_AFTER ) encode_space (enc);
345 encode_sv (enc, HeVAL (he));
346} 363}
347 364
348// compare hash entries, used when all keys are bytestrings 365// compare hash entries, used when all keys are bytestrings
349static int 366static int
350he_cmp_fast (const void *a_, const void *b_) 367he_cmp_fast (const void *a_, const void *b_)
355 HE *b = *(HE **)b_; 372 HE *b = *(HE **)b_;
356 373
357 STRLEN la = HeKLEN (a); 374 STRLEN la = HeKLEN (a);
358 STRLEN lb = HeKLEN (b); 375 STRLEN lb = HeKLEN (b);
359 376
360 if (!(cmp = memcmp (HeKEY (a), HeKEY (b), la < lb ? la : lb))) 377 if (!(cmp = memcmp (HeKEY (b), HeKEY (a), lb < la ? lb : la)))
361 cmp = la - lb; 378 cmp = lb - la;
362 379
363 return cmp; 380 return cmp;
364} 381}
365 382
366// 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
367static int 384static int
368he_cmp_slow (const void *a, const void *b) 385he_cmp_slow (const void *a, const void *b)
369{ 386{
370 return sv_cmp (HeSVKEY_force (*(HE **)a), HeSVKEY_force (*(HE **)b)); 387 return sv_cmp (HeSVKEY_force (*(HE **)b), HeSVKEY_force (*(HE **)a));
371} 388}
372 389
373static void 390static void
374encode_hv (enc_t *enc, HV *hv) 391encode_hv (enc_t *enc, HV *hv)
375{ 392{
393 HE *he;
376 int count, i; 394 int count;
377 395
378 if (enc->indent >= enc->maxdepth) 396 if (enc->indent >= enc->maxdepth)
379 croak ("data structure too deep (hit recursion limit)"); 397 croak ("data structure too deep (hit recursion limit)");
380 398
381 encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; 399 encode_ch (enc, '{'); encode_nl (enc); ++enc->indent;
382 400
383 if ((count = hv_iterinit (hv)))
384 {
385 // for canonical output we have to sort by keys first 401 // for canonical output we have to sort by keys first
386 // actually, this is mostly due to the stupid so-called 402 // actually, this is mostly due to the stupid so-called
387 // security workaround added somewhere in 5.8.x. 403 // security workaround added somewhere in 5.8.x.
388 // that randomises hash orderings 404 // that randomises hash orderings
389 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))
390 { 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 {
391 int fast = 1; 425 int i, fast = 1;
392 HE *he;
393#if defined(__BORLANDC__) || defined(_MSC_VER) 426#if defined(__BORLANDC__) || defined(_MSC_VER)
394 HE **hes = _alloca (count * sizeof (HE)); 427 HE **hes = _alloca (count * sizeof (HE));
395#else 428#else
396 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
397#endif 430#endif
424 457
425 FREETMPS; 458 FREETMPS;
426 LEAVE; 459 LEAVE;
427 } 460 }
428 461
429 for (i = 0; i < count; ++i) 462 while (count--)
430 { 463 {
431 encode_indent (enc); 464 encode_indent (enc);
465 he = hes [count];
432 encode_he (enc, hes [i]); 466 encode_hk (enc, he);
467 encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he));
433 468
434 if (i < count - 1) 469 if (count)
435 encode_comma (enc); 470 encode_comma (enc);
436 } 471 }
437
438 encode_nl (enc);
439 } 472 }
473 }
440 else 474 else
441 { 475 {
476 if (hv_iterinit (hv) || SvMAGICAL (hv))
442 HE *he = hv_iternext (hv); 477 if ((he = hv_iternext (hv)))
443
444 for (;;) 478 for (;;)
445 { 479 {
446 encode_indent (enc); 480 encode_indent (enc);
447 encode_he (enc, he); 481 encode_hk (enc, he);
482 encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he));
448 483
449 if (!(he = hv_iternext (hv))) 484 if (!(he = hv_iternext (hv)))
450 break; 485 break;
451 486
452 encode_comma (enc); 487 encode_comma (enc);
453 } 488 }
489 }
454 490
455 encode_nl (enc); 491 encode_nl (enc);
456 }
457 }
458 492
459 --enc->indent; encode_indent (enc); encode_ch (enc, '}'); 493 --enc->indent; encode_indent (enc); encode_ch (enc, '}');
460} 494}
461 495
462// encode objects, arrays and special \0=false and \1=true values. 496// encode objects, arrays and special \0=false and \1=true values.
468 SvGETMAGIC (sv); 502 SvGETMAGIC (sv);
469 svt = SvTYPE (sv); 503 svt = SvTYPE (sv);
470 504
471 if (expect_false (SvOBJECT (sv))) 505 if (expect_false (SvOBJECT (sv)))
472 { 506 {
507 HV *stash = !JSON_SLOW || json_boolean_stash
508 ? json_boolean_stash
509 : gv_stashpv ("JSON::XS::Boolean", 1);
510
473 if (SvSTASH (sv) == json_boolean_stash) 511 if (SvSTASH (sv) == stash)
474 { 512 {
475 if (SvIV (sv) == 0) 513 if (SvIV (sv))
514 encode_str (enc, "true", 4, 0);
515 else
476 encode_str (enc, "false", 5, 0); 516 encode_str (enc, "false", 5, 0);
477 else
478 encode_str (enc, "true", 4, 0);
479 } 517 }
480 else 518 else
481 { 519 {
482#if 0 520#if 0
483 if (0 && sv_derived_from (rv, "JSON::Literal")) 521 if (0 && sv_derived_from (rv, "JSON::Literal"))
486 } 524 }
487#endif 525#endif
488 if (enc->json.flags & F_CONV_BLESSED) 526 if (enc->json.flags & F_CONV_BLESSED)
489 { 527 {
490 // 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
491 GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 1); 529 GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 0);
492 530
493 if (to_json) 531 if (to_json)
494 { 532 {
495 dSP; 533 dSP;
496 ENTER; 534
497 SAVETMPS;
498 PUSHMARK (SP); 535 ENTER; SAVETMPS; PUSHMARK (SP);
499 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); 536 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv)));
500 537
501 // 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
502 // check anyways.
503 PUTBACK; 539 PUTBACK;
504 assert (1 == call_sv ((SV *)GvCV (to_json), G_SCALAR)); 540 call_sv ((SV *)GvCV (to_json), G_SCALAR);
505 SPAGAIN; 541 SPAGAIN;
506 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
507 encode_sv (enc, POPs); 550 encode_sv (enc, sv);
508 551
509 FREETMPS; 552 FREETMPS; LEAVE;
510 LEAVE;
511 } 553 }
512 else if (enc->json.flags & F_ALLOW_BLESSED) 554 else if (enc->json.flags & F_ALLOW_BLESSED)
513 encode_str (enc, "null", 4, 0); 555 encode_str (enc, "null", 4, 0);
514 else 556 else
515 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",
526 encode_hv (enc, (HV *)sv); 568 encode_hv (enc, (HV *)sv);
527 else if (svt == SVt_PVAV) 569 else if (svt == SVt_PVAV)
528 encode_av (enc, (AV *)sv); 570 encode_av (enc, (AV *)sv);
529 else if (svt < SVt_PVAV) 571 else if (svt < SVt_PVAV)
530 { 572 {
531 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')
532 encode_str (enc, "false", 5, 0); 579 encode_str (enc, "false", 5, 0);
533 else if (SvNIOK (sv) && SvIV (sv) == 1)
534 encode_str (enc, "true", 4, 0);
535 else 580 else
536 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",
537 SvPV_nolen (sv_2mortal (newRV_inc (sv)))); 582 SvPV_nolen (sv_2mortal (newRV_inc (sv))));
538 } 583 }
539 else 584 else
920 is_nv = 1; 965 is_nv = 1;
921 } 966 }
922 967
923 if (!is_nv) 968 if (!is_nv)
924 { 969 {
970 int len = dec->cur - start;
971
925 // 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
926 if (*start == '-') 973 if (*start == '-')
927 switch (dec->cur - start) 974 switch (len)
928 { 975 {
929 case 2: return newSViv (-( start [1] - '0' * 1)); 976 case 2: return newSViv (-( start [1] - '0' * 1));
930 case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); 977 case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11));
931 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));
932 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));
933 } 980 }
934 else 981 else
935 switch (dec->cur - start) 982 switch (len)
936 { 983 {
937 case 1: return newSViv ( start [0] - '0' * 1); 984 case 1: return newSViv ( start [0] - '0' * 1);
938 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); 985 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11);
939 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);
940 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);
941 } 988 }
942 989
943 { 990 {
944 UV uv; 991 UV uv;
945 int numtype = grok_number (start, dec->cur - start, &uv); 992 int numtype = grok_number (start, len, &uv);
946 if (numtype & IS_NUMBER_IN_UV) 993 if (numtype & IS_NUMBER_IN_UV)
947 if (numtype & IS_NUMBER_NEG) 994 if (numtype & IS_NUMBER_NEG)
948 { 995 {
949 if (uv < (UV)IV_MIN) 996 if (uv < (UV)IV_MIN)
950 return newSViv (-(IV)uv); 997 return newSViv (-(IV)uv);
951 } 998 }
952 else 999 else
953 return newSVuv (uv); 1000 return newSVuv (uv);
954
955 // here would likely be the place for bigint support
956 } 1001 }
957 }
958 1002
959 // 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
960 return newSVnv (Atof (start)); 1019 return newSVnv (Atof (start));
961 1020
962fail: 1021fail:
963 return 0; 1022 return 0;
964} 1023}
1008} 1067}
1009 1068
1010static SV * 1069static SV *
1011decode_hv (dec_t *dec) 1070decode_hv (dec_t *dec)
1012{ 1071{
1072 SV *sv;
1013 HV *hv = newHV (); 1073 HV *hv = newHV ();
1014 1074
1015 DEC_INC_DEPTH; 1075 DEC_INC_DEPTH;
1016 decode_ws (dec); 1076 decode_ws (dec);
1017 1077
1091 1151
1092 ++dec->cur; 1152 ++dec->cur;
1093 } 1153 }
1094 1154
1095 DEC_DEC_DEPTH; 1155 DEC_DEC_DEPTH;
1096 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;
1097 1217
1098fail: 1218fail:
1099 SvREFCNT_dec (hv); 1219 SvREFCNT_dec (hv);
1100 DEC_DEC_DEPTH; 1220 DEC_DEC_DEPTH;
1101 return 0; 1221 return 0;
1121 1241
1122 case 't': 1242 case 't':
1123 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) 1243 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4))
1124 { 1244 {
1125 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
1126 return SvREFCNT_inc (json_true); 1249 return SvREFCNT_inc (json_true);
1127 } 1250 }
1128 else 1251 else
1129 ERR ("'true' expected"); 1252 ERR ("'true' expected");
1130 1253
1132 1255
1133 case 'f': 1256 case 'f':
1134 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) 1257 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5))
1135 { 1258 {
1136 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
1137 return SvREFCNT_inc (json_false); 1263 return SvREFCNT_inc (json_false);
1138 } 1264 }
1139 else 1265 else
1140 ERR ("'false' expected"); 1266 ERR ("'false' expected");
1141 1267
1187 dec.end = SvEND (string); 1313 dec.end = SvEND (string);
1188 dec.err = 0; 1314 dec.err = 0;
1189 dec.depth = 0; 1315 dec.depth = 0;
1190 dec.maxdepth = DEC_DEPTH (dec.json.flags); 1316 dec.maxdepth = DEC_DEPTH (dec.json.flags);
1191 1317
1318 if (dec.json.cb_object || dec.json.cb_sk_object)
1319 dec.json.flags |= F_HOOK;
1320
1192 *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
1193 sv = decode_sv (&dec); 1322 sv = decode_sv (&dec);
1194 1323
1195 if (!(offset_return || !sv)) 1324 if (!(offset_return || !sv))
1196 { 1325 {
1265 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);
1266} 1395}
1267 1396
1268PROTOTYPES: DISABLE 1397PROTOTYPES: DISABLE
1269 1398
1399void CLONE (...)
1400 CODE:
1401 json_stash = 0;
1402 json_boolean_stash = 0;
1403
1270void new (char *klass) 1404void new (char *klass)
1271 PPCODE: 1405 PPCODE:
1272{ 1406{
1273 SV *pv = NEWSV (0, sizeof (JSON)); 1407 SV *pv = NEWSV (0, sizeof (JSON));
1274 SvPOK_only (pv); 1408 SvPOK_only (pv);
1275 Zero (SvPVX (pv), 1, sizeof (JSON)); 1409 Zero (SvPVX (pv), 1, JSON);
1276 ((JSON *)SvPVX (pv))->flags = F_DEFAULT; 1410 ((JSON *)SvPVX (pv))->flags = F_DEFAULT;
1277 XPUSHs (sv_2mortal (sv_bless (newRV_noinc (pv), json_stash))); 1411 XPUSHs (sv_2mortal (sv_bless (newRV_noinc (pv), JSON_STASH)));
1278} 1412}
1279 1413
1280void ascii (JSON *self, int enable = 1) 1414void ascii (JSON *self, int enable = 1)
1281 ALIAS: 1415 ALIAS:
1282 ascii = F_ASCII 1416 ascii = F_ASCII
1330 self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE); 1464 self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE);
1331 1465
1332 XPUSHs (ST (0)); 1466 XPUSHs (ST (0));
1333} 1467}
1334 1468
1469void filter_json_object (JSON *self, SV *cb = &PL_sv_undef)
1470 PPCODE:
1471{
1472 SvREFCNT_dec (self->cb_object);
1473 self->cb_object = SvOK (cb) ? newSVsv (cb) : 0;
1474
1475 XPUSHs (ST (0));
1476}
1477
1478void 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
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
1335void encode (JSON *self, SV *scalar) 1500void encode (JSON *self, SV *scalar)
1336 PPCODE: 1501 PPCODE:
1337 XPUSHs (encode_json (scalar, self)); 1502 XPUSHs (encode_json (scalar, self));
1338 1503
1339void decode (JSON *self, SV *jsonstr) 1504void decode (JSON *self, SV *jsonstr)
1347 EXTEND (SP, 2); 1512 EXTEND (SP, 2);
1348 PUSHs (decode_json (jsonstr, self, &offset)); 1513 PUSHs (decode_json (jsonstr, self, &offset));
1349 PUSHs (sv_2mortal (newSVuv (offset))); 1514 PUSHs (sv_2mortal (newSVuv (offset)));
1350} 1515}
1351 1516
1517void DESTROY (JSON *self)
1518 CODE:
1519 SvREFCNT_dec (self->cb_sk_object);
1520 SvREFCNT_dec (self->cb_object);
1521
1352PROTOTYPES: ENABLE 1522PROTOTYPES: ENABLE
1353 1523
1354void to_json (SV *scalar) 1524void to_json (SV *scalar)
1355 PPCODE: 1525 PPCODE:
1356{ 1526{

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines