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.76 by root, Thu Mar 20 00:56:37 2008 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 <limits.h>
10#include <float.h>
9 11
10#if defined(__BORLANDC__) || defined(_MSC_VER) 12#if defined(__BORLANDC__) || defined(_MSC_VER)
11# define snprintf _snprintf // C compilers have this in stdio.h 13# define snprintf _snprintf // C compilers have this in stdio.h
12#endif 14#endif
13 15
14// some old perls do not have this, try to make it work, no 16// some old perls do not have this, try to make it work, no
15// guarentees, though. if it breaks, you get to keep the pieces. 17// guarentees, though. if it breaks, you get to keep the pieces.
16#ifndef UTF8_MAXBYTES 18#ifndef UTF8_MAXBYTES
17# define UTF8_MAXBYTES 13 19# define UTF8_MAXBYTES 13
18#endif 20#endif
21
22#define IVUV_MAXCHARS (sizeof (UV) * CHAR_BIT * 28 / 93 + 2)
19 23
20#define F_ASCII 0x00000001UL 24#define F_ASCII 0x00000001UL
21#define F_LATIN1 0x00000002UL 25#define F_LATIN1 0x00000002UL
22#define F_UTF8 0x00000004UL 26#define F_UTF8 0x00000004UL
23#define F_INDENT 0x00000008UL 27#define F_INDENT 0x00000008UL
25#define F_SPACE_BEFORE 0x00000020UL 29#define F_SPACE_BEFORE 0x00000020UL
26#define F_SPACE_AFTER 0x00000040UL 30#define F_SPACE_AFTER 0x00000040UL
27#define F_ALLOW_NONREF 0x00000100UL 31#define F_ALLOW_NONREF 0x00000100UL
28#define F_SHRINK 0x00000200UL 32#define F_SHRINK 0x00000200UL
29#define F_ALLOW_BLESSED 0x00000400UL 33#define F_ALLOW_BLESSED 0x00000400UL
30#define F_CONV_BLESSED 0x00000800UL // NYI 34#define F_CONV_BLESSED 0x00000800UL
35#define F_RELAXED 0x00001000UL
36
31#define F_MAXDEPTH 0xf8000000UL 37#define F_MAXDEPTH 0xf8000000UL
32#define S_MAXDEPTH 27 38#define S_MAXDEPTH 27
33#define F_MAXSIZE 0x01f00000UL 39#define F_MAXSIZE 0x01f00000UL
34#define S_MAXSIZE 20 40#define S_MAXSIZE 20
41#define F_HOOK 0x00080000UL // some hooks exist, so slow-path processing
35 42
36#define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) 43#define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH))
37#define DEC_SIZE(flags) (1UL << ((flags & F_MAXSIZE ) >> S_MAXSIZE )) 44#define DEC_SIZE(flags) (1UL << ((flags & F_MAXSIZE ) >> S_MAXSIZE ))
38 45
39#define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER 46#define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER
46 53
47#define SB do { 54#define SB do {
48#define SE } while (0) 55#define SE } while (0)
49 56
50#if __GNUC__ >= 3 57#if __GNUC__ >= 3
51# define expect(expr,value) __builtin_expect ((expr),(value)) 58# define expect(expr,value) __builtin_expect ((expr), (value))
52# define inline inline 59# define INLINE static inline
53#else 60#else
54# define expect(expr,value) (expr) 61# define expect(expr,value) (expr)
55# define inline static 62# define INLINE static
56#endif 63#endif
57 64
58#define expect_false(expr) expect ((expr) != 0, 0) 65#define expect_false(expr) expect ((expr) != 0, 0)
59#define expect_true(expr) expect ((expr) != 0, 1) 66#define expect_true(expr) expect ((expr) != 0, 1)
60 67
68#define IN_RANGE_INC(type,val,beg,end) \
69 ((unsigned type)((unsigned type)(val) - (unsigned type)(beg)) \
70 <= (unsigned type)((unsigned type)(end) - (unsigned type)(beg)))
71
72#ifdef USE_ITHREADS
73# define JSON_SLOW 1
74# define JSON_STASH (json_stash ? json_stash : gv_stashpv ("JSON::XS", 1))
75#else
76# define JSON_SLOW 0
77# define JSON_STASH json_stash
78#endif
79
61static HV *json_stash, *json_boolean_stash; // JSON::XS:: 80static HV *json_stash, *json_boolean_stash; // JSON::XS::
62static SV *json_true, *json_false; 81static SV *json_true, *json_false;
63 82
64typedef struct { 83typedef struct {
65 U32 flags; 84 U32 flags;
85 SV *cb_object;
86 HV *cb_sk_object;
66} JSON; 87} JSON;
67 88
68///////////////////////////////////////////////////////////////////////////// 89/////////////////////////////////////////////////////////////////////////////
69// utility functions 90// utility functions
70 91
71inline void 92INLINE void
72shrink (SV *sv) 93shrink (SV *sv)
73{ 94{
74 sv_utf8_downgrade (sv, 1); 95 sv_utf8_downgrade (sv, 1);
75 if (SvLEN (sv) > SvCUR (sv) + 1) 96 if (SvLEN (sv) > SvCUR (sv) + 1)
76 { 97 {
85// decode an utf-8 character and return it, or (UV)-1 in 106// decode an utf-8 character and return it, or (UV)-1 in
86// case of an error. 107// case of an error.
87// we special-case "safe" characters from U+80 .. U+7FF, 108// we special-case "safe" characters from U+80 .. U+7FF,
88// but use the very good perl function to parse anything else. 109// but use the very good perl function to parse anything else.
89// note that we never call this function for a ascii codepoints 110// note that we never call this function for a ascii codepoints
90inline UV 111INLINE UV
91decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen) 112decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen)
92{ 113{
93 if (expect_false (s[0] > 0xdf || s[0] < 0xc2)) 114 if (expect_true (len >= 2
94 return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY); 115 && IN_RANGE_INC (char, s[0], 0xc2, 0xdf)
95 else if (len > 1 && s[1] >= 0x80 && s[1] <= 0xbf) 116 && IN_RANGE_INC (char, s[1], 0x80, 0xbf)))
96 { 117 {
97 *clen = 2; 118 *clen = 2;
98 return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); 119 return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f);
99 } 120 }
100 else 121 else
101 { 122 return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY);
102 *clen = (STRLEN)-1; 123}
103 return (UV)-1; 124
104 } 125// likewise for encoding, also never called for ascii codepoints
126// this function takes advantage of this fact, although current gccs
127// seem to optimise the check for >= 0x80 away anyways
128INLINE unsigned char *
129encode_utf8 (unsigned char *s, UV ch)
130{
131 if (expect_false (ch < 0x000080))
132 *s++ = ch;
133 else if (expect_true (ch < 0x000800))
134 *s++ = 0xc0 | ( ch >> 6),
135 *s++ = 0x80 | ( ch & 0x3f);
136 else if ( ch < 0x010000)
137 *s++ = 0xe0 | ( ch >> 12),
138 *s++ = 0x80 | ((ch >> 6) & 0x3f),
139 *s++ = 0x80 | ( ch & 0x3f);
140 else if ( ch < 0x110000)
141 *s++ = 0xf0 | ( ch >> 18),
142 *s++ = 0x80 | ((ch >> 12) & 0x3f),
143 *s++ = 0x80 | ((ch >> 6) & 0x3f),
144 *s++ = 0x80 | ( ch & 0x3f);
145
146 return s;
105} 147}
106 148
107///////////////////////////////////////////////////////////////////////////// 149/////////////////////////////////////////////////////////////////////////////
108// encoder 150// encoder
109 151
114 char *end; // SvEND (sv) 156 char *end; // SvEND (sv)
115 SV *sv; // result scalar 157 SV *sv; // result scalar
116 JSON json; 158 JSON json;
117 U32 indent; // indentation level 159 U32 indent; // indentation level
118 U32 maxdepth; // max. indentation/recursion level 160 U32 maxdepth; // max. indentation/recursion level
161 UV limit; // escape character values >= this value when encoding
119} enc_t; 162} enc_t;
120 163
121inline void 164INLINE void
122need (enc_t *enc, STRLEN len) 165need (enc_t *enc, STRLEN len)
123{ 166{
124 if (expect_false (enc->cur + len >= enc->end)) 167 if (expect_false (enc->cur + len >= enc->end))
125 { 168 {
126 STRLEN cur = enc->cur - SvPVX (enc->sv); 169 STRLEN cur = enc->cur - SvPVX (enc->sv);
128 enc->cur = SvPVX (enc->sv) + cur; 171 enc->cur = SvPVX (enc->sv) + cur;
129 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; 172 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1;
130 } 173 }
131} 174}
132 175
133inline void 176INLINE void
134encode_ch (enc_t *enc, char ch) 177encode_ch (enc_t *enc, char ch)
135{ 178{
136 need (enc, 1); 179 need (enc, 1);
137 *enc->cur++ = ch; 180 *enc->cur++ = ch;
138} 181}
192 { 235 {
193 uch = ch; 236 uch = ch;
194 clen = 1; 237 clen = 1;
195 } 238 }
196 239
197 if (uch > 0x10FFFFUL) 240 if (uch < 0x80/*0x20*/ || uch >= enc->limit)
198 croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch);
199
200 if (uch < 0x80 || enc->json.flags & F_ASCII || (enc->json.flags & F_LATIN1 && uch > 0xFF))
201 { 241 {
202 if (uch > 0xFFFFUL) 242 if (uch >= 0x10000UL)
203 { 243 {
244 if (uch >= 0x110000UL)
245 croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch);
246
204 need (enc, len += 11); 247 need (enc, len += 11);
205 sprintf (enc->cur, "\\u%04x\\u%04x", 248 sprintf (enc->cur, "\\u%04x\\u%04x",
206 (int)((uch - 0x10000) / 0x400 + 0xD800), 249 (int)((uch - 0x10000) / 0x400 + 0xD800),
207 (int)((uch - 0x10000) % 0x400 + 0xDC00)); 250 (int)((uch - 0x10000) % 0x400 + 0xDC00));
208 enc->cur += 12; 251 enc->cur += 12;
236 while (--clen); 279 while (--clen);
237 } 280 }
238 else 281 else
239 { 282 {
240 need (enc, len += UTF8_MAXBYTES - 1); // never more than 11 bytes needed 283 need (enc, len += UTF8_MAXBYTES - 1); // never more than 11 bytes needed
241 enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); 284 enc->cur = encode_utf8 (enc->cur, uch);
242 ++str; 285 ++str;
243 } 286 }
244 } 287 }
245 } 288 }
246 } 289 }
247 290
248 --len; 291 --len;
249 } 292 }
250} 293}
251 294
252inline void 295INLINE void
253encode_indent (enc_t *enc) 296encode_indent (enc_t *enc)
254{ 297{
255 if (enc->json.flags & F_INDENT) 298 if (enc->json.flags & F_INDENT)
256 { 299 {
257 int spaces = enc->indent * INDENT_STEP; 300 int spaces = enc->indent * INDENT_STEP;
260 memset (enc->cur, ' ', spaces); 303 memset (enc->cur, ' ', spaces);
261 enc->cur += spaces; 304 enc->cur += spaces;
262 } 305 }
263} 306}
264 307
265inline void 308INLINE void
266encode_space (enc_t *enc) 309encode_space (enc_t *enc)
267{ 310{
268 need (enc, 1); 311 need (enc, 1);
269 encode_ch (enc, ' '); 312 encode_ch (enc, ' ');
270} 313}
271 314
272inline void 315INLINE void
273encode_nl (enc_t *enc) 316encode_nl (enc_t *enc)
274{ 317{
275 if (enc->json.flags & F_INDENT) 318 if (enc->json.flags & F_INDENT)
276 { 319 {
277 need (enc, 1); 320 need (enc, 1);
278 encode_ch (enc, '\n'); 321 encode_ch (enc, '\n');
279 } 322 }
280} 323}
281 324
282inline void 325INLINE void
283encode_comma (enc_t *enc) 326encode_comma (enc_t *enc)
284{ 327{
285 encode_ch (enc, ','); 328 encode_ch (enc, ',');
286 329
287 if (enc->json.flags & F_INDENT) 330 if (enc->json.flags & F_INDENT)
298 int i, len = av_len (av); 341 int i, len = av_len (av);
299 342
300 if (enc->indent >= enc->maxdepth) 343 if (enc->indent >= enc->maxdepth)
301 croak ("data structure too deep (hit recursion limit)"); 344 croak ("data structure too deep (hit recursion limit)");
302 345
303 encode_ch (enc, '['); encode_nl (enc); 346 encode_ch (enc, '[');
304 ++enc->indent; 347
348 if (len >= 0)
349 {
350 encode_nl (enc); ++enc->indent;
305 351
306 for (i = 0; i <= len; ++i) 352 for (i = 0; i <= len; ++i)
307 { 353 {
354 SV **svp = av_fetch (av, i, 0);
355
308 encode_indent (enc); 356 encode_indent (enc);
309 encode_sv (enc, *av_fetch (av, i, 0));
310 357
358 if (svp)
359 encode_sv (enc, *svp);
360 else
361 encode_str (enc, "null", 4, 0);
362
311 if (i < len) 363 if (i < len)
312 encode_comma (enc); 364 encode_comma (enc);
313 } 365 }
314 366
367 encode_nl (enc); --enc->indent; encode_indent (enc);
368 }
369
315 encode_nl (enc); 370 encode_ch (enc, ']');
316
317 --enc->indent;
318 encode_indent (enc); encode_ch (enc, ']');
319} 371}
320 372
321static void 373static void
322encode_he (enc_t *enc, HE *he) 374encode_hk (enc_t *enc, HE *he)
323{ 375{
324 encode_ch (enc, '"'); 376 encode_ch (enc, '"');
325 377
326 if (HeKLEN (he) == HEf_SVKEY) 378 if (HeKLEN (he) == HEf_SVKEY)
327 { 379 {
340 encode_ch (enc, '"'); 392 encode_ch (enc, '"');
341 393
342 if (enc->json.flags & F_SPACE_BEFORE) encode_space (enc); 394 if (enc->json.flags & F_SPACE_BEFORE) encode_space (enc);
343 encode_ch (enc, ':'); 395 encode_ch (enc, ':');
344 if (enc->json.flags & F_SPACE_AFTER ) encode_space (enc); 396 if (enc->json.flags & F_SPACE_AFTER ) encode_space (enc);
345 encode_sv (enc, HeVAL (he));
346} 397}
347 398
348// compare hash entries, used when all keys are bytestrings 399// compare hash entries, used when all keys are bytestrings
349static int 400static int
350he_cmp_fast (const void *a_, const void *b_) 401he_cmp_fast (const void *a_, const void *b_)
355 HE *b = *(HE **)b_; 406 HE *b = *(HE **)b_;
356 407
357 STRLEN la = HeKLEN (a); 408 STRLEN la = HeKLEN (a);
358 STRLEN lb = HeKLEN (b); 409 STRLEN lb = HeKLEN (b);
359 410
360 if (!(cmp = memcmp (HeKEY (a), HeKEY (b), la < lb ? la : lb))) 411 if (!(cmp = memcmp (HeKEY (b), HeKEY (a), lb < la ? lb : la)))
361 cmp = la - lb; 412 cmp = lb - la;
362 413
363 return cmp; 414 return cmp;
364} 415}
365 416
366// compare hash entries, used when some keys are sv's or utf-x 417// compare hash entries, used when some keys are sv's or utf-x
367static int 418static int
368he_cmp_slow (const void *a, const void *b) 419he_cmp_slow (const void *a, const void *b)
369{ 420{
370 return sv_cmp (HeSVKEY_force (*(HE **)a), HeSVKEY_force (*(HE **)b)); 421 return sv_cmp (HeSVKEY_force (*(HE **)b), HeSVKEY_force (*(HE **)a));
371} 422}
372 423
373static void 424static void
374encode_hv (enc_t *enc, HV *hv) 425encode_hv (enc_t *enc, HV *hv)
375{ 426{
427 HE *he;
376 int count, i; 428 int count;
377 429
378 if (enc->indent >= enc->maxdepth) 430 if (enc->indent >= enc->maxdepth)
379 croak ("data structure too deep (hit recursion limit)"); 431 croak ("data structure too deep (hit recursion limit)");
380 432
381 encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; 433 encode_ch (enc, '{');
382 434
383 if ((count = hv_iterinit (hv)))
384 {
385 // for canonical output we have to sort by keys first 435 // for canonical output we have to sort by keys first
386 // actually, this is mostly due to the stupid so-called 436 // actually, this is mostly due to the stupid so-called
387 // security workaround added somewhere in 5.8.x. 437 // security workaround added somewhere in 5.8.x.
388 // that randomises hash orderings 438 // that randomises hash orderings
389 if (enc->json.flags & F_CANONICAL) 439 if (enc->json.flags & F_CANONICAL)
440 {
441 int count = hv_iterinit (hv);
442
443 if (SvMAGICAL (hv))
390 { 444 {
445 // need to count by iterating. could improve by dynamically building the vector below
446 // but I don't care for the speed of this special case.
447 // note also that we will run into undefined behaviour when the two iterations
448 // do not result in the same count, something I might care for in some later release.
449
450 count = 0;
451 while (hv_iternext (hv))
452 ++count;
453
454 hv_iterinit (hv);
455 }
456
457 if (count)
458 {
391 int fast = 1; 459 int i, fast = 1;
392 HE *he;
393#if defined(__BORLANDC__) || defined(_MSC_VER) 460#if defined(__BORLANDC__) || defined(_MSC_VER)
394 HE **hes = _alloca (count * sizeof (HE)); 461 HE **hes = _alloca (count * sizeof (HE));
395#else 462#else
396 HE *hes [count]; // if your compiler dies here, you need to enable C99 mode 463 HE *hes [count]; // if your compiler dies here, you need to enable C99 mode
397#endif 464#endif
424 491
425 FREETMPS; 492 FREETMPS;
426 LEAVE; 493 LEAVE;
427 } 494 }
428 495
429 for (i = 0; i < count; ++i) 496 encode_nl (enc); ++enc->indent;
497
498 while (count--)
430 { 499 {
431 encode_indent (enc); 500 encode_indent (enc);
501 he = hes [count];
432 encode_he (enc, hes [i]); 502 encode_hk (enc, he);
503 encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he));
433 504
434 if (i < count - 1) 505 if (count)
435 encode_comma (enc); 506 encode_comma (enc);
436 } 507 }
437 508
438 encode_nl (enc); 509 encode_nl (enc); --enc->indent; encode_indent (enc);
439 } 510 }
511 }
440 else 512 else
513 {
514 if (hv_iterinit (hv) || SvMAGICAL (hv))
515 if ((he = hv_iternext (hv)))
441 { 516 {
442 HE *he = hv_iternext (hv); 517 encode_nl (enc); ++enc->indent;
443 518
444 for (;;) 519 for (;;)
445 { 520 {
446 encode_indent (enc); 521 encode_indent (enc);
447 encode_he (enc, he); 522 encode_hk (enc, he);
523 encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he));
448 524
449 if (!(he = hv_iternext (hv))) 525 if (!(he = hv_iternext (hv)))
450 break; 526 break;
451 527
452 encode_comma (enc); 528 encode_comma (enc);
453 } 529 }
454 530
455 encode_nl (enc); 531 encode_nl (enc); --enc->indent; encode_indent (enc);
456 } 532 }
457 } 533 }
458 534
459 --enc->indent; encode_indent (enc); encode_ch (enc, '}'); 535 encode_ch (enc, '}');
460} 536}
461 537
462// encode objects, arrays and special \0=false and \1=true values. 538// encode objects, arrays and special \0=false and \1=true values.
463static void 539static void
464encode_rv (enc_t *enc, SV *sv) 540encode_rv (enc_t *enc, SV *sv)
468 SvGETMAGIC (sv); 544 SvGETMAGIC (sv);
469 svt = SvTYPE (sv); 545 svt = SvTYPE (sv);
470 546
471 if (expect_false (SvOBJECT (sv))) 547 if (expect_false (SvOBJECT (sv)))
472 { 548 {
549 HV *stash = !JSON_SLOW || json_boolean_stash
550 ? json_boolean_stash
551 : gv_stashpv ("JSON::XS::Boolean", 1);
552
473 if (SvSTASH (sv) == json_boolean_stash) 553 if (SvSTASH (sv) == stash)
474 { 554 {
475 if (SvIV (sv) == 0) 555 if (SvIV (sv))
556 encode_str (enc, "true", 4, 0);
557 else
476 encode_str (enc, "false", 5, 0); 558 encode_str (enc, "false", 5, 0);
477 else
478 encode_str (enc, "true", 4, 0);
479 } 559 }
480 else 560 else
481 { 561 {
482#if 0 562#if 0
483 if (0 && sv_derived_from (rv, "JSON::Literal")) 563 if (0 && sv_derived_from (rv, "JSON::Literal"))
486 } 566 }
487#endif 567#endif
488 if (enc->json.flags & F_CONV_BLESSED) 568 if (enc->json.flags & F_CONV_BLESSED)
489 { 569 {
490 // we re-bless the reference to get overload and other niceties right 570 // we re-bless the reference to get overload and other niceties right
491 GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 1); 571 GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 0);
492 572
493 if (to_json) 573 if (to_json)
494 { 574 {
495 dSP; 575 dSP;
496 ENTER; 576
497 SAVETMPS;
498 PUSHMARK (SP); 577 ENTER; SAVETMPS; PUSHMARK (SP);
499 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); 578 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv)));
500 579
501 // calling with G_SCALAR ensures that we always get a 1 reutrn value 580 // calling with G_SCALAR ensures that we always get a 1 return value
502 // check anyways.
503 PUTBACK; 581 PUTBACK;
504 assert (1 == call_sv ((SV *)GvCV (to_json), G_SCALAR)); 582 call_sv ((SV *)GvCV (to_json), G_SCALAR);
505 SPAGAIN; 583 SPAGAIN;
506 584
585 // catch this surprisingly common error
586 if (SvROK (TOPs) && SvRV (TOPs) == sv)
587 croak ("%s::TO_JSON method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv)));
588
589 sv = POPs;
590 PUTBACK;
591
507 encode_sv (enc, POPs); 592 encode_sv (enc, sv);
508 593
509 FREETMPS; 594 FREETMPS; LEAVE;
510 LEAVE;
511 } 595 }
512 else if (enc->json.flags & F_ALLOW_BLESSED) 596 else if (enc->json.flags & F_ALLOW_BLESSED)
513 encode_str (enc, "null", 4, 0); 597 encode_str (enc, "null", 4, 0);
514 else 598 else
515 croak ("encountered object '%s', but neither allow_blessed enabled nor TO_JSON method available on it", 599 croak ("encountered object '%s', but neither allow_blessed enabled nor TO_JSON method available on it",
526 encode_hv (enc, (HV *)sv); 610 encode_hv (enc, (HV *)sv);
527 else if (svt == SVt_PVAV) 611 else if (svt == SVt_PVAV)
528 encode_av (enc, (AV *)sv); 612 encode_av (enc, (AV *)sv);
529 else if (svt < SVt_PVAV) 613 else if (svt < SVt_PVAV)
530 { 614 {
531 if (SvNIOK (sv) && SvIV (sv) == 0) 615 STRLEN len = 0;
616 char *pv = svt ? SvPV (sv, len) : 0;
617
618 if (len == 1 && *pv == '1')
619 encode_str (enc, "true", 4, 0);
620 else if (len == 1 && *pv == '0')
532 encode_str (enc, "false", 5, 0); 621 encode_str (enc, "false", 5, 0);
533 else if (SvNIOK (sv) && SvIV (sv) == 1)
534 encode_str (enc, "true", 4, 0);
535 else 622 else
536 croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", 623 croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1",
537 SvPV_nolen (sv_2mortal (newRV_inc (sv)))); 624 SvPV_nolen (sv_2mortal (newRV_inc (sv))));
538 } 625 }
539 else 626 else
561 Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur); 648 Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur);
562 enc->cur += strlen (enc->cur); 649 enc->cur += strlen (enc->cur);
563 } 650 }
564 else if (SvIOKp (sv)) 651 else if (SvIOKp (sv))
565 { 652 {
566 // we assume we can always read an IV as a UV 653 // we assume we can always read an IV as a UV and vice versa
567 if (SvUV (sv) & ~(UV)0x7fff) 654 // we assume two's complement
568 { 655 // we assume no aliasing issues in the union
569 // large integer, use the (rather slow) snprintf way. 656 if (SvIsUV (sv) ? SvUVX (sv) <= 59000
570 need (enc, sizeof (UV) * 3); 657 : SvIVX (sv) <= 59000 && SvIVX (sv) >= -59000)
571 enc->cur +=
572 SvIsUV(sv)
573 ? snprintf (enc->cur, sizeof (UV) * 3, "%"UVuf, (UV)SvUVX (sv))
574 : snprintf (enc->cur, sizeof (UV) * 3, "%"IVdf, (IV)SvIVX (sv));
575 }
576 else
577 { 658 {
578 // optimise the "small number case" 659 // optimise the "small number case"
579 // code will likely be branchless and use only a single multiplication 660 // code will likely be branchless and use only a single multiplication
661 // works for numbers up to 59074
580 I32 i = SvIV (sv); 662 I32 i = SvIVX (sv);
581 U32 u; 663 U32 u;
582 char digit, nz = 0; 664 char digit, nz = 0;
583 665
584 need (enc, 6); 666 need (enc, 6);
585 667
591 673
592 // now output digit by digit, each time masking out the integer part 674 // now output digit by digit, each time masking out the integer part
593 // and multiplying by 5 while moving the decimal point one to the right, 675 // and multiplying by 5 while moving the decimal point one to the right,
594 // resulting in a net multiplication by 10. 676 // resulting in a net multiplication by 10.
595 // we always write the digit to memory but conditionally increment 677 // we always write the digit to memory but conditionally increment
596 // the pointer, to ease the usage of conditional move instructions. 678 // the pointer, to enable the use of conditional move instructions.
597 digit = u >> 28; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0xfffffff) * 5; 679 digit = u >> 28; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0xfffffffUL) * 5;
598 digit = u >> 27; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x7ffffff) * 5; 680 digit = u >> 27; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x7ffffffUL) * 5;
599 digit = u >> 26; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x3ffffff) * 5; 681 digit = u >> 26; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x3ffffffUL) * 5;
600 digit = u >> 25; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x1ffffff) * 5; 682 digit = u >> 25; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x1ffffffUL) * 5;
601 digit = u >> 24; *enc->cur = digit + '0'; enc->cur += 1; // correctly generate '0' 683 digit = u >> 24; *enc->cur = digit + '0'; enc->cur += 1; // correctly generate '0'
684 }
685 else
686 {
687 // large integer, use the (rather slow) snprintf way.
688 need (enc, IVUV_MAXCHARS);
689 enc->cur +=
690 SvIsUV(sv)
691 ? snprintf (enc->cur, IVUV_MAXCHARS, "%"UVuf, (UV)SvUVX (sv))
692 : snprintf (enc->cur, IVUV_MAXCHARS, "%"IVdf, (IV)SvIVX (sv));
602 } 693 }
603 } 694 }
604 else if (SvROK (sv)) 695 else if (SvROK (sv))
605 encode_rv (enc, SvRV (sv)); 696 encode_rv (enc, SvRV (sv));
606 else if (!SvOK (sv)) 697 else if (!SvOK (sv))
622 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); 713 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE));
623 enc.cur = SvPVX (enc.sv); 714 enc.cur = SvPVX (enc.sv);
624 enc.end = SvEND (enc.sv); 715 enc.end = SvEND (enc.sv);
625 enc.indent = 0; 716 enc.indent = 0;
626 enc.maxdepth = DEC_DEPTH (enc.json.flags); 717 enc.maxdepth = DEC_DEPTH (enc.json.flags);
718 enc.limit = enc.json.flags & F_ASCII ? 0x000080UL
719 : enc.json.flags & F_LATIN1 ? 0x000100UL
720 : 0x110000UL;
627 721
628 SvPOK_only (enc.sv); 722 SvPOK_only (enc.sv);
629 encode_sv (&enc, scalar); 723 encode_sv (&enc, scalar);
630 724
631 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); 725 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv));
652 JSON json; 746 JSON json;
653 U32 depth; // recursion depth 747 U32 depth; // recursion depth
654 U32 maxdepth; // recursion depth limit 748 U32 maxdepth; // recursion depth limit
655} dec_t; 749} dec_t;
656 750
657inline void 751INLINE void
752decode_comment (dec_t *dec)
753{
754 // only '#'-style comments allowed a.t.m.
755
756 while (*dec->cur && *dec->cur != 0x0a && *dec->cur != 0x0d)
757 ++dec->cur;
758}
759
760INLINE void
658decode_ws (dec_t *dec) 761decode_ws (dec_t *dec)
659{ 762{
660 for (;;) 763 for (;;)
661 { 764 {
662 char ch = *dec->cur; 765 char ch = *dec->cur;
663 766
664 if (ch > 0x20 767 if (ch > 0x20)
768 {
769 if (expect_false (ch == '#'))
770 {
771 if (dec->json.flags & F_RELAXED)
772 decode_comment (dec);
773 else
774 break;
775 }
776 else
777 break;
778 }
665 || (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)) 779 else if (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)
666 break; 780 break; // parse error, but let higher level handle it, gives better error messages
667 781
668 ++dec->cur; 782 ++dec->cur;
669 } 783 }
670} 784}
671 785
777 891
778 if (hi >= 0x80) 892 if (hi >= 0x80)
779 { 893 {
780 utf8 = 1; 894 utf8 = 1;
781 895
782 cur = (char *)uvuni_to_utf8_flags (cur, hi, 0); 896 cur = encode_utf8 (cur, hi);
783 } 897 }
784 else 898 else
785 *cur++ = hi; 899 *cur++ = hi;
786 } 900 }
787 break; 901 break;
789 default: 903 default:
790 --dec_cur; 904 --dec_cur;
791 ERR ("illegal backslash escape sequence in string"); 905 ERR ("illegal backslash escape sequence in string");
792 } 906 }
793 } 907 }
794 else if (expect_true (ch >= 0x20 && ch <= 0x7f)) 908 else if (expect_true (ch >= 0x20 && ch < 0x80))
795 *cur++ = ch; 909 *cur++ = ch;
796 else if (ch >= 0x80) 910 else if (ch >= 0x80)
797 { 911 {
798 STRLEN clen; 912 STRLEN clen;
799 UV uch; 913 UV uch;
920 is_nv = 1; 1034 is_nv = 1;
921 } 1035 }
922 1036
923 if (!is_nv) 1037 if (!is_nv)
924 { 1038 {
1039 int len = dec->cur - start;
1040
925 // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so 1041 // special case the rather common 1..5-digit-int case
926 if (*start == '-') 1042 if (*start == '-')
927 switch (dec->cur - start) 1043 switch (len)
928 { 1044 {
929 case 2: return newSViv (-( start [1] - '0' * 1)); 1045 case 2: return newSViv (-( start [1] - '0' * 1));
930 case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); 1046 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)); 1047 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)); 1048 case 5: return newSViv (-( start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111));
1049 case 6: return newSViv (-(start [1] * 10000 + start [2] * 1000 + start [3] * 100 + start [4] * 10 + start [5] - '0' * 11111));
933 } 1050 }
934 else 1051 else
935 switch (dec->cur - start) 1052 switch (len)
936 { 1053 {
937 case 1: return newSViv ( start [0] - '0' * 1); 1054 case 1: return newSViv ( start [0] - '0' * 1);
938 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); 1055 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); 1056 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); 1057 case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111);
1058 case 5: return newSViv ( start [0] * 10000 + start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 11111);
941 } 1059 }
942 1060
943 { 1061 {
944 UV uv; 1062 UV uv;
945 int numtype = grok_number (start, dec->cur - start, &uv); 1063 int numtype = grok_number (start, len, &uv);
946 if (numtype & IS_NUMBER_IN_UV) 1064 if (numtype & IS_NUMBER_IN_UV)
947 if (numtype & IS_NUMBER_NEG) 1065 if (numtype & IS_NUMBER_NEG)
948 { 1066 {
949 if (uv < (UV)IV_MIN) 1067 if (uv < (UV)IV_MIN)
950 return newSViv (-(IV)uv); 1068 return newSViv (-(IV)uv);
951 } 1069 }
952 else 1070 else
953 return newSVuv (uv); 1071 return newSVuv (uv);
954
955 // here would likely be the place for bigint support
956 } 1072 }
957 }
958 1073
959 // if we ever support bigint or bigfloat, this is the place for bigfloat 1074 len -= *start == '-' ? 1 : 0;
1075
1076 // does not fit into IV or UV, try NV
1077 if ((sizeof (NV) == sizeof (double) && DBL_DIG >= len)
1078 #if defined (LDBL_DIG)
1079 || (sizeof (NV) == sizeof (long double) && LDBL_DIG >= len)
1080 #endif
1081 )
1082 // fits into NV without loss of precision
1083 return newSVnv (Atof (start));
1084
1085 // everything else fails, convert it to a string
1086 return newSVpvn (start, dec->cur - start);
1087 }
1088
1089 // loss of precision here
960 return newSVnv (Atof (start)); 1090 return newSVnv (Atof (start));
961 1091
962fail: 1092fail:
963 return 0; 1093 return 0;
964} 1094}
994 1124
995 if (*dec->cur != ',') 1125 if (*dec->cur != ',')
996 ERR (", or ] expected while parsing array"); 1126 ERR (", or ] expected while parsing array");
997 1127
998 ++dec->cur; 1128 ++dec->cur;
1129
1130 decode_ws (dec);
1131
1132 if (*dec->cur == ']' && dec->json.flags & F_RELAXED)
1133 {
1134 ++dec->cur;
1135 break;
1136 }
999 } 1137 }
1000 1138
1001 DEC_DEC_DEPTH; 1139 DEC_DEC_DEPTH;
1002 return newRV_noinc ((SV *)av); 1140 return newRV_noinc ((SV *)av);
1003 1141
1008} 1146}
1009 1147
1010static SV * 1148static SV *
1011decode_hv (dec_t *dec) 1149decode_hv (dec_t *dec)
1012{ 1150{
1151 SV *sv;
1013 HV *hv = newHV (); 1152 HV *hv = newHV ();
1014 1153
1015 DEC_INC_DEPTH; 1154 DEC_INC_DEPTH;
1016 decode_ws (dec); 1155 decode_ws (dec);
1017 1156
1018 if (*dec->cur == '}') 1157 if (*dec->cur == '}')
1019 ++dec->cur; 1158 ++dec->cur;
1020 else 1159 else
1021 for (;;) 1160 for (;;)
1022 { 1161 {
1023 decode_ws (dec); EXPECT_CH ('"'); 1162 EXPECT_CH ('"');
1024 1163
1025 // heuristic: assume that 1164 // heuristic: assume that
1026 // a) decode_str + hv_store_ent are abysmally slow. 1165 // a) decode_str + hv_store_ent are abysmally slow.
1027 // b) most hash keys are short, simple ascii text. 1166 // b) most hash keys are short, simple ascii text.
1028 // => try to "fast-match" such strings to avoid 1167 // => try to "fast-match" such strings to avoid
1042 if (!key) 1181 if (!key)
1043 goto fail; 1182 goto fail;
1044 1183
1045 decode_ws (dec); EXPECT_CH (':'); 1184 decode_ws (dec); EXPECT_CH (':');
1046 1185
1186 decode_ws (dec);
1047 value = decode_sv (dec); 1187 value = decode_sv (dec);
1048 if (!value) 1188 if (!value)
1049 { 1189 {
1050 SvREFCNT_dec (key); 1190 SvREFCNT_dec (key);
1051 goto fail; 1191 goto fail;
1063 int len = p - key; 1203 int len = p - key;
1064 dec->cur = p + 1; 1204 dec->cur = p + 1;
1065 1205
1066 decode_ws (dec); EXPECT_CH (':'); 1206 decode_ws (dec); EXPECT_CH (':');
1067 1207
1208 decode_ws (dec);
1068 value = decode_sv (dec); 1209 value = decode_sv (dec);
1069 if (!value) 1210 if (!value)
1070 goto fail; 1211 goto fail;
1071 1212
1072 hv_store (hv, key, len, value, 0); 1213 hv_store (hv, key, len, value, 0);
1088 1229
1089 if (*dec->cur != ',') 1230 if (*dec->cur != ',')
1090 ERR (", or } expected while parsing object/hash"); 1231 ERR (", or } expected while parsing object/hash");
1091 1232
1092 ++dec->cur; 1233 ++dec->cur;
1234
1235 decode_ws (dec);
1236
1237 if (*dec->cur == '}' && dec->json.flags & F_RELAXED)
1238 {
1239 ++dec->cur;
1240 break;
1241 }
1093 } 1242 }
1094 1243
1095 DEC_DEC_DEPTH; 1244 DEC_DEC_DEPTH;
1096 return newRV_noinc ((SV *)hv); 1245 sv = newRV_noinc ((SV *)hv);
1246
1247 // check filter callbacks
1248 if (dec->json.flags & F_HOOK)
1249 {
1250 if (dec->json.cb_sk_object && HvKEYS (hv) == 1)
1251 {
1252 HE *cb, *he;
1253
1254 hv_iterinit (hv);
1255 he = hv_iternext (hv);
1256 hv_iterinit (hv);
1257
1258 // the next line creates a mortal sv each time its called.
1259 // might want to optimise this for common cases.
1260 cb = hv_fetch_ent (dec->json.cb_sk_object, hv_iterkeysv (he), 0, 0);
1261
1262 if (cb)
1263 {
1264 dSP;
1265 int count;
1266
1267 ENTER; SAVETMPS; PUSHMARK (SP);
1268 XPUSHs (HeVAL (he));
1269
1270 PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN;
1271
1272 if (count == 1)
1273 {
1274 sv = newSVsv (POPs);
1275 FREETMPS; LEAVE;
1276 return sv;
1277 }
1278
1279 FREETMPS; LEAVE;
1280 }
1281 }
1282
1283 if (dec->json.cb_object)
1284 {
1285 dSP;
1286 int count;
1287
1288 ENTER; SAVETMPS; PUSHMARK (SP);
1289 XPUSHs (sv_2mortal (sv));
1290
1291 PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN;
1292
1293 if (count == 1)
1294 {
1295 sv = newSVsv (POPs);
1296 FREETMPS; LEAVE;
1297 return sv;
1298 }
1299
1300 SvREFCNT_inc (sv);
1301 FREETMPS; LEAVE;
1302 }
1303 }
1304
1305 return sv;
1097 1306
1098fail: 1307fail:
1099 SvREFCNT_dec (hv); 1308 SvREFCNT_dec (hv);
1100 DEC_DEC_DEPTH; 1309 DEC_DEC_DEPTH;
1101 return 0; 1310 return 0;
1102} 1311}
1103 1312
1104static SV * 1313static SV *
1105decode_sv (dec_t *dec) 1314decode_sv (dec_t *dec)
1106{ 1315{
1107 decode_ws (dec);
1108
1109 // the beauty of JSON: you need exactly one character lookahead 1316 // the beauty of JSON: you need exactly one character lookahead
1110 // to parse anything. 1317 // to parse everything.
1111 switch (*dec->cur) 1318 switch (*dec->cur)
1112 { 1319 {
1113 case '"': ++dec->cur; return decode_str (dec); 1320 case '"': ++dec->cur; return decode_str (dec);
1114 case '[': ++dec->cur; return decode_av (dec); 1321 case '[': ++dec->cur; return decode_av (dec);
1115 case '{': ++dec->cur; return decode_hv (dec); 1322 case '{': ++dec->cur; return decode_hv (dec);
1116 1323
1117 case '-': 1324 case '-':
1118 case '0': case '1': case '2': case '3': case '4': 1325 case '0': case '1': case '2': case '3': case '4':
1119 case '5': case '6': case '7': case '8': case '9': 1326 case '5': case '6': case '7': case '8': case '9':
1120 return decode_num (dec); 1327 return decode_num (dec);
1121 1328
1122 case 't': 1329 case 't':
1123 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) 1330 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4))
1124 { 1331 {
1125 dec->cur += 4; 1332 dec->cur += 4;
1333#if JSON_SLOW
1334 json_true = get_sv ("JSON::XS::true", 1); SvREADONLY_on (json_true);
1335#endif
1126 return SvREFCNT_inc (json_true); 1336 return SvREFCNT_inc (json_true);
1127 } 1337 }
1128 else 1338 else
1129 ERR ("'true' expected"); 1339 ERR ("'true' expected");
1130 1340
1132 1342
1133 case 'f': 1343 case 'f':
1134 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) 1344 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5))
1135 { 1345 {
1136 dec->cur += 5; 1346 dec->cur += 5;
1347#if JSON_SLOW
1348 json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false);
1349#endif
1137 return SvREFCNT_inc (json_false); 1350 return SvREFCNT_inc (json_false);
1138 } 1351 }
1139 else 1352 else
1140 ERR ("'false' expected"); 1353 ERR ("'false' expected");
1141 1354
1187 dec.end = SvEND (string); 1400 dec.end = SvEND (string);
1188 dec.err = 0; 1401 dec.err = 0;
1189 dec.depth = 0; 1402 dec.depth = 0;
1190 dec.maxdepth = DEC_DEPTH (dec.json.flags); 1403 dec.maxdepth = DEC_DEPTH (dec.json.flags);
1191 1404
1405 if (dec.json.cb_object || dec.json.cb_sk_object)
1406 dec.json.flags |= F_HOOK;
1407
1192 *dec.end = 0; // this should basically be a nop, too, but make sure it's there 1408 *dec.end = 0; // this should basically be a nop, too, but make sure it's there
1409
1410 decode_ws (&dec);
1193 sv = decode_sv (&dec); 1411 sv = decode_sv (&dec);
1194 1412
1195 if (!(offset_return || !sv)) 1413 if (!(offset_return || !sv))
1196 { 1414 {
1197 // check for trailing garbage 1415 // check for trailing garbage
1265 json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false); 1483 json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false);
1266} 1484}
1267 1485
1268PROTOTYPES: DISABLE 1486PROTOTYPES: DISABLE
1269 1487
1488void CLONE (...)
1489 CODE:
1490 json_stash = 0;
1491 json_boolean_stash = 0;
1492
1270void new (char *klass) 1493void new (char *klass)
1271 PPCODE: 1494 PPCODE:
1272{ 1495{
1273 SV *pv = NEWSV (0, sizeof (JSON)); 1496 SV *pv = NEWSV (0, sizeof (JSON));
1274 SvPOK_only (pv); 1497 SvPOK_only (pv);
1275 Zero (SvPVX (pv), 1, sizeof (JSON)); 1498 Zero (SvPVX (pv), 1, JSON);
1276 ((JSON *)SvPVX (pv))->flags = F_DEFAULT; 1499 ((JSON *)SvPVX (pv))->flags = F_DEFAULT;
1277 XPUSHs (sv_2mortal (sv_bless (newRV_noinc (pv), json_stash))); 1500 XPUSHs (sv_2mortal (sv_bless (
1501 newRV_noinc (pv),
1502 strEQ (klass, "JSON::XS") ? JSON_STASH : gv_stashpv (klass, 1)
1503 )));
1278} 1504}
1279 1505
1280void ascii (JSON *self, int enable = 1) 1506void ascii (JSON *self, int enable = 1)
1281 ALIAS: 1507 ALIAS:
1282 ascii = F_ASCII 1508 ascii = F_ASCII
1289 pretty = F_PRETTY 1515 pretty = F_PRETTY
1290 allow_nonref = F_ALLOW_NONREF 1516 allow_nonref = F_ALLOW_NONREF
1291 shrink = F_SHRINK 1517 shrink = F_SHRINK
1292 allow_blessed = F_ALLOW_BLESSED 1518 allow_blessed = F_ALLOW_BLESSED
1293 convert_blessed = F_CONV_BLESSED 1519 convert_blessed = F_CONV_BLESSED
1520 relaxed = F_RELAXED
1294 PPCODE: 1521 PPCODE:
1295{ 1522{
1296 if (enable) 1523 if (enable)
1297 self->flags |= ix; 1524 self->flags |= ix;
1298 else 1525 else
1299 self->flags &= ~ix; 1526 self->flags &= ~ix;
1300 1527
1301 XPUSHs (ST (0)); 1528 XPUSHs (ST (0));
1302} 1529}
1303 1530
1531void get_ascii (JSON *self)
1532 ALIAS:
1533 get_ascii = F_ASCII
1534 get_latin1 = F_LATIN1
1535 get_utf8 = F_UTF8
1536 get_indent = F_INDENT
1537 get_canonical = F_CANONICAL
1538 get_space_before = F_SPACE_BEFORE
1539 get_space_after = F_SPACE_AFTER
1540 get_allow_nonref = F_ALLOW_NONREF
1541 get_shrink = F_SHRINK
1542 get_allow_blessed = F_ALLOW_BLESSED
1543 get_convert_blessed = F_CONV_BLESSED
1544 get_relaxed = F_RELAXED
1545 PPCODE:
1546 XPUSHs (boolSV (self->flags & ix));
1547
1304void max_depth (JSON *self, UV max_depth = 0x80000000UL) 1548void max_depth (JSON *self, UV max_depth = 0x80000000UL)
1305 PPCODE: 1549 PPCODE:
1306{ 1550{
1307 UV log2 = 0; 1551 UV log2 = 0;
1308 1552
1314 self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); 1558 self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH);
1315 1559
1316 XPUSHs (ST (0)); 1560 XPUSHs (ST (0));
1317} 1561}
1318 1562
1563U32 get_max_depth (JSON *self)
1564 CODE:
1565 RETVAL = DEC_DEPTH (self->flags);
1566 OUTPUT:
1567 RETVAL
1568
1319void max_size (JSON *self, UV max_size = 0) 1569void max_size (JSON *self, UV max_size = 0)
1320 PPCODE: 1570 PPCODE:
1321{ 1571{
1322 UV log2 = 0; 1572 UV log2 = 0;
1323 1573
1326 1576
1327 while ((1UL << log2) < max_size) 1577 while ((1UL << log2) < max_size)
1328 ++log2; 1578 ++log2;
1329 1579
1330 self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE); 1580 self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE);
1581
1582 XPUSHs (ST (0));
1583}
1584
1585int get_max_size (JSON *self)
1586 CODE:
1587 RETVAL = DEC_SIZE (self->flags);
1588 OUTPUT:
1589 RETVAL
1590
1591void filter_json_object (JSON *self, SV *cb = &PL_sv_undef)
1592 PPCODE:
1593{
1594 SvREFCNT_dec (self->cb_object);
1595 self->cb_object = SvOK (cb) ? newSVsv (cb) : 0;
1596
1597 XPUSHs (ST (0));
1598}
1599
1600void filter_json_single_key_object (JSON *self, SV *key, SV *cb = &PL_sv_undef)
1601 PPCODE:
1602{
1603 if (!self->cb_sk_object)
1604 self->cb_sk_object = newHV ();
1605
1606 if (SvOK (cb))
1607 hv_store_ent (self->cb_sk_object, key, newSVsv (cb), 0);
1608 else
1609 {
1610 hv_delete_ent (self->cb_sk_object, key, G_DISCARD, 0);
1611
1612 if (!HvKEYS (self->cb_sk_object))
1613 {
1614 SvREFCNT_dec (self->cb_sk_object);
1615 self->cb_sk_object = 0;
1616 }
1617 }
1331 1618
1332 XPUSHs (ST (0)); 1619 XPUSHs (ST (0));
1333} 1620}
1334 1621
1335void encode (JSON *self, SV *scalar) 1622void encode (JSON *self, SV *scalar)
1347 EXTEND (SP, 2); 1634 EXTEND (SP, 2);
1348 PUSHs (decode_json (jsonstr, self, &offset)); 1635 PUSHs (decode_json (jsonstr, self, &offset));
1349 PUSHs (sv_2mortal (newSVuv (offset))); 1636 PUSHs (sv_2mortal (newSVuv (offset)));
1350} 1637}
1351 1638
1639void DESTROY (JSON *self)
1640 CODE:
1641 SvREFCNT_dec (self->cb_sk_object);
1642 SvREFCNT_dec (self->cb_object);
1643
1352PROTOTYPES: ENABLE 1644PROTOTYPES: ENABLE
1353 1645
1354void to_json (SV *scalar) 1646void encode_json (SV *scalar)
1647 ALIAS:
1648 to_json_ = 0
1649 encode_json = F_UTF8
1355 PPCODE: 1650 PPCODE:
1356{ 1651{
1357 JSON json = { F_DEFAULT | F_UTF8 }; 1652 JSON json = { F_DEFAULT | ix };
1358 XPUSHs (encode_json (scalar, &json)); 1653 XPUSHs (encode_json (scalar, &json));
1359} 1654}
1360 1655
1361void from_json (SV *jsonstr) 1656void decode_json (SV *jsonstr)
1657 ALIAS:
1658 from_json_ = 0
1659 decode_json = F_UTF8
1362 PPCODE: 1660 PPCODE:
1363{ 1661{
1364 JSON json = { F_DEFAULT | F_UTF8 }; 1662 JSON json = { F_DEFAULT | ix };
1365 XPUSHs (decode_json (jsonstr, &json, 0)); 1663 XPUSHs (decode_json (jsonstr, &json, 0));
1366} 1664}
1367 1665

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines