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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines