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.12 by root, Sat Mar 24 22:10:08 2007 UTC vs.
Revision 1.32 by root, Wed May 9 16:41:12 2007 UTC

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 9
10#if defined(__BORLANDC__) || defined(_MSC_VER)
11# define snprintf _snprintf // C compilers have this in stdio.h
12#endif
13
9#define F_ASCII 0x00000001 14#define F_ASCII 0x00000001UL
15#define F_LATIN1 0x00000002UL
10#define F_UTF8 0x00000002 16#define F_UTF8 0x00000004UL
11#define F_INDENT 0x00000004 17#define F_INDENT 0x00000008UL
12#define F_CANONICAL 0x00000008 18#define F_CANONICAL 0x00000010UL
13#define F_SPACE_BEFORE 0x00000010 19#define F_SPACE_BEFORE 0x00000020UL
14#define F_SPACE_AFTER 0x00000020 20#define F_SPACE_AFTER 0x00000040UL
15#define F_ALLOW_NONREF 0x00000080 21#define F_ALLOW_NONREF 0x00000100UL
16#define F_SHRINK 0x00000100 22#define F_SHRINK 0x00000200UL
23#define F_MAXDEPTH 0xf8000000UL
24#define S_MAXDEPTH 27
25
26#define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH))
27
28// F_SELFCONVERT? <=> to_json/toJson
29// F_BLESSED? <=> { $__class__$ => }
17 30
18#define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER 31#define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER
19#define F_DEFAULT 0 32#define F_DEFAULT (9UL << S_MAXDEPTH)
20 33
21#define INIT_SIZE 32 // initial scalar size to be allocated 34#define INIT_SIZE 32 // initial scalar size to be allocated
22#define INDENT_STEP 3 // spaces per indentation level 35#define INDENT_STEP 3 // spaces per indentation level
23 36
24#define UTF8_MAX_LEN 11 // for perls UTF-X: max. number of octets per character
25#define SHORT_STRING_LEN 256 // special-case strings of up to this size 37#define SHORT_STRING_LEN 512 // special-case strings of up to this size
26 38
27#define SB do { 39#define SB do {
28#define SE } while (0) 40#define SE } while (0)
29 41
30static HV *json_stash; // JSON::XS:: 42static HV *json_stash; // JSON::XS::
53 SvPV_renew (sv, SvCUR (sv) + 1); 65 SvPV_renew (sv, SvCUR (sv) + 1);
54#endif 66#endif
55 } 67 }
56} 68}
57 69
70// decode an utf-8 character and return it, or (UV)-1 in
71// case of an error.
72// we special-case "safe" characters from U+80 .. U+7FF,
73// but use the very good perl function to parse anything else.
74// note that we never call this function for a ascii codepoints
75static UV
76decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen)
77{
78 if (s[0] > 0xdf || s[0] < 0xc2)
79 return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY);
80 else if (len > 1 && s[1] >= 0x80 && s[1] <= 0xbf)
81 {
82 *clen = 2;
83 return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f);
84 }
85 else
86 {
87 *clen = (STRLEN)-1;
88 return (UV)-1;
89 }
90}
91
58///////////////////////////////////////////////////////////////////////////// 92/////////////////////////////////////////////////////////////////////////////
59// encoder 93// encoder
60 94
61// structure used for encoding JSON 95// structure used for encoding JSON
62typedef struct 96typedef struct
63{ 97{
64 char *cur; // SvPVX (sv) + current output position 98 char *cur; // SvPVX (sv) + current output position
65 char *end; // SvEND (sv) 99 char *end; // SvEND (sv)
66 SV *sv; // result scalar 100 SV *sv; // result scalar
67 UV flags; // F_* 101 U32 flags; // F_*
68 int indent; // indentation level 102 U32 indent; // indentation level
69 int max_depth; // max. recursion level 103 U32 maxdepth; // max. indentation/recursion level
70} enc_t; 104} enc_t;
71 105
72static void 106static void
73need (enc_t *enc, STRLEN len) 107need (enc_t *enc, STRLEN len)
74{ 108{
75 if (enc->cur + len >= enc->end) 109 if (enc->cur + len >= enc->end)
76 { 110 {
77 STRLEN cur = enc->cur - SvPVX (enc->sv); 111 STRLEN cur = enc->cur - SvPVX (enc->sv);
78 SvGROW (enc->sv, cur + len + 1); 112 SvGROW (enc->sv, cur + len + 1);
79 enc->cur = SvPVX (enc->sv) + cur; 113 enc->cur = SvPVX (enc->sv) + cur;
80 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv); 114 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1;
81 } 115 }
82} 116}
83 117
84static void 118static void
85encode_ch (enc_t *enc, char ch) 119encode_ch (enc_t *enc, char ch)
133 STRLEN clen; 167 STRLEN clen;
134 UV uch; 168 UV uch;
135 169
136 if (is_utf8) 170 if (is_utf8)
137 { 171 {
138 uch = utf8n_to_uvuni (str, end - str, &clen, UTF8_CHECK_ONLY); 172 //uch = utf8n_to_uvuni (str, end - str, &clen, UTF8_CHECK_ONLY);
173 uch = decode_utf8 (str, end - str, &clen);
139 if (clen == (STRLEN)-1) 174 if (clen == (STRLEN)-1)
140 croak ("malformed or illegal unicode character in string [%.11s], cannot convert to JSON", str); 175 croak ("malformed or illegal unicode character in string [%.11s], cannot convert to JSON", str);
141 } 176 }
142 else 177 else
143 { 178 {
146 } 181 }
147 182
148 if (uch > 0x10FFFFUL) 183 if (uch > 0x10FFFFUL)
149 croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); 184 croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch);
150 185
151 if (uch < 0x80 || enc->flags & F_ASCII) 186 if (uch < 0x80 || enc->flags & F_ASCII || (enc->flags & F_LATIN1 && uch > 0xFF))
152 { 187 {
153 if (uch > 0xFFFFUL) 188 if (uch > 0xFFFFUL)
154 { 189 {
155 need (enc, len += 11); 190 need (enc, len += 11);
156 sprintf (enc->cur, "\\u%04x\\u%04x", 191 sprintf (enc->cur, "\\u%04x\\u%04x",
170 *enc->cur++ = hexdigit [(uch >> 0) & 15]; 205 *enc->cur++ = hexdigit [(uch >> 0) & 15];
171 } 206 }
172 207
173 str += clen; 208 str += clen;
174 } 209 }
210 else if (enc->flags & F_LATIN1)
211 {
212 *enc->cur++ = uch;
213 str += clen;
214 }
175 else if (is_utf8) 215 else if (is_utf8)
176 { 216 {
177 need (enc, len += clen); 217 need (enc, len += clen);
178 do 218 do
179 { 219 {
181 } 221 }
182 while (--clen); 222 while (--clen);
183 } 223 }
184 else 224 else
185 { 225 {
186 need (enc, len += UTF8_MAX_LEN - 1); // never more than 11 bytes needed 226 need (enc, len += UTF8_MAXBYTES - 1); // never more than 11 bytes needed
187 enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); 227 enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0);
188 ++str; 228 ++str;
189 } 229 }
190 } 230 }
191 } 231 }
241static void 281static void
242encode_av (enc_t *enc, AV *av) 282encode_av (enc_t *enc, AV *av)
243{ 283{
244 int i, len = av_len (av); 284 int i, len = av_len (av);
245 285
286 if (enc->indent >= enc->maxdepth)
287 croak ("data structure too deep (hit recursion limit)");
288
246 encode_ch (enc, '['); encode_nl (enc); 289 encode_ch (enc, '['); encode_nl (enc);
247 ++enc->indent; 290 ++enc->indent;
248 291
249 for (i = 0; i <= len; ++i) 292 for (i = 0; i <= len; ++i)
250 { 293 {
315 358
316static void 359static void
317encode_hv (enc_t *enc, HV *hv) 360encode_hv (enc_t *enc, HV *hv)
318{ 361{
319 int count, i; 362 int count, i;
363
364 if (enc->indent >= enc->maxdepth)
365 croak ("data structure too deep (hit recursion limit)");
320 366
321 encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; 367 encode_ch (enc, '{'); encode_nl (enc); ++enc->indent;
322 368
323 if ((count = hv_iterinit (hv))) 369 if ((count = hv_iterinit (hv)))
324 { 370 {
372 418
373 encode_nl (enc); 419 encode_nl (enc);
374 } 420 }
375 else 421 else
376 { 422 {
377 SV *sv;
378 HE *he = hv_iternext (hv); 423 HE *he = hv_iternext (hv);
379 424
380 for (;;) 425 for (;;)
381 { 426 {
382 encode_indent (enc); 427 encode_indent (enc);
391 encode_nl (enc); 436 encode_nl (enc);
392 } 437 }
393 } 438 }
394 439
395 --enc->indent; encode_indent (enc); encode_ch (enc, '}'); 440 --enc->indent; encode_indent (enc); encode_ch (enc, '}');
441}
442
443// encode objects, arrays and special \0=false and \1=true values.
444static void
445encode_rv (enc_t *enc, SV *sv)
446{
447 svtype svt;
448
449 SvGETMAGIC (sv);
450 svt = SvTYPE (sv);
451
452 if (svt == SVt_PVHV)
453 encode_hv (enc, (HV *)sv);
454 else if (svt == SVt_PVAV)
455 encode_av (enc, (AV *)sv);
456 else if (svt < SVt_PVAV)
457 {
458 if (SvNIOK (sv) && SvIV (sv) == 0)
459 encode_str (enc, "false", 5, 0);
460 else if (SvNIOK (sv) && SvIV (sv) == 1)
461 encode_str (enc, "true", 4, 0);
462 else
463 croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1",
464 SvPV_nolen (sv_2mortal (newRV_inc (sv))));
465 }
466 else
467 croak ("encountered %s, but JSON can only represent references to arrays or hashes",
468 SvPV_nolen (sv_2mortal (newRV_inc (sv))));
396} 469}
397 470
398static void 471static void
399encode_sv (enc_t *enc, SV *sv) 472encode_sv (enc_t *enc, SV *sv)
400{ 473{
421 SvIsUV(sv) 494 SvIsUV(sv)
422 ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv)) 495 ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv))
423 : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv)); 496 : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv));
424 } 497 }
425 else if (SvROK (sv)) 498 else if (SvROK (sv))
426 { 499 encode_rv (enc, SvRV (sv));
427 SV *rv = SvRV (sv);
428
429 if (enc->indent >= enc->max_depth)
430 croak ("data structure too deep (hit recursion limit)");
431
432 switch (SvTYPE (rv))
433 {
434 case SVt_PVAV: encode_av (enc, (AV *)rv); break;
435 case SVt_PVHV: encode_hv (enc, (HV *)rv); break;
436
437 default:
438 croak ("encountered %s, but JSON can only represent references to arrays or hashes",
439 SvPV_nolen (sv));
440 }
441 }
442 else if (!SvOK (sv)) 500 else if (!SvOK (sv))
443 encode_str (enc, "null", 4, 0); 501 encode_str (enc, "null", 4, 0);
444 else 502 else
445 croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", 503 croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this",
446 SvPV_nolen (sv), SvFLAGS (sv)); 504 SvPV_nolen (sv), SvFLAGS (sv));
447} 505}
448 506
449static SV * 507static SV *
450encode_json (SV *scalar, UV flags) 508encode_json (SV *scalar, U32 flags)
451{ 509{
510 enc_t enc;
511
452 if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) 512 if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar))
453 croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); 513 croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)");
454 514
455 enc_t enc;
456 enc.flags = flags; 515 enc.flags = flags;
457 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); 516 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE));
458 enc.cur = SvPVX (enc.sv); 517 enc.cur = SvPVX (enc.sv);
459 enc.end = SvEND (enc.sv); 518 enc.end = SvEND (enc.sv);
460 enc.indent = 0; 519 enc.indent = 0;
461 enc.max_depth = 0x7fffffffUL; 520 enc.maxdepth = DEC_DEPTH (flags);
462 521
463 SvPOK_only (enc.sv); 522 SvPOK_only (enc.sv);
464 encode_sv (&enc, scalar); 523 encode_sv (&enc, scalar);
465 524
525 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv));
526 *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings
527
466 if (!(flags & (F_ASCII | F_UTF8))) 528 if (!(flags & (F_ASCII | F_LATIN1 | F_UTF8)))
467 SvUTF8_on (enc.sv); 529 SvUTF8_on (enc.sv);
468
469 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv));
470 530
471 if (enc.flags & F_SHRINK) 531 if (enc.flags & F_SHRINK)
472 shrink (enc.sv); 532 shrink (enc.sv);
473 533
474 return enc.sv; 534 return enc.sv;
481typedef struct 541typedef struct
482{ 542{
483 char *cur; // current parser pointer 543 char *cur; // current parser pointer
484 char *end; // end of input string 544 char *end; // end of input string
485 const char *err; // parse error, if != 0 545 const char *err; // parse error, if != 0
486 UV flags; // F_* 546 U32 flags; // F_*
547 U32 depth; // recursion depth
548 U32 maxdepth; // recursion depth limit
487} dec_t; 549} dec_t;
488 550
489static void 551static void
490decode_ws (dec_t *dec) 552decode_ws (dec_t *dec)
491{ 553{
500 ++dec->cur; 562 ++dec->cur;
501 } 563 }
502} 564}
503 565
504#define ERR(reason) SB dec->err = reason; goto fail; SE 566#define ERR(reason) SB dec->err = reason; goto fail; SE
567
505#define EXPECT_CH(ch) SB \ 568#define EXPECT_CH(ch) SB \
506 if (*dec->cur != ch) \ 569 if (*dec->cur != ch) \
507 ERR (# ch " expected"); \ 570 ERR (# ch " expected"); \
508 ++dec->cur; \ 571 ++dec->cur; \
509 SE 572 SE
510 573
574#define DEC_INC_DEPTH if (++dec->depth > dec->maxdepth) ERR ("json datastructure exceeds maximum nesting level (set a higher max_depth)")
575#define DEC_DEC_DEPTH --dec->depth
576
511static SV *decode_sv (dec_t *dec); 577static SV *decode_sv (dec_t *dec);
512 578
513static signed char decode_hexdigit[256]; 579static signed char decode_hexdigit[256];
514 580
515static UV 581static UV
540 SV *sv = 0; 606 SV *sv = 0;
541 int utf8 = 0; 607 int utf8 = 0;
542 608
543 do 609 do
544 { 610 {
545 char buf [SHORT_STRING_LEN + UTF8_MAX_LEN]; 611 char buf [SHORT_STRING_LEN + UTF8_MAXBYTES];
546 char *cur = buf; 612 char *cur = buf;
547 613
548 do 614 do
549 { 615 {
550 unsigned char ch = *(unsigned char *)dec->cur++; 616 unsigned char ch = *(unsigned char *)dec->cur++;
616 } 682 }
617 else if (ch >= 0x20 && ch <= 0x7f) 683 else if (ch >= 0x20 && ch <= 0x7f)
618 *cur++ = ch; 684 *cur++ = ch;
619 else if (ch >= 0x80) 685 else if (ch >= 0x80)
620 { 686 {
687 STRLEN clen;
688 UV uch;
689
621 --dec->cur; 690 --dec->cur;
622 691
623 STRLEN clen;
624 UV uch = utf8n_to_uvuni (dec->cur, dec->end - dec->cur, &clen, UTF8_CHECK_ONLY); 692 uch = decode_utf8 (dec->cur, dec->end - dec->cur, &clen);
625 if (clen == (STRLEN)-1) 693 if (clen == (STRLEN)-1)
626 ERR ("malformed UTF-8 character in JSON string"); 694 ERR ("malformed UTF-8 character in JSON string");
627 695
628 do 696 do
629 {
630 *cur++ = *dec->cur++; 697 *cur++ = *dec->cur++;
631 }
632 while (--clen); 698 while (--clen);
633 699
634 utf8 = 1; 700 utf8 = 1;
635 } 701 }
636 else if (!ch)
637 ERR ("unexpected end of string while parsing json string");
638 else 702 else
703 {
704 --dec->cur;
705
706 if (!ch)
707 ERR ("unexpected end of string while parsing JSON string");
708 else
639 ERR ("invalid character encountered"); 709 ERR ("invalid character encountered while parsing JSON string");
640 710 }
641 } 711 }
642 while (cur < buf + SHORT_STRING_LEN); 712 while (cur < buf + SHORT_STRING_LEN);
643 713
714 {
644 STRLEN len = cur - buf; 715 STRLEN len = cur - buf;
645 716
646 if (sv) 717 if (sv)
647 { 718 {
648 SvGROW (sv, SvCUR (sv) + len + 1); 719 SvGROW (sv, SvCUR (sv) + len + 1);
649 memcpy (SvPVX (sv) + SvCUR (sv), buf, len); 720 memcpy (SvPVX (sv) + SvCUR (sv), buf, len);
650 SvCUR_set (sv, SvCUR (sv) + len); 721 SvCUR_set (sv, SvCUR (sv) + len);
651 } 722 }
652 else 723 else
653 sv = newSVpvn (buf, len); 724 sv = newSVpvn (buf, len);
725 }
654 } 726 }
655 while (*dec->cur != '"'); 727 while (*dec->cur != '"');
656 728
657 ++dec->cur; 729 ++dec->cur;
658 730
758static SV * 830static SV *
759decode_av (dec_t *dec) 831decode_av (dec_t *dec)
760{ 832{
761 AV *av = newAV (); 833 AV *av = newAV ();
762 834
835 DEC_INC_DEPTH;
763 decode_ws (dec); 836 decode_ws (dec);
837
764 if (*dec->cur == ']') 838 if (*dec->cur == ']')
765 ++dec->cur; 839 ++dec->cur;
766 else 840 else
767 for (;;) 841 for (;;)
768 { 842 {
786 ERR (", or ] expected while parsing array"); 860 ERR (", or ] expected while parsing array");
787 861
788 ++dec->cur; 862 ++dec->cur;
789 } 863 }
790 864
865 DEC_DEC_DEPTH;
791 return newRV_noinc ((SV *)av); 866 return newRV_noinc ((SV *)av);
792 867
793fail: 868fail:
794 SvREFCNT_dec (av); 869 SvREFCNT_dec (av);
870 DEC_DEC_DEPTH;
795 return 0; 871 return 0;
796} 872}
797 873
798static SV * 874static SV *
799decode_hv (dec_t *dec) 875decode_hv (dec_t *dec)
800{ 876{
801 HV *hv = newHV (); 877 HV *hv = newHV ();
802 878
879 DEC_INC_DEPTH;
803 decode_ws (dec); 880 decode_ws (dec);
881
804 if (*dec->cur == '}') 882 if (*dec->cur == '}')
805 ++dec->cur; 883 ++dec->cur;
806 else 884 else
807 for (;;) 885 for (;;)
808 { 886 {
821 { 899 {
822 SvREFCNT_dec (key); 900 SvREFCNT_dec (key);
823 goto fail; 901 goto fail;
824 } 902 }
825 903
826 //TODO: optimise
827 hv_store_ent (hv, key, value, 0); 904 hv_store_ent (hv, key, value, 0);
905 SvREFCNT_dec (key);
828 906
829 decode_ws (dec); 907 decode_ws (dec);
830 908
831 if (*dec->cur == '}') 909 if (*dec->cur == '}')
832 { 910 {
838 ERR (", or } expected while parsing object/hash"); 916 ERR (", or } expected while parsing object/hash");
839 917
840 ++dec->cur; 918 ++dec->cur;
841 } 919 }
842 920
921 DEC_DEC_DEPTH;
843 return newRV_noinc ((SV *)hv); 922 return newRV_noinc ((SV *)hv);
844 923
845fail: 924fail:
846 SvREFCNT_dec (hv); 925 SvREFCNT_dec (hv);
926 DEC_DEC_DEPTH;
847 return 0; 927 return 0;
848} 928}
849 929
850static SV * 930static SV *
851decode_sv (dec_t *dec) 931decode_sv (dec_t *dec)
894 ERR ("'null' expected"); 974 ERR ("'null' expected");
895 975
896 break; 976 break;
897 977
898 default: 978 default:
899 ERR ("malformed json string, neither array, object, number, string or atom"); 979 ERR ("malformed JSON string, neither array, object, number, string or atom");
900 break; 980 break;
901 } 981 }
902 982
903fail: 983fail:
904 return 0; 984 return 0;
905} 985}
906 986
907static SV * 987static SV *
908decode_json (SV *string, UV flags) 988decode_json (SV *string, U32 flags, UV *offset_return)
909{ 989{
990 dec_t dec;
991 UV offset;
910 SV *sv; 992 SV *sv;
993
994 SvGETMAGIC (string);
995 SvUPGRADE (string, SVt_PV);
911 996
912 if (flags & F_UTF8) 997 if (flags & F_UTF8)
913 sv_utf8_downgrade (string, 0); 998 sv_utf8_downgrade (string, 0);
914 else 999 else
915 sv_utf8_upgrade (string); 1000 sv_utf8_upgrade (string);
916 1001
917 SvGROW (string, SvCUR (string) + 1); // should basically be a NOP 1002 SvGROW (string, SvCUR (string) + 1); // should basically be a NOP
918 1003
919 dec_t dec;
920 dec.flags = flags; 1004 dec.flags = flags;
921 dec.cur = SvPVX (string); 1005 dec.cur = SvPVX (string);
922 dec.end = SvEND (string); 1006 dec.end = SvEND (string);
923 dec.err = 0; 1007 dec.err = 0;
1008 dec.depth = 0;
1009 dec.maxdepth = DEC_DEPTH (dec.flags);
924 1010
1011 *dec.end = 0; // this should basically be a nop, too, but make sure it's there
925 sv = decode_sv (&dec); 1012 sv = decode_sv (&dec);
926 1013
1014 if (!(offset_return || !sv))
1015 {
1016 // check for trailing garbage
1017 decode_ws (&dec);
1018
1019 if (*dec.cur)
1020 {
1021 dec.err = "garbage after JSON object";
1022 SvREFCNT_dec (sv);
1023 sv = 0;
1024 }
1025 }
1026
1027 if (offset_return || !sv)
1028 {
1029 offset = dec.flags & F_UTF8
1030 ? dec.cur - SvPVX (string)
1031 : utf8_distance (dec.cur, SvPVX (string));
1032
1033 if (offset_return)
1034 *offset_return = offset;
1035 }
1036
927 if (!sv) 1037 if (!sv)
928 { 1038 {
929 IV offset = dec.flags & F_UTF8
930 ? dec.cur - SvPVX (string)
931 : utf8_distance (dec.cur, SvPVX (string));
932 SV *uni = sv_newmortal (); 1039 SV *uni = sv_newmortal ();
933 1040
934 // horrible hack to silence warning inside pv_uni_display 1041 // horrible hack to silence warning inside pv_uni_display
935 COP cop = *PL_curcop; 1042 COP cop = *PL_curcop;
936 cop.cop_warnings = pWARN_NONE; 1043 cop.cop_warnings = pWARN_NONE;
938 SAVEVPTR (PL_curcop); 1045 SAVEVPTR (PL_curcop);
939 PL_curcop = &cop; 1046 PL_curcop = &cop;
940 pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); 1047 pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ);
941 LEAVE; 1048 LEAVE;
942 1049
943 croak ("%s, at character offset %d (%s)", 1050 croak ("%s, at character offset %d [\"%s\"]",
944 dec.err, 1051 dec.err,
945 (int)offset, 1052 (int)offset,
946 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); 1053 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)");
947 } 1054 }
948 1055
962BOOT: 1069BOOT:
963{ 1070{
964 int i; 1071 int i;
965 1072
966 memset (decode_hexdigit, 0xff, 256); 1073 memset (decode_hexdigit, 0xff, 256);
1074
967 for (i = 10; i--; ) 1075 for (i = 0; i < 256; ++i)
968 decode_hexdigit ['0' + i] = i; 1076 decode_hexdigit [i] =
969 1077 i >= '0' && i <= '9' ? i - '0'
970 for (i = 7; i--; ) 1078 : i >= 'a' && i <= 'f' ? i - 'a' + 10
971 { 1079 : i >= 'A' && i <= 'F' ? i - 'A' + 10
972 decode_hexdigit ['a' + i] = 10 + i; 1080 : -1;
973 decode_hexdigit ['A' + i] = 10 + i;
974 }
975 1081
976 json_stash = gv_stashpv ("JSON::XS", 1); 1082 json_stash = gv_stashpv ("JSON::XS", 1);
977} 1083}
978 1084
979PROTOTYPES: DISABLE 1085PROTOTYPES: DISABLE
985 RETVAL 1091 RETVAL
986 1092
987SV *ascii (SV *self, int enable = 1) 1093SV *ascii (SV *self, int enable = 1)
988 ALIAS: 1094 ALIAS:
989 ascii = F_ASCII 1095 ascii = F_ASCII
1096 latin1 = F_LATIN1
990 utf8 = F_UTF8 1097 utf8 = F_UTF8
991 indent = F_INDENT 1098 indent = F_INDENT
992 canonical = F_CANONICAL 1099 canonical = F_CANONICAL
993 space_before = F_SPACE_BEFORE 1100 space_before = F_SPACE_BEFORE
994 space_after = F_SPACE_AFTER 1101 space_after = F_SPACE_AFTER
1006 RETVAL = newSVsv (self); 1113 RETVAL = newSVsv (self);
1007} 1114}
1008 OUTPUT: 1115 OUTPUT:
1009 RETVAL 1116 RETVAL
1010 1117
1118SV *max_depth (SV *self, UV max_depth = 0x80000000UL)
1119 CODE:
1120{
1121 UV *uv = SvJSON (self);
1122 UV log2 = 0;
1123
1124 if (max_depth > 0x80000000UL) max_depth = 0x80000000UL;
1125
1126 while ((1UL << log2) < max_depth)
1127 ++log2;
1128
1129 *uv = *uv & ~F_MAXDEPTH | (log2 << S_MAXDEPTH);
1130
1131 RETVAL = newSVsv (self);
1132}
1133 OUTPUT:
1134 RETVAL
1135
1011void encode (SV *self, SV *scalar) 1136void encode (SV *self, SV *scalar)
1012 PPCODE: 1137 PPCODE:
1013 XPUSHs (encode_json (scalar, *SvJSON (self))); 1138 XPUSHs (encode_json (scalar, *SvJSON (self)));
1014 1139
1015void decode (SV *self, SV *jsonstr) 1140void decode (SV *self, SV *jsonstr)
1016 PPCODE: 1141 PPCODE:
1017 XPUSHs (decode_json (jsonstr, *SvJSON (self))); 1142 XPUSHs (decode_json (jsonstr, *SvJSON (self), 0));
1143
1144void decode_prefix (SV *self, SV *jsonstr)
1145 PPCODE:
1146{
1147 UV offset;
1148 EXTEND (SP, 2);
1149 PUSHs (decode_json (jsonstr, *SvJSON (self), &offset));
1150 PUSHs (sv_2mortal (newSVuv (offset)));
1151}
1018 1152
1019PROTOTYPES: ENABLE 1153PROTOTYPES: ENABLE
1020 1154
1021void to_json (SV *scalar) 1155void to_json (SV *scalar)
1156 ALIAS:
1157 objToJson = 0
1022 PPCODE: 1158 PPCODE:
1023 XPUSHs (encode_json (scalar, F_UTF8)); 1159 XPUSHs (encode_json (scalar, F_DEFAULT | F_UTF8));
1024 1160
1025void from_json (SV *jsonstr) 1161void from_json (SV *jsonstr)
1162 ALIAS:
1163 jsonToObj = 0
1026 PPCODE: 1164 PPCODE:
1027 XPUSHs (decode_json (jsonstr, F_UTF8)); 1165 XPUSHs (decode_json (jsonstr, F_DEFAULT | F_UTF8, 0));
1028 1166

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines