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.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 { 80typedef struct {
65 U32 flags; 81 U32 flags;
82 SV *cb_object;
83 HV *cb_sk_object;
66} JSON; 84} JSON;
67 85
68///////////////////////////////////////////////////////////////////////////// 86/////////////////////////////////////////////////////////////////////////////
69// utility functions 87// utility functions
70 88
71inline void 89INLINE void
72shrink (SV *sv) 90shrink (SV *sv)
73{ 91{
74 sv_utf8_downgrade (sv, 1); 92 sv_utf8_downgrade (sv, 1);
75 if (SvLEN (sv) > SvCUR (sv) + 1) 93 if (SvLEN (sv) > SvCUR (sv) + 1)
76 { 94 {
85// 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
86// case of an error. 104// case of an error.
87// we special-case "safe" characters from U+80 .. U+7FF, 105// we special-case "safe" characters from U+80 .. U+7FF,
88// but use the very good perl function to parse anything else. 106// but use the very good perl function to parse anything else.
89// note that we never call this function for a ascii codepoints 107// note that we never call this function for a ascii codepoints
90inline UV 108INLINE UV
91decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen) 109decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen)
92{ 110{
93 if (expect_false (s[0] > 0xdf || s[0] < 0xc2)) 111 if (expect_true (len >= 2
94 return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY); 112 && IN_RANGE_INC (char, s[0], 0xc2, 0xdf)
95 else if (len > 1 && s[1] >= 0x80 && s[1] <= 0xbf) 113 && IN_RANGE_INC (char, s[1], 0x80, 0xbf)))
96 { 114 {
97 *clen = 2; 115 *clen = 2;
98 return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); 116 return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f);
99 } 117 }
100 else 118 else
101 { 119 return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY);
102 *clen = (STRLEN)-1; 120}
103 return (UV)-1; 121
104 } 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;
105} 144}
106 145
107///////////////////////////////////////////////////////////////////////////// 146/////////////////////////////////////////////////////////////////////////////
108// encoder 147// encoder
109 148
114 char *end; // SvEND (sv) 153 char *end; // SvEND (sv)
115 SV *sv; // result scalar 154 SV *sv; // result scalar
116 JSON json; 155 JSON json;
117 U32 indent; // indentation level 156 U32 indent; // indentation level
118 U32 maxdepth; // max. indentation/recursion level 157 U32 maxdepth; // max. indentation/recursion level
158 UV limit; // escape character values >= this value when encoding
119} enc_t; 159} enc_t;
120 160
121inline void 161INLINE void
122need (enc_t *enc, STRLEN len) 162need (enc_t *enc, STRLEN len)
123{ 163{
124 if (expect_false (enc->cur + len >= enc->end)) 164 if (expect_false (enc->cur + len >= enc->end))
125 { 165 {
126 STRLEN cur = enc->cur - SvPVX (enc->sv); 166 STRLEN cur = enc->cur - SvPVX (enc->sv);
128 enc->cur = SvPVX (enc->sv) + cur; 168 enc->cur = SvPVX (enc->sv) + cur;
129 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; 169 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1;
130 } 170 }
131} 171}
132 172
133inline void 173INLINE void
134encode_ch (enc_t *enc, char ch) 174encode_ch (enc_t *enc, char ch)
135{ 175{
136 need (enc, 1); 176 need (enc, 1);
137 *enc->cur++ = ch; 177 *enc->cur++ = ch;
138} 178}
192 { 232 {
193 uch = ch; 233 uch = ch;
194 clen = 1; 234 clen = 1;
195 } 235 }
196 236
197 if (uch > 0x10FFFFUL) 237 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 { 238 {
202 if (uch > 0xFFFFUL) 239 if (uch >= 0x10000UL)
203 { 240 {
241 if (uch >= 0x110000UL)
242 croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch);
243
204 need (enc, len += 11); 244 need (enc, len += 11);
205 sprintf (enc->cur, "\\u%04x\\u%04x", 245 sprintf (enc->cur, "\\u%04x\\u%04x",
206 (int)((uch - 0x10000) / 0x400 + 0xD800), 246 (int)((uch - 0x10000) / 0x400 + 0xD800),
207 (int)((uch - 0x10000) % 0x400 + 0xDC00)); 247 (int)((uch - 0x10000) % 0x400 + 0xDC00));
208 enc->cur += 12; 248 enc->cur += 12;
236 while (--clen); 276 while (--clen);
237 } 277 }
238 else 278 else
239 { 279 {
240 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
241 enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); 281 enc->cur = encode_utf8 (enc->cur, uch);
242 ++str; 282 ++str;
243 } 283 }
244 } 284 }
245 } 285 }
246 } 286 }
247 287
248 --len; 288 --len;
249 } 289 }
250} 290}
251 291
252inline void 292INLINE void
253encode_indent (enc_t *enc) 293encode_indent (enc_t *enc)
254{ 294{
255 if (enc->json.flags & F_INDENT) 295 if (enc->json.flags & F_INDENT)
256 { 296 {
257 int spaces = enc->indent * INDENT_STEP; 297 int spaces = enc->indent * INDENT_STEP;
260 memset (enc->cur, ' ', spaces); 300 memset (enc->cur, ' ', spaces);
261 enc->cur += spaces; 301 enc->cur += spaces;
262 } 302 }
263} 303}
264 304
265inline void 305INLINE void
266encode_space (enc_t *enc) 306encode_space (enc_t *enc)
267{ 307{
268 need (enc, 1); 308 need (enc, 1);
269 encode_ch (enc, ' '); 309 encode_ch (enc, ' ');
270} 310}
271 311
272inline void 312INLINE void
273encode_nl (enc_t *enc) 313encode_nl (enc_t *enc)
274{ 314{
275 if (enc->json.flags & F_INDENT) 315 if (enc->json.flags & F_INDENT)
276 { 316 {
277 need (enc, 1); 317 need (enc, 1);
278 encode_ch (enc, '\n'); 318 encode_ch (enc, '\n');
279 } 319 }
280} 320}
281 321
282inline void 322INLINE void
283encode_comma (enc_t *enc) 323encode_comma (enc_t *enc)
284{ 324{
285 encode_ch (enc, ','); 325 encode_ch (enc, ',');
286 326
287 if (enc->json.flags & F_INDENT) 327 if (enc->json.flags & F_INDENT)
298 int i, len = av_len (av); 338 int i, len = av_len (av);
299 339
300 if (enc->indent >= enc->maxdepth) 340 if (enc->indent >= enc->maxdepth)
301 croak ("data structure too deep (hit recursion limit)"); 341 croak ("data structure too deep (hit recursion limit)");
302 342
303 encode_ch (enc, '['); encode_nl (enc); 343 encode_ch (enc, '[');
304 ++enc->indent; 344
345 if (len >= 0)
346 {
347 encode_nl (enc); ++enc->indent;
305 348
306 for (i = 0; i <= len; ++i) 349 for (i = 0; i <= len; ++i)
307 { 350 {
351 SV **svp = av_fetch (av, i, 0);
352
308 encode_indent (enc); 353 encode_indent (enc);
309 encode_sv (enc, *av_fetch (av, i, 0));
310 354
355 if (svp)
356 encode_sv (enc, *svp);
357 else
358 encode_str (enc, "null", 4, 0);
359
311 if (i < len) 360 if (i < len)
312 encode_comma (enc); 361 encode_comma (enc);
313 } 362 }
314 363
364 encode_nl (enc); --enc->indent; encode_indent (enc);
365 }
366
315 encode_nl (enc); 367 encode_ch (enc, ']');
316
317 --enc->indent;
318 encode_indent (enc); encode_ch (enc, ']');
319} 368}
320 369
321static void 370static void
322encode_he (enc_t *enc, HE *he) 371encode_hk (enc_t *enc, HE *he)
323{ 372{
324 encode_ch (enc, '"'); 373 encode_ch (enc, '"');
325 374
326 if (HeKLEN (he) == HEf_SVKEY) 375 if (HeKLEN (he) == HEf_SVKEY)
327 { 376 {
340 encode_ch (enc, '"'); 389 encode_ch (enc, '"');
341 390
342 if (enc->json.flags & F_SPACE_BEFORE) encode_space (enc); 391 if (enc->json.flags & F_SPACE_BEFORE) encode_space (enc);
343 encode_ch (enc, ':'); 392 encode_ch (enc, ':');
344 if (enc->json.flags & F_SPACE_AFTER ) encode_space (enc); 393 if (enc->json.flags & F_SPACE_AFTER ) encode_space (enc);
345 encode_sv (enc, HeVAL (he));
346} 394}
347 395
348// compare hash entries, used when all keys are bytestrings 396// compare hash entries, used when all keys are bytestrings
349static int 397static int
350he_cmp_fast (const void *a_, const void *b_) 398he_cmp_fast (const void *a_, const void *b_)
355 HE *b = *(HE **)b_; 403 HE *b = *(HE **)b_;
356 404
357 STRLEN la = HeKLEN (a); 405 STRLEN la = HeKLEN (a);
358 STRLEN lb = HeKLEN (b); 406 STRLEN lb = HeKLEN (b);
359 407
360 if (!(cmp = memcmp (HeKEY (a), HeKEY (b), la < lb ? la : lb))) 408 if (!(cmp = memcmp (HeKEY (b), HeKEY (a), lb < la ? lb : la)))
361 cmp = la - lb; 409 cmp = lb - la;
362 410
363 return cmp; 411 return cmp;
364} 412}
365 413
366// 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
367static int 415static int
368he_cmp_slow (const void *a, const void *b) 416he_cmp_slow (const void *a, const void *b)
369{ 417{
370 return sv_cmp (HeSVKEY_force (*(HE **)a), HeSVKEY_force (*(HE **)b)); 418 return sv_cmp (HeSVKEY_force (*(HE **)b), HeSVKEY_force (*(HE **)a));
371} 419}
372 420
373static void 421static void
374encode_hv (enc_t *enc, HV *hv) 422encode_hv (enc_t *enc, HV *hv)
375{ 423{
424 HE *he;
376 int count, i; 425 int count;
377 426
378 if (enc->indent >= enc->maxdepth) 427 if (enc->indent >= enc->maxdepth)
379 croak ("data structure too deep (hit recursion limit)"); 428 croak ("data structure too deep (hit recursion limit)");
380 429
381 encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; 430 encode_ch (enc, '{');
382 431
383 if ((count = hv_iterinit (hv)))
384 {
385 // for canonical output we have to sort by keys first 432 // for canonical output we have to sort by keys first
386 // actually, this is mostly due to the stupid so-called 433 // actually, this is mostly due to the stupid so-called
387 // security workaround added somewhere in 5.8.x. 434 // security workaround added somewhere in 5.8.x.
388 // that randomises hash orderings 435 // that randomises hash orderings
389 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))
390 { 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 {
391 int fast = 1; 456 int i, fast = 1;
392 HE *he;
393#if defined(__BORLANDC__) || defined(_MSC_VER) 457#if defined(__BORLANDC__) || defined(_MSC_VER)
394 HE **hes = _alloca (count * sizeof (HE)); 458 HE **hes = _alloca (count * sizeof (HE));
395#else 459#else
396 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
397#endif 461#endif
424 488
425 FREETMPS; 489 FREETMPS;
426 LEAVE; 490 LEAVE;
427 } 491 }
428 492
429 for (i = 0; i < count; ++i) 493 encode_nl (enc); ++enc->indent;
494
495 while (count--)
430 { 496 {
431 encode_indent (enc); 497 encode_indent (enc);
498 he = hes [count];
432 encode_he (enc, hes [i]); 499 encode_hk (enc, he);
500 encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he));
433 501
434 if (i < count - 1) 502 if (count)
435 encode_comma (enc); 503 encode_comma (enc);
436 } 504 }
437 505
438 encode_nl (enc); 506 encode_nl (enc); --enc->indent; encode_indent (enc);
439 } 507 }
508 }
440 else 509 else
510 {
511 if (hv_iterinit (hv) || SvMAGICAL (hv))
512 if ((he = hv_iternext (hv)))
441 { 513 {
442 HE *he = hv_iternext (hv); 514 encode_nl (enc); ++enc->indent;
443 515
444 for (;;) 516 for (;;)
445 { 517 {
446 encode_indent (enc); 518 encode_indent (enc);
447 encode_he (enc, he); 519 encode_hk (enc, he);
520 encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he));
448 521
449 if (!(he = hv_iternext (hv))) 522 if (!(he = hv_iternext (hv)))
450 break; 523 break;
451 524
452 encode_comma (enc); 525 encode_comma (enc);
453 } 526 }
454 527
455 encode_nl (enc); 528 encode_nl (enc); --enc->indent; encode_indent (enc);
456 } 529 }
457 } 530 }
458 531
459 --enc->indent; encode_indent (enc); encode_ch (enc, '}'); 532 encode_ch (enc, '}');
460} 533}
461 534
462// encode objects, arrays and special \0=false and \1=true values. 535// encode objects, arrays and special \0=false and \1=true values.
463static void 536static void
464encode_rv (enc_t *enc, SV *sv) 537encode_rv (enc_t *enc, SV *sv)
468 SvGETMAGIC (sv); 541 SvGETMAGIC (sv);
469 svt = SvTYPE (sv); 542 svt = SvTYPE (sv);
470 543
471 if (expect_false (SvOBJECT (sv))) 544 if (expect_false (SvOBJECT (sv)))
472 { 545 {
546 HV *stash = !JSON_SLOW || json_boolean_stash
547 ? json_boolean_stash
548 : gv_stashpv ("JSON::XS::Boolean", 1);
549
473 if (SvSTASH (sv) == json_boolean_stash) 550 if (SvSTASH (sv) == stash)
474 { 551 {
475 if (SvIV (sv) == 0) 552 if (SvIV (sv))
553 encode_str (enc, "true", 4, 0);
554 else
476 encode_str (enc, "false", 5, 0); 555 encode_str (enc, "false", 5, 0);
477 else
478 encode_str (enc, "true", 4, 0);
479 } 556 }
480 else 557 else
481 { 558 {
482#if 0 559#if 0
483 if (0 && sv_derived_from (rv, "JSON::Literal")) 560 if (0 && sv_derived_from (rv, "JSON::Literal"))
486 } 563 }
487#endif 564#endif
488 if (enc->json.flags & F_CONV_BLESSED) 565 if (enc->json.flags & F_CONV_BLESSED)
489 { 566 {
490 // 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
491 GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 1); 568 GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 0);
492 569
493 if (to_json) 570 if (to_json)
494 { 571 {
495 dSP; 572 dSP;
496 ENTER; 573
497 SAVETMPS;
498 PUSHMARK (SP); 574 ENTER; SAVETMPS; PUSHMARK (SP);
499 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); 575 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv)));
500 576
501 // 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
502 // check anyways.
503 PUTBACK; 578 PUTBACK;
504 assert (1 == call_sv ((SV *)GvCV (to_json), G_SCALAR)); 579 call_sv ((SV *)GvCV (to_json), G_SCALAR);
505 SPAGAIN; 580 SPAGAIN;
506 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
507 encode_sv (enc, POPs); 589 encode_sv (enc, sv);
508 590
509 FREETMPS; 591 FREETMPS; LEAVE;
510 LEAVE;
511 } 592 }
512 else if (enc->json.flags & F_ALLOW_BLESSED) 593 else if (enc->json.flags & F_ALLOW_BLESSED)
513 encode_str (enc, "null", 4, 0); 594 encode_str (enc, "null", 4, 0);
514 else 595 else
515 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",
526 encode_hv (enc, (HV *)sv); 607 encode_hv (enc, (HV *)sv);
527 else if (svt == SVt_PVAV) 608 else if (svt == SVt_PVAV)
528 encode_av (enc, (AV *)sv); 609 encode_av (enc, (AV *)sv);
529 else if (svt < SVt_PVAV) 610 else if (svt < SVt_PVAV)
530 { 611 {
531 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')
532 encode_str (enc, "false", 5, 0); 618 encode_str (enc, "false", 5, 0);
533 else if (SvNIOK (sv) && SvIV (sv) == 1)
534 encode_str (enc, "true", 4, 0);
535 else 619 else
536 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",
537 SvPV_nolen (sv_2mortal (newRV_inc (sv)))); 621 SvPV_nolen (sv_2mortal (newRV_inc (sv))));
538 } 622 }
539 else 623 else
561 Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur); 645 Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur);
562 enc->cur += strlen (enc->cur); 646 enc->cur += strlen (enc->cur);
563 } 647 }
564 else if (SvIOKp (sv)) 648 else if (SvIOKp (sv))
565 { 649 {
566 // 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
567 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)
568 { 655 {
569 // large integer, use the (rather slow) snprintf way. 656 // large integer, use the (rather slow) snprintf way.
570 need (enc, sizeof (UV) * 3); 657 need (enc, sizeof (UV) * 5 / 2 + 1); // CHAR_BIT is at least 8
571 enc->cur += 658 enc->cur +=
572 SvIsUV(sv) 659 SvIsUV(sv)
573 ? snprintf (enc->cur, sizeof (UV) * 3, "%"UVuf, (UV)SvUVX (sv)) 660 ? snprintf (enc->cur, sizeof (UV) * 3, "%"UVuf, (UV)SvUVX (sv))
574 : snprintf (enc->cur, sizeof (UV) * 3, "%"IVdf, (IV)SvIVX (sv)); 661 : snprintf (enc->cur, sizeof (UV) * 3, "%"IVdf, (IV)SvIVX (sv));
575 } 662 }
576 else 663 else
577 { 664 {
578 // optimise the "small number case" 665 // optimise the "small number case"
579 // 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
580 I32 i = SvIV (sv); 668 I32 i = SvIVX (sv);
581 U32 u; 669 U32 u;
582 char digit, nz = 0; 670 char digit, nz = 0;
583 671
584 need (enc, 6); 672 need (enc, 6);
585 673
622 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); 710 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE));
623 enc.cur = SvPVX (enc.sv); 711 enc.cur = SvPVX (enc.sv);
624 enc.end = SvEND (enc.sv); 712 enc.end = SvEND (enc.sv);
625 enc.indent = 0; 713 enc.indent = 0;
626 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;
627 718
628 SvPOK_only (enc.sv); 719 SvPOK_only (enc.sv);
629 encode_sv (&enc, scalar); 720 encode_sv (&enc, scalar);
630 721
631 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); 722 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv));
652 JSON json; 743 JSON json;
653 U32 depth; // recursion depth 744 U32 depth; // recursion depth
654 U32 maxdepth; // recursion depth limit 745 U32 maxdepth; // recursion depth limit
655} dec_t; 746} dec_t;
656 747
657inline 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
658decode_ws (dec_t *dec) 758decode_ws (dec_t *dec)
659{ 759{
660 for (;;) 760 for (;;)
661 { 761 {
662 char ch = *dec->cur; 762 char ch = *dec->cur;
663 763
664 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 }
665 || (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)) 776 else if (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)
666 break; 777 break; // parse error, but let higher level handle it, gives better error messages
667 778
668 ++dec->cur; 779 ++dec->cur;
669 } 780 }
670} 781}
671 782
777 888
778 if (hi >= 0x80) 889 if (hi >= 0x80)
779 { 890 {
780 utf8 = 1; 891 utf8 = 1;
781 892
782 cur = (char *)uvuni_to_utf8_flags (cur, hi, 0); 893 cur = encode_utf8 (cur, hi);
783 } 894 }
784 else 895 else
785 *cur++ = hi; 896 *cur++ = hi;
786 } 897 }
787 break; 898 break;
789 default: 900 default:
790 --dec_cur; 901 --dec_cur;
791 ERR ("illegal backslash escape sequence in string"); 902 ERR ("illegal backslash escape sequence in string");
792 } 903 }
793 } 904 }
794 else if (expect_true (ch >= 0x20 && ch <= 0x7f)) 905 else if (expect_true (ch >= 0x20 && ch < 0x80))
795 *cur++ = ch; 906 *cur++ = ch;
796 else if (ch >= 0x80) 907 else if (ch >= 0x80)
797 { 908 {
798 STRLEN clen; 909 STRLEN clen;
799 UV uch; 910 UV uch;
920 is_nv = 1; 1031 is_nv = 1;
921 } 1032 }
922 1033
923 if (!is_nv) 1034 if (!is_nv)
924 { 1035 {
1036 int len = dec->cur - start;
1037
925 // 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
926 if (*start == '-') 1039 if (*start == '-')
927 switch (dec->cur - start) 1040 switch (len)
928 { 1041 {
929 case 2: return newSViv (-( start [1] - '0' * 1)); 1042 case 2: return newSViv (-( start [1] - '0' * 1));
930 case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); 1043 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)); 1044 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)); 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));
933 } 1047 }
934 else 1048 else
935 switch (dec->cur - start) 1049 switch (len)
936 { 1050 {
937 case 1: return newSViv ( start [0] - '0' * 1); 1051 case 1: return newSViv ( start [0] - '0' * 1);
938 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); 1052 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); 1053 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); 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);
941 } 1056 }
942 1057
943 { 1058 {
944 UV uv; 1059 UV uv;
945 int numtype = grok_number (start, dec->cur - start, &uv); 1060 int numtype = grok_number (start, len, &uv);
946 if (numtype & IS_NUMBER_IN_UV) 1061 if (numtype & IS_NUMBER_IN_UV)
947 if (numtype & IS_NUMBER_NEG) 1062 if (numtype & IS_NUMBER_NEG)
948 { 1063 {
949 if (uv < (UV)IV_MIN) 1064 if (uv < (UV)IV_MIN)
950 return newSViv (-(IV)uv); 1065 return newSViv (-(IV)uv);
951 } 1066 }
952 else 1067 else
953 return newSVuv (uv); 1068 return newSVuv (uv);
954
955 // here would likely be the place for bigint support
956 } 1069 }
957 }
958 1070
959 // 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
960 return newSVnv (Atof (start)); 1087 return newSVnv (Atof (start));
961 1088
962fail: 1089fail:
963 return 0; 1090 return 0;
964} 1091}
994 1121
995 if (*dec->cur != ',') 1122 if (*dec->cur != ',')
996 ERR (", or ] expected while parsing array"); 1123 ERR (", or ] expected while parsing array");
997 1124
998 ++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 }
999 } 1134 }
1000 1135
1001 DEC_DEC_DEPTH; 1136 DEC_DEC_DEPTH;
1002 return newRV_noinc ((SV *)av); 1137 return newRV_noinc ((SV *)av);
1003 1138
1008} 1143}
1009 1144
1010static SV * 1145static SV *
1011decode_hv (dec_t *dec) 1146decode_hv (dec_t *dec)
1012{ 1147{
1148 SV *sv;
1013 HV *hv = newHV (); 1149 HV *hv = newHV ();
1014 1150
1015 DEC_INC_DEPTH; 1151 DEC_INC_DEPTH;
1016 decode_ws (dec); 1152 decode_ws (dec);
1017 1153
1018 if (*dec->cur == '}') 1154 if (*dec->cur == '}')
1019 ++dec->cur; 1155 ++dec->cur;
1020 else 1156 else
1021 for (;;) 1157 for (;;)
1022 { 1158 {
1023 decode_ws (dec); EXPECT_CH ('"'); 1159 EXPECT_CH ('"');
1024 1160
1025 // heuristic: assume that 1161 // heuristic: assume that
1026 // a) decode_str + hv_store_ent are abysmally slow. 1162 // a) decode_str + hv_store_ent are abysmally slow.
1027 // b) most hash keys are short, simple ascii text. 1163 // b) most hash keys are short, simple ascii text.
1028 // => try to "fast-match" such strings to avoid 1164 // => try to "fast-match" such strings to avoid
1042 if (!key) 1178 if (!key)
1043 goto fail; 1179 goto fail;
1044 1180
1045 decode_ws (dec); EXPECT_CH (':'); 1181 decode_ws (dec); EXPECT_CH (':');
1046 1182
1183 decode_ws (dec);
1047 value = decode_sv (dec); 1184 value = decode_sv (dec);
1048 if (!value) 1185 if (!value)
1049 { 1186 {
1050 SvREFCNT_dec (key); 1187 SvREFCNT_dec (key);
1051 goto fail; 1188 goto fail;
1063 int len = p - key; 1200 int len = p - key;
1064 dec->cur = p + 1; 1201 dec->cur = p + 1;
1065 1202
1066 decode_ws (dec); EXPECT_CH (':'); 1203 decode_ws (dec); EXPECT_CH (':');
1067 1204
1205 decode_ws (dec);
1068 value = decode_sv (dec); 1206 value = decode_sv (dec);
1069 if (!value) 1207 if (!value)
1070 goto fail; 1208 goto fail;
1071 1209
1072 hv_store (hv, key, len, value, 0); 1210 hv_store (hv, key, len, value, 0);
1088 1226
1089 if (*dec->cur != ',') 1227 if (*dec->cur != ',')
1090 ERR (", or } expected while parsing object/hash"); 1228 ERR (", or } expected while parsing object/hash");
1091 1229
1092 ++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 }
1093 } 1239 }
1094 1240
1095 DEC_DEC_DEPTH; 1241 DEC_DEC_DEPTH;
1096 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;
1097 1303
1098fail: 1304fail:
1099 SvREFCNT_dec (hv); 1305 SvREFCNT_dec (hv);
1100 DEC_DEC_DEPTH; 1306 DEC_DEC_DEPTH;
1101 return 0; 1307 return 0;
1102} 1308}
1103 1309
1104static SV * 1310static SV *
1105decode_sv (dec_t *dec) 1311decode_sv (dec_t *dec)
1106{ 1312{
1107 decode_ws (dec);
1108
1109 // the beauty of JSON: you need exactly one character lookahead 1313 // the beauty of JSON: you need exactly one character lookahead
1110 // to parse anything. 1314 // to parse everything.
1111 switch (*dec->cur) 1315 switch (*dec->cur)
1112 { 1316 {
1113 case '"': ++dec->cur; return decode_str (dec); 1317 case '"': ++dec->cur; return decode_str (dec);
1114 case '[': ++dec->cur; return decode_av (dec); 1318 case '[': ++dec->cur; return decode_av (dec);
1115 case '{': ++dec->cur; return decode_hv (dec); 1319 case '{': ++dec->cur; return decode_hv (dec);
1116 1320
1117 case '-': 1321 case '-':
1118 case '0': case '1': case '2': case '3': case '4': 1322 case '0': case '1': case '2': case '3': case '4':
1119 case '5': case '6': case '7': case '8': case '9': 1323 case '5': case '6': case '7': case '8': case '9':
1120 return decode_num (dec); 1324 return decode_num (dec);
1121 1325
1122 case 't': 1326 case 't':
1123 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) 1327 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4))
1124 { 1328 {
1125 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
1126 return SvREFCNT_inc (json_true); 1333 return SvREFCNT_inc (json_true);
1127 } 1334 }
1128 else 1335 else
1129 ERR ("'true' expected"); 1336 ERR ("'true' expected");
1130 1337
1132 1339
1133 case 'f': 1340 case 'f':
1134 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) 1341 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5))
1135 { 1342 {
1136 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
1137 return SvREFCNT_inc (json_false); 1347 return SvREFCNT_inc (json_false);
1138 } 1348 }
1139 else 1349 else
1140 ERR ("'false' expected"); 1350 ERR ("'false' expected");
1141 1351
1187 dec.end = SvEND (string); 1397 dec.end = SvEND (string);
1188 dec.err = 0; 1398 dec.err = 0;
1189 dec.depth = 0; 1399 dec.depth = 0;
1190 dec.maxdepth = DEC_DEPTH (dec.json.flags); 1400 dec.maxdepth = DEC_DEPTH (dec.json.flags);
1191 1401
1402 if (dec.json.cb_object || dec.json.cb_sk_object)
1403 dec.json.flags |= F_HOOK;
1404
1192 *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);
1193 sv = decode_sv (&dec); 1408 sv = decode_sv (&dec);
1194 1409
1195 if (!(offset_return || !sv)) 1410 if (!(offset_return || !sv))
1196 { 1411 {
1197 // check for trailing garbage 1412 // check for trailing garbage
1265 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);
1266} 1481}
1267 1482
1268PROTOTYPES: DISABLE 1483PROTOTYPES: DISABLE
1269 1484
1485void CLONE (...)
1486 CODE:
1487 json_stash = 0;
1488 json_boolean_stash = 0;
1489
1270void new (char *klass) 1490void new (char *klass)
1271 PPCODE: 1491 PPCODE:
1272{ 1492{
1273 SV *pv = NEWSV (0, sizeof (JSON)); 1493 SV *pv = NEWSV (0, sizeof (JSON));
1274 SvPOK_only (pv); 1494 SvPOK_only (pv);
1275 Zero (SvPVX (pv), 1, sizeof (JSON)); 1495 Zero (SvPVX (pv), 1, JSON);
1276 ((JSON *)SvPVX (pv))->flags = F_DEFAULT; 1496 ((JSON *)SvPVX (pv))->flags = F_DEFAULT;
1277 XPUSHs (sv_2mortal (sv_bless (newRV_noinc (pv), json_stash))); 1497 XPUSHs (sv_2mortal (sv_bless (
1498 newRV_noinc (pv),
1499 strEQ (klass, "JSON::XS") ? JSON_STASH : gv_stashpv (klass, 1)
1500 )));
1278} 1501}
1279 1502
1280void ascii (JSON *self, int enable = 1) 1503void ascii (JSON *self, int enable = 1)
1281 ALIAS: 1504 ALIAS:
1282 ascii = F_ASCII 1505 ascii = F_ASCII
1289 pretty = F_PRETTY 1512 pretty = F_PRETTY
1290 allow_nonref = F_ALLOW_NONREF 1513 allow_nonref = F_ALLOW_NONREF
1291 shrink = F_SHRINK 1514 shrink = F_SHRINK
1292 allow_blessed = F_ALLOW_BLESSED 1515 allow_blessed = F_ALLOW_BLESSED
1293 convert_blessed = F_CONV_BLESSED 1516 convert_blessed = F_CONV_BLESSED
1517 relaxed = F_RELAXED
1294 PPCODE: 1518 PPCODE:
1295{ 1519{
1296 if (enable) 1520 if (enable)
1297 self->flags |= ix; 1521 self->flags |= ix;
1298 else 1522 else
1299 self->flags &= ~ix; 1523 self->flags &= ~ix;
1300 1524
1301 XPUSHs (ST (0)); 1525 XPUSHs (ST (0));
1302} 1526}
1303 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
1304void max_depth (JSON *self, UV max_depth = 0x80000000UL) 1545void max_depth (JSON *self, UV max_depth = 0x80000000UL)
1305 PPCODE: 1546 PPCODE:
1306{ 1547{
1307 UV log2 = 0; 1548 UV log2 = 0;
1308 1549
1314 self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); 1555 self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH);
1315 1556
1316 XPUSHs (ST (0)); 1557 XPUSHs (ST (0));
1317} 1558}
1318 1559
1560U32 get_max_depth (JSON *self)
1561 CODE:
1562 RETVAL = DEC_DEPTH (self->flags);
1563 OUTPUT:
1564 RETVAL
1565
1319void max_size (JSON *self, UV max_size = 0) 1566void max_size (JSON *self, UV max_size = 0)
1320 PPCODE: 1567 PPCODE:
1321{ 1568{
1322 UV log2 = 0; 1569 UV log2 = 0;
1323 1570
1326 1573
1327 while ((1UL << log2) < max_size) 1574 while ((1UL << log2) < max_size)
1328 ++log2; 1575 ++log2;
1329 1576
1330 self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE); 1577 self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE);
1578
1579 XPUSHs (ST (0));
1580}
1581
1582int get_max_size (JSON *self)
1583 CODE:
1584 RETVAL = DEC_SIZE (self->flags);
1585 OUTPUT:
1586 RETVAL
1587
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 }
1331 1615
1332 XPUSHs (ST (0)); 1616 XPUSHs (ST (0));
1333} 1617}
1334 1618
1335void encode (JSON *self, SV *scalar) 1619void encode (JSON *self, SV *scalar)
1347 EXTEND (SP, 2); 1631 EXTEND (SP, 2);
1348 PUSHs (decode_json (jsonstr, self, &offset)); 1632 PUSHs (decode_json (jsonstr, self, &offset));
1349 PUSHs (sv_2mortal (newSVuv (offset))); 1633 PUSHs (sv_2mortal (newSVuv (offset)));
1350} 1634}
1351 1635
1636void DESTROY (JSON *self)
1637 CODE:
1638 SvREFCNT_dec (self->cb_sk_object);
1639 SvREFCNT_dec (self->cb_object);
1640
1352PROTOTYPES: ENABLE 1641PROTOTYPES: ENABLE
1353 1642
1354void to_json (SV *scalar) 1643void encode_json (SV *scalar)
1355 PPCODE: 1644 PPCODE:
1356{ 1645{
1357 JSON json = { F_DEFAULT | F_UTF8 }; 1646 JSON json = { F_DEFAULT | F_UTF8 };
1358 XPUSHs (encode_json (scalar, &json)); 1647 XPUSHs (encode_json (scalar, &json));
1359} 1648}
1360 1649
1361void from_json (SV *jsonstr) 1650void decode_json (SV *jsonstr)
1362 PPCODE: 1651 PPCODE:
1363{ 1652{
1364 JSON json = { F_DEFAULT | F_UTF8 }; 1653 JSON json = { F_DEFAULT | F_UTF8 };
1365 XPUSHs (decode_json (jsonstr, &json, 0)); 1654 XPUSHs (decode_json (jsonstr, &json, 0));
1366} 1655}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines