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.55 by root, Mon Jul 23 22:57:40 2007 UTC vs.
Revision 1.66 by root, Sun Nov 25 19:02:42 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
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
56# define inline static 59# define inline static
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)
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
61 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 {
301 int i, len = av_len (av); 312 int i, len = av_len (av);
302 313
303 if (enc->indent >= enc->maxdepth) 314 if (enc->indent >= enc->maxdepth)
304 croak ("data structure too deep (hit recursion limit)"); 315 croak ("data structure too deep (hit recursion limit)");
305 316
306 encode_ch (enc, '['); encode_nl (enc); 317 encode_ch (enc, '[');
307 ++enc->indent; 318
319 if (len >= 0)
320 {
321 encode_nl (enc); ++enc->indent;
308 322
309 for (i = 0; i <= len; ++i) 323 for (i = 0; i <= len; ++i)
310 { 324 {
311 SV **svp = av_fetch (av, i, 0); 325 SV **svp = av_fetch (av, i, 0);
312 326
313 encode_indent (enc); 327 encode_indent (enc);
314 328
315 if (svp) 329 if (svp)
316 encode_sv (enc, *svp); 330 encode_sv (enc, *svp);
317 else 331 else
318 encode_str (enc, "null", 4, 0); 332 encode_str (enc, "null", 4, 0);
319 333
320 if (i < len) 334 if (i < len)
321 encode_comma (enc); 335 encode_comma (enc);
322 } 336 }
323 337
338 encode_nl (enc); --enc->indent; encode_indent (enc);
339 }
340
324 encode_nl (enc); 341 encode_ch (enc, ']');
325
326 --enc->indent;
327 encode_indent (enc); encode_ch (enc, ']');
328} 342}
329 343
330static void 344static void
331encode_he (enc_t *enc, HE *he) 345encode_hk (enc_t *enc, HE *he)
332{ 346{
333 encode_ch (enc, '"'); 347 encode_ch (enc, '"');
334 348
335 if (HeKLEN (he) == HEf_SVKEY) 349 if (HeKLEN (he) == HEf_SVKEY)
336 { 350 {
349 encode_ch (enc, '"'); 363 encode_ch (enc, '"');
350 364
351 if (enc->json.flags & F_SPACE_BEFORE) encode_space (enc); 365 if (enc->json.flags & F_SPACE_BEFORE) encode_space (enc);
352 encode_ch (enc, ':'); 366 encode_ch (enc, ':');
353 if (enc->json.flags & F_SPACE_AFTER ) encode_space (enc); 367 if (enc->json.flags & F_SPACE_AFTER ) encode_space (enc);
354 encode_sv (enc, HeVAL (he));
355} 368}
356 369
357// compare hash entries, used when all keys are bytestrings 370// compare hash entries, used when all keys are bytestrings
358static int 371static int
359he_cmp_fast (const void *a_, const void *b_) 372he_cmp_fast (const void *a_, const void *b_)
364 HE *b = *(HE **)b_; 377 HE *b = *(HE **)b_;
365 378
366 STRLEN la = HeKLEN (a); 379 STRLEN la = HeKLEN (a);
367 STRLEN lb = HeKLEN (b); 380 STRLEN lb = HeKLEN (b);
368 381
369 if (!(cmp = memcmp (HeKEY (a), HeKEY (b), la < lb ? la : lb))) 382 if (!(cmp = memcmp (HeKEY (b), HeKEY (a), lb < la ? lb : la)))
370 cmp = la - lb; 383 cmp = lb - la;
371 384
372 return cmp; 385 return cmp;
373} 386}
374 387
375// 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
376static int 389static int
377he_cmp_slow (const void *a, const void *b) 390he_cmp_slow (const void *a, const void *b)
378{ 391{
379 return sv_cmp (HeSVKEY_force (*(HE **)a), HeSVKEY_force (*(HE **)b)); 392 return sv_cmp (HeSVKEY_force (*(HE **)b), HeSVKEY_force (*(HE **)a));
380} 393}
381 394
382static void 395static void
383encode_hv (enc_t *enc, HV *hv) 396encode_hv (enc_t *enc, HV *hv)
384{ 397{
398 HE *he;
385 int count, i; 399 int count;
386 400
387 if (enc->indent >= enc->maxdepth) 401 if (enc->indent >= enc->maxdepth)
388 croak ("data structure too deep (hit recursion limit)"); 402 croak ("data structure too deep (hit recursion limit)");
389 403
390 encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; 404 encode_ch (enc, '{');
391 405
392 if ((count = hv_iterinit (hv)))
393 {
394 // for canonical output we have to sort by keys first 406 // for canonical output we have to sort by keys first
395 // actually, this is mostly due to the stupid so-called 407 // actually, this is mostly due to the stupid so-called
396 // security workaround added somewhere in 5.8.x. 408 // security workaround added somewhere in 5.8.x.
397 // that randomises hash orderings 409 // that randomises hash orderings
398 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))
399 { 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 {
400 int fast = 1; 430 int i, fast = 1;
401 HE *he;
402#if defined(__BORLANDC__) || defined(_MSC_VER) 431#if defined(__BORLANDC__) || defined(_MSC_VER)
403 HE **hes = _alloca (count * sizeof (HE)); 432 HE **hes = _alloca (count * sizeof (HE));
404#else 433#else
405 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
406#endif 435#endif
433 462
434 FREETMPS; 463 FREETMPS;
435 LEAVE; 464 LEAVE;
436 } 465 }
437 466
438 for (i = 0; i < count; ++i) 467 encode_nl (enc); ++enc->indent;
468
469 while (count--)
439 { 470 {
440 encode_indent (enc); 471 encode_indent (enc);
472 he = hes [count];
441 encode_he (enc, hes [i]); 473 encode_hk (enc, he);
474 encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he));
442 475
443 if (i < count - 1) 476 if (count)
444 encode_comma (enc); 477 encode_comma (enc);
445 } 478 }
446 479
447 encode_nl (enc); 480 encode_nl (enc); --enc->indent; encode_indent (enc);
448 } 481 }
482 }
449 else 483 else
484 {
485 if (hv_iterinit (hv) || SvMAGICAL (hv))
486 if ((he = hv_iternext (hv)))
450 { 487 {
451 HE *he = hv_iternext (hv); 488 encode_nl (enc); ++enc->indent;
452 489
453 for (;;) 490 for (;;)
454 { 491 {
455 encode_indent (enc); 492 encode_indent (enc);
456 encode_he (enc, he); 493 encode_hk (enc, he);
494 encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he));
457 495
458 if (!(he = hv_iternext (hv))) 496 if (!(he = hv_iternext (hv)))
459 break; 497 break;
460 498
461 encode_comma (enc); 499 encode_comma (enc);
462 } 500 }
463 501
464 encode_nl (enc); 502 encode_nl (enc); --enc->indent; encode_indent (enc);
465 } 503 }
466 } 504 }
467 505
468 --enc->indent; encode_indent (enc); encode_ch (enc, '}'); 506 encode_ch (enc, '}');
469} 507}
470 508
471// encode objects, arrays and special \0=false and \1=true values. 509// encode objects, arrays and special \0=false and \1=true values.
472static void 510static void
473encode_rv (enc_t *enc, SV *sv) 511encode_rv (enc_t *enc, SV *sv)
477 SvGETMAGIC (sv); 515 SvGETMAGIC (sv);
478 svt = SvTYPE (sv); 516 svt = SvTYPE (sv);
479 517
480 if (expect_false (SvOBJECT (sv))) 518 if (expect_false (SvOBJECT (sv)))
481 { 519 {
520 HV *stash = !JSON_SLOW || json_boolean_stash
521 ? json_boolean_stash
522 : gv_stashpv ("JSON::XS::Boolean", 1);
523
482 if (SvSTASH (sv) == json_boolean_stash) 524 if (SvSTASH (sv) == stash)
483 { 525 {
484 if (SvIV (sv)) 526 if (SvIV (sv))
485 encode_str (enc, "true", 4, 0); 527 encode_str (enc, "true", 4, 0);
486 else 528 else
487 encode_str (enc, "false", 5, 0); 529 encode_str (enc, "false", 5, 0);
499 // 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
500 GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 0); 542 GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 0);
501 543
502 if (to_json) 544 if (to_json)
503 { 545 {
504 int count;
505 dSP; 546 dSP;
506 547
507 ENTER; SAVETMPS; PUSHMARK (SP); 548 ENTER; SAVETMPS; PUSHMARK (SP);
508 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); 549 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv)));
509 550
670 U32 depth; // recursion depth 711 U32 depth; // recursion depth
671 U32 maxdepth; // recursion depth limit 712 U32 maxdepth; // recursion depth limit
672} dec_t; 713} dec_t;
673 714
674inline 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
675decode_ws (dec_t *dec) 725decode_ws (dec_t *dec)
676{ 726{
677 for (;;) 727 for (;;)
678 { 728 {
679 char ch = *dec->cur; 729 char ch = *dec->cur;
680 730
681 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 }
682 || (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)) 743 else if (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)
683 break; 744 break; // parse error, but let higher level handle it, gives better error messages
684 745
685 ++dec->cur; 746 ++dec->cur;
686 } 747 }
687} 748}
688 749
937 is_nv = 1; 998 is_nv = 1;
938 } 999 }
939 1000
940 if (!is_nv) 1001 if (!is_nv)
941 { 1002 {
1003 int len = dec->cur - start;
1004
942 // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so 1005 // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so
943 if (*start == '-') 1006 if (*start == '-')
944 switch (dec->cur - start) 1007 switch (len)
945 { 1008 {
946 case 2: return newSViv (-( start [1] - '0' * 1)); 1009 case 2: return newSViv (-( start [1] - '0' * 1));
947 case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); 1010 case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11));
948 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));
949 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));
950 } 1013 }
951 else 1014 else
952 switch (dec->cur - start) 1015 switch (len)
953 { 1016 {
954 case 1: return newSViv ( start [0] - '0' * 1); 1017 case 1: return newSViv ( start [0] - '0' * 1);
955 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); 1018 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11);
956 case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); 1019 case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111);
957 case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); 1020 case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111);
958 } 1021 }
959 1022
960 { 1023 {
961 UV uv; 1024 UV uv;
962 int numtype = grok_number (start, dec->cur - start, &uv); 1025 int numtype = grok_number (start, len, &uv);
963 if (numtype & IS_NUMBER_IN_UV) 1026 if (numtype & IS_NUMBER_IN_UV)
964 if (numtype & IS_NUMBER_NEG) 1027 if (numtype & IS_NUMBER_NEG)
965 { 1028 {
966 if (uv < (UV)IV_MIN) 1029 if (uv < (UV)IV_MIN)
967 return newSViv (-(IV)uv); 1030 return newSViv (-(IV)uv);
968 } 1031 }
969 else 1032 else
970 return newSVuv (uv); 1033 return newSVuv (uv);
971
972 // here would likely be the place for bigint support
973 } 1034 }
974 }
975 1035
976 // if we ever support bigint or bigfloat, this is the place for bigfloat 1036 len -= *start == '-' ? 1 : 0;
1037
1038 // does not fit into IV or UV, try NV
1039 if ((sizeof (NV) == sizeof (double) && DBL_DIG >= len)
1040 #if defined (LDBL_DIG)
1041 || (sizeof (NV) == sizeof (long double) && LDBL_DIG >= len)
1042 #endif
1043 )
1044 // fits into NV without loss of precision
1045 return newSVnv (Atof (start));
1046
1047 // everything else fails, convert it to a string
1048 return newSVpvn (start, dec->cur - start);
1049 }
1050
1051 // loss of precision here
977 return newSVnv (Atof (start)); 1052 return newSVnv (Atof (start));
978 1053
979fail: 1054fail:
980 return 0; 1055 return 0;
981} 1056}
1011 1086
1012 if (*dec->cur != ',') 1087 if (*dec->cur != ',')
1013 ERR (", or ] expected while parsing array"); 1088 ERR (", or ] expected while parsing array");
1014 1089
1015 ++dec->cur; 1090 ++dec->cur;
1091
1092 decode_ws (dec);
1093
1094 if (*dec->cur == ']' && dec->json.flags & F_RELAXED)
1095 {
1096 ++dec->cur;
1097 break;
1098 }
1016 } 1099 }
1017 1100
1018 DEC_DEC_DEPTH; 1101 DEC_DEC_DEPTH;
1019 return newRV_noinc ((SV *)av); 1102 return newRV_noinc ((SV *)av);
1020 1103
1036 if (*dec->cur == '}') 1119 if (*dec->cur == '}')
1037 ++dec->cur; 1120 ++dec->cur;
1038 else 1121 else
1039 for (;;) 1122 for (;;)
1040 { 1123 {
1041 decode_ws (dec); EXPECT_CH ('"'); 1124 EXPECT_CH ('"');
1042 1125
1043 // heuristic: assume that 1126 // heuristic: assume that
1044 // a) decode_str + hv_store_ent are abysmally slow. 1127 // a) decode_str + hv_store_ent are abysmally slow.
1045 // b) most hash keys are short, simple ascii text. 1128 // b) most hash keys are short, simple ascii text.
1046 // => try to "fast-match" such strings to avoid 1129 // => try to "fast-match" such strings to avoid
1060 if (!key) 1143 if (!key)
1061 goto fail; 1144 goto fail;
1062 1145
1063 decode_ws (dec); EXPECT_CH (':'); 1146 decode_ws (dec); EXPECT_CH (':');
1064 1147
1148 decode_ws (dec);
1065 value = decode_sv (dec); 1149 value = decode_sv (dec);
1066 if (!value) 1150 if (!value)
1067 { 1151 {
1068 SvREFCNT_dec (key); 1152 SvREFCNT_dec (key);
1069 goto fail; 1153 goto fail;
1081 int len = p - key; 1165 int len = p - key;
1082 dec->cur = p + 1; 1166 dec->cur = p + 1;
1083 1167
1084 decode_ws (dec); EXPECT_CH (':'); 1168 decode_ws (dec); EXPECT_CH (':');
1085 1169
1170 decode_ws (dec);
1086 value = decode_sv (dec); 1171 value = decode_sv (dec);
1087 if (!value) 1172 if (!value)
1088 goto fail; 1173 goto fail;
1089 1174
1090 hv_store (hv, key, len, value, 0); 1175 hv_store (hv, key, len, value, 0);
1106 1191
1107 if (*dec->cur != ',') 1192 if (*dec->cur != ',')
1108 ERR (", or } expected while parsing object/hash"); 1193 ERR (", or } expected while parsing object/hash");
1109 1194
1110 ++dec->cur; 1195 ++dec->cur;
1196
1197 decode_ws (dec);
1198
1199 if (*dec->cur == '}' && dec->json.flags & F_RELAXED)
1200 {
1201 ++dec->cur;
1202 break;
1203 }
1111 } 1204 }
1112 1205
1113 DEC_DEC_DEPTH; 1206 DEC_DEC_DEPTH;
1114 sv = newRV_noinc ((SV *)hv); 1207 sv = newRV_noinc ((SV *)hv);
1115 1208
1180} 1273}
1181 1274
1182static SV * 1275static SV *
1183decode_sv (dec_t *dec) 1276decode_sv (dec_t *dec)
1184{ 1277{
1185 decode_ws (dec);
1186
1187 // the beauty of JSON: you need exactly one character lookahead 1278 // the beauty of JSON: you need exactly one character lookahead
1188 // to parse anything. 1279 // to parse anything.
1189 switch (*dec->cur) 1280 switch (*dec->cur)
1190 { 1281 {
1191 case '"': ++dec->cur; return decode_str (dec); 1282 case '"': ++dec->cur; return decode_str (dec);
1199 1290
1200 case 't': 1291 case 't':
1201 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) 1292 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4))
1202 { 1293 {
1203 dec->cur += 4; 1294 dec->cur += 4;
1295#if JSON_SLOW
1296 json_true = get_sv ("JSON::XS::true", 1); SvREADONLY_on (json_true);
1297#endif
1204 return SvREFCNT_inc (json_true); 1298 return SvREFCNT_inc (json_true);
1205 } 1299 }
1206 else 1300 else
1207 ERR ("'true' expected"); 1301 ERR ("'true' expected");
1208 1302
1210 1304
1211 case 'f': 1305 case 'f':
1212 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) 1306 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5))
1213 { 1307 {
1214 dec->cur += 5; 1308 dec->cur += 5;
1309#if JSON_SLOW
1310 json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false);
1311#endif
1215 return SvREFCNT_inc (json_false); 1312 return SvREFCNT_inc (json_false);
1216 } 1313 }
1217 else 1314 else
1218 ERR ("'false' expected"); 1315 ERR ("'false' expected");
1219 1316
1269 1366
1270 if (dec.json.cb_object || dec.json.cb_sk_object) 1367 if (dec.json.cb_object || dec.json.cb_sk_object)
1271 dec.json.flags |= F_HOOK; 1368 dec.json.flags |= F_HOOK;
1272 1369
1273 *dec.end = 0; // this should basically be a nop, too, but make sure it's there 1370 *dec.end = 0; // this should basically be a nop, too, but make sure it's there
1371
1372 decode_ws (&dec);
1274 sv = decode_sv (&dec); 1373 sv = decode_sv (&dec);
1275 1374
1276 if (!(offset_return || !sv)) 1375 if (!(offset_return || !sv))
1277 { 1376 {
1278 // check for trailing garbage 1377 // check for trailing garbage
1346 json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false); 1445 json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false);
1347} 1446}
1348 1447
1349PROTOTYPES: DISABLE 1448PROTOTYPES: DISABLE
1350 1449
1450void CLONE (...)
1451 CODE:
1452 json_stash = 0;
1453 json_boolean_stash = 0;
1454
1351void new (char *klass) 1455void new (char *klass)
1352 PPCODE: 1456 PPCODE:
1353{ 1457{
1354 SV *pv = NEWSV (0, sizeof (JSON)); 1458 SV *pv = NEWSV (0, sizeof (JSON));
1355 SvPOK_only (pv); 1459 SvPOK_only (pv);
1356 Zero (SvPVX (pv), 1, JSON); 1460 Zero (SvPVX (pv), 1, JSON);
1357 ((JSON *)SvPVX (pv))->flags = F_DEFAULT; 1461 ((JSON *)SvPVX (pv))->flags = F_DEFAULT;
1358 XPUSHs (sv_2mortal (sv_bless (newRV_noinc (pv), json_stash))); 1462 XPUSHs (sv_2mortal (sv_bless (newRV_noinc (pv), JSON_STASH)));
1359} 1463}
1360 1464
1361void ascii (JSON *self, int enable = 1) 1465void ascii (JSON *self, int enable = 1)
1362 ALIAS: 1466 ALIAS:
1363 ascii = F_ASCII 1467 ascii = F_ASCII
1370 pretty = F_PRETTY 1474 pretty = F_PRETTY
1371 allow_nonref = F_ALLOW_NONREF 1475 allow_nonref = F_ALLOW_NONREF
1372 shrink = F_SHRINK 1476 shrink = F_SHRINK
1373 allow_blessed = F_ALLOW_BLESSED 1477 allow_blessed = F_ALLOW_BLESSED
1374 convert_blessed = F_CONV_BLESSED 1478 convert_blessed = F_CONV_BLESSED
1479 relaxed = F_RELAXED
1375 PPCODE: 1480 PPCODE:
1376{ 1481{
1377 if (enable) 1482 if (enable)
1378 self->flags |= ix; 1483 self->flags |= ix;
1379 else 1484 else
1380 self->flags &= ~ix; 1485 self->flags &= ~ix;
1381 1486
1382 XPUSHs (ST (0)); 1487 XPUSHs (ST (0));
1383} 1488}
1384 1489
1490void get_ascii (JSON *self)
1491 ALIAS:
1492 get_ascii = F_ASCII
1493 get_latin1 = F_LATIN1
1494 get_utf8 = F_UTF8
1495 get_indent = F_INDENT
1496 get_canonical = F_CANONICAL
1497 get_space_before = F_SPACE_BEFORE
1498 get_space_after = F_SPACE_AFTER
1499 get_pretty = F_PRETTY
1500 get_allow_nonref = F_ALLOW_NONREF
1501 get_shrink = F_SHRINK
1502 get_allow_blessed = F_ALLOW_BLESSED
1503 get_convert_blessed = F_CONV_BLESSED
1504 get_relaxed = F_RELAXED
1505 PPCODE:
1506 XPUSHs (boolSV (self->flags & ix));
1507
1385void max_depth (JSON *self, UV max_depth = 0x80000000UL) 1508void max_depth (JSON *self, UV max_depth = 0x80000000UL)
1386 PPCODE: 1509 PPCODE:
1387{ 1510{
1388 UV log2 = 0; 1511 UV log2 = 0;
1389 1512
1395 self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); 1518 self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH);
1396 1519
1397 XPUSHs (ST (0)); 1520 XPUSHs (ST (0));
1398} 1521}
1399 1522
1523int get_max_depth (JSON *self)
1524 CODE:
1525 RETVAL = DEC_DEPTH (self->flags);
1526 OUTPUT:
1527 RETVAL
1528
1400void max_size (JSON *self, UV max_size = 0) 1529void max_size (JSON *self, UV max_size = 0)
1401 PPCODE: 1530 PPCODE:
1402{ 1531{
1403 UV log2 = 0; 1532 UV log2 = 0;
1404 1533
1410 1539
1411 self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE); 1540 self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE);
1412 1541
1413 XPUSHs (ST (0)); 1542 XPUSHs (ST (0));
1414} 1543}
1544
1545int get_max_size (JSON *self)
1546 CODE:
1547 RETVAL = DEC_SIZE (self->flags);
1548 OUTPUT:
1549 RETVAL
1415 1550
1416void filter_json_object (JSON *self, SV *cb = &PL_sv_undef) 1551void filter_json_object (JSON *self, SV *cb = &PL_sv_undef)
1417 PPCODE: 1552 PPCODE:
1418{ 1553{
1419 SvREFCNT_dec (self->cb_object); 1554 SvREFCNT_dec (self->cb_object);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines