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.57 by root, Mon Aug 13 16:05:42 2007 UTC vs.
Revision 1.70 by root, Wed Mar 19 00:44:54 2008 UTC

27#define F_SPACE_AFTER 0x00000040UL 27#define F_SPACE_AFTER 0x00000040UL
28#define F_ALLOW_NONREF 0x00000100UL 28#define F_ALLOW_NONREF 0x00000100UL
29#define F_SHRINK 0x00000200UL 29#define F_SHRINK 0x00000200UL
30#define F_ALLOW_BLESSED 0x00000400UL 30#define F_ALLOW_BLESSED 0x00000400UL
31#define F_CONV_BLESSED 0x00000800UL 31#define F_CONV_BLESSED 0x00000800UL
32#define F_RELAXED 0x00001000UL
33
32#define F_MAXDEPTH 0xf8000000UL 34#define F_MAXDEPTH 0xf8000000UL
33#define S_MAXDEPTH 27 35#define S_MAXDEPTH 27
34#define F_MAXSIZE 0x01f00000UL 36#define F_MAXSIZE 0x01f00000UL
35#define S_MAXSIZE 20 37#define S_MAXSIZE 20
36#define F_HOOK 0x00080000UL // some hooks exist, so slow-path processing 38#define F_HOOK 0x00080000UL // some hooks exist, so slow-path processing
60#define expect_false(expr) expect ((expr) != 0, 0) 62#define expect_false(expr) expect ((expr) != 0, 0)
61#define expect_true(expr) expect ((expr) != 0, 1) 63#define expect_true(expr) expect ((expr) != 0, 1)
62 64
63#ifdef USE_ITHREADS 65#ifdef USE_ITHREADS
64# define JSON_SLOW 1 66# define JSON_SLOW 1
67# define JSON_STASH (json_stash ? json_stash : gv_stashpv ("JSON::XS", 1))
65#else 68#else
66# define JSON_SLOW 1 69# define JSON_SLOW 0
70# define JSON_STASH json_stash
67#endif 71#endif
68 72
69static HV *json_stash, *json_boolean_stash; // JSON::XS:: 73static HV *json_stash, *json_boolean_stash; // JSON::XS::
70static SV *json_true, *json_false; 74static SV *json_true, *json_false;
71 75
308 int i, len = av_len (av); 312 int i, len = av_len (av);
309 313
310 if (enc->indent >= enc->maxdepth) 314 if (enc->indent >= enc->maxdepth)
311 croak ("data structure too deep (hit recursion limit)"); 315 croak ("data structure too deep (hit recursion limit)");
312 316
313 encode_ch (enc, '['); encode_nl (enc); 317 encode_ch (enc, '[');
314 ++enc->indent; 318
319 if (len >= 0)
320 {
321 encode_nl (enc); ++enc->indent;
315 322
316 for (i = 0; i <= len; ++i) 323 for (i = 0; i <= len; ++i)
317 { 324 {
318 SV **svp = av_fetch (av, i, 0); 325 SV **svp = av_fetch (av, i, 0);
319 326
320 encode_indent (enc); 327 encode_indent (enc);
321 328
322 if (svp) 329 if (svp)
323 encode_sv (enc, *svp); 330 encode_sv (enc, *svp);
324 else 331 else
325 encode_str (enc, "null", 4, 0); 332 encode_str (enc, "null", 4, 0);
326 333
327 if (i < len) 334 if (i < len)
328 encode_comma (enc); 335 encode_comma (enc);
329 } 336 }
330 337
338 encode_nl (enc); --enc->indent; encode_indent (enc);
339 }
340
331 encode_nl (enc); 341 encode_ch (enc, ']');
332
333 --enc->indent;
334 encode_indent (enc); encode_ch (enc, ']');
335} 342}
336 343
337static void 344static void
338encode_he (enc_t *enc, HE *he) 345encode_hk (enc_t *enc, HE *he)
339{ 346{
340 encode_ch (enc, '"'); 347 encode_ch (enc, '"');
341 348
342 if (HeKLEN (he) == HEf_SVKEY) 349 if (HeKLEN (he) == HEf_SVKEY)
343 { 350 {
356 encode_ch (enc, '"'); 363 encode_ch (enc, '"');
357 364
358 if (enc->json.flags & F_SPACE_BEFORE) encode_space (enc); 365 if (enc->json.flags & F_SPACE_BEFORE) encode_space (enc);
359 encode_ch (enc, ':'); 366 encode_ch (enc, ':');
360 if (enc->json.flags & F_SPACE_AFTER ) encode_space (enc); 367 if (enc->json.flags & F_SPACE_AFTER ) encode_space (enc);
361 encode_sv (enc, HeVAL (he));
362} 368}
363 369
364// compare hash entries, used when all keys are bytestrings 370// compare hash entries, used when all keys are bytestrings
365static int 371static int
366he_cmp_fast (const void *a_, const void *b_) 372he_cmp_fast (const void *a_, const void *b_)
371 HE *b = *(HE **)b_; 377 HE *b = *(HE **)b_;
372 378
373 STRLEN la = HeKLEN (a); 379 STRLEN la = HeKLEN (a);
374 STRLEN lb = HeKLEN (b); 380 STRLEN lb = HeKLEN (b);
375 381
376 if (!(cmp = memcmp (HeKEY (a), HeKEY (b), la < lb ? la : lb))) 382 if (!(cmp = memcmp (HeKEY (b), HeKEY (a), lb < la ? lb : la)))
377 cmp = la - lb; 383 cmp = lb - la;
378 384
379 return cmp; 385 return cmp;
380} 386}
381 387
382// 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
383static int 389static int
384he_cmp_slow (const void *a, const void *b) 390he_cmp_slow (const void *a, const void *b)
385{ 391{
386 return sv_cmp (HeSVKEY_force (*(HE **)a), HeSVKEY_force (*(HE **)b)); 392 return sv_cmp (HeSVKEY_force (*(HE **)b), HeSVKEY_force (*(HE **)a));
387} 393}
388 394
389static void 395static void
390encode_hv (enc_t *enc, HV *hv) 396encode_hv (enc_t *enc, HV *hv)
391{ 397{
398 HE *he;
392 int count, i; 399 int count;
393 400
394 if (enc->indent >= enc->maxdepth) 401 if (enc->indent >= enc->maxdepth)
395 croak ("data structure too deep (hit recursion limit)"); 402 croak ("data structure too deep (hit recursion limit)");
396 403
397 encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; 404 encode_ch (enc, '{');
398 405
399 if ((count = hv_iterinit (hv)))
400 {
401 // for canonical output we have to sort by keys first 406 // for canonical output we have to sort by keys first
402 // actually, this is mostly due to the stupid so-called 407 // actually, this is mostly due to the stupid so-called
403 // security workaround added somewhere in 5.8.x. 408 // security workaround added somewhere in 5.8.x.
404 // that randomises hash orderings 409 // that randomises hash orderings
405 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))
406 { 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 {
407 int fast = 1; 430 int i, fast = 1;
408 HE *he;
409#if defined(__BORLANDC__) || defined(_MSC_VER) 431#if defined(__BORLANDC__) || defined(_MSC_VER)
410 HE **hes = _alloca (count * sizeof (HE)); 432 HE **hes = _alloca (count * sizeof (HE));
411#else 433#else
412 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
413#endif 435#endif
440 462
441 FREETMPS; 463 FREETMPS;
442 LEAVE; 464 LEAVE;
443 } 465 }
444 466
445 for (i = 0; i < count; ++i) 467 encode_nl (enc); ++enc->indent;
468
469 while (count--)
446 { 470 {
447 encode_indent (enc); 471 encode_indent (enc);
472 he = hes [count];
448 encode_he (enc, hes [i]); 473 encode_hk (enc, he);
474 encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he));
449 475
450 if (i < count - 1) 476 if (count)
451 encode_comma (enc); 477 encode_comma (enc);
452 } 478 }
453 479
454 encode_nl (enc); 480 encode_nl (enc); --enc->indent; encode_indent (enc);
455 } 481 }
482 }
456 else 483 else
484 {
485 if (hv_iterinit (hv) || SvMAGICAL (hv))
486 if ((he = hv_iternext (hv)))
457 { 487 {
458 HE *he = hv_iternext (hv); 488 encode_nl (enc); ++enc->indent;
459 489
460 for (;;) 490 for (;;)
461 { 491 {
462 encode_indent (enc); 492 encode_indent (enc);
463 encode_he (enc, he); 493 encode_hk (enc, he);
494 encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he));
464 495
465 if (!(he = hv_iternext (hv))) 496 if (!(he = hv_iternext (hv)))
466 break; 497 break;
467 498
468 encode_comma (enc); 499 encode_comma (enc);
469 } 500 }
470 501
471 encode_nl (enc); 502 encode_nl (enc); --enc->indent; encode_indent (enc);
472 } 503 }
473 } 504 }
474 505
475 --enc->indent; encode_indent (enc); encode_ch (enc, '}'); 506 encode_ch (enc, '}');
476} 507}
477 508
478// encode objects, arrays and special \0=false and \1=true values. 509// encode objects, arrays and special \0=false and \1=true values.
479static void 510static void
480encode_rv (enc_t *enc, SV *sv) 511encode_rv (enc_t *enc, SV *sv)
510 // 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
511 GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 0); 542 GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 0);
512 543
513 if (to_json) 544 if (to_json)
514 { 545 {
515 int count;
516 dSP; 546 dSP;
517 547
518 ENTER; SAVETMPS; PUSHMARK (SP); 548 ENTER; SAVETMPS; PUSHMARK (SP);
519 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); 549 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv)));
520 550
681 U32 depth; // recursion depth 711 U32 depth; // recursion depth
682 U32 maxdepth; // recursion depth limit 712 U32 maxdepth; // recursion depth limit
683} dec_t; 713} dec_t;
684 714
685inline 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
686decode_ws (dec_t *dec) 725decode_ws (dec_t *dec)
687{ 726{
688 for (;;) 727 for (;;)
689 { 728 {
690 char ch = *dec->cur; 729 char ch = *dec->cur;
691 730
692 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 }
693 || (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)) 743 else if (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)
694 break; 744 break; // parse error, but let higher level handle it, gives better error messages
695 745
696 ++dec->cur; 746 ++dec->cur;
697 } 747 }
698} 748}
699 749
950 1000
951 if (!is_nv) 1001 if (!is_nv)
952 { 1002 {
953 int len = dec->cur - start; 1003 int len = dec->cur - start;
954 1004
955 // 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
956 if (*start == '-') 1006 if (*start == '-')
957 switch (len) 1007 switch (len)
958 { 1008 {
959 case 2: return newSViv (-( start [1] - '0' * 1)); 1009 case 2: return newSViv (-( start [1] - '0' * 1));
960 case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); 1010 case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11));
961 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));
962 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));
963 } 1014 }
964 else 1015 else
965 switch (len) 1016 switch (len)
966 { 1017 {
967 case 1: return newSViv ( start [0] - '0' * 1); 1018 case 1: return newSViv ( start [0] - '0' * 1);
968 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); 1019 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11);
969 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);
970 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);
971 } 1023 }
972 1024
973 { 1025 {
974 UV uv; 1026 UV uv;
975 int numtype = grok_number (start, len, &uv); 1027 int numtype = grok_number (start, len, &uv);
1036 1088
1037 if (*dec->cur != ',') 1089 if (*dec->cur != ',')
1038 ERR (", or ] expected while parsing array"); 1090 ERR (", or ] expected while parsing array");
1039 1091
1040 ++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 }
1041 } 1101 }
1042 1102
1043 DEC_DEC_DEPTH; 1103 DEC_DEC_DEPTH;
1044 return newRV_noinc ((SV *)av); 1104 return newRV_noinc ((SV *)av);
1045 1105
1061 if (*dec->cur == '}') 1121 if (*dec->cur == '}')
1062 ++dec->cur; 1122 ++dec->cur;
1063 else 1123 else
1064 for (;;) 1124 for (;;)
1065 { 1125 {
1066 decode_ws (dec); EXPECT_CH ('"'); 1126 EXPECT_CH ('"');
1067 1127
1068 // heuristic: assume that 1128 // heuristic: assume that
1069 // a) decode_str + hv_store_ent are abysmally slow. 1129 // a) decode_str + hv_store_ent are abysmally slow.
1070 // b) most hash keys are short, simple ascii text. 1130 // b) most hash keys are short, simple ascii text.
1071 // => try to "fast-match" such strings to avoid 1131 // => try to "fast-match" such strings to avoid
1085 if (!key) 1145 if (!key)
1086 goto fail; 1146 goto fail;
1087 1147
1088 decode_ws (dec); EXPECT_CH (':'); 1148 decode_ws (dec); EXPECT_CH (':');
1089 1149
1150 decode_ws (dec);
1090 value = decode_sv (dec); 1151 value = decode_sv (dec);
1091 if (!value) 1152 if (!value)
1092 { 1153 {
1093 SvREFCNT_dec (key); 1154 SvREFCNT_dec (key);
1094 goto fail; 1155 goto fail;
1106 int len = p - key; 1167 int len = p - key;
1107 dec->cur = p + 1; 1168 dec->cur = p + 1;
1108 1169
1109 decode_ws (dec); EXPECT_CH (':'); 1170 decode_ws (dec); EXPECT_CH (':');
1110 1171
1172 decode_ws (dec);
1111 value = decode_sv (dec); 1173 value = decode_sv (dec);
1112 if (!value) 1174 if (!value)
1113 goto fail; 1175 goto fail;
1114 1176
1115 hv_store (hv, key, len, value, 0); 1177 hv_store (hv, key, len, value, 0);
1131 1193
1132 if (*dec->cur != ',') 1194 if (*dec->cur != ',')
1133 ERR (", or } expected while parsing object/hash"); 1195 ERR (", or } expected while parsing object/hash");
1134 1196
1135 ++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 }
1136 } 1206 }
1137 1207
1138 DEC_DEC_DEPTH; 1208 DEC_DEC_DEPTH;
1139 sv = newRV_noinc ((SV *)hv); 1209 sv = newRV_noinc ((SV *)hv);
1140 1210
1205} 1275}
1206 1276
1207static SV * 1277static SV *
1208decode_sv (dec_t *dec) 1278decode_sv (dec_t *dec)
1209{ 1279{
1210 decode_ws (dec);
1211
1212 // the beauty of JSON: you need exactly one character lookahead 1280 // the beauty of JSON: you need exactly one character lookahead
1213 // to parse anything. 1281 // to parse anything.
1214 switch (*dec->cur) 1282 switch (*dec->cur)
1215 { 1283 {
1216 case '"': ++dec->cur; return decode_str (dec); 1284 case '"': ++dec->cur; return decode_str (dec);
1224 1292
1225 case 't': 1293 case 't':
1226 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) 1294 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4))
1227 { 1295 {
1228 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
1229 return SvREFCNT_inc (json_true); 1300 return SvREFCNT_inc (json_true);
1230 } 1301 }
1231 else 1302 else
1232 ERR ("'true' expected"); 1303 ERR ("'true' expected");
1233 1304
1235 1306
1236 case 'f': 1307 case 'f':
1237 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) 1308 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5))
1238 { 1309 {
1239 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
1240 return SvREFCNT_inc (json_false); 1314 return SvREFCNT_inc (json_false);
1241 } 1315 }
1242 else 1316 else
1243 ERR ("'false' expected"); 1317 ERR ("'false' expected");
1244 1318
1294 1368
1295 if (dec.json.cb_object || dec.json.cb_sk_object) 1369 if (dec.json.cb_object || dec.json.cb_sk_object)
1296 dec.json.flags |= F_HOOK; 1370 dec.json.flags |= F_HOOK;
1297 1371
1298 *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);
1299 sv = decode_sv (&dec); 1375 sv = decode_sv (&dec);
1300 1376
1301 if (!(offset_return || !sv)) 1377 if (!(offset_return || !sv))
1302 { 1378 {
1303 // check for trailing garbage 1379 // check for trailing garbage
1379 json_boolean_stash = 0; 1455 json_boolean_stash = 0;
1380 1456
1381void new (char *klass) 1457void new (char *klass)
1382 PPCODE: 1458 PPCODE:
1383{ 1459{
1384 HV *stash = !JSON_SLOW || json_stash
1385 ? json_stash
1386 : gv_stashpv ("JSON::XS", 1);
1387 SV *pv = NEWSV (0, sizeof (JSON)); 1460 SV *pv = NEWSV (0, sizeof (JSON));
1388 SvPOK_only (pv); 1461 SvPOK_only (pv);
1389 Zero (SvPVX (pv), 1, JSON); 1462 Zero (SvPVX (pv), 1, JSON);
1390 ((JSON *)SvPVX (pv))->flags = F_DEFAULT; 1463 ((JSON *)SvPVX (pv))->flags = F_DEFAULT;
1391 XPUSHs (sv_2mortal (sv_bless (newRV_noinc (pv), stash))); 1464 XPUSHs (sv_2mortal (sv_bless (
1465 newRV_noinc (pv),
1466 strEQ (klass, "JSON::XS") ? JSON_STASH : gv_stashpv (klass, 1)
1467 )));
1392} 1468}
1393 1469
1394void ascii (JSON *self, int enable = 1) 1470void ascii (JSON *self, int enable = 1)
1395 ALIAS: 1471 ALIAS:
1396 ascii = F_ASCII 1472 ascii = F_ASCII
1403 pretty = F_PRETTY 1479 pretty = F_PRETTY
1404 allow_nonref = F_ALLOW_NONREF 1480 allow_nonref = F_ALLOW_NONREF
1405 shrink = F_SHRINK 1481 shrink = F_SHRINK
1406 allow_blessed = F_ALLOW_BLESSED 1482 allow_blessed = F_ALLOW_BLESSED
1407 convert_blessed = F_CONV_BLESSED 1483 convert_blessed = F_CONV_BLESSED
1484 relaxed = F_RELAXED
1408 PPCODE: 1485 PPCODE:
1409{ 1486{
1410 if (enable) 1487 if (enable)
1411 self->flags |= ix; 1488 self->flags |= ix;
1412 else 1489 else
1413 self->flags &= ~ix; 1490 self->flags &= ~ix;
1414 1491
1415 XPUSHs (ST (0)); 1492 XPUSHs (ST (0));
1416} 1493}
1417 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
1418void max_depth (JSON *self, UV max_depth = 0x80000000UL) 1512void max_depth (JSON *self, UV max_depth = 0x80000000UL)
1419 PPCODE: 1513 PPCODE:
1420{ 1514{
1421 UV log2 = 0; 1515 UV log2 = 0;
1422 1516
1428 self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); 1522 self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH);
1429 1523
1430 XPUSHs (ST (0)); 1524 XPUSHs (ST (0));
1431} 1525}
1432 1526
1527U32 get_max_depth (JSON *self)
1528 CODE:
1529 RETVAL = DEC_DEPTH (self->flags);
1530 OUTPUT:
1531 RETVAL
1532
1433void max_size (JSON *self, UV max_size = 0) 1533void max_size (JSON *self, UV max_size = 0)
1434 PPCODE: 1534 PPCODE:
1435{ 1535{
1436 UV log2 = 0; 1536 UV log2 = 0;
1437 1537
1443 1543
1444 self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE); 1544 self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE);
1445 1545
1446 XPUSHs (ST (0)); 1546 XPUSHs (ST (0));
1447} 1547}
1548
1549int get_max_size (JSON *self)
1550 CODE:
1551 RETVAL = DEC_SIZE (self->flags);
1552 OUTPUT:
1553 RETVAL
1448 1554
1449void filter_json_object (JSON *self, SV *cb = &PL_sv_undef) 1555void filter_json_object (JSON *self, SV *cb = &PL_sv_undef)
1450 PPCODE: 1556 PPCODE:
1451{ 1557{
1452 SvREFCNT_dec (self->cb_object); 1558 SvREFCNT_dec (self->cb_object);
1499 SvREFCNT_dec (self->cb_sk_object); 1605 SvREFCNT_dec (self->cb_sk_object);
1500 SvREFCNT_dec (self->cb_object); 1606 SvREFCNT_dec (self->cb_object);
1501 1607
1502PROTOTYPES: ENABLE 1608PROTOTYPES: ENABLE
1503 1609
1504void to_json (SV *scalar) 1610void encode_json (SV *scalar)
1505 PPCODE: 1611 PPCODE:
1506{ 1612{
1507 JSON json = { F_DEFAULT | F_UTF8 }; 1613 JSON json = { F_DEFAULT | F_UTF8 };
1508 XPUSHs (encode_json (scalar, &json)); 1614 XPUSHs (encode_json (scalar, &json));
1509} 1615}
1510 1616
1511void from_json (SV *jsonstr) 1617void decode_json (SV *jsonstr)
1512 PPCODE: 1618 PPCODE:
1513{ 1619{
1514 JSON json = { F_DEFAULT | F_UTF8 }; 1620 JSON json = { F_DEFAULT | F_UTF8 };
1515 XPUSHs (decode_json (jsonstr, &json, 0)); 1621 XPUSHs (decode_json (jsonstr, &json, 0));
1516} 1622}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines