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.50 by root, Mon Jul 2 00:29:38 2007 UTC vs.
Revision 1.75 by root, Wed Mar 19 15:37:54 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
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 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
35#define F_HOOK 0x00080000UL // some hooks exist, so slow-path processing 41#define F_HOOK 0x00080000UL // some hooks exist, so slow-path processing
47 53
48#define SB do { 54#define SB do {
49#define SE } while (0) 55#define SE } while (0)
50 56
51#if __GNUC__ >= 3 57#if __GNUC__ >= 3
52# define expect(expr,value) __builtin_expect ((expr),(value)) 58# define expect(expr,value) __builtin_expect ((expr), (value))
53# define inline inline 59# define INLINE static inline
54#else 60#else
55# define expect(expr,value) (expr) 61# define expect(expr,value) (expr)
56# define inline static 62# define INLINE static
57#endif 63#endif
58 64
59#define expect_false(expr) expect ((expr) != 0, 0) 65#define expect_false(expr) expect ((expr) != 0, 0)
60#define expect_true(expr) expect ((expr) != 0, 1) 66#define expect_true(expr) expect ((expr) != 0, 1)
61 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
62static HV *json_stash, *json_boolean_stash; // JSON::XS:: 80static HV *json_stash, *json_boolean_stash; // JSON::XS::
63static SV *json_true, *json_false; 81static SV *json_true, *json_false;
64 82
65typedef struct { 83typedef struct {
66 U32 flags; 84 U32 flags;
67 SV *cb_object, *cb_sk_object; 85 SV *cb_object;
86 HV *cb_sk_object;
68} JSON; 87} JSON;
69 88
70///////////////////////////////////////////////////////////////////////////// 89/////////////////////////////////////////////////////////////////////////////
71// utility functions 90// utility functions
72 91
73inline void 92INLINE void
74shrink (SV *sv) 93shrink (SV *sv)
75{ 94{
76 sv_utf8_downgrade (sv, 1); 95 sv_utf8_downgrade (sv, 1);
77 if (SvLEN (sv) > SvCUR (sv) + 1) 96 if (SvLEN (sv) > SvCUR (sv) + 1)
78 { 97 {
87// 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
88// case of an error. 107// case of an error.
89// we special-case "safe" characters from U+80 .. U+7FF, 108// we special-case "safe" characters from U+80 .. U+7FF,
90// but use the very good perl function to parse anything else. 109// but use the very good perl function to parse anything else.
91// note that we never call this function for a ascii codepoints 110// note that we never call this function for a ascii codepoints
92inline UV 111INLINE UV
93decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen) 112decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen)
94{ 113{
95 if (expect_false (s[0] > 0xdf || s[0] < 0xc2)) 114 if (expect_true (len >= 2
96 return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY); 115 && IN_RANGE_INC (char, s[0], 0xc2, 0xdf)
97 else if (len > 1 && s[1] >= 0x80 && s[1] <= 0xbf) 116 && IN_RANGE_INC (char, s[1], 0x80, 0xbf)))
98 { 117 {
99 *clen = 2; 118 *clen = 2;
100 return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); 119 return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f);
101 } 120 }
102 else 121 else
103 { 122 return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY);
104 *clen = (STRLEN)-1; 123}
105 return (UV)-1; 124
106 } 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;
107} 147}
108 148
109///////////////////////////////////////////////////////////////////////////// 149/////////////////////////////////////////////////////////////////////////////
110// encoder 150// encoder
111 151
116 char *end; // SvEND (sv) 156 char *end; // SvEND (sv)
117 SV *sv; // result scalar 157 SV *sv; // result scalar
118 JSON json; 158 JSON json;
119 U32 indent; // indentation level 159 U32 indent; // indentation level
120 U32 maxdepth; // max. indentation/recursion level 160 U32 maxdepth; // max. indentation/recursion level
161 UV limit; // escape character values >= this value when encoding
121} enc_t; 162} enc_t;
122 163
123inline void 164INLINE void
124need (enc_t *enc, STRLEN len) 165need (enc_t *enc, STRLEN len)
125{ 166{
126 if (expect_false (enc->cur + len >= enc->end)) 167 if (expect_false (enc->cur + len >= enc->end))
127 { 168 {
128 STRLEN cur = enc->cur - SvPVX (enc->sv); 169 STRLEN cur = enc->cur - SvPVX (enc->sv);
130 enc->cur = SvPVX (enc->sv) + cur; 171 enc->cur = SvPVX (enc->sv) + cur;
131 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; 172 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1;
132 } 173 }
133} 174}
134 175
135inline void 176INLINE void
136encode_ch (enc_t *enc, char ch) 177encode_ch (enc_t *enc, char ch)
137{ 178{
138 need (enc, 1); 179 need (enc, 1);
139 *enc->cur++ = ch; 180 *enc->cur++ = ch;
140} 181}
194 { 235 {
195 uch = ch; 236 uch = ch;
196 clen = 1; 237 clen = 1;
197 } 238 }
198 239
199 if (uch > 0x10FFFFUL) 240 if (uch < 0x80/*0x20*/ || uch >= enc->limit)
200 croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch);
201
202 if (uch < 0x80 || enc->json.flags & F_ASCII || (enc->json.flags & F_LATIN1 && uch > 0xFF))
203 { 241 {
204 if (uch > 0xFFFFUL) 242 if (uch >= 0x10000UL)
205 { 243 {
244 if (uch >= 0x110000UL)
245 croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch);
246
206 need (enc, len += 11); 247 need (enc, len += 11);
207 sprintf (enc->cur, "\\u%04x\\u%04x", 248 sprintf (enc->cur, "\\u%04x\\u%04x",
208 (int)((uch - 0x10000) / 0x400 + 0xD800), 249 (int)((uch - 0x10000) / 0x400 + 0xD800),
209 (int)((uch - 0x10000) % 0x400 + 0xDC00)); 250 (int)((uch - 0x10000) % 0x400 + 0xDC00));
210 enc->cur += 12; 251 enc->cur += 12;
238 while (--clen); 279 while (--clen);
239 } 280 }
240 else 281 else
241 { 282 {
242 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
243 enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); 284 enc->cur = encode_utf8 (enc->cur, uch);
244 ++str; 285 ++str;
245 } 286 }
246 } 287 }
247 } 288 }
248 } 289 }
249 290
250 --len; 291 --len;
251 } 292 }
252} 293}
253 294
254inline void 295INLINE void
255encode_indent (enc_t *enc) 296encode_indent (enc_t *enc)
256{ 297{
257 if (enc->json.flags & F_INDENT) 298 if (enc->json.flags & F_INDENT)
258 { 299 {
259 int spaces = enc->indent * INDENT_STEP; 300 int spaces = enc->indent * INDENT_STEP;
262 memset (enc->cur, ' ', spaces); 303 memset (enc->cur, ' ', spaces);
263 enc->cur += spaces; 304 enc->cur += spaces;
264 } 305 }
265} 306}
266 307
267inline void 308INLINE void
268encode_space (enc_t *enc) 309encode_space (enc_t *enc)
269{ 310{
270 need (enc, 1); 311 need (enc, 1);
271 encode_ch (enc, ' '); 312 encode_ch (enc, ' ');
272} 313}
273 314
274inline void 315INLINE void
275encode_nl (enc_t *enc) 316encode_nl (enc_t *enc)
276{ 317{
277 if (enc->json.flags & F_INDENT) 318 if (enc->json.flags & F_INDENT)
278 { 319 {
279 need (enc, 1); 320 need (enc, 1);
280 encode_ch (enc, '\n'); 321 encode_ch (enc, '\n');
281 } 322 }
282} 323}
283 324
284inline void 325INLINE void
285encode_comma (enc_t *enc) 326encode_comma (enc_t *enc)
286{ 327{
287 encode_ch (enc, ','); 328 encode_ch (enc, ',');
288 329
289 if (enc->json.flags & F_INDENT) 330 if (enc->json.flags & F_INDENT)
300 int i, len = av_len (av); 341 int i, len = av_len (av);
301 342
302 if (enc->indent >= enc->maxdepth) 343 if (enc->indent >= enc->maxdepth)
303 croak ("data structure too deep (hit recursion limit)"); 344 croak ("data structure too deep (hit recursion limit)");
304 345
305 encode_ch (enc, '['); encode_nl (enc); 346 encode_ch (enc, '[');
306 ++enc->indent; 347
348 if (len >= 0)
349 {
350 encode_nl (enc); ++enc->indent;
307 351
308 for (i = 0; i <= len; ++i) 352 for (i = 0; i <= len; ++i)
309 { 353 {
354 SV **svp = av_fetch (av, i, 0);
355
310 encode_indent (enc); 356 encode_indent (enc);
311 encode_sv (enc, *av_fetch (av, i, 0));
312 357
358 if (svp)
359 encode_sv (enc, *svp);
360 else
361 encode_str (enc, "null", 4, 0);
362
313 if (i < len) 363 if (i < len)
314 encode_comma (enc); 364 encode_comma (enc);
315 } 365 }
316 366
367 encode_nl (enc); --enc->indent; encode_indent (enc);
368 }
369
317 encode_nl (enc); 370 encode_ch (enc, ']');
318
319 --enc->indent;
320 encode_indent (enc); encode_ch (enc, ']');
321} 371}
322 372
323static void 373static void
324encode_he (enc_t *enc, HE *he) 374encode_hk (enc_t *enc, HE *he)
325{ 375{
326 encode_ch (enc, '"'); 376 encode_ch (enc, '"');
327 377
328 if (HeKLEN (he) == HEf_SVKEY) 378 if (HeKLEN (he) == HEf_SVKEY)
329 { 379 {
342 encode_ch (enc, '"'); 392 encode_ch (enc, '"');
343 393
344 if (enc->json.flags & F_SPACE_BEFORE) encode_space (enc); 394 if (enc->json.flags & F_SPACE_BEFORE) encode_space (enc);
345 encode_ch (enc, ':'); 395 encode_ch (enc, ':');
346 if (enc->json.flags & F_SPACE_AFTER ) encode_space (enc); 396 if (enc->json.flags & F_SPACE_AFTER ) encode_space (enc);
347 encode_sv (enc, HeVAL (he));
348} 397}
349 398
350// compare hash entries, used when all keys are bytestrings 399// compare hash entries, used when all keys are bytestrings
351static int 400static int
352he_cmp_fast (const void *a_, const void *b_) 401he_cmp_fast (const void *a_, const void *b_)
357 HE *b = *(HE **)b_; 406 HE *b = *(HE **)b_;
358 407
359 STRLEN la = HeKLEN (a); 408 STRLEN la = HeKLEN (a);
360 STRLEN lb = HeKLEN (b); 409 STRLEN lb = HeKLEN (b);
361 410
362 if (!(cmp = memcmp (HeKEY (a), HeKEY (b), la < lb ? la : lb))) 411 if (!(cmp = memcmp (HeKEY (b), HeKEY (a), lb < la ? lb : la)))
363 cmp = la - lb; 412 cmp = lb - la;
364 413
365 return cmp; 414 return cmp;
366} 415}
367 416
368// 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
369static int 418static int
370he_cmp_slow (const void *a, const void *b) 419he_cmp_slow (const void *a, const void *b)
371{ 420{
372 return sv_cmp (HeSVKEY_force (*(HE **)a), HeSVKEY_force (*(HE **)b)); 421 return sv_cmp (HeSVKEY_force (*(HE **)b), HeSVKEY_force (*(HE **)a));
373} 422}
374 423
375static void 424static void
376encode_hv (enc_t *enc, HV *hv) 425encode_hv (enc_t *enc, HV *hv)
377{ 426{
427 HE *he;
378 int count, i; 428 int count;
379 429
380 if (enc->indent >= enc->maxdepth) 430 if (enc->indent >= enc->maxdepth)
381 croak ("data structure too deep (hit recursion limit)"); 431 croak ("data structure too deep (hit recursion limit)");
382 432
383 encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; 433 encode_ch (enc, '{');
384 434
385 if ((count = hv_iterinit (hv)))
386 {
387 // for canonical output we have to sort by keys first 435 // for canonical output we have to sort by keys first
388 // actually, this is mostly due to the stupid so-called 436 // actually, this is mostly due to the stupid so-called
389 // security workaround added somewhere in 5.8.x. 437 // security workaround added somewhere in 5.8.x.
390 // that randomises hash orderings 438 // that randomises hash orderings
391 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))
392 { 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 {
393 int fast = 1; 459 int i, fast = 1;
394 HE *he;
395#if defined(__BORLANDC__) || defined(_MSC_VER) 460#if defined(__BORLANDC__) || defined(_MSC_VER)
396 HE **hes = _alloca (count * sizeof (HE)); 461 HE **hes = _alloca (count * sizeof (HE));
397#else 462#else
398 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
399#endif 464#endif
426 491
427 FREETMPS; 492 FREETMPS;
428 LEAVE; 493 LEAVE;
429 } 494 }
430 495
431 for (i = 0; i < count; ++i) 496 encode_nl (enc); ++enc->indent;
497
498 while (count--)
432 { 499 {
433 encode_indent (enc); 500 encode_indent (enc);
501 he = hes [count];
434 encode_he (enc, hes [i]); 502 encode_hk (enc, he);
503 encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he));
435 504
436 if (i < count - 1) 505 if (count)
437 encode_comma (enc); 506 encode_comma (enc);
438 } 507 }
439 508
440 encode_nl (enc); 509 encode_nl (enc); --enc->indent; encode_indent (enc);
441 } 510 }
511 }
442 else 512 else
513 {
514 if (hv_iterinit (hv) || SvMAGICAL (hv))
515 if ((he = hv_iternext (hv)))
443 { 516 {
444 HE *he = hv_iternext (hv); 517 encode_nl (enc); ++enc->indent;
445 518
446 for (;;) 519 for (;;)
447 { 520 {
448 encode_indent (enc); 521 encode_indent (enc);
449 encode_he (enc, he); 522 encode_hk (enc, he);
523 encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he));
450 524
451 if (!(he = hv_iternext (hv))) 525 if (!(he = hv_iternext (hv)))
452 break; 526 break;
453 527
454 encode_comma (enc); 528 encode_comma (enc);
455 } 529 }
456 530
457 encode_nl (enc); 531 encode_nl (enc); --enc->indent; encode_indent (enc);
458 } 532 }
459 } 533 }
460 534
461 --enc->indent; encode_indent (enc); encode_ch (enc, '}'); 535 encode_ch (enc, '}');
462} 536}
463 537
464// encode objects, arrays and special \0=false and \1=true values. 538// encode objects, arrays and special \0=false and \1=true values.
465static void 539static void
466encode_rv (enc_t *enc, SV *sv) 540encode_rv (enc_t *enc, SV *sv)
470 SvGETMAGIC (sv); 544 SvGETMAGIC (sv);
471 svt = SvTYPE (sv); 545 svt = SvTYPE (sv);
472 546
473 if (expect_false (SvOBJECT (sv))) 547 if (expect_false (SvOBJECT (sv)))
474 { 548 {
549 HV *stash = !JSON_SLOW || json_boolean_stash
550 ? json_boolean_stash
551 : gv_stashpv ("JSON::XS::Boolean", 1);
552
475 if (SvSTASH (sv) == json_boolean_stash) 553 if (SvSTASH (sv) == stash)
476 { 554 {
477 if (SvIV (sv) == 0) 555 if (SvIV (sv))
556 encode_str (enc, "true", 4, 0);
557 else
478 encode_str (enc, "false", 5, 0); 558 encode_str (enc, "false", 5, 0);
479 else
480 encode_str (enc, "true", 4, 0);
481 } 559 }
482 else 560 else
483 { 561 {
484#if 0 562#if 0
485 if (0 && sv_derived_from (rv, "JSON::Literal")) 563 if (0 && sv_derived_from (rv, "JSON::Literal"))
488 } 566 }
489#endif 567#endif
490 if (enc->json.flags & F_CONV_BLESSED) 568 if (enc->json.flags & F_CONV_BLESSED)
491 { 569 {
492 // 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
493 GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 1); 571 GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 0);
494 572
495 if (to_json) 573 if (to_json)
496 { 574 {
575 dSP;
576
497 dSP; ENTER; SAVETMPS; PUSHMARK (SP); 577 ENTER; SAVETMPS; PUSHMARK (SP);
498 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); 578 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv)));
499 579
500 // 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
501 // check anyways.
502 PUTBACK; 581 PUTBACK;
503 assert (1 == call_sv ((SV *)GvCV (to_json), G_SCALAR)); 582 call_sv ((SV *)GvCV (to_json), G_SCALAR);
504 SPAGAIN; 583 SPAGAIN;
505 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
506 encode_sv (enc, POPs); 592 encode_sv (enc, sv);
507 593
508 FREETMPS; LEAVE; 594 FREETMPS; LEAVE;
509 } 595 }
510 else if (enc->json.flags & F_ALLOW_BLESSED) 596 else if (enc->json.flags & F_ALLOW_BLESSED)
511 encode_str (enc, "null", 4, 0); 597 encode_str (enc, "null", 4, 0);
527 else if (svt < SVt_PVAV) 613 else if (svt < SVt_PVAV)
528 { 614 {
529 STRLEN len = 0; 615 STRLEN len = 0;
530 char *pv = svt ? SvPV (sv, len) : 0; 616 char *pv = svt ? SvPV (sv, len) : 0;
531 617
532 if (len == 1 && *pv == '0') 618 if (len == 1 && *pv == '1')
619 encode_str (enc, "true", 4, 0);
620 else if (len == 1 && *pv == '0')
533 encode_str (enc, "false", 5, 0); 621 encode_str (enc, "false", 5, 0);
534 else if (len == 1 && *pv == '1')
535 encode_str (enc, "true", 4, 0);
536 else 622 else
537 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",
538 SvPV_nolen (sv_2mortal (newRV_inc (sv)))); 624 SvPV_nolen (sv_2mortal (newRV_inc (sv))));
539 } 625 }
540 else 626 else
562 Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur); 648 Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur);
563 enc->cur += strlen (enc->cur); 649 enc->cur += strlen (enc->cur);
564 } 650 }
565 else if (SvIOKp (sv)) 651 else if (SvIOKp (sv))
566 { 652 {
567 // 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
568 if (SvUV (sv) & ~(UV)0x7fff) 654 // we assume two's complement
569 { 655 // we assume no aliasing issues in the union
570 // large integer, use the (rather slow) snprintf way. 656 if (SvIsUV (sv) ? SvUVX (sv) <= 59000
571 need (enc, sizeof (UV) * 3); 657 : SvIVX (sv) <= 59000 && SvIVX (sv) >= -59000)
572 enc->cur +=
573 SvIsUV(sv)
574 ? snprintf (enc->cur, sizeof (UV) * 3, "%"UVuf, (UV)SvUVX (sv))
575 : snprintf (enc->cur, sizeof (UV) * 3, "%"IVdf, (IV)SvIVX (sv));
576 }
577 else
578 { 658 {
579 // optimise the "small number case" 659 // optimise the "small number case"
580 // 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
581 I32 i = SvIV (sv); 662 I32 i = SvIVX (sv);
582 U32 u; 663 U32 u;
583 char digit, nz = 0; 664 char digit, nz = 0;
584 665
585 need (enc, 6); 666 need (enc, 6);
586 667
592 673
593 // 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
594 // 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,
595 // resulting in a net multiplication by 10. 676 // resulting in a net multiplication by 10.
596 // we always write the digit to memory but conditionally increment 677 // we always write the digit to memory but conditionally increment
597 // the pointer, to ease the usage of conditional move instructions. 678 // the pointer, to enable the use of conditional move instructions.
598 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;
599 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;
600 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;
601 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;
602 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));
603 } 693 }
604 } 694 }
605 else if (SvROK (sv)) 695 else if (SvROK (sv))
606 encode_rv (enc, SvRV (sv)); 696 encode_rv (enc, SvRV (sv));
607 else if (!SvOK (sv)) 697 else if (!SvOK (sv))
623 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); 713 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE));
624 enc.cur = SvPVX (enc.sv); 714 enc.cur = SvPVX (enc.sv);
625 enc.end = SvEND (enc.sv); 715 enc.end = SvEND (enc.sv);
626 enc.indent = 0; 716 enc.indent = 0;
627 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;
628 721
629 SvPOK_only (enc.sv); 722 SvPOK_only (enc.sv);
630 encode_sv (&enc, scalar); 723 encode_sv (&enc, scalar);
631 724
632 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); 725 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv));
653 JSON json; 746 JSON json;
654 U32 depth; // recursion depth 747 U32 depth; // recursion depth
655 U32 maxdepth; // recursion depth limit 748 U32 maxdepth; // recursion depth limit
656} dec_t; 749} dec_t;
657 750
658inline 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
659decode_ws (dec_t *dec) 761decode_ws (dec_t *dec)
660{ 762{
661 for (;;) 763 for (;;)
662 { 764 {
663 char ch = *dec->cur; 765 char ch = *dec->cur;
664 766
665 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 }
666 || (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)) 779 else if (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)
667 break; 780 break; // parse error, but let higher level handle it, gives better error messages
668 781
669 ++dec->cur; 782 ++dec->cur;
670 } 783 }
671} 784}
672 785
778 891
779 if (hi >= 0x80) 892 if (hi >= 0x80)
780 { 893 {
781 utf8 = 1; 894 utf8 = 1;
782 895
783 cur = (char *)uvuni_to_utf8_flags (cur, hi, 0); 896 cur = encode_utf8 (cur, hi);
784 } 897 }
785 else 898 else
786 *cur++ = hi; 899 *cur++ = hi;
787 } 900 }
788 break; 901 break;
790 default: 903 default:
791 --dec_cur; 904 --dec_cur;
792 ERR ("illegal backslash escape sequence in string"); 905 ERR ("illegal backslash escape sequence in string");
793 } 906 }
794 } 907 }
795 else if (expect_true (ch >= 0x20 && ch <= 0x7f)) 908 else if (expect_true (ch >= 0x20 && ch < 0x80))
796 *cur++ = ch; 909 *cur++ = ch;
797 else if (ch >= 0x80) 910 else if (ch >= 0x80)
798 { 911 {
799 STRLEN clen; 912 STRLEN clen;
800 UV uch; 913 UV uch;
921 is_nv = 1; 1034 is_nv = 1;
922 } 1035 }
923 1036
924 if (!is_nv) 1037 if (!is_nv)
925 { 1038 {
1039 int len = dec->cur - start;
1040
926 // 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
927 if (*start == '-') 1042 if (*start == '-')
928 switch (dec->cur - start) 1043 switch (len)
929 { 1044 {
930 case 2: return newSViv (-( start [1] - '0' * 1)); 1045 case 2: return newSViv (-( start [1] - '0' * 1));
931 case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); 1046 case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11));
932 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));
933 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));
934 } 1050 }
935 else 1051 else
936 switch (dec->cur - start) 1052 switch (len)
937 { 1053 {
938 case 1: return newSViv ( start [0] - '0' * 1); 1054 case 1: return newSViv ( start [0] - '0' * 1);
939 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); 1055 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11);
940 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);
941 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);
942 } 1059 }
943 1060
944 { 1061 {
945 UV uv; 1062 UV uv;
946 int numtype = grok_number (start, dec->cur - start, &uv); 1063 int numtype = grok_number (start, len, &uv);
947 if (numtype & IS_NUMBER_IN_UV) 1064 if (numtype & IS_NUMBER_IN_UV)
948 if (numtype & IS_NUMBER_NEG) 1065 if (numtype & IS_NUMBER_NEG)
949 { 1066 {
950 if (uv < (UV)IV_MIN) 1067 if (uv < (UV)IV_MIN)
951 return newSViv (-(IV)uv); 1068 return newSViv (-(IV)uv);
952 } 1069 }
953 else 1070 else
954 return newSVuv (uv); 1071 return newSVuv (uv);
955
956 // here would likely be the place for bigint support
957 } 1072 }
958 }
959 1073
960 // 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
961 return newSVnv (Atof (start)); 1090 return newSVnv (Atof (start));
962 1091
963fail: 1092fail:
964 return 0; 1093 return 0;
965} 1094}
995 1124
996 if (*dec->cur != ',') 1125 if (*dec->cur != ',')
997 ERR (", or ] expected while parsing array"); 1126 ERR (", or ] expected while parsing array");
998 1127
999 ++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 }
1000 } 1137 }
1001 1138
1002 DEC_DEC_DEPTH; 1139 DEC_DEC_DEPTH;
1003 return newRV_noinc ((SV *)av); 1140 return newRV_noinc ((SV *)av);
1004 1141
1020 if (*dec->cur == '}') 1157 if (*dec->cur == '}')
1021 ++dec->cur; 1158 ++dec->cur;
1022 else 1159 else
1023 for (;;) 1160 for (;;)
1024 { 1161 {
1025 decode_ws (dec); EXPECT_CH ('"'); 1162 EXPECT_CH ('"');
1026 1163
1027 // heuristic: assume that 1164 // heuristic: assume that
1028 // a) decode_str + hv_store_ent are abysmally slow. 1165 // a) decode_str + hv_store_ent are abysmally slow.
1029 // b) most hash keys are short, simple ascii text. 1166 // b) most hash keys are short, simple ascii text.
1030 // => try to "fast-match" such strings to avoid 1167 // => try to "fast-match" such strings to avoid
1044 if (!key) 1181 if (!key)
1045 goto fail; 1182 goto fail;
1046 1183
1047 decode_ws (dec); EXPECT_CH (':'); 1184 decode_ws (dec); EXPECT_CH (':');
1048 1185
1186 decode_ws (dec);
1049 value = decode_sv (dec); 1187 value = decode_sv (dec);
1050 if (!value) 1188 if (!value)
1051 { 1189 {
1052 SvREFCNT_dec (key); 1190 SvREFCNT_dec (key);
1053 goto fail; 1191 goto fail;
1065 int len = p - key; 1203 int len = p - key;
1066 dec->cur = p + 1; 1204 dec->cur = p + 1;
1067 1205
1068 decode_ws (dec); EXPECT_CH (':'); 1206 decode_ws (dec); EXPECT_CH (':');
1069 1207
1208 decode_ws (dec);
1070 value = decode_sv (dec); 1209 value = decode_sv (dec);
1071 if (!value) 1210 if (!value)
1072 goto fail; 1211 goto fail;
1073 1212
1074 hv_store (hv, key, len, value, 0); 1213 hv_store (hv, key, len, value, 0);
1090 1229
1091 if (*dec->cur != ',') 1230 if (*dec->cur != ',')
1092 ERR (", or } expected while parsing object/hash"); 1231 ERR (", or } expected while parsing object/hash");
1093 1232
1094 ++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 }
1095 } 1242 }
1096 1243
1097 DEC_DEC_DEPTH; 1244 DEC_DEC_DEPTH;
1098 sv = newRV_noinc ((SV *)hv); 1245 sv = newRV_noinc ((SV *)hv);
1099 1246
1100 // check filter callbacks 1247 // check filter callbacks
1101 if (dec->json.flags & F_HOOK) 1248 if (dec->json.flags & F_HOOK)
1102 { 1249 {
1103 ENTER; SAVETMPS; 1250 if (dec->json.cb_sk_object && HvKEYS (hv) == 1)
1104
1105 if (HvKEYS (hv) == 1 && dec->json.cb_sk_object)
1106 { 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;
1107 int count; 1265 int count;
1108 1266
1109 dSP; PUSHMARK (SP); 1267 ENTER; SAVETMPS; PUSHMARK (SP);
1110 XPUSHs (sv_2mortal (sv)); 1268 XPUSHs (HeVAL (he));
1111 1269
1112 PUTBACK; count = call_sv (dec->json.cb_sk_object, G_ARRAY); SPAGAIN; 1270 PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN;
1113 1271
1114 if (count == 1) 1272 if (count == 1)
1273 {
1115 sv = newSVsv (POPs); 1274 sv = newSVsv (POPs);
1116 else 1275 FREETMPS; LEAVE;
1117 SvREFCNT_inc (sv); 1276 return sv;
1277 }
1278
1279 FREETMPS; LEAVE;
1280 }
1118 } 1281 }
1119 1282
1120 if (dec->json.cb_object) 1283 if (dec->json.cb_object)
1121 { 1284 {
1285 dSP;
1122 int count; 1286 int count;
1123 1287
1124 dSP; ENTER; SAVETMPS; PUSHMARK (SP); 1288 ENTER; SAVETMPS; PUSHMARK (SP);
1125 XPUSHs (sv_2mortal (sv)); 1289 XPUSHs (sv_2mortal (sv));
1126 1290
1127 PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN; 1291 PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN;
1128 1292
1129 if (count == 1) 1293 if (count == 1)
1294 {
1130 sv = newSVsv (POPs); 1295 sv = newSVsv (POPs);
1131 else 1296 FREETMPS; LEAVE;
1297 return sv;
1298 }
1299
1132 SvREFCNT_inc (sv); 1300 SvREFCNT_inc (sv);
1301 FREETMPS; LEAVE;
1133 } 1302 }
1134
1135 FREETMPS; LEAVE;
1136 } 1303 }
1137 1304
1138 return newRV_noinc ((SV *)hv); 1305 return sv;
1139 1306
1140fail: 1307fail:
1141 SvREFCNT_dec (hv); 1308 SvREFCNT_dec (hv);
1142 DEC_DEC_DEPTH; 1309 DEC_DEC_DEPTH;
1143 return 0; 1310 return 0;
1144} 1311}
1145 1312
1146static SV * 1313static SV *
1147decode_sv (dec_t *dec) 1314decode_sv (dec_t *dec)
1148{ 1315{
1149 decode_ws (dec);
1150
1151 // the beauty of JSON: you need exactly one character lookahead 1316 // the beauty of JSON: you need exactly one character lookahead
1152 // to parse anything. 1317 // to parse everything.
1153 switch (*dec->cur) 1318 switch (*dec->cur)
1154 { 1319 {
1155 case '"': ++dec->cur; return decode_str (dec); 1320 case '"': ++dec->cur; return decode_str (dec);
1156 case '[': ++dec->cur; return decode_av (dec); 1321 case '[': ++dec->cur; return decode_av (dec);
1157 case '{': ++dec->cur; return decode_hv (dec); 1322 case '{': ++dec->cur; return decode_hv (dec);
1158 1323
1159 case '-': 1324 case '-':
1160 case '0': case '1': case '2': case '3': case '4': 1325 case '0': case '1': case '2': case '3': case '4':
1161 case '5': case '6': case '7': case '8': case '9': 1326 case '5': case '6': case '7': case '8': case '9':
1162 return decode_num (dec); 1327 return decode_num (dec);
1163 1328
1164 case 't': 1329 case 't':
1165 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) 1330 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4))
1166 { 1331 {
1167 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
1168 return SvREFCNT_inc (json_true); 1336 return SvREFCNT_inc (json_true);
1169 } 1337 }
1170 else 1338 else
1171 ERR ("'true' expected"); 1339 ERR ("'true' expected");
1172 1340
1174 1342
1175 case 'f': 1343 case 'f':
1176 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) 1344 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5))
1177 { 1345 {
1178 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
1179 return SvREFCNT_inc (json_false); 1350 return SvREFCNT_inc (json_false);
1180 } 1351 }
1181 else 1352 else
1182 ERR ("'false' expected"); 1353 ERR ("'false' expected");
1183 1354
1233 1404
1234 if (dec.json.cb_object || dec.json.cb_sk_object) 1405 if (dec.json.cb_object || dec.json.cb_sk_object)
1235 dec.json.flags |= F_HOOK; 1406 dec.json.flags |= F_HOOK;
1236 1407
1237 *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);
1238 sv = decode_sv (&dec); 1411 sv = decode_sv (&dec);
1239 1412
1240 if (!(offset_return || !sv)) 1413 if (!(offset_return || !sv))
1241 { 1414 {
1242 // check for trailing garbage 1415 // check for trailing garbage
1310 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);
1311} 1484}
1312 1485
1313PROTOTYPES: DISABLE 1486PROTOTYPES: DISABLE
1314 1487
1488void CLONE (...)
1489 CODE:
1490 json_stash = 0;
1491 json_boolean_stash = 0;
1492
1315void new (char *klass) 1493void new (char *klass)
1316 PPCODE: 1494 PPCODE:
1317{ 1495{
1318 SV *pv = NEWSV (0, sizeof (JSON)); 1496 SV *pv = NEWSV (0, sizeof (JSON));
1319 SvPOK_only (pv); 1497 SvPOK_only (pv);
1320 Zero (SvPVX (pv), 1, JSON); 1498 Zero (SvPVX (pv), 1, JSON);
1321 ((JSON *)SvPVX (pv))->flags = F_DEFAULT; 1499 ((JSON *)SvPVX (pv))->flags = F_DEFAULT;
1322 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 )));
1323} 1504}
1324 1505
1325void ascii (JSON *self, int enable = 1) 1506void ascii (JSON *self, int enable = 1)
1326 ALIAS: 1507 ALIAS:
1327 ascii = F_ASCII 1508 ascii = F_ASCII
1334 pretty = F_PRETTY 1515 pretty = F_PRETTY
1335 allow_nonref = F_ALLOW_NONREF 1516 allow_nonref = F_ALLOW_NONREF
1336 shrink = F_SHRINK 1517 shrink = F_SHRINK
1337 allow_blessed = F_ALLOW_BLESSED 1518 allow_blessed = F_ALLOW_BLESSED
1338 convert_blessed = F_CONV_BLESSED 1519 convert_blessed = F_CONV_BLESSED
1520 relaxed = F_RELAXED
1339 PPCODE: 1521 PPCODE:
1340{ 1522{
1341 if (enable) 1523 if (enable)
1342 self->flags |= ix; 1524 self->flags |= ix;
1343 else 1525 else
1344 self->flags &= ~ix; 1526 self->flags &= ~ix;
1345 1527
1346 XPUSHs (ST (0)); 1528 XPUSHs (ST (0));
1347} 1529}
1348 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
1349void max_depth (JSON *self, UV max_depth = 0x80000000UL) 1548void max_depth (JSON *self, UV max_depth = 0x80000000UL)
1350 PPCODE: 1549 PPCODE:
1351{ 1550{
1352 UV log2 = 0; 1551 UV log2 = 0;
1353 1552
1359 self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); 1558 self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH);
1360 1559
1361 XPUSHs (ST (0)); 1560 XPUSHs (ST (0));
1362} 1561}
1363 1562
1563U32 get_max_depth (JSON *self)
1564 CODE:
1565 RETVAL = DEC_DEPTH (self->flags);
1566 OUTPUT:
1567 RETVAL
1568
1364void max_size (JSON *self, UV max_size = 0) 1569void max_size (JSON *self, UV max_size = 0)
1365 PPCODE: 1570 PPCODE:
1366{ 1571{
1367 UV log2 = 0; 1572 UV log2 = 0;
1368 1573
1375 self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE); 1580 self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE);
1376 1581
1377 XPUSHs (ST (0)); 1582 XPUSHs (ST (0));
1378} 1583}
1379 1584
1585int get_max_size (JSON *self)
1586 CODE:
1587 RETVAL = DEC_SIZE (self->flags);
1588 OUTPUT:
1589 RETVAL
1590
1380void filter_json_objects (JSON *self, SV *cb = &PL_sv_undef) 1591void filter_json_object (JSON *self, SV *cb = &PL_sv_undef)
1381 ALIAS:
1382 filter_sk_json_objects = 1
1383 PPCODE: 1592 PPCODE:
1384{ 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
1385 if (!SvOK (cb)) 1606 if (SvOK (cb))
1386 cb = 0; 1607 hv_store_ent (self->cb_sk_object, key, newSVsv (cb), 0);
1387 1608 else
1388 switch (ix)
1389 { 1609 {
1390 case 0: self->cb_object = cb; break; 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);
1391 case 1: self->cb_sk_object = cb; break; 1615 self->cb_sk_object = 0;
1616 }
1392 } 1617 }
1393 1618
1394 XPUSHs (ST (0)); 1619 XPUSHs (ST (0));
1395} 1620}
1396 1621
1409 EXTEND (SP, 2); 1634 EXTEND (SP, 2);
1410 PUSHs (decode_json (jsonstr, self, &offset)); 1635 PUSHs (decode_json (jsonstr, self, &offset));
1411 PUSHs (sv_2mortal (newSVuv (offset))); 1636 PUSHs (sv_2mortal (newSVuv (offset)));
1412} 1637}
1413 1638
1639void DESTROY (JSON *self)
1640 CODE:
1641 SvREFCNT_dec (self->cb_sk_object);
1642 SvREFCNT_dec (self->cb_object);
1643
1414PROTOTYPES: ENABLE 1644PROTOTYPES: ENABLE
1415 1645
1416void to_json (SV *scalar) 1646void encode_json (SV *scalar)
1417 PPCODE: 1647 PPCODE:
1418{ 1648{
1419 JSON json = { F_DEFAULT | F_UTF8 }; 1649 JSON json = { F_DEFAULT | F_UTF8 };
1420 XPUSHs (encode_json (scalar, &json)); 1650 XPUSHs (encode_json (scalar, &json));
1421} 1651}
1422 1652
1423void from_json (SV *jsonstr) 1653void decode_json (SV *jsonstr)
1424 PPCODE: 1654 PPCODE:
1425{ 1655{
1426 JSON json = { F_DEFAULT | F_UTF8 }; 1656 JSON json = { F_DEFAULT | F_UTF8 };
1427 XPUSHs (decode_json (jsonstr, &json, 0)); 1657 XPUSHs (decode_json (jsonstr, &json, 0));
1428} 1658}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines