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.47 by root, Sun Jul 1 14:08:03 2007 UTC vs.
Revision 1.74 by root, Wed Mar 19 15:17:53 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 <float.h>
9 10
10#if defined(__BORLANDC__) || defined(_MSC_VER) 11#if defined(__BORLANDC__) || defined(_MSC_VER)
11# define snprintf _snprintf // C compilers have this in stdio.h 12# define snprintf _snprintf // C compilers have this in stdio.h
12#endif 13#endif
13 14
25#define F_SPACE_BEFORE 0x00000020UL 26#define F_SPACE_BEFORE 0x00000020UL
26#define F_SPACE_AFTER 0x00000040UL 27#define F_SPACE_AFTER 0x00000040UL
27#define F_ALLOW_NONREF 0x00000100UL 28#define F_ALLOW_NONREF 0x00000100UL
28#define F_SHRINK 0x00000200UL 29#define F_SHRINK 0x00000200UL
29#define F_ALLOW_BLESSED 0x00000400UL 30#define F_ALLOW_BLESSED 0x00000400UL
30#define F_CONV_BLESSED 0x00000800UL // NYI 31#define F_CONV_BLESSED 0x00000800UL
32#define F_RELAXED 0x00001000UL
33
31#define F_MAXDEPTH 0xf8000000UL 34#define F_MAXDEPTH 0xf8000000UL
32#define S_MAXDEPTH 27 35#define S_MAXDEPTH 27
33#define F_MAXSIZE 0x01f00000UL 36#define F_MAXSIZE 0x01f00000UL
34#define S_MAXSIZE 20 37#define S_MAXSIZE 20
38#define F_HOOK 0x00080000UL // some hooks exist, so slow-path processing
35 39
36#define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) 40#define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH))
37#define DEC_SIZE(flags) (1UL << ((flags & F_MAXSIZE ) >> S_MAXSIZE )) 41#define DEC_SIZE(flags) (1UL << ((flags & F_MAXSIZE ) >> S_MAXSIZE ))
38 42
39#define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER 43#define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER
46 50
47#define SB do { 51#define SB do {
48#define SE } while (0) 52#define SE } while (0)
49 53
50#if __GNUC__ >= 3 54#if __GNUC__ >= 3
51# define expect(expr,value) __builtin_expect ((expr),(value)) 55# define expect(expr,value) __builtin_expect ((expr), (value))
52# define inline inline 56# define INLINE static inline
53#else 57#else
54# define expect(expr,value) (expr) 58# define expect(expr,value) (expr)
55# define inline static 59# define INLINE static
56#endif 60#endif
57 61
58#define expect_false(expr) expect ((expr) != 0, 0) 62#define expect_false(expr) expect ((expr) != 0, 0)
59#define expect_true(expr) expect ((expr) != 0, 1) 63#define expect_true(expr) expect ((expr) != 0, 1)
60 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)))
68
69#ifdef USE_ITHREADS
70# define JSON_SLOW 1
71# define JSON_STASH (json_stash ? json_stash : gv_stashpv ("JSON::XS", 1))
72#else
73# define JSON_SLOW 0
74# define JSON_STASH json_stash
75#endif
76
61static HV *json_stash, *json_boolean_stash; // JSON::XS:: 77static HV *json_stash, *json_boolean_stash; // JSON::XS::
62static SV *json_true, *json_false; 78static SV *json_true, *json_false;
63 79
64typedef struct json { 80typedef struct {
65 U32 flags; 81 U32 flags;
82 SV *cb_object;
83 HV *cb_sk_object;
66} JSON__XS; 84} JSON;
67 85
68///////////////////////////////////////////////////////////////////////////// 86/////////////////////////////////////////////////////////////////////////////
69// utility functions 87// utility functions
70 88
71static UV * 89INLINE void
72SvJSON (SV *sv)
73{
74 if (!(SvROK (sv) && SvOBJECT (SvRV (sv)) && SvSTASH (SvRV (sv)) == json_stash))
75 croak ("object is not of type JSON::XS");
76
77 return &SvUVX (SvRV (sv));
78}
79
80static void
81shrink (SV *sv) 90shrink (SV *sv)
82{ 91{
83 sv_utf8_downgrade (sv, 1); 92 sv_utf8_downgrade (sv, 1);
84 if (SvLEN (sv) > SvCUR (sv) + 1) 93 if (SvLEN (sv) > SvCUR (sv) + 1)
85 { 94 {
94// 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
95// case of an error. 104// case of an error.
96// we special-case "safe" characters from U+80 .. U+7FF, 105// we special-case "safe" characters from U+80 .. U+7FF,
97// but use the very good perl function to parse anything else. 106// but use the very good perl function to parse anything else.
98// note that we never call this function for a ascii codepoints 107// note that we never call this function for a ascii codepoints
99inline UV 108INLINE UV
100decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen) 109decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen)
101{ 110{
102 if (expect_false (s[0] > 0xdf || s[0] < 0xc2)) 111 if (expect_true (len >= 2
103 return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY); 112 && IN_RANGE_INC (char, s[0], 0xc2, 0xdf)
104 else if (len > 1 && s[1] >= 0x80 && s[1] <= 0xbf) 113 && IN_RANGE_INC (char, s[1], 0x80, 0xbf)))
105 { 114 {
106 *clen = 2; 115 *clen = 2;
107 return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); 116 return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f);
108 } 117 }
109 else 118 else
110 { 119 return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY);
111 *clen = (STRLEN)-1; 120}
112 return (UV)-1; 121
113 } 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;
114} 144}
115 145
116///////////////////////////////////////////////////////////////////////////// 146/////////////////////////////////////////////////////////////////////////////
117// encoder 147// encoder
118 148
120typedef struct 150typedef struct
121{ 151{
122 char *cur; // SvPVX (sv) + current output position 152 char *cur; // SvPVX (sv) + current output position
123 char *end; // SvEND (sv) 153 char *end; // SvEND (sv)
124 SV *sv; // result scalar 154 SV *sv; // result scalar
125 struct json json; 155 JSON json;
126 U32 indent; // indentation level 156 U32 indent; // indentation level
127 U32 maxdepth; // max. indentation/recursion level 157 U32 maxdepth; // max. indentation/recursion level
158 UV limit; // escape character values >= this value when encoding
128} enc_t; 159} enc_t;
129 160
130inline void 161INLINE void
131need (enc_t *enc, STRLEN len) 162need (enc_t *enc, STRLEN len)
132{ 163{
133 if (expect_false (enc->cur + len >= enc->end)) 164 if (expect_false (enc->cur + len >= enc->end))
134 { 165 {
135 STRLEN cur = enc->cur - SvPVX (enc->sv); 166 STRLEN cur = enc->cur - SvPVX (enc->sv);
137 enc->cur = SvPVX (enc->sv) + cur; 168 enc->cur = SvPVX (enc->sv) + cur;
138 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; 169 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1;
139 } 170 }
140} 171}
141 172
142inline void 173INLINE void
143encode_ch (enc_t *enc, char ch) 174encode_ch (enc_t *enc, char ch)
144{ 175{
145 need (enc, 1); 176 need (enc, 1);
146 *enc->cur++ = ch; 177 *enc->cur++ = ch;
147} 178}
201 { 232 {
202 uch = ch; 233 uch = ch;
203 clen = 1; 234 clen = 1;
204 } 235 }
205 236
206 if (uch > 0x10FFFFUL) 237 if (uch < 0x80/*0x20*/ || uch >= enc->limit)
207 croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch);
208
209 if (uch < 0x80 || enc->json.flags & F_ASCII || (enc->json.flags & F_LATIN1 && uch > 0xFF))
210 { 238 {
211 if (uch > 0xFFFFUL) 239 if (uch >= 0x10000UL)
212 { 240 {
241 if (uch >= 0x110000UL)
242 croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch);
243
213 need (enc, len += 11); 244 need (enc, len += 11);
214 sprintf (enc->cur, "\\u%04x\\u%04x", 245 sprintf (enc->cur, "\\u%04x\\u%04x",
215 (int)((uch - 0x10000) / 0x400 + 0xD800), 246 (int)((uch - 0x10000) / 0x400 + 0xD800),
216 (int)((uch - 0x10000) % 0x400 + 0xDC00)); 247 (int)((uch - 0x10000) % 0x400 + 0xDC00));
217 enc->cur += 12; 248 enc->cur += 12;
245 while (--clen); 276 while (--clen);
246 } 277 }
247 else 278 else
248 { 279 {
249 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
250 enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); 281 enc->cur = encode_utf8 (enc->cur, uch);
251 ++str; 282 ++str;
252 } 283 }
253 } 284 }
254 } 285 }
255 } 286 }
256 287
257 --len; 288 --len;
258 } 289 }
259} 290}
260 291
261inline void 292INLINE void
262encode_indent (enc_t *enc) 293encode_indent (enc_t *enc)
263{ 294{
264 if (enc->json.flags & F_INDENT) 295 if (enc->json.flags & F_INDENT)
265 { 296 {
266 int spaces = enc->indent * INDENT_STEP; 297 int spaces = enc->indent * INDENT_STEP;
269 memset (enc->cur, ' ', spaces); 300 memset (enc->cur, ' ', spaces);
270 enc->cur += spaces; 301 enc->cur += spaces;
271 } 302 }
272} 303}
273 304
274inline void 305INLINE void
275encode_space (enc_t *enc) 306encode_space (enc_t *enc)
276{ 307{
277 need (enc, 1); 308 need (enc, 1);
278 encode_ch (enc, ' '); 309 encode_ch (enc, ' ');
279} 310}
280 311
281inline void 312INLINE void
282encode_nl (enc_t *enc) 313encode_nl (enc_t *enc)
283{ 314{
284 if (enc->json.flags & F_INDENT) 315 if (enc->json.flags & F_INDENT)
285 { 316 {
286 need (enc, 1); 317 need (enc, 1);
287 encode_ch (enc, '\n'); 318 encode_ch (enc, '\n');
288 } 319 }
289} 320}
290 321
291inline void 322INLINE void
292encode_comma (enc_t *enc) 323encode_comma (enc_t *enc)
293{ 324{
294 encode_ch (enc, ','); 325 encode_ch (enc, ',');
295 326
296 if (enc->json.flags & F_INDENT) 327 if (enc->json.flags & F_INDENT)
307 int i, len = av_len (av); 338 int i, len = av_len (av);
308 339
309 if (enc->indent >= enc->maxdepth) 340 if (enc->indent >= enc->maxdepth)
310 croak ("data structure too deep (hit recursion limit)"); 341 croak ("data structure too deep (hit recursion limit)");
311 342
312 encode_ch (enc, '['); encode_nl (enc); 343 encode_ch (enc, '[');
313 ++enc->indent; 344
345 if (len >= 0)
346 {
347 encode_nl (enc); ++enc->indent;
314 348
315 for (i = 0; i <= len; ++i) 349 for (i = 0; i <= len; ++i)
316 { 350 {
351 SV **svp = av_fetch (av, i, 0);
352
317 encode_indent (enc); 353 encode_indent (enc);
318 encode_sv (enc, *av_fetch (av, i, 0));
319 354
355 if (svp)
356 encode_sv (enc, *svp);
357 else
358 encode_str (enc, "null", 4, 0);
359
320 if (i < len) 360 if (i < len)
321 encode_comma (enc); 361 encode_comma (enc);
322 } 362 }
323 363
364 encode_nl (enc); --enc->indent; encode_indent (enc);
365 }
366
324 encode_nl (enc); 367 encode_ch (enc, ']');
325
326 --enc->indent;
327 encode_indent (enc); encode_ch (enc, ']');
328} 368}
329 369
330static void 370static void
331encode_he (enc_t *enc, HE *he) 371encode_hk (enc_t *enc, HE *he)
332{ 372{
333 encode_ch (enc, '"'); 373 encode_ch (enc, '"');
334 374
335 if (HeKLEN (he) == HEf_SVKEY) 375 if (HeKLEN (he) == HEf_SVKEY)
336 { 376 {
349 encode_ch (enc, '"'); 389 encode_ch (enc, '"');
350 390
351 if (enc->json.flags & F_SPACE_BEFORE) encode_space (enc); 391 if (enc->json.flags & F_SPACE_BEFORE) encode_space (enc);
352 encode_ch (enc, ':'); 392 encode_ch (enc, ':');
353 if (enc->json.flags & F_SPACE_AFTER ) encode_space (enc); 393 if (enc->json.flags & F_SPACE_AFTER ) encode_space (enc);
354 encode_sv (enc, HeVAL (he));
355} 394}
356 395
357// compare hash entries, used when all keys are bytestrings 396// compare hash entries, used when all keys are bytestrings
358static int 397static int
359he_cmp_fast (const void *a_, const void *b_) 398he_cmp_fast (const void *a_, const void *b_)
364 HE *b = *(HE **)b_; 403 HE *b = *(HE **)b_;
365 404
366 STRLEN la = HeKLEN (a); 405 STRLEN la = HeKLEN (a);
367 STRLEN lb = HeKLEN (b); 406 STRLEN lb = HeKLEN (b);
368 407
369 if (!(cmp = memcmp (HeKEY (a), HeKEY (b), la < lb ? la : lb))) 408 if (!(cmp = memcmp (HeKEY (b), HeKEY (a), lb < la ? lb : la)))
370 cmp = la - lb; 409 cmp = lb - la;
371 410
372 return cmp; 411 return cmp;
373} 412}
374 413
375// compare hash entries, used when some keys are sv's or utf-x 414// compare hash entries, used when some keys are sv's or utf-x
376static int 415static int
377he_cmp_slow (const void *a, const void *b) 416he_cmp_slow (const void *a, const void *b)
378{ 417{
379 return sv_cmp (HeSVKEY_force (*(HE **)a), HeSVKEY_force (*(HE **)b)); 418 return sv_cmp (HeSVKEY_force (*(HE **)b), HeSVKEY_force (*(HE **)a));
380} 419}
381 420
382static void 421static void
383encode_hv (enc_t *enc, HV *hv) 422encode_hv (enc_t *enc, HV *hv)
384{ 423{
424 HE *he;
385 int count, i; 425 int count;
386 426
387 if (enc->indent >= enc->maxdepth) 427 if (enc->indent >= enc->maxdepth)
388 croak ("data structure too deep (hit recursion limit)"); 428 croak ("data structure too deep (hit recursion limit)");
389 429
390 encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; 430 encode_ch (enc, '{');
391 431
392 if ((count = hv_iterinit (hv)))
393 {
394 // for canonical output we have to sort by keys first 432 // for canonical output we have to sort by keys first
395 // actually, this is mostly due to the stupid so-called 433 // actually, this is mostly due to the stupid so-called
396 // security workaround added somewhere in 5.8.x. 434 // security workaround added somewhere in 5.8.x.
397 // that randomises hash orderings 435 // that randomises hash orderings
398 if (enc->json.flags & F_CANONICAL) 436 if (enc->json.flags & F_CANONICAL)
437 {
438 int count = hv_iterinit (hv);
439
440 if (SvMAGICAL (hv))
399 { 441 {
442 // need to count by iterating. could improve by dynamically building the vector below
443 // but I don't care for the speed of this special case.
444 // note also that we will run into undefined behaviour when the two iterations
445 // do not result in the same count, something I might care for in some later release.
446
447 count = 0;
448 while (hv_iternext (hv))
449 ++count;
450
451 hv_iterinit (hv);
452 }
453
454 if (count)
455 {
400 int fast = 1; 456 int i, fast = 1;
401 HE *he;
402#if defined(__BORLANDC__) || defined(_MSC_VER) 457#if defined(__BORLANDC__) || defined(_MSC_VER)
403 HE **hes = _alloca (count * sizeof (HE)); 458 HE **hes = _alloca (count * sizeof (HE));
404#else 459#else
405 HE *hes [count]; // if your compiler dies here, you need to enable C99 mode 460 HE *hes [count]; // if your compiler dies here, you need to enable C99 mode
406#endif 461#endif
433 488
434 FREETMPS; 489 FREETMPS;
435 LEAVE; 490 LEAVE;
436 } 491 }
437 492
438 for (i = 0; i < count; ++i) 493 encode_nl (enc); ++enc->indent;
494
495 while (count--)
439 { 496 {
440 encode_indent (enc); 497 encode_indent (enc);
498 he = hes [count];
441 encode_he (enc, hes [i]); 499 encode_hk (enc, he);
500 encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he));
442 501
443 if (i < count - 1) 502 if (count)
444 encode_comma (enc); 503 encode_comma (enc);
445 } 504 }
446 505
447 encode_nl (enc); 506 encode_nl (enc); --enc->indent; encode_indent (enc);
448 } 507 }
508 }
449 else 509 else
510 {
511 if (hv_iterinit (hv) || SvMAGICAL (hv))
512 if ((he = hv_iternext (hv)))
450 { 513 {
451 HE *he = hv_iternext (hv); 514 encode_nl (enc); ++enc->indent;
452 515
453 for (;;) 516 for (;;)
454 { 517 {
455 encode_indent (enc); 518 encode_indent (enc);
456 encode_he (enc, he); 519 encode_hk (enc, he);
520 encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he));
457 521
458 if (!(he = hv_iternext (hv))) 522 if (!(he = hv_iternext (hv)))
459 break; 523 break;
460 524
461 encode_comma (enc); 525 encode_comma (enc);
462 } 526 }
463 527
464 encode_nl (enc); 528 encode_nl (enc); --enc->indent; encode_indent (enc);
465 } 529 }
466 } 530 }
467 531
468 --enc->indent; encode_indent (enc); encode_ch (enc, '}'); 532 encode_ch (enc, '}');
469} 533}
470 534
471// encode objects, arrays and special \0=false and \1=true values. 535// encode objects, arrays and special \0=false and \1=true values.
472static void 536static void
473encode_rv (enc_t *enc, SV *sv) 537encode_rv (enc_t *enc, SV *sv)
477 SvGETMAGIC (sv); 541 SvGETMAGIC (sv);
478 svt = SvTYPE (sv); 542 svt = SvTYPE (sv);
479 543
480 if (expect_false (SvOBJECT (sv))) 544 if (expect_false (SvOBJECT (sv)))
481 { 545 {
546 HV *stash = !JSON_SLOW || json_boolean_stash
547 ? json_boolean_stash
548 : gv_stashpv ("JSON::XS::Boolean", 1);
549
482 if (SvSTASH (sv) == json_boolean_stash) 550 if (SvSTASH (sv) == stash)
483 { 551 {
484 if (SvIV (sv) == 0) 552 if (SvIV (sv))
553 encode_str (enc, "true", 4, 0);
554 else
485 encode_str (enc, "false", 5, 0); 555 encode_str (enc, "false", 5, 0);
486 else
487 encode_str (enc, "true", 4, 0);
488 } 556 }
489 else 557 else
490 { 558 {
491#if 0 559#if 0
492 if (0 && sv_derived_from (rv, "JSON::Literal")) 560 if (0 && sv_derived_from (rv, "JSON::Literal"))
495 } 563 }
496#endif 564#endif
497 if (enc->json.flags & F_CONV_BLESSED) 565 if (enc->json.flags & F_CONV_BLESSED)
498 { 566 {
499 // we re-bless the reference to get overload and other niceties right 567 // we re-bless the reference to get overload and other niceties right
500 GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 1); 568 GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 0);
501 569
502 if (to_json) 570 if (to_json)
503 { 571 {
504 dSP; 572 dSP;
505 ENTER; 573
506 SAVETMPS;
507 PUSHMARK (SP); 574 ENTER; SAVETMPS; PUSHMARK (SP);
508 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); 575 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv)));
509 576
510 // calling with G_SCALAR ensures that we always get a 1 reutrn value 577 // calling with G_SCALAR ensures that we always get a 1 return value
511 // check anyways.
512 PUTBACK; 578 PUTBACK;
513 assert (1 == call_sv ((SV *)GvCV (to_json), G_SCALAR)); 579 call_sv ((SV *)GvCV (to_json), G_SCALAR);
514 SPAGAIN; 580 SPAGAIN;
515 581
582 // catch this surprisingly common error
583 if (SvROK (TOPs) && SvRV (TOPs) == sv)
584 croak ("%s::TO_JSON method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv)));
585
586 sv = POPs;
587 PUTBACK;
588
516 encode_sv (enc, POPs); 589 encode_sv (enc, sv);
517 590
518 FREETMPS; 591 FREETMPS; LEAVE;
519 LEAVE;
520 } 592 }
521 else if (enc->json.flags & F_ALLOW_BLESSED) 593 else if (enc->json.flags & F_ALLOW_BLESSED)
522 encode_str (enc, "null", 4, 0); 594 encode_str (enc, "null", 4, 0);
523 else 595 else
524 croak ("encountered object '%s', but neither allow_blessed enabled nor TO_JSON method available on it", 596 croak ("encountered object '%s', but neither allow_blessed enabled nor TO_JSON method available on it",
535 encode_hv (enc, (HV *)sv); 607 encode_hv (enc, (HV *)sv);
536 else if (svt == SVt_PVAV) 608 else if (svt == SVt_PVAV)
537 encode_av (enc, (AV *)sv); 609 encode_av (enc, (AV *)sv);
538 else if (svt < SVt_PVAV) 610 else if (svt < SVt_PVAV)
539 { 611 {
540 if (SvNIOK (sv) && SvIV (sv) == 0) 612 STRLEN len = 0;
613 char *pv = svt ? SvPV (sv, len) : 0;
614
615 if (len == 1 && *pv == '1')
616 encode_str (enc, "true", 4, 0);
617 else if (len == 1 && *pv == '0')
541 encode_str (enc, "false", 5, 0); 618 encode_str (enc, "false", 5, 0);
542 else if (SvNIOK (sv) && SvIV (sv) == 1)
543 encode_str (enc, "true", 4, 0);
544 else 619 else
545 croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", 620 croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1",
546 SvPV_nolen (sv_2mortal (newRV_inc (sv)))); 621 SvPV_nolen (sv_2mortal (newRV_inc (sv))));
547 } 622 }
548 else 623 else
570 Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur); 645 Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur);
571 enc->cur += strlen (enc->cur); 646 enc->cur += strlen (enc->cur);
572 } 647 }
573 else if (SvIOKp (sv)) 648 else if (SvIOKp (sv))
574 { 649 {
575 // 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
576 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)
577 { 655 {
578 // large integer, use the (rather slow) snprintf way. 656 // large integer, use the (rather slow) snprintf way.
579 need (enc, sizeof (UV) * 3); 657 need (enc, sizeof (UV) * 5 / 2 + 1); // CHAR_BIT is at least 8
580 enc->cur += 658 enc->cur +=
581 SvIsUV(sv) 659 SvIsUV(sv)
582 ? snprintf (enc->cur, sizeof (UV) * 3, "%"UVuf, (UV)SvUVX (sv)) 660 ? snprintf (enc->cur, sizeof (UV) * 3, "%"UVuf, (UV)SvUVX (sv))
583 : snprintf (enc->cur, sizeof (UV) * 3, "%"IVdf, (IV)SvIVX (sv)); 661 : snprintf (enc->cur, sizeof (UV) * 3, "%"IVdf, (IV)SvIVX (sv));
584 } 662 }
585 else 663 else
586 { 664 {
587 // optimise the "small number case" 665 // optimise the "small number case"
588 // 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
589 I32 i = SvIV (sv); 668 I32 i = SvIVX (sv);
590 U32 u; 669 U32 u;
591 char digit, nz = 0; 670 char digit, nz = 0;
592 671
593 need (enc, 6); 672 need (enc, 6);
594 673
618 croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", 697 croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this",
619 SvPV_nolen (sv), SvFLAGS (sv)); 698 SvPV_nolen (sv), SvFLAGS (sv));
620} 699}
621 700
622static SV * 701static SV *
623encode_json (SV *scalar, struct json *json) 702encode_json (SV *scalar, JSON *json)
624{ 703{
625 enc_t enc; 704 enc_t enc;
626 705
627 if (!(json->flags & F_ALLOW_NONREF) && !SvROK (scalar)) 706 if (!(json->flags & F_ALLOW_NONREF) && !SvROK (scalar))
628 croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); 707 croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)");
631 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); 710 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE));
632 enc.cur = SvPVX (enc.sv); 711 enc.cur = SvPVX (enc.sv);
633 enc.end = SvEND (enc.sv); 712 enc.end = SvEND (enc.sv);
634 enc.indent = 0; 713 enc.indent = 0;
635 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;
636 718
637 SvPOK_only (enc.sv); 719 SvPOK_only (enc.sv);
638 encode_sv (&enc, scalar); 720 encode_sv (&enc, scalar);
639 721
640 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); 722 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv));
656typedef struct 738typedef struct
657{ 739{
658 char *cur; // current parser pointer 740 char *cur; // current parser pointer
659 char *end; // end of input string 741 char *end; // end of input string
660 const char *err; // parse error, if != 0 742 const char *err; // parse error, if != 0
661 struct json json; 743 JSON json;
662 U32 depth; // recursion depth 744 U32 depth; // recursion depth
663 U32 maxdepth; // recursion depth limit 745 U32 maxdepth; // recursion depth limit
664} dec_t; 746} dec_t;
665 747
666inline 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
667decode_ws (dec_t *dec) 758decode_ws (dec_t *dec)
668{ 759{
669 for (;;) 760 for (;;)
670 { 761 {
671 char ch = *dec->cur; 762 char ch = *dec->cur;
672 763
673 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 }
674 || (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)) 776 else if (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)
675 break; 777 break; // parse error, but let higher level handle it, gives better error messages
676 778
677 ++dec->cur; 779 ++dec->cur;
678 } 780 }
679} 781}
680 782
786 888
787 if (hi >= 0x80) 889 if (hi >= 0x80)
788 { 890 {
789 utf8 = 1; 891 utf8 = 1;
790 892
791 cur = (char *)uvuni_to_utf8_flags (cur, hi, 0); 893 cur = encode_utf8 (cur, hi);
792 } 894 }
793 else 895 else
794 *cur++ = hi; 896 *cur++ = hi;
795 } 897 }
796 break; 898 break;
798 default: 900 default:
799 --dec_cur; 901 --dec_cur;
800 ERR ("illegal backslash escape sequence in string"); 902 ERR ("illegal backslash escape sequence in string");
801 } 903 }
802 } 904 }
803 else if (expect_true (ch >= 0x20 && ch <= 0x7f)) 905 else if (expect_true (ch >= 0x20 && ch < 0x80))
804 *cur++ = ch; 906 *cur++ = ch;
805 else if (ch >= 0x80) 907 else if (ch >= 0x80)
806 { 908 {
807 STRLEN clen; 909 STRLEN clen;
808 UV uch; 910 UV uch;
929 is_nv = 1; 1031 is_nv = 1;
930 } 1032 }
931 1033
932 if (!is_nv) 1034 if (!is_nv)
933 { 1035 {
1036 int len = dec->cur - start;
1037
934 // 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
935 if (*start == '-') 1039 if (*start == '-')
936 switch (dec->cur - start) 1040 switch (len)
937 { 1041 {
938 case 2: return newSViv (-( start [1] - '0' * 1)); 1042 case 2: return newSViv (-( start [1] - '0' * 1));
939 case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); 1043 case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11));
940 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));
941 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));
942 } 1047 }
943 else 1048 else
944 switch (dec->cur - start) 1049 switch (len)
945 { 1050 {
946 case 1: return newSViv ( start [0] - '0' * 1); 1051 case 1: return newSViv ( start [0] - '0' * 1);
947 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); 1052 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11);
948 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);
949 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);
950 } 1056 }
951 1057
952 { 1058 {
953 UV uv; 1059 UV uv;
954 int numtype = grok_number (start, dec->cur - start, &uv); 1060 int numtype = grok_number (start, len, &uv);
955 if (numtype & IS_NUMBER_IN_UV) 1061 if (numtype & IS_NUMBER_IN_UV)
956 if (numtype & IS_NUMBER_NEG) 1062 if (numtype & IS_NUMBER_NEG)
957 { 1063 {
958 if (uv < (UV)IV_MIN) 1064 if (uv < (UV)IV_MIN)
959 return newSViv (-(IV)uv); 1065 return newSViv (-(IV)uv);
960 } 1066 }
961 else 1067 else
962 return newSVuv (uv); 1068 return newSVuv (uv);
963
964 // here would likely be the place for bigint support
965 } 1069 }
966 }
967 1070
968 // if we ever support bigint or bigfloat, this is the place for bigfloat 1071 len -= *start == '-' ? 1 : 0;
1072
1073 // does not fit into IV or UV, try NV
1074 if ((sizeof (NV) == sizeof (double) && DBL_DIG >= len)
1075 #if defined (LDBL_DIG)
1076 || (sizeof (NV) == sizeof (long double) && LDBL_DIG >= len)
1077 #endif
1078 )
1079 // fits into NV without loss of precision
1080 return newSVnv (Atof (start));
1081
1082 // everything else fails, convert it to a string
1083 return newSVpvn (start, dec->cur - start);
1084 }
1085
1086 // loss of precision here
969 return newSVnv (Atof (start)); 1087 return newSVnv (Atof (start));
970 1088
971fail: 1089fail:
972 return 0; 1090 return 0;
973} 1091}
1003 1121
1004 if (*dec->cur != ',') 1122 if (*dec->cur != ',')
1005 ERR (", or ] expected while parsing array"); 1123 ERR (", or ] expected while parsing array");
1006 1124
1007 ++dec->cur; 1125 ++dec->cur;
1126
1127 decode_ws (dec);
1128
1129 if (*dec->cur == ']' && dec->json.flags & F_RELAXED)
1130 {
1131 ++dec->cur;
1132 break;
1133 }
1008 } 1134 }
1009 1135
1010 DEC_DEC_DEPTH; 1136 DEC_DEC_DEPTH;
1011 return newRV_noinc ((SV *)av); 1137 return newRV_noinc ((SV *)av);
1012 1138
1017} 1143}
1018 1144
1019static SV * 1145static SV *
1020decode_hv (dec_t *dec) 1146decode_hv (dec_t *dec)
1021{ 1147{
1148 SV *sv;
1022 HV *hv = newHV (); 1149 HV *hv = newHV ();
1023 1150
1024 DEC_INC_DEPTH; 1151 DEC_INC_DEPTH;
1025 decode_ws (dec); 1152 decode_ws (dec);
1026 1153
1027 if (*dec->cur == '}') 1154 if (*dec->cur == '}')
1028 ++dec->cur; 1155 ++dec->cur;
1029 else 1156 else
1030 for (;;) 1157 for (;;)
1031 { 1158 {
1032 decode_ws (dec); EXPECT_CH ('"'); 1159 EXPECT_CH ('"');
1033 1160
1034 // heuristic: assume that 1161 // heuristic: assume that
1035 // a) decode_str + hv_store_ent are abysmally slow. 1162 // a) decode_str + hv_store_ent are abysmally slow.
1036 // b) most hash keys are short, simple ascii text. 1163 // b) most hash keys are short, simple ascii text.
1037 // => try to "fast-match" such strings to avoid 1164 // => try to "fast-match" such strings to avoid
1051 if (!key) 1178 if (!key)
1052 goto fail; 1179 goto fail;
1053 1180
1054 decode_ws (dec); EXPECT_CH (':'); 1181 decode_ws (dec); EXPECT_CH (':');
1055 1182
1183 decode_ws (dec);
1056 value = decode_sv (dec); 1184 value = decode_sv (dec);
1057 if (!value) 1185 if (!value)
1058 { 1186 {
1059 SvREFCNT_dec (key); 1187 SvREFCNT_dec (key);
1060 goto fail; 1188 goto fail;
1072 int len = p - key; 1200 int len = p - key;
1073 dec->cur = p + 1; 1201 dec->cur = p + 1;
1074 1202
1075 decode_ws (dec); EXPECT_CH (':'); 1203 decode_ws (dec); EXPECT_CH (':');
1076 1204
1205 decode_ws (dec);
1077 value = decode_sv (dec); 1206 value = decode_sv (dec);
1078 if (!value) 1207 if (!value)
1079 goto fail; 1208 goto fail;
1080 1209
1081 hv_store (hv, key, len, value, 0); 1210 hv_store (hv, key, len, value, 0);
1097 1226
1098 if (*dec->cur != ',') 1227 if (*dec->cur != ',')
1099 ERR (", or } expected while parsing object/hash"); 1228 ERR (", or } expected while parsing object/hash");
1100 1229
1101 ++dec->cur; 1230 ++dec->cur;
1231
1232 decode_ws (dec);
1233
1234 if (*dec->cur == '}' && dec->json.flags & F_RELAXED)
1235 {
1236 ++dec->cur;
1237 break;
1238 }
1102 } 1239 }
1103 1240
1104 DEC_DEC_DEPTH; 1241 DEC_DEC_DEPTH;
1105 return newRV_noinc ((SV *)hv); 1242 sv = newRV_noinc ((SV *)hv);
1243
1244 // check filter callbacks
1245 if (dec->json.flags & F_HOOK)
1246 {
1247 if (dec->json.cb_sk_object && HvKEYS (hv) == 1)
1248 {
1249 HE *cb, *he;
1250
1251 hv_iterinit (hv);
1252 he = hv_iternext (hv);
1253 hv_iterinit (hv);
1254
1255 // the next line creates a mortal sv each time its called.
1256 // might want to optimise this for common cases.
1257 cb = hv_fetch_ent (dec->json.cb_sk_object, hv_iterkeysv (he), 0, 0);
1258
1259 if (cb)
1260 {
1261 dSP;
1262 int count;
1263
1264 ENTER; SAVETMPS; PUSHMARK (SP);
1265 XPUSHs (HeVAL (he));
1266
1267 PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN;
1268
1269 if (count == 1)
1270 {
1271 sv = newSVsv (POPs);
1272 FREETMPS; LEAVE;
1273 return sv;
1274 }
1275
1276 FREETMPS; LEAVE;
1277 }
1278 }
1279
1280 if (dec->json.cb_object)
1281 {
1282 dSP;
1283 int count;
1284
1285 ENTER; SAVETMPS; PUSHMARK (SP);
1286 XPUSHs (sv_2mortal (sv));
1287
1288 PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN;
1289
1290 if (count == 1)
1291 {
1292 sv = newSVsv (POPs);
1293 FREETMPS; LEAVE;
1294 return sv;
1295 }
1296
1297 SvREFCNT_inc (sv);
1298 FREETMPS; LEAVE;
1299 }
1300 }
1301
1302 return sv;
1106 1303
1107fail: 1304fail:
1108 SvREFCNT_dec (hv); 1305 SvREFCNT_dec (hv);
1109 DEC_DEC_DEPTH; 1306 DEC_DEC_DEPTH;
1110 return 0; 1307 return 0;
1111} 1308}
1112 1309
1113static SV * 1310static SV *
1114decode_sv (dec_t *dec) 1311decode_sv (dec_t *dec)
1115{ 1312{
1116 decode_ws (dec);
1117
1118 // the beauty of JSON: you need exactly one character lookahead 1313 // the beauty of JSON: you need exactly one character lookahead
1119 // to parse anything. 1314 // to parse everything.
1120 switch (*dec->cur) 1315 switch (*dec->cur)
1121 { 1316 {
1122 case '"': ++dec->cur; return decode_str (dec); 1317 case '"': ++dec->cur; return decode_str (dec);
1123 case '[': ++dec->cur; return decode_av (dec); 1318 case '[': ++dec->cur; return decode_av (dec);
1124 case '{': ++dec->cur; return decode_hv (dec); 1319 case '{': ++dec->cur; return decode_hv (dec);
1125 1320
1126 case '-': 1321 case '-':
1127 case '0': case '1': case '2': case '3': case '4': 1322 case '0': case '1': case '2': case '3': case '4':
1128 case '5': case '6': case '7': case '8': case '9': 1323 case '5': case '6': case '7': case '8': case '9':
1129 return decode_num (dec); 1324 return decode_num (dec);
1130 1325
1131 case 't': 1326 case 't':
1132 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) 1327 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4))
1133 { 1328 {
1134 dec->cur += 4; 1329 dec->cur += 4;
1330#if JSON_SLOW
1331 json_true = get_sv ("JSON::XS::true", 1); SvREADONLY_on (json_true);
1332#endif
1135 return SvREFCNT_inc (json_true); 1333 return SvREFCNT_inc (json_true);
1136 } 1334 }
1137 else 1335 else
1138 ERR ("'true' expected"); 1336 ERR ("'true' expected");
1139 1337
1141 1339
1142 case 'f': 1340 case 'f':
1143 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) 1341 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5))
1144 { 1342 {
1145 dec->cur += 5; 1343 dec->cur += 5;
1344#if JSON_SLOW
1345 json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false);
1346#endif
1146 return SvREFCNT_inc (json_false); 1347 return SvREFCNT_inc (json_false);
1147 } 1348 }
1148 else 1349 else
1149 ERR ("'false' expected"); 1350 ERR ("'false' expected");
1150 1351
1169fail: 1370fail:
1170 return 0; 1371 return 0;
1171} 1372}
1172 1373
1173static SV * 1374static SV *
1174decode_json (SV *string, struct json *json, UV *offset_return) 1375decode_json (SV *string, JSON *json, UV *offset_return)
1175{ 1376{
1176 dec_t dec; 1377 dec_t dec;
1177 UV offset; 1378 UV offset;
1178 SV *sv; 1379 SV *sv;
1179 1380
1196 dec.end = SvEND (string); 1397 dec.end = SvEND (string);
1197 dec.err = 0; 1398 dec.err = 0;
1198 dec.depth = 0; 1399 dec.depth = 0;
1199 dec.maxdepth = DEC_DEPTH (dec.json.flags); 1400 dec.maxdepth = DEC_DEPTH (dec.json.flags);
1200 1401
1402 if (dec.json.cb_object || dec.json.cb_sk_object)
1403 dec.json.flags |= F_HOOK;
1404
1201 *dec.end = 0; // this should basically be a nop, too, but make sure it's there 1405 *dec.end = 0; // this should basically be a nop, too, but make sure it's there
1406
1407 decode_ws (&dec);
1202 sv = decode_sv (&dec); 1408 sv = decode_sv (&dec);
1203 1409
1204 if (!(offset_return || !sv)) 1410 if (!(offset_return || !sv))
1205 { 1411 {
1206 // check for trailing garbage 1412 // check for trailing garbage
1274 json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false); 1480 json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false);
1275} 1481}
1276 1482
1277PROTOTYPES: DISABLE 1483PROTOTYPES: DISABLE
1278 1484
1279SV *new (char *dummy) 1485void CLONE (...)
1280 CODE: 1486 CODE:
1281 RETVAL = sv_bless (newRV_noinc (newSVuv (F_DEFAULT)), json_stash); 1487 json_stash = 0;
1282 OUTPUT: 1488 json_boolean_stash = 0;
1283 RETVAL
1284 1489
1490void new (char *klass)
1491 PPCODE:
1492{
1493 SV *pv = NEWSV (0, sizeof (JSON));
1494 SvPOK_only (pv);
1495 Zero (SvPVX (pv), 1, JSON);
1496 ((JSON *)SvPVX (pv))->flags = F_DEFAULT;
1497 XPUSHs (sv_2mortal (sv_bless (
1498 newRV_noinc (pv),
1499 strEQ (klass, "JSON::XS") ? JSON_STASH : gv_stashpv (klass, 1)
1500 )));
1501}
1502
1285SV *ascii (SV *self, int enable = 1) 1503void ascii (JSON *self, int enable = 1)
1286 ALIAS: 1504 ALIAS:
1287 ascii = F_ASCII 1505 ascii = F_ASCII
1288 latin1 = F_LATIN1 1506 latin1 = F_LATIN1
1289 utf8 = F_UTF8 1507 utf8 = F_UTF8
1290 indent = F_INDENT 1508 indent = F_INDENT
1294 pretty = F_PRETTY 1512 pretty = F_PRETTY
1295 allow_nonref = F_ALLOW_NONREF 1513 allow_nonref = F_ALLOW_NONREF
1296 shrink = F_SHRINK 1514 shrink = F_SHRINK
1297 allow_blessed = F_ALLOW_BLESSED 1515 allow_blessed = F_ALLOW_BLESSED
1298 convert_blessed = F_CONV_BLESSED 1516 convert_blessed = F_CONV_BLESSED
1517 relaxed = F_RELAXED
1518 PPCODE:
1519{
1520 if (enable)
1521 self->flags |= ix;
1522 else
1523 self->flags &= ~ix;
1524
1525 XPUSHs (ST (0));
1526}
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
1545void max_depth (JSON *self, UV max_depth = 0x80000000UL)
1546 PPCODE:
1547{
1548 UV log2 = 0;
1549
1550 if (max_depth > 0x80000000UL) max_depth = 0x80000000UL;
1551
1552 while ((1UL << log2) < max_depth)
1553 ++log2;
1554
1555 self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH);
1556
1557 XPUSHs (ST (0));
1558}
1559
1560U32 get_max_depth (JSON *self)
1299 CODE: 1561 CODE:
1300{ 1562 RETVAL = DEC_DEPTH (self->flags);
1301 UV *uv = SvJSON (self);
1302 if (enable)
1303 *uv |= ix;
1304 else
1305 *uv &= ~ix;
1306
1307 RETVAL = newSVsv (self);
1308}
1309 OUTPUT: 1563 OUTPUT:
1310 RETVAL 1564 RETVAL
1311 1565
1312SV *max_depth (SV *self, UV max_depth = 0x80000000UL) 1566void max_size (JSON *self, UV max_size = 0)
1567 PPCODE:
1568{
1569 UV log2 = 0;
1570
1571 if (max_size > 0x80000000UL) max_size = 0x80000000UL;
1572 if (max_size == 1) max_size = 2;
1573
1574 while ((1UL << log2) < max_size)
1575 ++log2;
1576
1577 self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE);
1578
1579 XPUSHs (ST (0));
1580}
1581
1582int get_max_size (JSON *self)
1313 CODE: 1583 CODE:
1314{ 1584 RETVAL = DEC_SIZE (self->flags);
1315 UV *uv = SvJSON (self);
1316 UV log2 = 0;
1317
1318 if (max_depth > 0x80000000UL) max_depth = 0x80000000UL;
1319
1320 while ((1UL << log2) < max_depth)
1321 ++log2;
1322
1323 *uv = *uv & ~F_MAXDEPTH | (log2 << S_MAXDEPTH);
1324
1325 RETVAL = newSVsv (self);
1326}
1327 OUTPUT: 1585 OUTPUT:
1328 RETVAL 1586 RETVAL
1329 1587
1330SV *max_size (SV *self, UV max_size = 0) 1588void filter_json_object (JSON *self, SV *cb = &PL_sv_undef)
1589 PPCODE:
1590{
1591 SvREFCNT_dec (self->cb_object);
1592 self->cb_object = SvOK (cb) ? newSVsv (cb) : 0;
1593
1594 XPUSHs (ST (0));
1595}
1596
1597void filter_json_single_key_object (JSON *self, SV *key, SV *cb = &PL_sv_undef)
1598 PPCODE:
1599{
1600 if (!self->cb_sk_object)
1601 self->cb_sk_object = newHV ();
1602
1603 if (SvOK (cb))
1604 hv_store_ent (self->cb_sk_object, key, newSVsv (cb), 0);
1605 else
1606 {
1607 hv_delete_ent (self->cb_sk_object, key, G_DISCARD, 0);
1608
1609 if (!HvKEYS (self->cb_sk_object))
1610 {
1611 SvREFCNT_dec (self->cb_sk_object);
1612 self->cb_sk_object = 0;
1613 }
1614 }
1615
1616 XPUSHs (ST (0));
1617}
1618
1619void encode (JSON *self, SV *scalar)
1620 PPCODE:
1621 XPUSHs (encode_json (scalar, self));
1622
1623void decode (JSON *self, SV *jsonstr)
1624 PPCODE:
1625 XPUSHs (decode_json (jsonstr, self, 0));
1626
1627void decode_prefix (JSON *self, SV *jsonstr)
1628 PPCODE:
1629{
1630 UV offset;
1631 EXTEND (SP, 2);
1632 PUSHs (decode_json (jsonstr, self, &offset));
1633 PUSHs (sv_2mortal (newSVuv (offset)));
1634}
1635
1636void DESTROY (JSON *self)
1331 CODE: 1637 CODE:
1332{ 1638 SvREFCNT_dec (self->cb_sk_object);
1333 UV *uv = SvJSON (self); 1639 SvREFCNT_dec (self->cb_object);
1334 UV log2 = 0;
1335 1640
1336 if (max_size > 0x80000000UL) max_size = 0x80000000UL; 1641PROTOTYPES: ENABLE
1337 if (max_size == 1) max_size = 2;
1338 1642
1339 while ((1UL << log2) < max_size)
1340 ++log2;
1341
1342 *uv = *uv & ~F_MAXSIZE | (log2 << S_MAXSIZE);
1343
1344 RETVAL = newSVsv (self);
1345}
1346 OUTPUT:
1347 RETVAL
1348
1349void encode (SV *self, SV *scalar) 1643void encode_json (SV *scalar)
1350 PPCODE: 1644 PPCODE:
1351{ 1645{
1352 struct json json = { *SvJSON (self) }; 1646 JSON json = { F_DEFAULT | F_UTF8 };
1353 XPUSHs (encode_json (scalar, &json)); 1647 XPUSHs (encode_json (scalar, &json));
1354} 1648}
1355 1649
1356void decode (SV *self, SV *jsonstr) 1650void decode_json (SV *jsonstr)
1357 PPCODE: 1651 PPCODE:
1358{ 1652{
1359 struct json json = { *SvJSON (self) }; 1653 JSON json = { F_DEFAULT | F_UTF8 };
1360 XPUSHs (decode_json (jsonstr, &json, 0)); 1654 XPUSHs (decode_json (jsonstr, &json, 0));
1361} 1655}
1362 1656
1363void decode_prefix (SV *self, SV *jsonstr)
1364 PPCODE:
1365{
1366 UV offset;
1367 struct json json = { *SvJSON (self) };
1368 EXTEND (SP, 2);
1369 PUSHs (decode_json (jsonstr, &json, &offset));
1370 PUSHs (sv_2mortal (newSVuv (offset)));
1371}
1372
1373PROTOTYPES: ENABLE
1374
1375void to_json (SV *scalar)
1376 PPCODE:
1377{
1378 struct json json = { F_DEFAULT | F_UTF8 };
1379 XPUSHs (encode_json (scalar, &json));
1380}
1381
1382void from_json (SV *jsonstr)
1383 PPCODE:
1384{
1385 struct json json = { F_DEFAULT | F_UTF8 };
1386 XPUSHs (decode_json (jsonstr, &json, 0));
1387}
1388

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines