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.63 by root, Mon Aug 27 01:49:01 2007 UTC vs.
Revision 1.74 by root, Wed Mar 19 15:17:53 2008 UTC

50 50
51#define SB do { 51#define SB do {
52#define SE } while (0) 52#define SE } while (0)
53 53
54#if __GNUC__ >= 3 54#if __GNUC__ >= 3
55# define expect(expr,value) __builtin_expect ((expr),(value)) 55# define expect(expr,value) __builtin_expect ((expr), (value))
56# define inline inline 56# define INLINE static inline
57#else 57#else
58# define expect(expr,value) (expr) 58# define expect(expr,value) (expr)
59# define inline static 59# define INLINE static
60#endif 60#endif
61 61
62#define expect_false(expr) expect ((expr) != 0, 0) 62#define expect_false(expr) expect ((expr) != 0, 0)
63#define expect_true(expr) expect ((expr) != 0, 1) 63#define expect_true(expr) expect ((expr) != 0, 1)
64
65#define IN_RANGE_INC(type,val,beg,end) \
66 ((unsigned type)((unsigned type)(val) - (unsigned type)(beg)) \
67 <= (unsigned type)((unsigned type)(end) - (unsigned type)(beg)))
64 68
65#ifdef USE_ITHREADS 69#ifdef USE_ITHREADS
66# define JSON_SLOW 1 70# define JSON_SLOW 1
67# define JSON_STASH (json_stash ? json_stash : gv_stashpv ("JSON::XS", 1)) 71# define JSON_STASH (json_stash ? json_stash : gv_stashpv ("JSON::XS", 1))
68#else 72#else
80} JSON; 84} JSON;
81 85
82///////////////////////////////////////////////////////////////////////////// 86/////////////////////////////////////////////////////////////////////////////
83// utility functions 87// utility functions
84 88
85inline void 89INLINE void
86shrink (SV *sv) 90shrink (SV *sv)
87{ 91{
88 sv_utf8_downgrade (sv, 1); 92 sv_utf8_downgrade (sv, 1);
89 if (SvLEN (sv) > SvCUR (sv) + 1) 93 if (SvLEN (sv) > SvCUR (sv) + 1)
90 { 94 {
99// decode an utf-8 character and return it, or (UV)-1 in 103// decode an utf-8 character and return it, or (UV)-1 in
100// case of an error. 104// case of an error.
101// we special-case "safe" characters from U+80 .. U+7FF, 105// we special-case "safe" characters from U+80 .. U+7FF,
102// but use the very good perl function to parse anything else. 106// but use the very good perl function to parse anything else.
103// note that we never call this function for a ascii codepoints 107// note that we never call this function for a ascii codepoints
104inline UV 108INLINE UV
105decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen) 109decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen)
106{ 110{
107 if (expect_false (s[0] > 0xdf || s[0] < 0xc2)) 111 if (expect_true (len >= 2
108 return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY); 112 && IN_RANGE_INC (char, s[0], 0xc2, 0xdf)
109 else if (len > 1 && s[1] >= 0x80 && s[1] <= 0xbf) 113 && IN_RANGE_INC (char, s[1], 0x80, 0xbf)))
110 { 114 {
111 *clen = 2; 115 *clen = 2;
112 return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); 116 return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f);
113 } 117 }
114 else 118 else
115 { 119 return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY);
116 *clen = (STRLEN)-1; 120}
117 return (UV)-1; 121
118 } 122// likewise for encoding, also never called for ascii codepoints
123// this function takes advantage of this fact, although current gccs
124// seem to optimise the check for >= 0x80 away anyways
125INLINE unsigned char *
126encode_utf8 (unsigned char *s, UV ch)
127{
128 if (expect_false (ch < 0x000080))
129 *s++ = ch;
130 else if (expect_true (ch < 0x000800))
131 *s++ = 0xc0 | ( ch >> 6),
132 *s++ = 0x80 | ( ch & 0x3f);
133 else if ( ch < 0x010000)
134 *s++ = 0xe0 | ( ch >> 12),
135 *s++ = 0x80 | ((ch >> 6) & 0x3f),
136 *s++ = 0x80 | ( ch & 0x3f);
137 else if ( ch < 0x110000)
138 *s++ = 0xf0 | ( ch >> 18),
139 *s++ = 0x80 | ((ch >> 12) & 0x3f),
140 *s++ = 0x80 | ((ch >> 6) & 0x3f),
141 *s++ = 0x80 | ( ch & 0x3f);
142
143 return s;
119} 144}
120 145
121///////////////////////////////////////////////////////////////////////////// 146/////////////////////////////////////////////////////////////////////////////
122// encoder 147// encoder
123 148
128 char *end; // SvEND (sv) 153 char *end; // SvEND (sv)
129 SV *sv; // result scalar 154 SV *sv; // result scalar
130 JSON json; 155 JSON json;
131 U32 indent; // indentation level 156 U32 indent; // indentation level
132 U32 maxdepth; // max. indentation/recursion level 157 U32 maxdepth; // max. indentation/recursion level
158 UV limit; // escape character values >= this value when encoding
133} enc_t; 159} enc_t;
134 160
135inline void 161INLINE void
136need (enc_t *enc, STRLEN len) 162need (enc_t *enc, STRLEN len)
137{ 163{
138 if (expect_false (enc->cur + len >= enc->end)) 164 if (expect_false (enc->cur + len >= enc->end))
139 { 165 {
140 STRLEN cur = enc->cur - SvPVX (enc->sv); 166 STRLEN cur = enc->cur - SvPVX (enc->sv);
142 enc->cur = SvPVX (enc->sv) + cur; 168 enc->cur = SvPVX (enc->sv) + cur;
143 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; 169 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1;
144 } 170 }
145} 171}
146 172
147inline void 173INLINE void
148encode_ch (enc_t *enc, char ch) 174encode_ch (enc_t *enc, char ch)
149{ 175{
150 need (enc, 1); 176 need (enc, 1);
151 *enc->cur++ = ch; 177 *enc->cur++ = ch;
152} 178}
206 { 232 {
207 uch = ch; 233 uch = ch;
208 clen = 1; 234 clen = 1;
209 } 235 }
210 236
211 if (uch > 0x10FFFFUL) 237 if (uch < 0x80/*0x20*/ || uch >= enc->limit)
212 croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch);
213
214 if (uch < 0x80 || enc->json.flags & F_ASCII || (enc->json.flags & F_LATIN1 && uch > 0xFF))
215 { 238 {
216 if (uch > 0xFFFFUL) 239 if (uch >= 0x10000UL)
217 { 240 {
241 if (uch >= 0x110000UL)
242 croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch);
243
218 need (enc, len += 11); 244 need (enc, len += 11);
219 sprintf (enc->cur, "\\u%04x\\u%04x", 245 sprintf (enc->cur, "\\u%04x\\u%04x",
220 (int)((uch - 0x10000) / 0x400 + 0xD800), 246 (int)((uch - 0x10000) / 0x400 + 0xD800),
221 (int)((uch - 0x10000) % 0x400 + 0xDC00)); 247 (int)((uch - 0x10000) % 0x400 + 0xDC00));
222 enc->cur += 12; 248 enc->cur += 12;
250 while (--clen); 276 while (--clen);
251 } 277 }
252 else 278 else
253 { 279 {
254 need (enc, len += UTF8_MAXBYTES - 1); // never more than 11 bytes needed 280 need (enc, len += UTF8_MAXBYTES - 1); // never more than 11 bytes needed
255 enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); 281 enc->cur = encode_utf8 (enc->cur, uch);
256 ++str; 282 ++str;
257 } 283 }
258 } 284 }
259 } 285 }
260 } 286 }
261 287
262 --len; 288 --len;
263 } 289 }
264} 290}
265 291
266inline void 292INLINE void
267encode_indent (enc_t *enc) 293encode_indent (enc_t *enc)
268{ 294{
269 if (enc->json.flags & F_INDENT) 295 if (enc->json.flags & F_INDENT)
270 { 296 {
271 int spaces = enc->indent * INDENT_STEP; 297 int spaces = enc->indent * INDENT_STEP;
274 memset (enc->cur, ' ', spaces); 300 memset (enc->cur, ' ', spaces);
275 enc->cur += spaces; 301 enc->cur += spaces;
276 } 302 }
277} 303}
278 304
279inline void 305INLINE void
280encode_space (enc_t *enc) 306encode_space (enc_t *enc)
281{ 307{
282 need (enc, 1); 308 need (enc, 1);
283 encode_ch (enc, ' '); 309 encode_ch (enc, ' ');
284} 310}
285 311
286inline void 312INLINE void
287encode_nl (enc_t *enc) 313encode_nl (enc_t *enc)
288{ 314{
289 if (enc->json.flags & F_INDENT) 315 if (enc->json.flags & F_INDENT)
290 { 316 {
291 need (enc, 1); 317 need (enc, 1);
292 encode_ch (enc, '\n'); 318 encode_ch (enc, '\n');
293 } 319 }
294} 320}
295 321
296inline void 322INLINE void
297encode_comma (enc_t *enc) 323encode_comma (enc_t *enc)
298{ 324{
299 encode_ch (enc, ','); 325 encode_ch (enc, ',');
300 326
301 if (enc->json.flags & F_INDENT) 327 if (enc->json.flags & F_INDENT)
312 int i, len = av_len (av); 338 int i, len = av_len (av);
313 339
314 if (enc->indent >= enc->maxdepth) 340 if (enc->indent >= enc->maxdepth)
315 croak ("data structure too deep (hit recursion limit)"); 341 croak ("data structure too deep (hit recursion limit)");
316 342
317 encode_ch (enc, '['); encode_nl (enc); 343 encode_ch (enc, '[');
318 ++enc->indent; 344
345 if (len >= 0)
346 {
347 encode_nl (enc); ++enc->indent;
319 348
320 for (i = 0; i <= len; ++i) 349 for (i = 0; i <= len; ++i)
321 { 350 {
322 SV **svp = av_fetch (av, i, 0); 351 SV **svp = av_fetch (av, i, 0);
323 352
324 encode_indent (enc); 353 encode_indent (enc);
325 354
326 if (svp) 355 if (svp)
327 encode_sv (enc, *svp); 356 encode_sv (enc, *svp);
328 else 357 else
329 encode_str (enc, "null", 4, 0); 358 encode_str (enc, "null", 4, 0);
330 359
331 if (i < len) 360 if (i < len)
332 encode_comma (enc); 361 encode_comma (enc);
333 } 362 }
334 363
364 encode_nl (enc); --enc->indent; encode_indent (enc);
365 }
366
335 encode_nl (enc); 367 encode_ch (enc, ']');
336
337 --enc->indent;
338 encode_indent (enc); encode_ch (enc, ']');
339} 368}
340 369
341static void 370static void
342encode_hk (enc_t *enc, HE *he) 371encode_hk (enc_t *enc, HE *he)
343{ 372{
396 int count; 425 int count;
397 426
398 if (enc->indent >= enc->maxdepth) 427 if (enc->indent >= enc->maxdepth)
399 croak ("data structure too deep (hit recursion limit)"); 428 croak ("data structure too deep (hit recursion limit)");
400 429
401 encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; 430 encode_ch (enc, '{');
402 431
403 // for canonical output we have to sort by keys first 432 // for canonical output we have to sort by keys first
404 // actually, this is mostly due to the stupid so-called 433 // actually, this is mostly due to the stupid so-called
405 // security workaround added somewhere in 5.8.x. 434 // security workaround added somewhere in 5.8.x.
406 // that randomises hash orderings 435 // that randomises hash orderings
459 488
460 FREETMPS; 489 FREETMPS;
461 LEAVE; 490 LEAVE;
462 } 491 }
463 492
493 encode_nl (enc); ++enc->indent;
494
464 while (count--) 495 while (count--)
465 { 496 {
466 encode_indent (enc); 497 encode_indent (enc);
467 he = hes [count]; 498 he = hes [count];
468 encode_hk (enc, he); 499 encode_hk (enc, he);
469 encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he)); 500 encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he));
470 501
471 if (count) 502 if (count)
472 encode_comma (enc); 503 encode_comma (enc);
473 } 504 }
505
506 encode_nl (enc); --enc->indent; encode_indent (enc);
474 } 507 }
475 } 508 }
476 else 509 else
477 { 510 {
478 if (hv_iterinit (hv) || SvMAGICAL (hv)) 511 if (hv_iterinit (hv) || SvMAGICAL (hv))
479 if ((he = hv_iternext (hv))) 512 if ((he = hv_iternext (hv)))
513 {
514 encode_nl (enc); ++enc->indent;
515
480 for (;;) 516 for (;;)
481 { 517 {
482 encode_indent (enc); 518 encode_indent (enc);
483 encode_hk (enc, he); 519 encode_hk (enc, he);
484 encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he)); 520 encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he));
485 521
486 if (!(he = hv_iternext (hv))) 522 if (!(he = hv_iternext (hv)))
487 break; 523 break;
488 524
489 encode_comma (enc); 525 encode_comma (enc);
490 } 526 }
491 }
492 527
528 encode_nl (enc); --enc->indent; encode_indent (enc);
529 }
530 }
531
493 encode_nl (enc); 532 encode_ch (enc, '}');
494
495 --enc->indent; encode_indent (enc); encode_ch (enc, '}');
496} 533}
497 534
498// encode objects, arrays and special \0=false and \1=true values. 535// encode objects, arrays and special \0=false and \1=true values.
499static void 536static void
500encode_rv (enc_t *enc, SV *sv) 537encode_rv (enc_t *enc, SV *sv)
608 Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur); 645 Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur);
609 enc->cur += strlen (enc->cur); 646 enc->cur += strlen (enc->cur);
610 } 647 }
611 else if (SvIOKp (sv)) 648 else if (SvIOKp (sv))
612 { 649 {
613 // we assume we can always read an IV as a UV 650 // we assume we can always read an IV as a UV and vice versa
614 if (SvUV (sv) & ~(UV)0x7fff) 651 // we assume two's complement
652 // we assume no aliasing issues in the union
653 if (SvIsUV (sv) ? SvUVX (sv) > 59000
654 : SvIVX (sv) > 59000 || SvIVX (sv) < -59000)
615 { 655 {
616 // large integer, use the (rather slow) snprintf way. 656 // large integer, use the (rather slow) snprintf way.
617 need (enc, sizeof (UV) * 3); 657 need (enc, sizeof (UV) * 5 / 2 + 1); // CHAR_BIT is at least 8
618 enc->cur += 658 enc->cur +=
619 SvIsUV(sv) 659 SvIsUV(sv)
620 ? snprintf (enc->cur, sizeof (UV) * 3, "%"UVuf, (UV)SvUVX (sv)) 660 ? snprintf (enc->cur, sizeof (UV) * 3, "%"UVuf, (UV)SvUVX (sv))
621 : snprintf (enc->cur, sizeof (UV) * 3, "%"IVdf, (IV)SvIVX (sv)); 661 : snprintf (enc->cur, sizeof (UV) * 3, "%"IVdf, (IV)SvIVX (sv));
622 } 662 }
623 else 663 else
624 { 664 {
625 // optimise the "small number case" 665 // optimise the "small number case"
626 // code will likely be branchless and use only a single multiplication 666 // code will likely be branchless and use only a single multiplication
667 // works for numbers up to 59074
627 I32 i = SvIV (sv); 668 I32 i = SvIVX (sv);
628 U32 u; 669 U32 u;
629 char digit, nz = 0; 670 char digit, nz = 0;
630 671
631 need (enc, 6); 672 need (enc, 6);
632 673
669 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); 710 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE));
670 enc.cur = SvPVX (enc.sv); 711 enc.cur = SvPVX (enc.sv);
671 enc.end = SvEND (enc.sv); 712 enc.end = SvEND (enc.sv);
672 enc.indent = 0; 713 enc.indent = 0;
673 enc.maxdepth = DEC_DEPTH (enc.json.flags); 714 enc.maxdepth = DEC_DEPTH (enc.json.flags);
715 enc.limit = enc.json.flags & F_ASCII ? 0x000080UL
716 : enc.json.flags & F_LATIN1 ? 0x000100UL
717 : 0x110000UL;
674 718
675 SvPOK_only (enc.sv); 719 SvPOK_only (enc.sv);
676 encode_sv (&enc, scalar); 720 encode_sv (&enc, scalar);
677 721
678 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); 722 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv));
699 JSON json; 743 JSON json;
700 U32 depth; // recursion depth 744 U32 depth; // recursion depth
701 U32 maxdepth; // recursion depth limit 745 U32 maxdepth; // recursion depth limit
702} dec_t; 746} dec_t;
703 747
704inline void 748INLINE void
749decode_comment (dec_t *dec)
750{
751 // only '#'-style comments allowed a.t.m.
752
753 while (*dec->cur && *dec->cur != 0x0a && *dec->cur != 0x0d)
754 ++dec->cur;
755}
756
757INLINE void
705decode_ws (dec_t *dec) 758decode_ws (dec_t *dec)
706{ 759{
707 for (;;) 760 for (;;)
708 { 761 {
709 char ch = *dec->cur; 762 char ch = *dec->cur;
710 763
711 if (ch > 0x20 764 if (ch > 0x20)
765 {
766 if (expect_false (ch == '#'))
767 {
768 if (dec->json.flags & F_RELAXED)
769 decode_comment (dec);
770 else
771 break;
772 }
773 else
774 break;
775 }
712 || (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)) 776 else if (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)
713 break; 777 break; // parse error, but let higher level handle it, gives better error messages
714
715 if (ch == '#' && dec->json.flags & F_RELAXED)
716 ++dec->cur;
717 778
718 ++dec->cur; 779 ++dec->cur;
719 } 780 }
720} 781}
721 782
827 888
828 if (hi >= 0x80) 889 if (hi >= 0x80)
829 { 890 {
830 utf8 = 1; 891 utf8 = 1;
831 892
832 cur = (char *)uvuni_to_utf8_flags (cur, hi, 0); 893 cur = encode_utf8 (cur, hi);
833 } 894 }
834 else 895 else
835 *cur++ = hi; 896 *cur++ = hi;
836 } 897 }
837 break; 898 break;
839 default: 900 default:
840 --dec_cur; 901 --dec_cur;
841 ERR ("illegal backslash escape sequence in string"); 902 ERR ("illegal backslash escape sequence in string");
842 } 903 }
843 } 904 }
844 else if (expect_true (ch >= 0x20 && ch <= 0x7f)) 905 else if (expect_true (ch >= 0x20 && ch < 0x80))
845 *cur++ = ch; 906 *cur++ = ch;
846 else if (ch >= 0x80) 907 else if (ch >= 0x80)
847 { 908 {
848 STRLEN clen; 909 STRLEN clen;
849 UV uch; 910 UV uch;
972 1033
973 if (!is_nv) 1034 if (!is_nv)
974 { 1035 {
975 int len = dec->cur - start; 1036 int len = dec->cur - start;
976 1037
977 // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so 1038 // special case the rather common 1..5-digit-int case
978 if (*start == '-') 1039 if (*start == '-')
979 switch (len) 1040 switch (len)
980 { 1041 {
981 case 2: return newSViv (-( start [1] - '0' * 1)); 1042 case 2: return newSViv (-( start [1] - '0' * 1));
982 case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); 1043 case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11));
983 case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); 1044 case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111));
984 case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); 1045 case 5: return newSViv (-( start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111));
1046 case 6: return newSViv (-(start [1] * 10000 + start [2] * 1000 + start [3] * 100 + start [4] * 10 + start [5] - '0' * 11111));
985 } 1047 }
986 else 1048 else
987 switch (len) 1049 switch (len)
988 { 1050 {
989 case 1: return newSViv ( start [0] - '0' * 1); 1051 case 1: return newSViv ( start [0] - '0' * 1);
990 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); 1052 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11);
991 case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); 1053 case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111);
992 case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); 1054 case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111);
1055 case 5: return newSViv ( start [0] * 10000 + start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 11111);
993 } 1056 }
994 1057
995 { 1058 {
996 UV uv; 1059 UV uv;
997 int numtype = grok_number (start, len, &uv); 1060 int numtype = grok_number (start, len, &uv);
1246 1309
1247static SV * 1310static SV *
1248decode_sv (dec_t *dec) 1311decode_sv (dec_t *dec)
1249{ 1312{
1250 // the beauty of JSON: you need exactly one character lookahead 1313 // the beauty of JSON: you need exactly one character lookahead
1251 // to parse anything. 1314 // to parse everything.
1252 switch (*dec->cur) 1315 switch (*dec->cur)
1253 { 1316 {
1254 case '"': ++dec->cur; return decode_str (dec); 1317 case '"': ++dec->cur; return decode_str (dec);
1255 case '[': ++dec->cur; return decode_av (dec); 1318 case '[': ++dec->cur; return decode_av (dec);
1256 case '{': ++dec->cur; return decode_hv (dec); 1319 case '{': ++dec->cur; return decode_hv (dec);
1257 1320
1258 case '-': 1321 case '-':
1259 case '0': case '1': case '2': case '3': case '4': 1322 case '0': case '1': case '2': case '3': case '4':
1260 case '5': case '6': case '7': case '8': case '9': 1323 case '5': case '6': case '7': case '8': case '9':
1261 return decode_num (dec); 1324 return decode_num (dec);
1429{ 1492{
1430 SV *pv = NEWSV (0, sizeof (JSON)); 1493 SV *pv = NEWSV (0, sizeof (JSON));
1431 SvPOK_only (pv); 1494 SvPOK_only (pv);
1432 Zero (SvPVX (pv), 1, JSON); 1495 Zero (SvPVX (pv), 1, JSON);
1433 ((JSON *)SvPVX (pv))->flags = F_DEFAULT; 1496 ((JSON *)SvPVX (pv))->flags = F_DEFAULT;
1434 XPUSHs (sv_2mortal (sv_bless (newRV_noinc (pv), JSON_STASH))); 1497 XPUSHs (sv_2mortal (sv_bless (
1498 newRV_noinc (pv),
1499 strEQ (klass, "JSON::XS") ? JSON_STASH : gv_stashpv (klass, 1)
1500 )));
1435} 1501}
1436 1502
1437void ascii (JSON *self, int enable = 1) 1503void ascii (JSON *self, int enable = 1)
1438 ALIAS: 1504 ALIAS:
1439 ascii = F_ASCII 1505 ascii = F_ASCII
1457 self->flags &= ~ix; 1523 self->flags &= ~ix;
1458 1524
1459 XPUSHs (ST (0)); 1525 XPUSHs (ST (0));
1460} 1526}
1461 1527
1528void get_ascii (JSON *self)
1529 ALIAS:
1530 get_ascii = F_ASCII
1531 get_latin1 = F_LATIN1
1532 get_utf8 = F_UTF8
1533 get_indent = F_INDENT
1534 get_canonical = F_CANONICAL
1535 get_space_before = F_SPACE_BEFORE
1536 get_space_after = F_SPACE_AFTER
1537 get_allow_nonref = F_ALLOW_NONREF
1538 get_shrink = F_SHRINK
1539 get_allow_blessed = F_ALLOW_BLESSED
1540 get_convert_blessed = F_CONV_BLESSED
1541 get_relaxed = F_RELAXED
1542 PPCODE:
1543 XPUSHs (boolSV (self->flags & ix));
1544
1462void max_depth (JSON *self, UV max_depth = 0x80000000UL) 1545void max_depth (JSON *self, UV max_depth = 0x80000000UL)
1463 PPCODE: 1546 PPCODE:
1464{ 1547{
1465 UV log2 = 0; 1548 UV log2 = 0;
1466 1549
1472 self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); 1555 self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH);
1473 1556
1474 XPUSHs (ST (0)); 1557 XPUSHs (ST (0));
1475} 1558}
1476 1559
1560U32 get_max_depth (JSON *self)
1561 CODE:
1562 RETVAL = DEC_DEPTH (self->flags);
1563 OUTPUT:
1564 RETVAL
1565
1477void max_size (JSON *self, UV max_size = 0) 1566void max_size (JSON *self, UV max_size = 0)
1478 PPCODE: 1567 PPCODE:
1479{ 1568{
1480 UV log2 = 0; 1569 UV log2 = 0;
1481 1570
1487 1576
1488 self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE); 1577 self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE);
1489 1578
1490 XPUSHs (ST (0)); 1579 XPUSHs (ST (0));
1491} 1580}
1581
1582int get_max_size (JSON *self)
1583 CODE:
1584 RETVAL = DEC_SIZE (self->flags);
1585 OUTPUT:
1586 RETVAL
1492 1587
1493void filter_json_object (JSON *self, SV *cb = &PL_sv_undef) 1588void filter_json_object (JSON *self, SV *cb = &PL_sv_undef)
1494 PPCODE: 1589 PPCODE:
1495{ 1590{
1496 SvREFCNT_dec (self->cb_object); 1591 SvREFCNT_dec (self->cb_object);
1543 SvREFCNT_dec (self->cb_sk_object); 1638 SvREFCNT_dec (self->cb_sk_object);
1544 SvREFCNT_dec (self->cb_object); 1639 SvREFCNT_dec (self->cb_object);
1545 1640
1546PROTOTYPES: ENABLE 1641PROTOTYPES: ENABLE
1547 1642
1548void to_json (SV *scalar) 1643void encode_json (SV *scalar)
1549 PPCODE: 1644 PPCODE:
1550{ 1645{
1551 JSON json = { F_DEFAULT | F_UTF8 }; 1646 JSON json = { F_DEFAULT | F_UTF8 };
1552 XPUSHs (encode_json (scalar, &json)); 1647 XPUSHs (encode_json (scalar, &json));
1553} 1648}
1554 1649
1555void from_json (SV *jsonstr) 1650void decode_json (SV *jsonstr)
1556 PPCODE: 1651 PPCODE:
1557{ 1652{
1558 JSON json = { F_DEFAULT | F_UTF8 }; 1653 JSON json = { F_DEFAULT | F_UTF8 };
1559 XPUSHs (decode_json (jsonstr, &json, 0)); 1654 XPUSHs (decode_json (jsonstr, &json, 0));
1560} 1655}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines