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.73 by root, Wed Mar 19 13:44:43 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
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 (ch <= 0x7FF)
113 } 129 {
130 *s++ = (ch >> 6) | 0xc0;
131 *s++ = (ch & 0x3f) | 0x80;
132 }
133 else
134 s = uvuni_to_utf8_flags (s, ch, 0);
135
136 return s;
114} 137}
115 138
116///////////////////////////////////////////////////////////////////////////// 139/////////////////////////////////////////////////////////////////////////////
117// encoder 140// encoder
118 141
120typedef struct 143typedef struct
121{ 144{
122 char *cur; // SvPVX (sv) + current output position 145 char *cur; // SvPVX (sv) + current output position
123 char *end; // SvEND (sv) 146 char *end; // SvEND (sv)
124 SV *sv; // result scalar 147 SV *sv; // result scalar
125 struct json json; 148 JSON json;
126 U32 indent; // indentation level 149 U32 indent; // indentation level
127 U32 maxdepth; // max. indentation/recursion level 150 U32 maxdepth; // max. indentation/recursion level
151 UV limit; // escape character values >= this value when encoding
128} enc_t; 152} enc_t;
129 153
130inline void 154INLINE void
131need (enc_t *enc, STRLEN len) 155need (enc_t *enc, STRLEN len)
132{ 156{
133 if (expect_false (enc->cur + len >= enc->end)) 157 if (expect_false (enc->cur + len >= enc->end))
134 { 158 {
135 STRLEN cur = enc->cur - SvPVX (enc->sv); 159 STRLEN cur = enc->cur - SvPVX (enc->sv);
137 enc->cur = SvPVX (enc->sv) + cur; 161 enc->cur = SvPVX (enc->sv) + cur;
138 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; 162 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1;
139 } 163 }
140} 164}
141 165
142inline void 166INLINE void
143encode_ch (enc_t *enc, char ch) 167encode_ch (enc_t *enc, char ch)
144{ 168{
145 need (enc, 1); 169 need (enc, 1);
146 *enc->cur++ = ch; 170 *enc->cur++ = ch;
147} 171}
201 { 225 {
202 uch = ch; 226 uch = ch;
203 clen = 1; 227 clen = 1;
204 } 228 }
205 229
206 if (uch > 0x10FFFFUL) 230 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 { 231 {
211 if (uch > 0xFFFFUL) 232 if (uch > 0xFFFFUL)
212 { 233 {
234 if (uch > 0x10FFFFUL)
235 croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch);
236
213 need (enc, len += 11); 237 need (enc, len += 11);
214 sprintf (enc->cur, "\\u%04x\\u%04x", 238 sprintf (enc->cur, "\\u%04x\\u%04x",
215 (int)((uch - 0x10000) / 0x400 + 0xD800), 239 (int)((uch - 0x10000) / 0x400 + 0xD800),
216 (int)((uch - 0x10000) % 0x400 + 0xDC00)); 240 (int)((uch - 0x10000) % 0x400 + 0xDC00));
217 enc->cur += 12; 241 enc->cur += 12;
245 while (--clen); 269 while (--clen);
246 } 270 }
247 else 271 else
248 { 272 {
249 need (enc, len += UTF8_MAXBYTES - 1); // never more than 11 bytes needed 273 need (enc, len += UTF8_MAXBYTES - 1); // never more than 11 bytes needed
250 enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); 274 enc->cur = encode_utf8 (enc->cur, uch);
251 ++str; 275 ++str;
252 } 276 }
253 } 277 }
254 } 278 }
255 } 279 }
256 280
257 --len; 281 --len;
258 } 282 }
259} 283}
260 284
261inline void 285INLINE void
262encode_indent (enc_t *enc) 286encode_indent (enc_t *enc)
263{ 287{
264 if (enc->json.flags & F_INDENT) 288 if (enc->json.flags & F_INDENT)
265 { 289 {
266 int spaces = enc->indent * INDENT_STEP; 290 int spaces = enc->indent * INDENT_STEP;
269 memset (enc->cur, ' ', spaces); 293 memset (enc->cur, ' ', spaces);
270 enc->cur += spaces; 294 enc->cur += spaces;
271 } 295 }
272} 296}
273 297
274inline void 298INLINE void
275encode_space (enc_t *enc) 299encode_space (enc_t *enc)
276{ 300{
277 need (enc, 1); 301 need (enc, 1);
278 encode_ch (enc, ' '); 302 encode_ch (enc, ' ');
279} 303}
280 304
281inline void 305INLINE void
282encode_nl (enc_t *enc) 306encode_nl (enc_t *enc)
283{ 307{
284 if (enc->json.flags & F_INDENT) 308 if (enc->json.flags & F_INDENT)
285 { 309 {
286 need (enc, 1); 310 need (enc, 1);
287 encode_ch (enc, '\n'); 311 encode_ch (enc, '\n');
288 } 312 }
289} 313}
290 314
291inline void 315INLINE void
292encode_comma (enc_t *enc) 316encode_comma (enc_t *enc)
293{ 317{
294 encode_ch (enc, ','); 318 encode_ch (enc, ',');
295 319
296 if (enc->json.flags & F_INDENT) 320 if (enc->json.flags & F_INDENT)
307 int i, len = av_len (av); 331 int i, len = av_len (av);
308 332
309 if (enc->indent >= enc->maxdepth) 333 if (enc->indent >= enc->maxdepth)
310 croak ("data structure too deep (hit recursion limit)"); 334 croak ("data structure too deep (hit recursion limit)");
311 335
312 encode_ch (enc, '['); encode_nl (enc); 336 encode_ch (enc, '[');
313 ++enc->indent; 337
338 if (len >= 0)
339 {
340 encode_nl (enc); ++enc->indent;
314 341
315 for (i = 0; i <= len; ++i) 342 for (i = 0; i <= len; ++i)
316 { 343 {
344 SV **svp = av_fetch (av, i, 0);
345
317 encode_indent (enc); 346 encode_indent (enc);
318 encode_sv (enc, *av_fetch (av, i, 0));
319 347
348 if (svp)
349 encode_sv (enc, *svp);
350 else
351 encode_str (enc, "null", 4, 0);
352
320 if (i < len) 353 if (i < len)
321 encode_comma (enc); 354 encode_comma (enc);
322 } 355 }
323 356
357 encode_nl (enc); --enc->indent; encode_indent (enc);
358 }
359
324 encode_nl (enc); 360 encode_ch (enc, ']');
325
326 --enc->indent;
327 encode_indent (enc); encode_ch (enc, ']');
328} 361}
329 362
330static void 363static void
331encode_he (enc_t *enc, HE *he) 364encode_hk (enc_t *enc, HE *he)
332{ 365{
333 encode_ch (enc, '"'); 366 encode_ch (enc, '"');
334 367
335 if (HeKLEN (he) == HEf_SVKEY) 368 if (HeKLEN (he) == HEf_SVKEY)
336 { 369 {
349 encode_ch (enc, '"'); 382 encode_ch (enc, '"');
350 383
351 if (enc->json.flags & F_SPACE_BEFORE) encode_space (enc); 384 if (enc->json.flags & F_SPACE_BEFORE) encode_space (enc);
352 encode_ch (enc, ':'); 385 encode_ch (enc, ':');
353 if (enc->json.flags & F_SPACE_AFTER ) encode_space (enc); 386 if (enc->json.flags & F_SPACE_AFTER ) encode_space (enc);
354 encode_sv (enc, HeVAL (he));
355} 387}
356 388
357// compare hash entries, used when all keys are bytestrings 389// compare hash entries, used when all keys are bytestrings
358static int 390static int
359he_cmp_fast (const void *a_, const void *b_) 391he_cmp_fast (const void *a_, const void *b_)
364 HE *b = *(HE **)b_; 396 HE *b = *(HE **)b_;
365 397
366 STRLEN la = HeKLEN (a); 398 STRLEN la = HeKLEN (a);
367 STRLEN lb = HeKLEN (b); 399 STRLEN lb = HeKLEN (b);
368 400
369 if (!(cmp = memcmp (HeKEY (a), HeKEY (b), la < lb ? la : lb))) 401 if (!(cmp = memcmp (HeKEY (b), HeKEY (a), lb < la ? lb : la)))
370 cmp = la - lb; 402 cmp = lb - la;
371 403
372 return cmp; 404 return cmp;
373} 405}
374 406
375// compare hash entries, used when some keys are sv's or utf-x 407// compare hash entries, used when some keys are sv's or utf-x
376static int 408static int
377he_cmp_slow (const void *a, const void *b) 409he_cmp_slow (const void *a, const void *b)
378{ 410{
379 return sv_cmp (HeSVKEY_force (*(HE **)a), HeSVKEY_force (*(HE **)b)); 411 return sv_cmp (HeSVKEY_force (*(HE **)b), HeSVKEY_force (*(HE **)a));
380} 412}
381 413
382static void 414static void
383encode_hv (enc_t *enc, HV *hv) 415encode_hv (enc_t *enc, HV *hv)
384{ 416{
417 HE *he;
385 int count, i; 418 int count;
386 419
387 if (enc->indent >= enc->maxdepth) 420 if (enc->indent >= enc->maxdepth)
388 croak ("data structure too deep (hit recursion limit)"); 421 croak ("data structure too deep (hit recursion limit)");
389 422
390 encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; 423 encode_ch (enc, '{');
391 424
392 if ((count = hv_iterinit (hv)))
393 {
394 // for canonical output we have to sort by keys first 425 // for canonical output we have to sort by keys first
395 // actually, this is mostly due to the stupid so-called 426 // actually, this is mostly due to the stupid so-called
396 // security workaround added somewhere in 5.8.x. 427 // security workaround added somewhere in 5.8.x.
397 // that randomises hash orderings 428 // that randomises hash orderings
398 if (enc->json.flags & F_CANONICAL) 429 if (enc->json.flags & F_CANONICAL)
430 {
431 int count = hv_iterinit (hv);
432
433 if (SvMAGICAL (hv))
399 { 434 {
435 // need to count by iterating. could improve by dynamically building the vector below
436 // but I don't care for the speed of this special case.
437 // note also that we will run into undefined behaviour when the two iterations
438 // do not result in the same count, something I might care for in some later release.
439
440 count = 0;
441 while (hv_iternext (hv))
442 ++count;
443
444 hv_iterinit (hv);
445 }
446
447 if (count)
448 {
400 int fast = 1; 449 int i, fast = 1;
401 HE *he;
402#if defined(__BORLANDC__) || defined(_MSC_VER) 450#if defined(__BORLANDC__) || defined(_MSC_VER)
403 HE **hes = _alloca (count * sizeof (HE)); 451 HE **hes = _alloca (count * sizeof (HE));
404#else 452#else
405 HE *hes [count]; // if your compiler dies here, you need to enable C99 mode 453 HE *hes [count]; // if your compiler dies here, you need to enable C99 mode
406#endif 454#endif
433 481
434 FREETMPS; 482 FREETMPS;
435 LEAVE; 483 LEAVE;
436 } 484 }
437 485
438 for (i = 0; i < count; ++i) 486 encode_nl (enc); ++enc->indent;
487
488 while (count--)
439 { 489 {
440 encode_indent (enc); 490 encode_indent (enc);
491 he = hes [count];
441 encode_he (enc, hes [i]); 492 encode_hk (enc, he);
493 encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he));
442 494
443 if (i < count - 1) 495 if (count)
444 encode_comma (enc); 496 encode_comma (enc);
445 } 497 }
446 498
447 encode_nl (enc); 499 encode_nl (enc); --enc->indent; encode_indent (enc);
448 } 500 }
501 }
449 else 502 else
503 {
504 if (hv_iterinit (hv) || SvMAGICAL (hv))
505 if ((he = hv_iternext (hv)))
450 { 506 {
451 HE *he = hv_iternext (hv); 507 encode_nl (enc); ++enc->indent;
452 508
453 for (;;) 509 for (;;)
454 { 510 {
455 encode_indent (enc); 511 encode_indent (enc);
456 encode_he (enc, he); 512 encode_hk (enc, he);
513 encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he));
457 514
458 if (!(he = hv_iternext (hv))) 515 if (!(he = hv_iternext (hv)))
459 break; 516 break;
460 517
461 encode_comma (enc); 518 encode_comma (enc);
462 } 519 }
463 520
464 encode_nl (enc); 521 encode_nl (enc); --enc->indent; encode_indent (enc);
465 } 522 }
466 } 523 }
467 524
468 --enc->indent; encode_indent (enc); encode_ch (enc, '}'); 525 encode_ch (enc, '}');
469} 526}
470 527
471// encode objects, arrays and special \0=false and \1=true values. 528// encode objects, arrays and special \0=false and \1=true values.
472static void 529static void
473encode_rv (enc_t *enc, SV *sv) 530encode_rv (enc_t *enc, SV *sv)
477 SvGETMAGIC (sv); 534 SvGETMAGIC (sv);
478 svt = SvTYPE (sv); 535 svt = SvTYPE (sv);
479 536
480 if (expect_false (SvOBJECT (sv))) 537 if (expect_false (SvOBJECT (sv)))
481 { 538 {
539 HV *stash = !JSON_SLOW || json_boolean_stash
540 ? json_boolean_stash
541 : gv_stashpv ("JSON::XS::Boolean", 1);
542
482 if (SvSTASH (sv) == json_boolean_stash) 543 if (SvSTASH (sv) == stash)
483 { 544 {
484 if (SvIV (sv) == 0) 545 if (SvIV (sv))
546 encode_str (enc, "true", 4, 0);
547 else
485 encode_str (enc, "false", 5, 0); 548 encode_str (enc, "false", 5, 0);
486 else
487 encode_str (enc, "true", 4, 0);
488 } 549 }
489 else 550 else
490 { 551 {
491#if 0 552#if 0
492 if (0 && sv_derived_from (rv, "JSON::Literal")) 553 if (0 && sv_derived_from (rv, "JSON::Literal"))
495 } 556 }
496#endif 557#endif
497 if (enc->json.flags & F_CONV_BLESSED) 558 if (enc->json.flags & F_CONV_BLESSED)
498 { 559 {
499 // we re-bless the reference to get overload and other niceties right 560 // we re-bless the reference to get overload and other niceties right
500 GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 1); 561 GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 0);
501 562
502 if (to_json) 563 if (to_json)
503 { 564 {
504 dSP; 565 dSP;
505 ENTER; 566
506 SAVETMPS;
507 PUSHMARK (SP); 567 ENTER; SAVETMPS; PUSHMARK (SP);
508 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); 568 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv)));
509 569
510 // calling with G_SCALAR ensures that we always get a 1 reutrn value 570 // calling with G_SCALAR ensures that we always get a 1 return value
511 // check anyways.
512 PUTBACK; 571 PUTBACK;
513 assert (1 == call_sv ((SV *)GvCV (to_json), G_SCALAR)); 572 call_sv ((SV *)GvCV (to_json), G_SCALAR);
514 SPAGAIN; 573 SPAGAIN;
515 574
575 // catch this surprisingly common error
576 if (SvROK (TOPs) && SvRV (TOPs) == sv)
577 croak ("%s::TO_JSON method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv)));
578
579 sv = POPs;
580 PUTBACK;
581
516 encode_sv (enc, POPs); 582 encode_sv (enc, sv);
517 583
518 FREETMPS; 584 FREETMPS; LEAVE;
519 LEAVE;
520 } 585 }
521 else if (enc->json.flags & F_ALLOW_BLESSED) 586 else if (enc->json.flags & F_ALLOW_BLESSED)
522 encode_str (enc, "null", 4, 0); 587 encode_str (enc, "null", 4, 0);
523 else 588 else
524 croak ("encountered object '%s', but neither allow_blessed enabled nor TO_JSON method available on it", 589 croak ("encountered object '%s', but neither allow_blessed enabled nor TO_JSON method available on it",
535 encode_hv (enc, (HV *)sv); 600 encode_hv (enc, (HV *)sv);
536 else if (svt == SVt_PVAV) 601 else if (svt == SVt_PVAV)
537 encode_av (enc, (AV *)sv); 602 encode_av (enc, (AV *)sv);
538 else if (svt < SVt_PVAV) 603 else if (svt < SVt_PVAV)
539 { 604 {
540 if (SvNIOK (sv) && SvIV (sv) == 0) 605 STRLEN len = 0;
606 char *pv = svt ? SvPV (sv, len) : 0;
607
608 if (len == 1 && *pv == '1')
609 encode_str (enc, "true", 4, 0);
610 else if (len == 1 && *pv == '0')
541 encode_str (enc, "false", 5, 0); 611 encode_str (enc, "false", 5, 0);
542 else if (SvNIOK (sv) && SvIV (sv) == 1)
543 encode_str (enc, "true", 4, 0);
544 else 612 else
545 croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", 613 croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1",
546 SvPV_nolen (sv_2mortal (newRV_inc (sv)))); 614 SvPV_nolen (sv_2mortal (newRV_inc (sv))));
547 } 615 }
548 else 616 else
618 croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", 686 croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this",
619 SvPV_nolen (sv), SvFLAGS (sv)); 687 SvPV_nolen (sv), SvFLAGS (sv));
620} 688}
621 689
622static SV * 690static SV *
623encode_json (SV *scalar, struct json *json) 691encode_json (SV *scalar, JSON *json)
624{ 692{
625 enc_t enc; 693 enc_t enc;
626 694
627 if (!(json->flags & F_ALLOW_NONREF) && !SvROK (scalar)) 695 if (!(json->flags & F_ALLOW_NONREF) && !SvROK (scalar))
628 croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); 696 croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)");
631 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); 699 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE));
632 enc.cur = SvPVX (enc.sv); 700 enc.cur = SvPVX (enc.sv);
633 enc.end = SvEND (enc.sv); 701 enc.end = SvEND (enc.sv);
634 enc.indent = 0; 702 enc.indent = 0;
635 enc.maxdepth = DEC_DEPTH (enc.json.flags); 703 enc.maxdepth = DEC_DEPTH (enc.json.flags);
704 enc.limit = enc.json.flags & F_ASCII ? 0x000080UL
705 : enc.json.flags & F_LATIN1 ? 0x000100UL
706 : 0x10FFFFUL;
636 707
637 SvPOK_only (enc.sv); 708 SvPOK_only (enc.sv);
638 encode_sv (&enc, scalar); 709 encode_sv (&enc, scalar);
639 710
640 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); 711 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv));
656typedef struct 727typedef struct
657{ 728{
658 char *cur; // current parser pointer 729 char *cur; // current parser pointer
659 char *end; // end of input string 730 char *end; // end of input string
660 const char *err; // parse error, if != 0 731 const char *err; // parse error, if != 0
661 struct json json; 732 JSON json;
662 U32 depth; // recursion depth 733 U32 depth; // recursion depth
663 U32 maxdepth; // recursion depth limit 734 U32 maxdepth; // recursion depth limit
664} dec_t; 735} dec_t;
665 736
666inline void 737INLINE void
738decode_comment (dec_t *dec)
739{
740 // only '#'-style comments allowed a.t.m.
741
742 while (*dec->cur && *dec->cur != 0x0a && *dec->cur != 0x0d)
743 ++dec->cur;
744}
745
746INLINE void
667decode_ws (dec_t *dec) 747decode_ws (dec_t *dec)
668{ 748{
669 for (;;) 749 for (;;)
670 { 750 {
671 char ch = *dec->cur; 751 char ch = *dec->cur;
672 752
673 if (ch > 0x20 753 if (ch > 0x20)
754 {
755 if (expect_false (ch == '#'))
756 {
757 if (dec->json.flags & F_RELAXED)
758 decode_comment (dec);
759 else
760 break;
761 }
762 else
763 break;
764 }
674 || (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)) 765 else if (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)
675 break; 766 break; // parse error, but let higher level handle it, gives better error messages
676 767
677 ++dec->cur; 768 ++dec->cur;
678 } 769 }
679} 770}
680 771
786 877
787 if (hi >= 0x80) 878 if (hi >= 0x80)
788 { 879 {
789 utf8 = 1; 880 utf8 = 1;
790 881
791 cur = (char *)uvuni_to_utf8_flags (cur, hi, 0); 882 cur = encode_utf8 (cur, hi);
792 } 883 }
793 else 884 else
794 *cur++ = hi; 885 *cur++ = hi;
795 } 886 }
796 break; 887 break;
929 is_nv = 1; 1020 is_nv = 1;
930 } 1021 }
931 1022
932 if (!is_nv) 1023 if (!is_nv)
933 { 1024 {
1025 int len = dec->cur - start;
1026
934 // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so 1027 // special case the rather common 1..5-digit-int case
935 if (*start == '-') 1028 if (*start == '-')
936 switch (dec->cur - start) 1029 switch (len)
937 { 1030 {
938 case 2: return newSViv (-( start [1] - '0' * 1)); 1031 case 2: return newSViv (-( start [1] - '0' * 1));
939 case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); 1032 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)); 1033 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)); 1034 case 5: return newSViv (-( start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111));
1035 case 6: return newSViv (-(start [1] * 10000 + start [2] * 1000 + start [3] * 100 + start [4] * 10 + start [5] - '0' * 11111));
942 } 1036 }
943 else 1037 else
944 switch (dec->cur - start) 1038 switch (len)
945 { 1039 {
946 case 1: return newSViv ( start [0] - '0' * 1); 1040 case 1: return newSViv ( start [0] - '0' * 1);
947 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); 1041 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); 1042 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); 1043 case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111);
1044 case 5: return newSViv ( start [0] * 10000 + start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 11111);
950 } 1045 }
951 1046
952 { 1047 {
953 UV uv; 1048 UV uv;
954 int numtype = grok_number (start, dec->cur - start, &uv); 1049 int numtype = grok_number (start, len, &uv);
955 if (numtype & IS_NUMBER_IN_UV) 1050 if (numtype & IS_NUMBER_IN_UV)
956 if (numtype & IS_NUMBER_NEG) 1051 if (numtype & IS_NUMBER_NEG)
957 { 1052 {
958 if (uv < (UV)IV_MIN) 1053 if (uv < (UV)IV_MIN)
959 return newSViv (-(IV)uv); 1054 return newSViv (-(IV)uv);
960 } 1055 }
961 else 1056 else
962 return newSVuv (uv); 1057 return newSVuv (uv);
963
964 // here would likely be the place for bigint support
965 } 1058 }
966 }
967 1059
968 // if we ever support bigint or bigfloat, this is the place for bigfloat 1060 len -= *start == '-' ? 1 : 0;
1061
1062 // does not fit into IV or UV, try NV
1063 if ((sizeof (NV) == sizeof (double) && DBL_DIG >= len)
1064 #if defined (LDBL_DIG)
1065 || (sizeof (NV) == sizeof (long double) && LDBL_DIG >= len)
1066 #endif
1067 )
1068 // fits into NV without loss of precision
1069 return newSVnv (Atof (start));
1070
1071 // everything else fails, convert it to a string
1072 return newSVpvn (start, dec->cur - start);
1073 }
1074
1075 // loss of precision here
969 return newSVnv (Atof (start)); 1076 return newSVnv (Atof (start));
970 1077
971fail: 1078fail:
972 return 0; 1079 return 0;
973} 1080}
1003 1110
1004 if (*dec->cur != ',') 1111 if (*dec->cur != ',')
1005 ERR (", or ] expected while parsing array"); 1112 ERR (", or ] expected while parsing array");
1006 1113
1007 ++dec->cur; 1114 ++dec->cur;
1115
1116 decode_ws (dec);
1117
1118 if (*dec->cur == ']' && dec->json.flags & F_RELAXED)
1119 {
1120 ++dec->cur;
1121 break;
1122 }
1008 } 1123 }
1009 1124
1010 DEC_DEC_DEPTH; 1125 DEC_DEC_DEPTH;
1011 return newRV_noinc ((SV *)av); 1126 return newRV_noinc ((SV *)av);
1012 1127
1017} 1132}
1018 1133
1019static SV * 1134static SV *
1020decode_hv (dec_t *dec) 1135decode_hv (dec_t *dec)
1021{ 1136{
1137 SV *sv;
1022 HV *hv = newHV (); 1138 HV *hv = newHV ();
1023 1139
1024 DEC_INC_DEPTH; 1140 DEC_INC_DEPTH;
1025 decode_ws (dec); 1141 decode_ws (dec);
1026 1142
1027 if (*dec->cur == '}') 1143 if (*dec->cur == '}')
1028 ++dec->cur; 1144 ++dec->cur;
1029 else 1145 else
1030 for (;;) 1146 for (;;)
1031 { 1147 {
1032 decode_ws (dec); EXPECT_CH ('"'); 1148 EXPECT_CH ('"');
1033 1149
1034 // heuristic: assume that 1150 // heuristic: assume that
1035 // a) decode_str + hv_store_ent are abysmally slow. 1151 // a) decode_str + hv_store_ent are abysmally slow.
1036 // b) most hash keys are short, simple ascii text. 1152 // b) most hash keys are short, simple ascii text.
1037 // => try to "fast-match" such strings to avoid 1153 // => try to "fast-match" such strings to avoid
1051 if (!key) 1167 if (!key)
1052 goto fail; 1168 goto fail;
1053 1169
1054 decode_ws (dec); EXPECT_CH (':'); 1170 decode_ws (dec); EXPECT_CH (':');
1055 1171
1172 decode_ws (dec);
1056 value = decode_sv (dec); 1173 value = decode_sv (dec);
1057 if (!value) 1174 if (!value)
1058 { 1175 {
1059 SvREFCNT_dec (key); 1176 SvREFCNT_dec (key);
1060 goto fail; 1177 goto fail;
1072 int len = p - key; 1189 int len = p - key;
1073 dec->cur = p + 1; 1190 dec->cur = p + 1;
1074 1191
1075 decode_ws (dec); EXPECT_CH (':'); 1192 decode_ws (dec); EXPECT_CH (':');
1076 1193
1194 decode_ws (dec);
1077 value = decode_sv (dec); 1195 value = decode_sv (dec);
1078 if (!value) 1196 if (!value)
1079 goto fail; 1197 goto fail;
1080 1198
1081 hv_store (hv, key, len, value, 0); 1199 hv_store (hv, key, len, value, 0);
1097 1215
1098 if (*dec->cur != ',') 1216 if (*dec->cur != ',')
1099 ERR (", or } expected while parsing object/hash"); 1217 ERR (", or } expected while parsing object/hash");
1100 1218
1101 ++dec->cur; 1219 ++dec->cur;
1220
1221 decode_ws (dec);
1222
1223 if (*dec->cur == '}' && dec->json.flags & F_RELAXED)
1224 {
1225 ++dec->cur;
1226 break;
1227 }
1102 } 1228 }
1103 1229
1104 DEC_DEC_DEPTH; 1230 DEC_DEC_DEPTH;
1105 return newRV_noinc ((SV *)hv); 1231 sv = newRV_noinc ((SV *)hv);
1232
1233 // check filter callbacks
1234 if (dec->json.flags & F_HOOK)
1235 {
1236 if (dec->json.cb_sk_object && HvKEYS (hv) == 1)
1237 {
1238 HE *cb, *he;
1239
1240 hv_iterinit (hv);
1241 he = hv_iternext (hv);
1242 hv_iterinit (hv);
1243
1244 // the next line creates a mortal sv each time its called.
1245 // might want to optimise this for common cases.
1246 cb = hv_fetch_ent (dec->json.cb_sk_object, hv_iterkeysv (he), 0, 0);
1247
1248 if (cb)
1249 {
1250 dSP;
1251 int count;
1252
1253 ENTER; SAVETMPS; PUSHMARK (SP);
1254 XPUSHs (HeVAL (he));
1255
1256 PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN;
1257
1258 if (count == 1)
1259 {
1260 sv = newSVsv (POPs);
1261 FREETMPS; LEAVE;
1262 return sv;
1263 }
1264
1265 FREETMPS; LEAVE;
1266 }
1267 }
1268
1269 if (dec->json.cb_object)
1270 {
1271 dSP;
1272 int count;
1273
1274 ENTER; SAVETMPS; PUSHMARK (SP);
1275 XPUSHs (sv_2mortal (sv));
1276
1277 PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN;
1278
1279 if (count == 1)
1280 {
1281 sv = newSVsv (POPs);
1282 FREETMPS; LEAVE;
1283 return sv;
1284 }
1285
1286 SvREFCNT_inc (sv);
1287 FREETMPS; LEAVE;
1288 }
1289 }
1290
1291 return sv;
1106 1292
1107fail: 1293fail:
1108 SvREFCNT_dec (hv); 1294 SvREFCNT_dec (hv);
1109 DEC_DEC_DEPTH; 1295 DEC_DEC_DEPTH;
1110 return 0; 1296 return 0;
1111} 1297}
1112 1298
1113static SV * 1299static SV *
1114decode_sv (dec_t *dec) 1300decode_sv (dec_t *dec)
1115{ 1301{
1116 decode_ws (dec);
1117
1118 // the beauty of JSON: you need exactly one character lookahead 1302 // the beauty of JSON: you need exactly one character lookahead
1119 // to parse anything. 1303 // to parse everything.
1120 switch (*dec->cur) 1304 switch (*dec->cur)
1121 { 1305 {
1122 case '"': ++dec->cur; return decode_str (dec); 1306 case '"': ++dec->cur; return decode_str (dec);
1123 case '[': ++dec->cur; return decode_av (dec); 1307 case '[': ++dec->cur; return decode_av (dec);
1124 case '{': ++dec->cur; return decode_hv (dec); 1308 case '{': ++dec->cur; return decode_hv (dec);
1125 1309
1126 case '-': 1310 case '-':
1127 case '0': case '1': case '2': case '3': case '4': 1311 case '0': case '1': case '2': case '3': case '4':
1128 case '5': case '6': case '7': case '8': case '9': 1312 case '5': case '6': case '7': case '8': case '9':
1129 return decode_num (dec); 1313 return decode_num (dec);
1130 1314
1131 case 't': 1315 case 't':
1132 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) 1316 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4))
1133 { 1317 {
1134 dec->cur += 4; 1318 dec->cur += 4;
1319#if JSON_SLOW
1320 json_true = get_sv ("JSON::XS::true", 1); SvREADONLY_on (json_true);
1321#endif
1135 return SvREFCNT_inc (json_true); 1322 return SvREFCNT_inc (json_true);
1136 } 1323 }
1137 else 1324 else
1138 ERR ("'true' expected"); 1325 ERR ("'true' expected");
1139 1326
1141 1328
1142 case 'f': 1329 case 'f':
1143 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) 1330 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5))
1144 { 1331 {
1145 dec->cur += 5; 1332 dec->cur += 5;
1333#if JSON_SLOW
1334 json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false);
1335#endif
1146 return SvREFCNT_inc (json_false); 1336 return SvREFCNT_inc (json_false);
1147 } 1337 }
1148 else 1338 else
1149 ERR ("'false' expected"); 1339 ERR ("'false' expected");
1150 1340
1169fail: 1359fail:
1170 return 0; 1360 return 0;
1171} 1361}
1172 1362
1173static SV * 1363static SV *
1174decode_json (SV *string, struct json *json, UV *offset_return) 1364decode_json (SV *string, JSON *json, UV *offset_return)
1175{ 1365{
1176 dec_t dec; 1366 dec_t dec;
1177 UV offset; 1367 UV offset;
1178 SV *sv; 1368 SV *sv;
1179 1369
1196 dec.end = SvEND (string); 1386 dec.end = SvEND (string);
1197 dec.err = 0; 1387 dec.err = 0;
1198 dec.depth = 0; 1388 dec.depth = 0;
1199 dec.maxdepth = DEC_DEPTH (dec.json.flags); 1389 dec.maxdepth = DEC_DEPTH (dec.json.flags);
1200 1390
1391 if (dec.json.cb_object || dec.json.cb_sk_object)
1392 dec.json.flags |= F_HOOK;
1393
1201 *dec.end = 0; // this should basically be a nop, too, but make sure it's there 1394 *dec.end = 0; // this should basically be a nop, too, but make sure it's there
1395
1396 decode_ws (&dec);
1202 sv = decode_sv (&dec); 1397 sv = decode_sv (&dec);
1203 1398
1204 if (!(offset_return || !sv)) 1399 if (!(offset_return || !sv))
1205 { 1400 {
1206 // check for trailing garbage 1401 // check for trailing garbage
1274 json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false); 1469 json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false);
1275} 1470}
1276 1471
1277PROTOTYPES: DISABLE 1472PROTOTYPES: DISABLE
1278 1473
1279SV *new (char *dummy) 1474void CLONE (...)
1280 CODE: 1475 CODE:
1281 RETVAL = sv_bless (newRV_noinc (newSVuv (F_DEFAULT)), json_stash); 1476 json_stash = 0;
1282 OUTPUT: 1477 json_boolean_stash = 0;
1283 RETVAL
1284 1478
1479void new (char *klass)
1480 PPCODE:
1481{
1482 SV *pv = NEWSV (0, sizeof (JSON));
1483 SvPOK_only (pv);
1484 Zero (SvPVX (pv), 1, JSON);
1485 ((JSON *)SvPVX (pv))->flags = F_DEFAULT;
1486 XPUSHs (sv_2mortal (sv_bless (
1487 newRV_noinc (pv),
1488 strEQ (klass, "JSON::XS") ? JSON_STASH : gv_stashpv (klass, 1)
1489 )));
1490}
1491
1285SV *ascii (SV *self, int enable = 1) 1492void ascii (JSON *self, int enable = 1)
1286 ALIAS: 1493 ALIAS:
1287 ascii = F_ASCII 1494 ascii = F_ASCII
1288 latin1 = F_LATIN1 1495 latin1 = F_LATIN1
1289 utf8 = F_UTF8 1496 utf8 = F_UTF8
1290 indent = F_INDENT 1497 indent = F_INDENT
1294 pretty = F_PRETTY 1501 pretty = F_PRETTY
1295 allow_nonref = F_ALLOW_NONREF 1502 allow_nonref = F_ALLOW_NONREF
1296 shrink = F_SHRINK 1503 shrink = F_SHRINK
1297 allow_blessed = F_ALLOW_BLESSED 1504 allow_blessed = F_ALLOW_BLESSED
1298 convert_blessed = F_CONV_BLESSED 1505 convert_blessed = F_CONV_BLESSED
1506 relaxed = F_RELAXED
1507 PPCODE:
1508{
1509 if (enable)
1510 self->flags |= ix;
1511 else
1512 self->flags &= ~ix;
1513
1514 XPUSHs (ST (0));
1515}
1516
1517void get_ascii (JSON *self)
1518 ALIAS:
1519 get_ascii = F_ASCII
1520 get_latin1 = F_LATIN1
1521 get_utf8 = F_UTF8
1522 get_indent = F_INDENT
1523 get_canonical = F_CANONICAL
1524 get_space_before = F_SPACE_BEFORE
1525 get_space_after = F_SPACE_AFTER
1526 get_allow_nonref = F_ALLOW_NONREF
1527 get_shrink = F_SHRINK
1528 get_allow_blessed = F_ALLOW_BLESSED
1529 get_convert_blessed = F_CONV_BLESSED
1530 get_relaxed = F_RELAXED
1531 PPCODE:
1532 XPUSHs (boolSV (self->flags & ix));
1533
1534void max_depth (JSON *self, UV max_depth = 0x80000000UL)
1535 PPCODE:
1536{
1537 UV log2 = 0;
1538
1539 if (max_depth > 0x80000000UL) max_depth = 0x80000000UL;
1540
1541 while ((1UL << log2) < max_depth)
1542 ++log2;
1543
1544 self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH);
1545
1546 XPUSHs (ST (0));
1547}
1548
1549U32 get_max_depth (JSON *self)
1299 CODE: 1550 CODE:
1300{ 1551 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: 1552 OUTPUT:
1310 RETVAL 1553 RETVAL
1311 1554
1312SV *max_depth (SV *self, UV max_depth = 0x80000000UL) 1555void max_size (JSON *self, UV max_size = 0)
1556 PPCODE:
1557{
1558 UV log2 = 0;
1559
1560 if (max_size > 0x80000000UL) max_size = 0x80000000UL;
1561 if (max_size == 1) max_size = 2;
1562
1563 while ((1UL << log2) < max_size)
1564 ++log2;
1565
1566 self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE);
1567
1568 XPUSHs (ST (0));
1569}
1570
1571int get_max_size (JSON *self)
1313 CODE: 1572 CODE:
1314{ 1573 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: 1574 OUTPUT:
1328 RETVAL 1575 RETVAL
1329 1576
1330SV *max_size (SV *self, UV max_size = 0) 1577void filter_json_object (JSON *self, SV *cb = &PL_sv_undef)
1578 PPCODE:
1579{
1580 SvREFCNT_dec (self->cb_object);
1581 self->cb_object = SvOK (cb) ? newSVsv (cb) : 0;
1582
1583 XPUSHs (ST (0));
1584}
1585
1586void filter_json_single_key_object (JSON *self, SV *key, SV *cb = &PL_sv_undef)
1587 PPCODE:
1588{
1589 if (!self->cb_sk_object)
1590 self->cb_sk_object = newHV ();
1591
1592 if (SvOK (cb))
1593 hv_store_ent (self->cb_sk_object, key, newSVsv (cb), 0);
1594 else
1595 {
1596 hv_delete_ent (self->cb_sk_object, key, G_DISCARD, 0);
1597
1598 if (!HvKEYS (self->cb_sk_object))
1599 {
1600 SvREFCNT_dec (self->cb_sk_object);
1601 self->cb_sk_object = 0;
1602 }
1603 }
1604
1605 XPUSHs (ST (0));
1606}
1607
1608void encode (JSON *self, SV *scalar)
1609 PPCODE:
1610 XPUSHs (encode_json (scalar, self));
1611
1612void decode (JSON *self, SV *jsonstr)
1613 PPCODE:
1614 XPUSHs (decode_json (jsonstr, self, 0));
1615
1616void decode_prefix (JSON *self, SV *jsonstr)
1617 PPCODE:
1618{
1619 UV offset;
1620 EXTEND (SP, 2);
1621 PUSHs (decode_json (jsonstr, self, &offset));
1622 PUSHs (sv_2mortal (newSVuv (offset)));
1623}
1624
1625void DESTROY (JSON *self)
1331 CODE: 1626 CODE:
1332{ 1627 SvREFCNT_dec (self->cb_sk_object);
1333 UV *uv = SvJSON (self); 1628 SvREFCNT_dec (self->cb_object);
1334 UV log2 = 0;
1335 1629
1336 if (max_size > 0x80000000UL) max_size = 0x80000000UL; 1630PROTOTYPES: ENABLE
1337 if (max_size == 1) max_size = 2;
1338 1631
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) 1632void encode_json (SV *scalar)
1350 PPCODE: 1633 PPCODE:
1351{ 1634{
1352 struct json json = { *SvJSON (self) }; 1635 JSON json = { F_DEFAULT | F_UTF8 };
1353 XPUSHs (encode_json (scalar, &json)); 1636 XPUSHs (encode_json (scalar, &json));
1354} 1637}
1355 1638
1356void decode (SV *self, SV *jsonstr) 1639void decode_json (SV *jsonstr)
1357 PPCODE: 1640 PPCODE:
1358{ 1641{
1359 struct json json = { *SvJSON (self) }; 1642 JSON json = { F_DEFAULT | F_UTF8 };
1360 XPUSHs (decode_json (jsonstr, &json, 0)); 1643 XPUSHs (decode_json (jsonstr, &json, 0));
1361} 1644}
1362 1645
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