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.49 by root, Sun Jul 1 23:40:07 2007 UTC vs.
Revision 1.61 by root, Sun Aug 26 21:56:47 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
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
62static HV *json_stash, *json_boolean_stash; // JSON::XS:: 71static HV *json_stash, *json_boolean_stash; // JSON::XS::
63static SV *json_true, *json_false; 72static SV *json_true, *json_false;
64 73
65typedef struct { 74typedef 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
323static void 339static void
324encode_he (enc_t *enc, HE *he) 340encode_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
351static int 366static int
352he_cmp_fast (const void *a_, const void *b_) 367he_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
369static int 384static int
370he_cmp_slow (const void *a, const void *b) 385he_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
375static void 390static void
376encode_hv (enc_t *enc, HV *hv) 391encode_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, 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, 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);
524 encode_hv (enc, (HV *)sv); 568 encode_hv (enc, (HV *)sv);
525 else if (svt == SVt_PVAV) 569 else if (svt == SVt_PVAV)
526 encode_av (enc, (AV *)sv); 570 encode_av (enc, (AV *)sv);
527 else if (svt < SVt_PVAV) 571 else if (svt < SVt_PVAV)
528 { 572 {
529 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')
530 encode_str (enc, "false", 5, 0); 579 encode_str (enc, "false", 5, 0);
531 else if (SvNIOK (sv) && SvIV (sv) == 1)
532 encode_str (enc, "true", 4, 0);
533 else 580 else
534 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",
535 SvPV_nolen (sv_2mortal (newRV_inc (sv)))); 582 SvPV_nolen (sv_2mortal (newRV_inc (sv))));
536 } 583 }
537 else 584 else
918 is_nv = 1; 965 is_nv = 1;
919 } 966 }
920 967
921 if (!is_nv) 968 if (!is_nv)
922 { 969 {
970 int len = dec->cur - start;
971
923 // 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
924 if (*start == '-') 973 if (*start == '-')
925 switch (dec->cur - start) 974 switch (len)
926 { 975 {
927 case 2: return newSViv (-( start [1] - '0' * 1)); 976 case 2: return newSViv (-( start [1] - '0' * 1));
928 case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); 977 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)); 978 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)); 979 case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111));
931 } 980 }
932 else 981 else
933 switch (dec->cur - start) 982 switch (len)
934 { 983 {
935 case 1: return newSViv ( start [0] - '0' * 1); 984 case 1: return newSViv ( start [0] - '0' * 1);
936 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); 985 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); 986 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); 987 case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111);
939 } 988 }
940 989
941 { 990 {
942 UV uv; 991 UV uv;
943 int numtype = grok_number (start, dec->cur - start, &uv); 992 int numtype = grok_number (start, len, &uv);
944 if (numtype & IS_NUMBER_IN_UV) 993 if (numtype & IS_NUMBER_IN_UV)
945 if (numtype & IS_NUMBER_NEG) 994 if (numtype & IS_NUMBER_NEG)
946 { 995 {
947 if (uv < (UV)IV_MIN) 996 if (uv < (UV)IV_MIN)
948 return newSViv (-(IV)uv); 997 return newSViv (-(IV)uv);
949 } 998 }
950 else 999 else
951 return newSVuv (uv); 1000 return newSVuv (uv);
952
953 // here would likely be the place for bigint support
954 } 1001 }
955 }
956 1002
957 // 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
958 return newSVnv (Atof (start)); 1019 return newSVnv (Atof (start));
959 1020
960fail: 1021fail:
961 return 0; 1022 return 0;
962} 1023}
1095 sv = newRV_noinc ((SV *)hv); 1156 sv = newRV_noinc ((SV *)hv);
1096 1157
1097 // check filter callbacks 1158 // check filter callbacks
1098 if (dec->json.flags & F_HOOK) 1159 if (dec->json.flags & F_HOOK)
1099 { 1160 {
1100 ENTER; SAVETMPS; 1161 if (dec->json.cb_sk_object && HvKEYS (hv) == 1)
1101
1102 if (HvKEYS (hv) == 1 && dec->json.cb_sk_object)
1103 { 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;
1104 int count; 1176 int count;
1105 1177
1106 dSP; PUSHMARK (SP); 1178 ENTER; SAVETMPS; PUSHMARK (SP);
1107 XPUSHs (sv_2mortal (sv)); 1179 XPUSHs (HeVAL (he));
1108 1180
1109 PUTBACK; count = call_sv (dec->json.cb_sk_object, G_ARRAY); SPAGAIN; 1181 PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN;
1110 1182
1111 if (count == 1) 1183 if (count == 1)
1184 {
1112 sv = newSVsv (POPs); 1185 sv = newSVsv (POPs);
1113 else 1186 FREETMPS; LEAVE;
1114 SvREFCNT_inc (sv); 1187 return sv;
1188 }
1189
1190 FREETMPS; LEAVE;
1191 }
1115 } 1192 }
1116 1193
1117 if (dec->json.cb_object) 1194 if (dec->json.cb_object)
1118 { 1195 {
1196 dSP;
1119 int count; 1197 int count;
1120 1198
1121 dSP; ENTER; SAVETMPS; PUSHMARK (SP); 1199 ENTER; SAVETMPS; PUSHMARK (SP);
1122 XPUSHs (sv_2mortal (sv)); 1200 XPUSHs (sv_2mortal (sv));
1123 1201
1124 PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN; 1202 PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN;
1125 1203
1126 if (count == 1) 1204 if (count == 1)
1205 {
1127 sv = newSVsv (POPs); 1206 sv = newSVsv (POPs);
1128 else 1207 FREETMPS; LEAVE;
1208 return sv;
1209 }
1210
1129 SvREFCNT_inc (sv); 1211 SvREFCNT_inc (sv);
1212 FREETMPS; LEAVE;
1130 } 1213 }
1131
1132 FREETMPS; LEAVE;
1133 } 1214 }
1134 1215
1135 return newRV_noinc ((SV *)hv); 1216 return sv;
1136 1217
1137fail: 1218fail:
1138 SvREFCNT_dec (hv); 1219 SvREFCNT_dec (hv);
1139 DEC_DEC_DEPTH; 1220 DEC_DEC_DEPTH;
1140 return 0; 1221 return 0;
1160 1241
1161 case 't': 1242 case 't':
1162 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) 1243 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4))
1163 { 1244 {
1164 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
1165 return SvREFCNT_inc (json_true); 1249 return SvREFCNT_inc (json_true);
1166 } 1250 }
1167 else 1251 else
1168 ERR ("'true' expected"); 1252 ERR ("'true' expected");
1169 1253
1171 1255
1172 case 'f': 1256 case 'f':
1173 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) 1257 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5))
1174 { 1258 {
1175 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
1176 return SvREFCNT_inc (json_false); 1263 return SvREFCNT_inc (json_false);
1177 } 1264 }
1178 else 1265 else
1179 ERR ("'false' expected"); 1266 ERR ("'false' expected");
1180 1267
1307 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);
1308} 1395}
1309 1396
1310PROTOTYPES: DISABLE 1397PROTOTYPES: DISABLE
1311 1398
1399void CLONE (...)
1400 CODE:
1401 json_stash = 0;
1402 json_boolean_stash = 0;
1403
1312void new (char *klass) 1404void new (char *klass)
1313 PPCODE: 1405 PPCODE:
1314{ 1406{
1315 SV *pv = NEWSV (0, sizeof (JSON)); 1407 SV *pv = NEWSV (0, sizeof (JSON));
1316 SvPOK_only (pv); 1408 SvPOK_only (pv);
1317 Zero (SvPVX (pv), 1, JSON); 1409 Zero (SvPVX (pv), 1, JSON);
1318 ((JSON *)SvPVX (pv))->flags = F_DEFAULT; 1410 ((JSON *)SvPVX (pv))->flags = F_DEFAULT;
1319 XPUSHs (sv_2mortal (sv_bless (newRV_noinc (pv), json_stash))); 1411 XPUSHs (sv_2mortal (sv_bless (newRV_noinc (pv), JSON_STASH)));
1320} 1412}
1321 1413
1322void ascii (JSON *self, int enable = 1) 1414void ascii (JSON *self, int enable = 1)
1323 ALIAS: 1415 ALIAS:
1324 ascii = F_ASCII 1416 ascii = F_ASCII
1372 self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE); 1464 self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE);
1373 1465
1374 XPUSHs (ST (0)); 1466 XPUSHs (ST (0));
1375} 1467}
1376 1468
1377void filter_json_objects (JSON *self, SV *cb = &PL_sv_undef) 1469void filter_json_object (JSON *self, SV *cb = &PL_sv_undef)
1378 ALIAS:
1379 filter_sk_json_objects = 1
1380 PPCODE: 1470 PPCODE:
1381{ 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
1382 if (!SvOK (cb)) 1484 if (SvOK (cb))
1383 cb = 0; 1485 hv_store_ent (self->cb_sk_object, key, newSVsv (cb), 0);
1384 1486 else
1385 switch (ix)
1386 { 1487 {
1387 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);
1388 case 1: self->cb_sk_object = cb; break; 1493 self->cb_sk_object = 0;
1494 }
1389 } 1495 }
1390 1496
1391 XPUSHs (ST (0)); 1497 XPUSHs (ST (0));
1392} 1498}
1393 1499
1406 EXTEND (SP, 2); 1512 EXTEND (SP, 2);
1407 PUSHs (decode_json (jsonstr, self, &offset)); 1513 PUSHs (decode_json (jsonstr, self, &offset));
1408 PUSHs (sv_2mortal (newSVuv (offset))); 1514 PUSHs (sv_2mortal (newSVuv (offset)));
1409} 1515}
1410 1516
1517void DESTROY (JSON *self)
1518 CODE:
1519 SvREFCNT_dec (self->cb_sk_object);
1520 SvREFCNT_dec (self->cb_object);
1521
1411PROTOTYPES: ENABLE 1522PROTOTYPES: ENABLE
1412 1523
1413void to_json (SV *scalar) 1524void to_json (SV *scalar)
1414 PPCODE: 1525 PPCODE:
1415{ 1526{

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines