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.53 by root, Tue Jul 10 15:45:34 2007 UTC vs.
Revision 1.71 by root, Wed Mar 19 03:17:38 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)
64
65#ifdef USE_ITHREADS
66# define JSON_SLOW 1
67# define JSON_STASH (json_stash ? json_stash : gv_stashpv ("JSON::XS", 1))
68#else
69# define JSON_SLOW 0
70# define JSON_STASH json_stash
71#endif
61 72
62static HV *json_stash, *json_boolean_stash; // JSON::XS:: 73static HV *json_stash, *json_boolean_stash; // JSON::XS::
63static SV *json_true, *json_false; 74static SV *json_true, *json_false;
64 75
65typedef struct { 76typedef struct {
69} JSON; 80} JSON;
70 81
71///////////////////////////////////////////////////////////////////////////// 82/////////////////////////////////////////////////////////////////////////////
72// utility functions 83// utility functions
73 84
74inline void 85INLINE void
75shrink (SV *sv) 86shrink (SV *sv)
76{ 87{
77 sv_utf8_downgrade (sv, 1); 88 sv_utf8_downgrade (sv, 1);
78 if (SvLEN (sv) > SvCUR (sv) + 1) 89 if (SvLEN (sv) > SvCUR (sv) + 1)
79 { 90 {
88// decode an utf-8 character and return it, or (UV)-1 in 99// decode an utf-8 character and return it, or (UV)-1 in
89// case of an error. 100// case of an error.
90// we special-case "safe" characters from U+80 .. U+7FF, 101// we special-case "safe" characters from U+80 .. U+7FF,
91// but use the very good perl function to parse anything else. 102// but use the very good perl function to parse anything else.
92// note that we never call this function for a ascii codepoints 103// note that we never call this function for a ascii codepoints
93inline UV 104INLINE UV
94decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen) 105decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen)
95{ 106{
96 if (expect_false (s[0] > 0xdf || s[0] < 0xc2)) 107 if (expect_false (s[0] > 0xdf || s[0] < 0xc2))
97 return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY); 108 return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY);
98 else if (len > 1 && s[1] >= 0x80 && s[1] <= 0xbf) 109 else if (len > 1 && s[1] >= 0x80 && s[1] <= 0xbf)
117 char *end; // SvEND (sv) 128 char *end; // SvEND (sv)
118 SV *sv; // result scalar 129 SV *sv; // result scalar
119 JSON json; 130 JSON json;
120 U32 indent; // indentation level 131 U32 indent; // indentation level
121 U32 maxdepth; // max. indentation/recursion level 132 U32 maxdepth; // max. indentation/recursion level
133 UV limit; // escape character values >= this value when encoding
122} enc_t; 134} enc_t;
123 135
124inline void 136INLINE void
125need (enc_t *enc, STRLEN len) 137need (enc_t *enc, STRLEN len)
126{ 138{
127 if (expect_false (enc->cur + len >= enc->end)) 139 if (expect_false (enc->cur + len >= enc->end))
128 { 140 {
129 STRLEN cur = enc->cur - SvPVX (enc->sv); 141 STRLEN cur = enc->cur - SvPVX (enc->sv);
131 enc->cur = SvPVX (enc->sv) + cur; 143 enc->cur = SvPVX (enc->sv) + cur;
132 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; 144 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1;
133 } 145 }
134} 146}
135 147
136inline void 148INLINE void
137encode_ch (enc_t *enc, char ch) 149encode_ch (enc_t *enc, char ch)
138{ 150{
139 need (enc, 1); 151 need (enc, 1);
140 *enc->cur++ = ch; 152 *enc->cur++ = ch;
141} 153}
195 { 207 {
196 uch = ch; 208 uch = ch;
197 clen = 1; 209 clen = 1;
198 } 210 }
199 211
200 if (uch > 0x10FFFFUL) 212 if (uch < 0x20 || uch >= enc->limit)
201 croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch);
202
203 if (uch < 0x80 || enc->json.flags & F_ASCII || (enc->json.flags & F_LATIN1 && uch > 0xFF))
204 { 213 {
205 if (uch > 0xFFFFUL) 214 if (uch > 0xFFFFUL)
206 { 215 {
216 if (uch > 0x10FFFFUL)
217 croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch);
218
207 need (enc, len += 11); 219 need (enc, len += 11);
208 sprintf (enc->cur, "\\u%04x\\u%04x", 220 sprintf (enc->cur, "\\u%04x\\u%04x",
209 (int)((uch - 0x10000) / 0x400 + 0xD800), 221 (int)((uch - 0x10000) / 0x400 + 0xD800),
210 (int)((uch - 0x10000) % 0x400 + 0xDC00)); 222 (int)((uch - 0x10000) % 0x400 + 0xDC00));
211 enc->cur += 12; 223 enc->cur += 12;
250 262
251 --len; 263 --len;
252 } 264 }
253} 265}
254 266
255inline void 267INLINE void
256encode_indent (enc_t *enc) 268encode_indent (enc_t *enc)
257{ 269{
258 if (enc->json.flags & F_INDENT) 270 if (enc->json.flags & F_INDENT)
259 { 271 {
260 int spaces = enc->indent * INDENT_STEP; 272 int spaces = enc->indent * INDENT_STEP;
263 memset (enc->cur, ' ', spaces); 275 memset (enc->cur, ' ', spaces);
264 enc->cur += spaces; 276 enc->cur += spaces;
265 } 277 }
266} 278}
267 279
268inline void 280INLINE void
269encode_space (enc_t *enc) 281encode_space (enc_t *enc)
270{ 282{
271 need (enc, 1); 283 need (enc, 1);
272 encode_ch (enc, ' '); 284 encode_ch (enc, ' ');
273} 285}
274 286
275inline void 287INLINE void
276encode_nl (enc_t *enc) 288encode_nl (enc_t *enc)
277{ 289{
278 if (enc->json.flags & F_INDENT) 290 if (enc->json.flags & F_INDENT)
279 { 291 {
280 need (enc, 1); 292 need (enc, 1);
281 encode_ch (enc, '\n'); 293 encode_ch (enc, '\n');
282 } 294 }
283} 295}
284 296
285inline void 297INLINE void
286encode_comma (enc_t *enc) 298encode_comma (enc_t *enc)
287{ 299{
288 encode_ch (enc, ','); 300 encode_ch (enc, ',');
289 301
290 if (enc->json.flags & F_INDENT) 302 if (enc->json.flags & F_INDENT)
301 int i, len = av_len (av); 313 int i, len = av_len (av);
302 314
303 if (enc->indent >= enc->maxdepth) 315 if (enc->indent >= enc->maxdepth)
304 croak ("data structure too deep (hit recursion limit)"); 316 croak ("data structure too deep (hit recursion limit)");
305 317
306 encode_ch (enc, '['); encode_nl (enc); 318 encode_ch (enc, '[');
307 ++enc->indent; 319
320 if (len >= 0)
321 {
322 encode_nl (enc); ++enc->indent;
308 323
309 for (i = 0; i <= len; ++i) 324 for (i = 0; i <= len; ++i)
310 { 325 {
326 SV **svp = av_fetch (av, i, 0);
327
311 encode_indent (enc); 328 encode_indent (enc);
312 encode_sv (enc, *av_fetch (av, i, 0));
313 329
330 if (svp)
331 encode_sv (enc, *svp);
332 else
333 encode_str (enc, "null", 4, 0);
334
314 if (i < len) 335 if (i < len)
315 encode_comma (enc); 336 encode_comma (enc);
316 } 337 }
317 338
339 encode_nl (enc); --enc->indent; encode_indent (enc);
340 }
341
318 encode_nl (enc); 342 encode_ch (enc, ']');
319
320 --enc->indent;
321 encode_indent (enc); encode_ch (enc, ']');
322} 343}
323 344
324static void 345static void
325encode_he (enc_t *enc, HE *he) 346encode_hk (enc_t *enc, HE *he)
326{ 347{
327 encode_ch (enc, '"'); 348 encode_ch (enc, '"');
328 349
329 if (HeKLEN (he) == HEf_SVKEY) 350 if (HeKLEN (he) == HEf_SVKEY)
330 { 351 {
343 encode_ch (enc, '"'); 364 encode_ch (enc, '"');
344 365
345 if (enc->json.flags & F_SPACE_BEFORE) encode_space (enc); 366 if (enc->json.flags & F_SPACE_BEFORE) encode_space (enc);
346 encode_ch (enc, ':'); 367 encode_ch (enc, ':');
347 if (enc->json.flags & F_SPACE_AFTER ) encode_space (enc); 368 if (enc->json.flags & F_SPACE_AFTER ) encode_space (enc);
348 encode_sv (enc, HeVAL (he));
349} 369}
350 370
351// compare hash entries, used when all keys are bytestrings 371// compare hash entries, used when all keys are bytestrings
352static int 372static int
353he_cmp_fast (const void *a_, const void *b_) 373he_cmp_fast (const void *a_, const void *b_)
358 HE *b = *(HE **)b_; 378 HE *b = *(HE **)b_;
359 379
360 STRLEN la = HeKLEN (a); 380 STRLEN la = HeKLEN (a);
361 STRLEN lb = HeKLEN (b); 381 STRLEN lb = HeKLEN (b);
362 382
363 if (!(cmp = memcmp (HeKEY (a), HeKEY (b), la < lb ? la : lb))) 383 if (!(cmp = memcmp (HeKEY (b), HeKEY (a), lb < la ? lb : la)))
364 cmp = la - lb; 384 cmp = lb - la;
365 385
366 return cmp; 386 return cmp;
367} 387}
368 388
369// compare hash entries, used when some keys are sv's or utf-x 389// compare hash entries, used when some keys are sv's or utf-x
370static int 390static int
371he_cmp_slow (const void *a, const void *b) 391he_cmp_slow (const void *a, const void *b)
372{ 392{
373 return sv_cmp (HeSVKEY_force (*(HE **)a), HeSVKEY_force (*(HE **)b)); 393 return sv_cmp (HeSVKEY_force (*(HE **)b), HeSVKEY_force (*(HE **)a));
374} 394}
375 395
376static void 396static void
377encode_hv (enc_t *enc, HV *hv) 397encode_hv (enc_t *enc, HV *hv)
378{ 398{
399 HE *he;
379 int count, i; 400 int count;
380 401
381 if (enc->indent >= enc->maxdepth) 402 if (enc->indent >= enc->maxdepth)
382 croak ("data structure too deep (hit recursion limit)"); 403 croak ("data structure too deep (hit recursion limit)");
383 404
384 encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; 405 encode_ch (enc, '{');
385 406
386 if ((count = hv_iterinit (hv)))
387 {
388 // for canonical output we have to sort by keys first 407 // for canonical output we have to sort by keys first
389 // actually, this is mostly due to the stupid so-called 408 // actually, this is mostly due to the stupid so-called
390 // security workaround added somewhere in 5.8.x. 409 // security workaround added somewhere in 5.8.x.
391 // that randomises hash orderings 410 // that randomises hash orderings
392 if (enc->json.flags & F_CANONICAL) 411 if (enc->json.flags & F_CANONICAL)
412 {
413 int count = hv_iterinit (hv);
414
415 if (SvMAGICAL (hv))
393 { 416 {
417 // need to count by iterating. could improve by dynamically building the vector below
418 // but I don't care for the speed of this special case.
419 // note also that we will run into undefined behaviour when the two iterations
420 // do not result in the same count, something I might care for in some later release.
421
422 count = 0;
423 while (hv_iternext (hv))
424 ++count;
425
426 hv_iterinit (hv);
427 }
428
429 if (count)
430 {
394 int fast = 1; 431 int i, fast = 1;
395 HE *he;
396#if defined(__BORLANDC__) || defined(_MSC_VER) 432#if defined(__BORLANDC__) || defined(_MSC_VER)
397 HE **hes = _alloca (count * sizeof (HE)); 433 HE **hes = _alloca (count * sizeof (HE));
398#else 434#else
399 HE *hes [count]; // if your compiler dies here, you need to enable C99 mode 435 HE *hes [count]; // if your compiler dies here, you need to enable C99 mode
400#endif 436#endif
427 463
428 FREETMPS; 464 FREETMPS;
429 LEAVE; 465 LEAVE;
430 } 466 }
431 467
432 for (i = 0; i < count; ++i) 468 encode_nl (enc); ++enc->indent;
469
470 while (count--)
433 { 471 {
434 encode_indent (enc); 472 encode_indent (enc);
473 he = hes [count];
435 encode_he (enc, hes [i]); 474 encode_hk (enc, he);
475 encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he));
436 476
437 if (i < count - 1) 477 if (count)
438 encode_comma (enc); 478 encode_comma (enc);
439 } 479 }
440 480
441 encode_nl (enc); 481 encode_nl (enc); --enc->indent; encode_indent (enc);
442 } 482 }
483 }
443 else 484 else
485 {
486 if (hv_iterinit (hv) || SvMAGICAL (hv))
487 if ((he = hv_iternext (hv)))
444 { 488 {
445 HE *he = hv_iternext (hv); 489 encode_nl (enc); ++enc->indent;
446 490
447 for (;;) 491 for (;;)
448 { 492 {
449 encode_indent (enc); 493 encode_indent (enc);
450 encode_he (enc, he); 494 encode_hk (enc, he);
495 encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he));
451 496
452 if (!(he = hv_iternext (hv))) 497 if (!(he = hv_iternext (hv)))
453 break; 498 break;
454 499
455 encode_comma (enc); 500 encode_comma (enc);
456 } 501 }
457 502
458 encode_nl (enc); 503 encode_nl (enc); --enc->indent; encode_indent (enc);
459 } 504 }
460 } 505 }
461 506
462 --enc->indent; encode_indent (enc); encode_ch (enc, '}'); 507 encode_ch (enc, '}');
463} 508}
464 509
465// encode objects, arrays and special \0=false and \1=true values. 510// encode objects, arrays and special \0=false and \1=true values.
466static void 511static void
467encode_rv (enc_t *enc, SV *sv) 512encode_rv (enc_t *enc, SV *sv)
471 SvGETMAGIC (sv); 516 SvGETMAGIC (sv);
472 svt = SvTYPE (sv); 517 svt = SvTYPE (sv);
473 518
474 if (expect_false (SvOBJECT (sv))) 519 if (expect_false (SvOBJECT (sv)))
475 { 520 {
521 HV *stash = !JSON_SLOW || json_boolean_stash
522 ? json_boolean_stash
523 : gv_stashpv ("JSON::XS::Boolean", 1);
524
476 if (SvSTASH (sv) == json_boolean_stash) 525 if (SvSTASH (sv) == stash)
477 { 526 {
478 if (SvIV (sv)) 527 if (SvIV (sv))
479 encode_str (enc, "true", 4, 0); 528 encode_str (enc, "true", 4, 0);
480 else 529 else
481 encode_str (enc, "false", 5, 0); 530 encode_str (enc, "false", 5, 0);
499 548
500 ENTER; SAVETMPS; PUSHMARK (SP); 549 ENTER; SAVETMPS; PUSHMARK (SP);
501 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); 550 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv)));
502 551
503 // calling with G_SCALAR ensures that we always get a 1 return value 552 // calling with G_SCALAR ensures that we always get a 1 return value
504 // check anyways.
505 PUTBACK; 553 PUTBACK;
506 assert (1 == call_sv ((SV *)GvCV (to_json), G_SCALAR)); 554 call_sv ((SV *)GvCV (to_json), G_SCALAR);
507 SPAGAIN; 555 SPAGAIN;
508 556
509 // catch this surprisingly common error 557 // catch this surprisingly common error
510 if (SvROK (TOPs) && SvRV (TOPs) == sv) 558 if (SvROK (TOPs) && SvRV (TOPs) == sv)
511 croak ("%s::TO_JSON method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); 559 croak ("%s::TO_JSON method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv)));
633 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); 681 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE));
634 enc.cur = SvPVX (enc.sv); 682 enc.cur = SvPVX (enc.sv);
635 enc.end = SvEND (enc.sv); 683 enc.end = SvEND (enc.sv);
636 enc.indent = 0; 684 enc.indent = 0;
637 enc.maxdepth = DEC_DEPTH (enc.json.flags); 685 enc.maxdepth = DEC_DEPTH (enc.json.flags);
686 enc.limit = enc.json.flags & F_ASCII ? 0x000080UL
687 : enc.json.flags & F_LATIN1 ? 0x000100UL
688 : 0x10FFFFUL;
638 689
639 SvPOK_only (enc.sv); 690 SvPOK_only (enc.sv);
640 encode_sv (&enc, scalar); 691 encode_sv (&enc, scalar);
641 692
642 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); 693 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv));
663 JSON json; 714 JSON json;
664 U32 depth; // recursion depth 715 U32 depth; // recursion depth
665 U32 maxdepth; // recursion depth limit 716 U32 maxdepth; // recursion depth limit
666} dec_t; 717} dec_t;
667 718
668inline void 719INLINE void
720decode_comment (dec_t *dec)
721{
722 // only '#'-style comments allowed a.t.m.
723
724 while (*dec->cur && *dec->cur != 0x0a && *dec->cur != 0x0d)
725 ++dec->cur;
726}
727
728INLINE void
669decode_ws (dec_t *dec) 729decode_ws (dec_t *dec)
670{ 730{
671 for (;;) 731 for (;;)
672 { 732 {
673 char ch = *dec->cur; 733 char ch = *dec->cur;
674 734
675 if (ch > 0x20 735 if (ch > 0x20)
736 {
737 if (expect_false (ch == '#'))
738 {
739 if (dec->json.flags & F_RELAXED)
740 decode_comment (dec);
741 else
742 break;
743 }
744 else
745 break;
746 }
676 || (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)) 747 else if (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)
677 break; 748 break; // parse error, but let higher level handle it, gives better error messages
678 749
679 ++dec->cur; 750 ++dec->cur;
680 } 751 }
681} 752}
682 753
931 is_nv = 1; 1002 is_nv = 1;
932 } 1003 }
933 1004
934 if (!is_nv) 1005 if (!is_nv)
935 { 1006 {
1007 int len = dec->cur - start;
1008
936 // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so 1009 // special case the rather common 1..5-digit-int case
937 if (*start == '-') 1010 if (*start == '-')
938 switch (dec->cur - start) 1011 switch (len)
939 { 1012 {
940 case 2: return newSViv (-( start [1] - '0' * 1)); 1013 case 2: return newSViv (-( start [1] - '0' * 1));
941 case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); 1014 case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11));
942 case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); 1015 case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111));
943 case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); 1016 case 5: return newSViv (-( start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111));
1017 case 6: return newSViv (-(start [1] * 10000 + start [2] * 1000 + start [3] * 100 + start [4] * 10 + start [5] - '0' * 11111));
944 } 1018 }
945 else 1019 else
946 switch (dec->cur - start) 1020 switch (len)
947 { 1021 {
948 case 1: return newSViv ( start [0] - '0' * 1); 1022 case 1: return newSViv ( start [0] - '0' * 1);
949 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); 1023 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11);
950 case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); 1024 case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111);
951 case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); 1025 case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111);
1026 case 5: return newSViv ( start [0] * 10000 + start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 11111);
952 } 1027 }
953 1028
954 { 1029 {
955 UV uv; 1030 UV uv;
956 int numtype = grok_number (start, dec->cur - start, &uv); 1031 int numtype = grok_number (start, len, &uv);
957 if (numtype & IS_NUMBER_IN_UV) 1032 if (numtype & IS_NUMBER_IN_UV)
958 if (numtype & IS_NUMBER_NEG) 1033 if (numtype & IS_NUMBER_NEG)
959 { 1034 {
960 if (uv < (UV)IV_MIN) 1035 if (uv < (UV)IV_MIN)
961 return newSViv (-(IV)uv); 1036 return newSViv (-(IV)uv);
962 } 1037 }
963 else 1038 else
964 return newSVuv (uv); 1039 return newSVuv (uv);
965
966 // here would likely be the place for bigint support
967 } 1040 }
968 }
969 1041
970 // if we ever support bigint or bigfloat, this is the place for bigfloat 1042 len -= *start == '-' ? 1 : 0;
1043
1044 // does not fit into IV or UV, try NV
1045 if ((sizeof (NV) == sizeof (double) && DBL_DIG >= len)
1046 #if defined (LDBL_DIG)
1047 || (sizeof (NV) == sizeof (long double) && LDBL_DIG >= len)
1048 #endif
1049 )
1050 // fits into NV without loss of precision
1051 return newSVnv (Atof (start));
1052
1053 // everything else fails, convert it to a string
1054 return newSVpvn (start, dec->cur - start);
1055 }
1056
1057 // loss of precision here
971 return newSVnv (Atof (start)); 1058 return newSVnv (Atof (start));
972 1059
973fail: 1060fail:
974 return 0; 1061 return 0;
975} 1062}
1005 1092
1006 if (*dec->cur != ',') 1093 if (*dec->cur != ',')
1007 ERR (", or ] expected while parsing array"); 1094 ERR (", or ] expected while parsing array");
1008 1095
1009 ++dec->cur; 1096 ++dec->cur;
1097
1098 decode_ws (dec);
1099
1100 if (*dec->cur == ']' && dec->json.flags & F_RELAXED)
1101 {
1102 ++dec->cur;
1103 break;
1104 }
1010 } 1105 }
1011 1106
1012 DEC_DEC_DEPTH; 1107 DEC_DEC_DEPTH;
1013 return newRV_noinc ((SV *)av); 1108 return newRV_noinc ((SV *)av);
1014 1109
1030 if (*dec->cur == '}') 1125 if (*dec->cur == '}')
1031 ++dec->cur; 1126 ++dec->cur;
1032 else 1127 else
1033 for (;;) 1128 for (;;)
1034 { 1129 {
1035 decode_ws (dec); EXPECT_CH ('"'); 1130 EXPECT_CH ('"');
1036 1131
1037 // heuristic: assume that 1132 // heuristic: assume that
1038 // a) decode_str + hv_store_ent are abysmally slow. 1133 // a) decode_str + hv_store_ent are abysmally slow.
1039 // b) most hash keys are short, simple ascii text. 1134 // b) most hash keys are short, simple ascii text.
1040 // => try to "fast-match" such strings to avoid 1135 // => try to "fast-match" such strings to avoid
1054 if (!key) 1149 if (!key)
1055 goto fail; 1150 goto fail;
1056 1151
1057 decode_ws (dec); EXPECT_CH (':'); 1152 decode_ws (dec); EXPECT_CH (':');
1058 1153
1154 decode_ws (dec);
1059 value = decode_sv (dec); 1155 value = decode_sv (dec);
1060 if (!value) 1156 if (!value)
1061 { 1157 {
1062 SvREFCNT_dec (key); 1158 SvREFCNT_dec (key);
1063 goto fail; 1159 goto fail;
1075 int len = p - key; 1171 int len = p - key;
1076 dec->cur = p + 1; 1172 dec->cur = p + 1;
1077 1173
1078 decode_ws (dec); EXPECT_CH (':'); 1174 decode_ws (dec); EXPECT_CH (':');
1079 1175
1176 decode_ws (dec);
1080 value = decode_sv (dec); 1177 value = decode_sv (dec);
1081 if (!value) 1178 if (!value)
1082 goto fail; 1179 goto fail;
1083 1180
1084 hv_store (hv, key, len, value, 0); 1181 hv_store (hv, key, len, value, 0);
1100 1197
1101 if (*dec->cur != ',') 1198 if (*dec->cur != ',')
1102 ERR (", or } expected while parsing object/hash"); 1199 ERR (", or } expected while parsing object/hash");
1103 1200
1104 ++dec->cur; 1201 ++dec->cur;
1202
1203 decode_ws (dec);
1204
1205 if (*dec->cur == '}' && dec->json.flags & F_RELAXED)
1206 {
1207 ++dec->cur;
1208 break;
1209 }
1105 } 1210 }
1106 1211
1107 DEC_DEC_DEPTH; 1212 DEC_DEC_DEPTH;
1108 sv = newRV_noinc ((SV *)hv); 1213 sv = newRV_noinc ((SV *)hv);
1109 1214
1174} 1279}
1175 1280
1176static SV * 1281static SV *
1177decode_sv (dec_t *dec) 1282decode_sv (dec_t *dec)
1178{ 1283{
1179 decode_ws (dec);
1180
1181 // the beauty of JSON: you need exactly one character lookahead 1284 // the beauty of JSON: you need exactly one character lookahead
1182 // to parse anything. 1285 // to parse anything.
1183 switch (*dec->cur) 1286 switch (*dec->cur)
1184 { 1287 {
1185 case '"': ++dec->cur; return decode_str (dec); 1288 case '"': ++dec->cur; return decode_str (dec);
1193 1296
1194 case 't': 1297 case 't':
1195 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) 1298 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4))
1196 { 1299 {
1197 dec->cur += 4; 1300 dec->cur += 4;
1301#if JSON_SLOW
1302 json_true = get_sv ("JSON::XS::true", 1); SvREADONLY_on (json_true);
1303#endif
1198 return SvREFCNT_inc (json_true); 1304 return SvREFCNT_inc (json_true);
1199 } 1305 }
1200 else 1306 else
1201 ERR ("'true' expected"); 1307 ERR ("'true' expected");
1202 1308
1204 1310
1205 case 'f': 1311 case 'f':
1206 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) 1312 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5))
1207 { 1313 {
1208 dec->cur += 5; 1314 dec->cur += 5;
1315#if JSON_SLOW
1316 json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false);
1317#endif
1209 return SvREFCNT_inc (json_false); 1318 return SvREFCNT_inc (json_false);
1210 } 1319 }
1211 else 1320 else
1212 ERR ("'false' expected"); 1321 ERR ("'false' expected");
1213 1322
1263 1372
1264 if (dec.json.cb_object || dec.json.cb_sk_object) 1373 if (dec.json.cb_object || dec.json.cb_sk_object)
1265 dec.json.flags |= F_HOOK; 1374 dec.json.flags |= F_HOOK;
1266 1375
1267 *dec.end = 0; // this should basically be a nop, too, but make sure it's there 1376 *dec.end = 0; // this should basically be a nop, too, but make sure it's there
1377
1378 decode_ws (&dec);
1268 sv = decode_sv (&dec); 1379 sv = decode_sv (&dec);
1269 1380
1270 if (!(offset_return || !sv)) 1381 if (!(offset_return || !sv))
1271 { 1382 {
1272 // check for trailing garbage 1383 // check for trailing garbage
1340 json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false); 1451 json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false);
1341} 1452}
1342 1453
1343PROTOTYPES: DISABLE 1454PROTOTYPES: DISABLE
1344 1455
1456void CLONE (...)
1457 CODE:
1458 json_stash = 0;
1459 json_boolean_stash = 0;
1460
1345void new (char *klass) 1461void new (char *klass)
1346 PPCODE: 1462 PPCODE:
1347{ 1463{
1348 SV *pv = NEWSV (0, sizeof (JSON)); 1464 SV *pv = NEWSV (0, sizeof (JSON));
1349 SvPOK_only (pv); 1465 SvPOK_only (pv);
1350 Zero (SvPVX (pv), 1, JSON); 1466 Zero (SvPVX (pv), 1, JSON);
1351 ((JSON *)SvPVX (pv))->flags = F_DEFAULT; 1467 ((JSON *)SvPVX (pv))->flags = F_DEFAULT;
1352 XPUSHs (sv_2mortal (sv_bless (newRV_noinc (pv), json_stash))); 1468 XPUSHs (sv_2mortal (sv_bless (
1469 newRV_noinc (pv),
1470 strEQ (klass, "JSON::XS") ? JSON_STASH : gv_stashpv (klass, 1)
1471 )));
1353} 1472}
1354 1473
1355void ascii (JSON *self, int enable = 1) 1474void ascii (JSON *self, int enable = 1)
1356 ALIAS: 1475 ALIAS:
1357 ascii = F_ASCII 1476 ascii = F_ASCII
1364 pretty = F_PRETTY 1483 pretty = F_PRETTY
1365 allow_nonref = F_ALLOW_NONREF 1484 allow_nonref = F_ALLOW_NONREF
1366 shrink = F_SHRINK 1485 shrink = F_SHRINK
1367 allow_blessed = F_ALLOW_BLESSED 1486 allow_blessed = F_ALLOW_BLESSED
1368 convert_blessed = F_CONV_BLESSED 1487 convert_blessed = F_CONV_BLESSED
1488 relaxed = F_RELAXED
1369 PPCODE: 1489 PPCODE:
1370{ 1490{
1371 if (enable) 1491 if (enable)
1372 self->flags |= ix; 1492 self->flags |= ix;
1373 else 1493 else
1374 self->flags &= ~ix; 1494 self->flags &= ~ix;
1375 1495
1376 XPUSHs (ST (0)); 1496 XPUSHs (ST (0));
1377} 1497}
1378 1498
1499void get_ascii (JSON *self)
1500 ALIAS:
1501 get_ascii = F_ASCII
1502 get_latin1 = F_LATIN1
1503 get_utf8 = F_UTF8
1504 get_indent = F_INDENT
1505 get_canonical = F_CANONICAL
1506 get_space_before = F_SPACE_BEFORE
1507 get_space_after = F_SPACE_AFTER
1508 get_allow_nonref = F_ALLOW_NONREF
1509 get_shrink = F_SHRINK
1510 get_allow_blessed = F_ALLOW_BLESSED
1511 get_convert_blessed = F_CONV_BLESSED
1512 get_relaxed = F_RELAXED
1513 PPCODE:
1514 XPUSHs (boolSV (self->flags & ix));
1515
1379void max_depth (JSON *self, UV max_depth = 0x80000000UL) 1516void max_depth (JSON *self, UV max_depth = 0x80000000UL)
1380 PPCODE: 1517 PPCODE:
1381{ 1518{
1382 UV log2 = 0; 1519 UV log2 = 0;
1383 1520
1389 self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); 1526 self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH);
1390 1527
1391 XPUSHs (ST (0)); 1528 XPUSHs (ST (0));
1392} 1529}
1393 1530
1531U32 get_max_depth (JSON *self)
1532 CODE:
1533 RETVAL = DEC_DEPTH (self->flags);
1534 OUTPUT:
1535 RETVAL
1536
1394void max_size (JSON *self, UV max_size = 0) 1537void max_size (JSON *self, UV max_size = 0)
1395 PPCODE: 1538 PPCODE:
1396{ 1539{
1397 UV log2 = 0; 1540 UV log2 = 0;
1398 1541
1404 1547
1405 self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE); 1548 self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE);
1406 1549
1407 XPUSHs (ST (0)); 1550 XPUSHs (ST (0));
1408} 1551}
1552
1553int get_max_size (JSON *self)
1554 CODE:
1555 RETVAL = DEC_SIZE (self->flags);
1556 OUTPUT:
1557 RETVAL
1409 1558
1410void filter_json_object (JSON *self, SV *cb = &PL_sv_undef) 1559void filter_json_object (JSON *self, SV *cb = &PL_sv_undef)
1411 PPCODE: 1560 PPCODE:
1412{ 1561{
1413 SvREFCNT_dec (self->cb_object); 1562 SvREFCNT_dec (self->cb_object);
1460 SvREFCNT_dec (self->cb_sk_object); 1609 SvREFCNT_dec (self->cb_sk_object);
1461 SvREFCNT_dec (self->cb_object); 1610 SvREFCNT_dec (self->cb_object);
1462 1611
1463PROTOTYPES: ENABLE 1612PROTOTYPES: ENABLE
1464 1613
1465void to_json (SV *scalar) 1614void encode_json (SV *scalar)
1466 PPCODE: 1615 PPCODE:
1467{ 1616{
1468 JSON json = { F_DEFAULT | F_UTF8 }; 1617 JSON json = { F_DEFAULT | F_UTF8 };
1469 XPUSHs (encode_json (scalar, &json)); 1618 XPUSHs (encode_json (scalar, &json));
1470} 1619}
1471 1620
1472void from_json (SV *jsonstr) 1621void decode_json (SV *jsonstr)
1473 PPCODE: 1622 PPCODE:
1474{ 1623{
1475 JSON json = { F_DEFAULT | F_UTF8 }; 1624 JSON json = { F_DEFAULT | F_UTF8 };
1476 XPUSHs (decode_json (jsonstr, &json, 0)); 1625 XPUSHs (decode_json (jsonstr, &json, 0));
1477} 1626}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines