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.39 by root, Mon Jun 11 03:42:57 2007 UTC vs.
Revision 1.47 by root, Sun Jul 1 14:08:03 2007 UTC

9 9
10#if defined(__BORLANDC__) || defined(_MSC_VER) 10#if defined(__BORLANDC__) || defined(_MSC_VER)
11# define snprintf _snprintf // C compilers have this in stdio.h 11# define snprintf _snprintf // C compilers have this in stdio.h
12#endif 12#endif
13 13
14// some old perls do not have this, try to make it work, no
15// guarentees, though. if it breaks, you get to keep the pieces.
16#ifndef UTF8_MAXBYTES
17# define UTF8_MAXBYTES 13
18#endif
19
14#define F_ASCII 0x00000001UL 20#define F_ASCII 0x00000001UL
15#define F_LATIN1 0x00000002UL 21#define F_LATIN1 0x00000002UL
16#define F_UTF8 0x00000004UL 22#define F_UTF8 0x00000004UL
17#define F_INDENT 0x00000008UL 23#define F_INDENT 0x00000008UL
18#define F_CANONICAL 0x00000010UL 24#define F_CANONICAL 0x00000010UL
19#define F_SPACE_BEFORE 0x00000020UL 25#define F_SPACE_BEFORE 0x00000020UL
20#define F_SPACE_AFTER 0x00000040UL 26#define F_SPACE_AFTER 0x00000040UL
21#define F_ALLOW_NONREF 0x00000100UL 27#define F_ALLOW_NONREF 0x00000100UL
22#define F_SHRINK 0x00000200UL 28#define F_SHRINK 0x00000200UL
29#define F_ALLOW_BLESSED 0x00000400UL
30#define F_CONV_BLESSED 0x00000800UL // NYI
23#define F_MAXDEPTH 0xf8000000UL 31#define F_MAXDEPTH 0xf8000000UL
24#define S_MAXDEPTH 27 32#define S_MAXDEPTH 27
33#define F_MAXSIZE 0x01f00000UL
34#define S_MAXSIZE 20
25 35
26#define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) 36#define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH))
27 37#define DEC_SIZE(flags) (1UL << ((flags & F_MAXSIZE ) >> S_MAXSIZE ))
28// F_SELFCONVERT? <=> to_json/toJson
29// F_BLESSED? <=> { $__class__$ => }
30 38
31#define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER 39#define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER
32#define F_DEFAULT (9UL << S_MAXDEPTH) 40#define F_DEFAULT (9UL << S_MAXDEPTH)
33 41
34#define INIT_SIZE 32 // initial scalar size to be allocated 42#define INIT_SIZE 32 // initial scalar size to be allocated
48#endif 56#endif
49 57
50#define expect_false(expr) expect ((expr) != 0, 0) 58#define expect_false(expr) expect ((expr) != 0, 0)
51#define expect_true(expr) expect ((expr) != 0, 1) 59#define expect_true(expr) expect ((expr) != 0, 1)
52 60
53static HV *json_stash; // JSON::XS:: 61static HV *json_stash, *json_boolean_stash; // JSON::XS::
62static SV *json_true, *json_false;
63
64typedef struct json {
65 U32 flags;
66} JSON__XS;
54 67
55///////////////////////////////////////////////////////////////////////////// 68/////////////////////////////////////////////////////////////////////////////
56// utility functions 69// utility functions
57 70
58static UV * 71static UV *
107typedef struct 120typedef struct
108{ 121{
109 char *cur; // SvPVX (sv) + current output position 122 char *cur; // SvPVX (sv) + current output position
110 char *end; // SvEND (sv) 123 char *end; // SvEND (sv)
111 SV *sv; // result scalar 124 SV *sv; // result scalar
112 U32 flags; // F_* 125 struct json json;
113 U32 indent; // indentation level 126 U32 indent; // indentation level
114 U32 maxdepth; // max. indentation/recursion level 127 U32 maxdepth; // max. indentation/recursion level
115} enc_t; 128} enc_t;
116 129
117inline void 130inline void
178 STRLEN clen; 191 STRLEN clen;
179 UV uch; 192 UV uch;
180 193
181 if (is_utf8) 194 if (is_utf8)
182 { 195 {
183 //uch = utf8n_to_uvuni (str, end - str, &clen, UTF8_CHECK_ONLY);
184 uch = decode_utf8 (str, end - str, &clen); 196 uch = decode_utf8 (str, end - str, &clen);
185 if (clen == (STRLEN)-1) 197 if (clen == (STRLEN)-1)
186 croak ("malformed or illegal unicode character in string [%.11s], cannot convert to JSON", str); 198 croak ("malformed or illegal unicode character in string [%.11s], cannot convert to JSON", str);
187 } 199 }
188 else 200 else
192 } 204 }
193 205
194 if (uch > 0x10FFFFUL) 206 if (uch > 0x10FFFFUL)
195 croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); 207 croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch);
196 208
197 if (uch < 0x80 || enc->flags & F_ASCII || (enc->flags & F_LATIN1 && uch > 0xFF)) 209 if (uch < 0x80 || enc->json.flags & F_ASCII || (enc->json.flags & F_LATIN1 && uch > 0xFF))
198 { 210 {
199 if (uch > 0xFFFFUL) 211 if (uch > 0xFFFFUL)
200 { 212 {
201 need (enc, len += 11); 213 need (enc, len += 11);
202 sprintf (enc->cur, "\\u%04x\\u%04x", 214 sprintf (enc->cur, "\\u%04x\\u%04x",
216 *enc->cur++ = hexdigit [(uch >> 0) & 15]; 228 *enc->cur++ = hexdigit [(uch >> 0) & 15];
217 } 229 }
218 230
219 str += clen; 231 str += clen;
220 } 232 }
221 else if (enc->flags & F_LATIN1) 233 else if (enc->json.flags & F_LATIN1)
222 { 234 {
223 *enc->cur++ = uch; 235 *enc->cur++ = uch;
224 str += clen; 236 str += clen;
225 } 237 }
226 else if (is_utf8) 238 else if (is_utf8)
247} 259}
248 260
249inline void 261inline void
250encode_indent (enc_t *enc) 262encode_indent (enc_t *enc)
251{ 263{
252 if (enc->flags & F_INDENT) 264 if (enc->json.flags & F_INDENT)
253 { 265 {
254 int spaces = enc->indent * INDENT_STEP; 266 int spaces = enc->indent * INDENT_STEP;
255 267
256 need (enc, spaces); 268 need (enc, spaces);
257 memset (enc->cur, ' ', spaces); 269 memset (enc->cur, ' ', spaces);
267} 279}
268 280
269inline void 281inline void
270encode_nl (enc_t *enc) 282encode_nl (enc_t *enc)
271{ 283{
272 if (enc->flags & F_INDENT) 284 if (enc->json.flags & F_INDENT)
273 { 285 {
274 need (enc, 1); 286 need (enc, 1);
275 encode_ch (enc, '\n'); 287 encode_ch (enc, '\n');
276 } 288 }
277} 289}
279inline void 291inline void
280encode_comma (enc_t *enc) 292encode_comma (enc_t *enc)
281{ 293{
282 encode_ch (enc, ','); 294 encode_ch (enc, ',');
283 295
284 if (enc->flags & F_INDENT) 296 if (enc->json.flags & F_INDENT)
285 encode_nl (enc); 297 encode_nl (enc);
286 else if (enc->flags & F_SPACE_AFTER) 298 else if (enc->json.flags & F_SPACE_AFTER)
287 encode_space (enc); 299 encode_space (enc);
288} 300}
289 301
290static void encode_sv (enc_t *enc, SV *sv); 302static void encode_sv (enc_t *enc, SV *sv);
291 303
334 else 346 else
335 encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); 347 encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he));
336 348
337 encode_ch (enc, '"'); 349 encode_ch (enc, '"');
338 350
339 if (enc->flags & F_SPACE_BEFORE) encode_space (enc); 351 if (enc->json.flags & F_SPACE_BEFORE) encode_space (enc);
340 encode_ch (enc, ':'); 352 encode_ch (enc, ':');
341 if (enc->flags & F_SPACE_AFTER ) encode_space (enc); 353 if (enc->json.flags & F_SPACE_AFTER ) encode_space (enc);
342 encode_sv (enc, HeVAL (he)); 354 encode_sv (enc, HeVAL (he));
343} 355}
344 356
345// compare hash entries, used when all keys are bytestrings 357// compare hash entries, used when all keys are bytestrings
346static int 358static int
381 { 393 {
382 // for canonical output we have to sort by keys first 394 // for canonical output we have to sort by keys first
383 // actually, this is mostly due to the stupid so-called 395 // actually, this is mostly due to the stupid so-called
384 // security workaround added somewhere in 5.8.x. 396 // security workaround added somewhere in 5.8.x.
385 // that randomises hash orderings 397 // that randomises hash orderings
386 if (enc->flags & F_CANONICAL) 398 if (enc->json.flags & F_CANONICAL)
387 { 399 {
388 int fast = 1; 400 int fast = 1;
389 HE *he; 401 HE *he;
390#if defined(__BORLANDC__) || defined(_MSC_VER) 402#if defined(__BORLANDC__) || defined(_MSC_VER)
391 HE **hes = _alloca (count * sizeof (HE)); 403 HE **hes = _alloca (count * sizeof (HE));
463 svtype svt; 475 svtype svt;
464 476
465 SvGETMAGIC (sv); 477 SvGETMAGIC (sv);
466 svt = SvTYPE (sv); 478 svt = SvTYPE (sv);
467 479
480 if (expect_false (SvOBJECT (sv)))
481 {
482 if (SvSTASH (sv) == json_boolean_stash)
483 {
484 if (SvIV (sv) == 0)
485 encode_str (enc, "false", 5, 0);
486 else
487 encode_str (enc, "true", 4, 0);
488 }
489 else
490 {
491#if 0
492 if (0 && sv_derived_from (rv, "JSON::Literal"))
493 {
494 // not yet
495 }
496#endif
497 if (enc->json.flags & F_CONV_BLESSED)
498 {
499 // we re-bless the reference to get overload and other niceties right
500 GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 1);
501
502 if (to_json)
503 {
504 dSP;
505 ENTER;
506 SAVETMPS;
507 PUSHMARK (SP);
508 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv)));
509
510 // calling with G_SCALAR ensures that we always get a 1 reutrn value
511 // check anyways.
512 PUTBACK;
513 assert (1 == call_sv ((SV *)GvCV (to_json), G_SCALAR));
514 SPAGAIN;
515
516 encode_sv (enc, POPs);
517
518 FREETMPS;
519 LEAVE;
520 }
521 else if (enc->json.flags & F_ALLOW_BLESSED)
522 encode_str (enc, "null", 4, 0);
523 else
524 croak ("encountered object '%s', but neither allow_blessed enabled nor TO_JSON method available on it",
525 SvPV_nolen (sv_2mortal (newRV_inc (sv))));
526 }
527 else if (enc->json.flags & F_ALLOW_BLESSED)
528 encode_str (enc, "null", 4, 0);
529 else
530 croak ("encountered object '%s', but neither allow_blessed nor convert_blessed settings are enabled",
531 SvPV_nolen (sv_2mortal (newRV_inc (sv))));
532 }
533 }
468 if (svt == SVt_PVHV) 534 else if (svt == SVt_PVHV)
469 encode_hv (enc, (HV *)sv); 535 encode_hv (enc, (HV *)sv);
470 else if (svt == SVt_PVAV) 536 else if (svt == SVt_PVAV)
471 encode_av (enc, (AV *)sv); 537 encode_av (enc, (AV *)sv);
472 else if (svt < SVt_PVAV) 538 else if (svt < SVt_PVAV)
473 { 539 {
552 croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", 618 croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this",
553 SvPV_nolen (sv), SvFLAGS (sv)); 619 SvPV_nolen (sv), SvFLAGS (sv));
554} 620}
555 621
556static SV * 622static SV *
557encode_json (SV *scalar, U32 flags) 623encode_json (SV *scalar, struct json *json)
558{ 624{
559 enc_t enc; 625 enc_t enc;
560 626
561 if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) 627 if (!(json->flags & F_ALLOW_NONREF) && !SvROK (scalar))
562 croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); 628 croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)");
563 629
564 enc.flags = flags; 630 enc.json = *json;
565 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); 631 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE));
566 enc.cur = SvPVX (enc.sv); 632 enc.cur = SvPVX (enc.sv);
567 enc.end = SvEND (enc.sv); 633 enc.end = SvEND (enc.sv);
568 enc.indent = 0; 634 enc.indent = 0;
569 enc.maxdepth = DEC_DEPTH (flags); 635 enc.maxdepth = DEC_DEPTH (enc.json.flags);
570 636
571 SvPOK_only (enc.sv); 637 SvPOK_only (enc.sv);
572 encode_sv (&enc, scalar); 638 encode_sv (&enc, scalar);
573 639
574 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); 640 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv));
575 *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings 641 *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings
576 642
577 if (!(flags & (F_ASCII | F_LATIN1 | F_UTF8))) 643 if (!(enc.json.flags & (F_ASCII | F_LATIN1 | F_UTF8)))
578 SvUTF8_on (enc.sv); 644 SvUTF8_on (enc.sv);
579 645
580 if (enc.flags & F_SHRINK) 646 if (enc.json.flags & F_SHRINK)
581 shrink (enc.sv); 647 shrink (enc.sv);
582 648
583 return enc.sv; 649 return enc.sv;
584} 650}
585 651
590typedef struct 656typedef struct
591{ 657{
592 char *cur; // current parser pointer 658 char *cur; // current parser pointer
593 char *end; // end of input string 659 char *end; // end of input string
594 const char *err; // parse error, if != 0 660 const char *err; // parse error, if != 0
595 U32 flags; // F_* 661 struct json json;
596 U32 depth; // recursion depth 662 U32 depth; // recursion depth
597 U32 maxdepth; // recursion depth limit 663 U32 maxdepth; // recursion depth limit
598} dec_t; 664} dec_t;
599 665
600inline void 666inline void
631decode_4hex (dec_t *dec) 697decode_4hex (dec_t *dec)
632{ 698{
633 signed char d1, d2, d3, d4; 699 signed char d1, d2, d3, d4;
634 unsigned char *cur = (unsigned char *)dec->cur; 700 unsigned char *cur = (unsigned char *)dec->cur;
635 701
636 d1 = decode_hexdigit [cur [0]]; if (expect_false (d1 < 0)) ERR ("four hexadecimal digits expected"); 702 d1 = decode_hexdigit [cur [0]]; if (expect_false (d1 < 0)) ERR ("exactly four hexadecimal digits expected");
637 d2 = decode_hexdigit [cur [1]]; if (expect_false (d2 < 0)) ERR ("four hexadecimal digits expected"); 703 d2 = decode_hexdigit [cur [1]]; if (expect_false (d2 < 0)) ERR ("exactly four hexadecimal digits expected");
638 d3 = decode_hexdigit [cur [2]]; if (expect_false (d3 < 0)) ERR ("four hexadecimal digits expected"); 704 d3 = decode_hexdigit [cur [2]]; if (expect_false (d3 < 0)) ERR ("exactly four hexadecimal digits expected");
639 d4 = decode_hexdigit [cur [3]]; if (expect_false (d4 < 0)) ERR ("four hexadecimal digits expected"); 705 d4 = decode_hexdigit [cur [3]]; if (expect_false (d4 < 0)) ERR ("exactly four hexadecimal digits expected");
640 706
641 dec->cur += 4; 707 dec->cur += 4;
642 708
643 return ((UV)d1) << 12 709 return ((UV)d1) << 12
644 | ((UV)d2) << 8 710 | ((UV)d2) << 8
867 { 933 {
868 // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so 934 // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so
869 if (*start == '-') 935 if (*start == '-')
870 switch (dec->cur - start) 936 switch (dec->cur - start)
871 { 937 {
872 case 2: return newSViv (-( start [1] - '0' )); 938 case 2: return newSViv (-( start [1] - '0' * 1));
873 case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); 939 case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11));
874 case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); 940 case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111));
875 case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); 941 case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111));
876 } 942 }
877 else 943 else
878 switch (dec->cur - start) 944 switch (dec->cur - start)
879 { 945 {
880 case 1: return newSViv ( start [0] - '0' ); 946 case 1: return newSViv ( start [0] - '0' * 1);
881 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); 947 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11);
882 case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); 948 case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111);
883 case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); 949 case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111);
884 } 950 }
885 951
892 if (uv < (UV)IV_MIN) 958 if (uv < (UV)IV_MIN)
893 return newSViv (-(IV)uv); 959 return newSViv (-(IV)uv);
894 } 960 }
895 else 961 else
896 return newSVuv (uv); 962 return newSVuv (uv);
963
964 // here would likely be the place for bigint support
897 } 965 }
898 } 966 }
899 967
968 // if we ever support bigint or bigfloat, this is the place for bigfloat
900 return newSVnv (Atof (start)); 969 return newSVnv (Atof (start));
901 970
902fail: 971fail:
903 return 0; 972 return 0;
904} 973}
958 if (*dec->cur == '}') 1027 if (*dec->cur == '}')
959 ++dec->cur; 1028 ++dec->cur;
960 else 1029 else
961 for (;;) 1030 for (;;)
962 { 1031 {
963 SV *key, *value;
964
965 decode_ws (dec); EXPECT_CH ('"'); 1032 decode_ws (dec); EXPECT_CH ('"');
966 1033
967 key = decode_str (dec); 1034 // heuristic: assume that
968 if (!key) 1035 // a) decode_str + hv_store_ent are abysmally slow.
969 goto fail; 1036 // b) most hash keys are short, simple ascii text.
1037 // => try to "fast-match" such strings to avoid
1038 // the overhead of decode_str + hv_store_ent.
1039 {
1040 SV *value;
1041 char *p = dec->cur;
1042 char *e = p + 24; // only try up to 24 bytes
970 1043
971 decode_ws (dec); EXPECT_CH (':'); 1044 for (;;)
972
973 value = decode_sv (dec);
974 if (!value)
975 { 1045 {
1046 // the >= 0x80 is true on most architectures
1047 if (p == e || *p < 0x20 || *p >= 0x80 || *p == '\\')
1048 {
1049 // slow path, back up and use decode_str
1050 SV *key = decode_str (dec);
1051 if (!key)
1052 goto fail;
1053
1054 decode_ws (dec); EXPECT_CH (':');
1055
1056 value = decode_sv (dec);
1057 if (!value)
1058 {
1059 SvREFCNT_dec (key);
1060 goto fail;
1061 }
1062
1063 hv_store_ent (hv, key, value, 0);
976 SvREFCNT_dec (key); 1064 SvREFCNT_dec (key);
1065
1066 break;
1067 }
1068 else if (*p == '"')
1069 {
1070 // fast path, got a simple key
1071 char *key = dec->cur;
1072 int len = p - key;
1073 dec->cur = p + 1;
1074
1075 decode_ws (dec); EXPECT_CH (':');
1076
1077 value = decode_sv (dec);
1078 if (!value)
977 goto fail; 1079 goto fail;
1080
1081 hv_store (hv, key, len, value, 0);
1082
1083 break;
1084 }
1085
1086 ++p;
978 } 1087 }
979 1088 }
980 hv_store_ent (hv, key, value, 0);
981 SvREFCNT_dec (key);
982 1089
983 decode_ws (dec); 1090 decode_ws (dec);
984 1091
985 if (*dec->cur == '}') 1092 if (*dec->cur == '}')
986 { 1093 {
1005 1112
1006static SV * 1113static SV *
1007decode_sv (dec_t *dec) 1114decode_sv (dec_t *dec)
1008{ 1115{
1009 decode_ws (dec); 1116 decode_ws (dec);
1117
1118 // the beauty of JSON: you need exactly one character lookahead
1119 // to parse anything.
1010 switch (*dec->cur) 1120 switch (*dec->cur)
1011 { 1121 {
1012 case '"': ++dec->cur; return decode_str (dec); 1122 case '"': ++dec->cur; return decode_str (dec);
1013 case '[': ++dec->cur; return decode_av (dec); 1123 case '[': ++dec->cur; return decode_av (dec);
1014 case '{': ++dec->cur; return decode_hv (dec); 1124 case '{': ++dec->cur; return decode_hv (dec);
1020 1130
1021 case 't': 1131 case 't':
1022 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) 1132 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4))
1023 { 1133 {
1024 dec->cur += 4; 1134 dec->cur += 4;
1025 return newSViv (1); 1135 return SvREFCNT_inc (json_true);
1026 } 1136 }
1027 else 1137 else
1028 ERR ("'true' expected"); 1138 ERR ("'true' expected");
1029 1139
1030 break; 1140 break;
1031 1141
1032 case 'f': 1142 case 'f':
1033 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) 1143 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5))
1034 { 1144 {
1035 dec->cur += 5; 1145 dec->cur += 5;
1036 return newSViv (0); 1146 return SvREFCNT_inc (json_false);
1037 } 1147 }
1038 else 1148 else
1039 ERR ("'false' expected"); 1149 ERR ("'false' expected");
1040 1150
1041 break; 1151 break;
1059fail: 1169fail:
1060 return 0; 1170 return 0;
1061} 1171}
1062 1172
1063static SV * 1173static SV *
1064decode_json (SV *string, U32 flags, UV *offset_return) 1174decode_json (SV *string, struct json *json, UV *offset_return)
1065{ 1175{
1066 dec_t dec; 1176 dec_t dec;
1067 UV offset; 1177 UV offset;
1068 SV *sv; 1178 SV *sv;
1069 1179
1070 SvGETMAGIC (string); 1180 SvGETMAGIC (string);
1071 SvUPGRADE (string, SVt_PV); 1181 SvUPGRADE (string, SVt_PV);
1072 1182
1183 if (json->flags & F_MAXSIZE && SvCUR (string) > DEC_SIZE (json->flags))
1184 croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu",
1185 (unsigned long)SvCUR (string), (unsigned long)DEC_SIZE (json->flags));
1186
1073 if (flags & F_UTF8) 1187 if (json->flags & F_UTF8)
1074 sv_utf8_downgrade (string, 0); 1188 sv_utf8_downgrade (string, 0);
1075 else 1189 else
1076 sv_utf8_upgrade (string); 1190 sv_utf8_upgrade (string);
1077 1191
1078 SvGROW (string, SvCUR (string) + 1); // should basically be a NOP 1192 SvGROW (string, SvCUR (string) + 1); // should basically be a NOP
1079 1193
1080 dec.flags = flags; 1194 dec.json = *json;
1081 dec.cur = SvPVX (string); 1195 dec.cur = SvPVX (string);
1082 dec.end = SvEND (string); 1196 dec.end = SvEND (string);
1083 dec.err = 0; 1197 dec.err = 0;
1084 dec.depth = 0; 1198 dec.depth = 0;
1085 dec.maxdepth = DEC_DEPTH (dec.flags); 1199 dec.maxdepth = DEC_DEPTH (dec.json.flags);
1086 1200
1087 *dec.end = 0; // this should basically be a nop, too, but make sure it's there 1201 *dec.end = 0; // this should basically be a nop, too, but make sure it's there
1088 sv = decode_sv (&dec); 1202 sv = decode_sv (&dec);
1089 1203
1090 if (!(offset_return || !sv)) 1204 if (!(offset_return || !sv))
1100 } 1214 }
1101 } 1215 }
1102 1216
1103 if (offset_return || !sv) 1217 if (offset_return || !sv)
1104 { 1218 {
1105 offset = dec.flags & F_UTF8 1219 offset = dec.json.flags & F_UTF8
1106 ? dec.cur - SvPVX (string) 1220 ? dec.cur - SvPVX (string)
1107 : utf8_distance (dec.cur, SvPVX (string)); 1221 : utf8_distance (dec.cur, SvPVX (string));
1108 1222
1109 if (offset_return) 1223 if (offset_return)
1110 *offset_return = offset; 1224 *offset_return = offset;
1129 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); 1243 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)");
1130 } 1244 }
1131 1245
1132 sv = sv_2mortal (sv); 1246 sv = sv_2mortal (sv);
1133 1247
1134 if (!(dec.flags & F_ALLOW_NONREF) && !SvROK (sv)) 1248 if (!(dec.json.flags & F_ALLOW_NONREF) && !SvROK (sv))
1135 croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)"); 1249 croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)");
1136 1250
1137 return sv; 1251 return sv;
1138} 1252}
1139 1253
1143MODULE = JSON::XS PACKAGE = JSON::XS 1257MODULE = JSON::XS PACKAGE = JSON::XS
1144 1258
1145BOOT: 1259BOOT:
1146{ 1260{
1147 int i; 1261 int i;
1148
1149 memset (decode_hexdigit, 0xff, 256);
1150 1262
1151 for (i = 0; i < 256; ++i) 1263 for (i = 0; i < 256; ++i)
1152 decode_hexdigit [i] = 1264 decode_hexdigit [i] =
1153 i >= '0' && i <= '9' ? i - '0' 1265 i >= '0' && i <= '9' ? i - '0'
1154 : i >= 'a' && i <= 'f' ? i - 'a' + 10 1266 : i >= 'a' && i <= 'f' ? i - 'a' + 10
1155 : i >= 'A' && i <= 'F' ? i - 'A' + 10 1267 : i >= 'A' && i <= 'F' ? i - 'A' + 10
1156 : -1; 1268 : -1;
1157 1269
1158 json_stash = gv_stashpv ("JSON::XS", 1); 1270 json_stash = gv_stashpv ("JSON::XS" , 1);
1271 json_boolean_stash = gv_stashpv ("JSON::XS::Boolean", 1);
1272
1273 json_true = get_sv ("JSON::XS::true" , 1); SvREADONLY_on (json_true );
1274 json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false);
1159} 1275}
1160 1276
1161PROTOTYPES: DISABLE 1277PROTOTYPES: DISABLE
1162 1278
1163SV *new (char *dummy) 1279SV *new (char *dummy)
1166 OUTPUT: 1282 OUTPUT:
1167 RETVAL 1283 RETVAL
1168 1284
1169SV *ascii (SV *self, int enable = 1) 1285SV *ascii (SV *self, int enable = 1)
1170 ALIAS: 1286 ALIAS:
1171 ascii = F_ASCII 1287 ascii = F_ASCII
1172 latin1 = F_LATIN1 1288 latin1 = F_LATIN1
1173 utf8 = F_UTF8 1289 utf8 = F_UTF8
1174 indent = F_INDENT 1290 indent = F_INDENT
1175 canonical = F_CANONICAL 1291 canonical = F_CANONICAL
1176 space_before = F_SPACE_BEFORE 1292 space_before = F_SPACE_BEFORE
1177 space_after = F_SPACE_AFTER 1293 space_after = F_SPACE_AFTER
1178 pretty = F_PRETTY 1294 pretty = F_PRETTY
1179 allow_nonref = F_ALLOW_NONREF 1295 allow_nonref = F_ALLOW_NONREF
1180 shrink = F_SHRINK 1296 shrink = F_SHRINK
1297 allow_blessed = F_ALLOW_BLESSED
1298 convert_blessed = F_CONV_BLESSED
1181 CODE: 1299 CODE:
1182{ 1300{
1183 UV *uv = SvJSON (self); 1301 UV *uv = SvJSON (self);
1184 if (enable) 1302 if (enable)
1185 *uv |= ix; 1303 *uv |= ix;
1207 RETVAL = newSVsv (self); 1325 RETVAL = newSVsv (self);
1208} 1326}
1209 OUTPUT: 1327 OUTPUT:
1210 RETVAL 1328 RETVAL
1211 1329
1330SV *max_size (SV *self, UV max_size = 0)
1331 CODE:
1332{
1333 UV *uv = SvJSON (self);
1334 UV log2 = 0;
1335
1336 if (max_size > 0x80000000UL) max_size = 0x80000000UL;
1337 if (max_size == 1) max_size = 2;
1338
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
1212void encode (SV *self, SV *scalar) 1349void encode (SV *self, SV *scalar)
1213 PPCODE: 1350 PPCODE:
1351{
1352 struct json json = { *SvJSON (self) };
1214 XPUSHs (encode_json (scalar, *SvJSON (self))); 1353 XPUSHs (encode_json (scalar, &json));
1354}
1215 1355
1216void decode (SV *self, SV *jsonstr) 1356void decode (SV *self, SV *jsonstr)
1217 PPCODE: 1357 PPCODE:
1358{
1359 struct json json = { *SvJSON (self) };
1218 XPUSHs (decode_json (jsonstr, *SvJSON (self), 0)); 1360 XPUSHs (decode_json (jsonstr, &json, 0));
1361}
1219 1362
1220void decode_prefix (SV *self, SV *jsonstr) 1363void decode_prefix (SV *self, SV *jsonstr)
1221 PPCODE: 1364 PPCODE:
1222{ 1365{
1223 UV offset; 1366 UV offset;
1367 struct json json = { *SvJSON (self) };
1224 EXTEND (SP, 2); 1368 EXTEND (SP, 2);
1225 PUSHs (decode_json (jsonstr, *SvJSON (self), &offset)); 1369 PUSHs (decode_json (jsonstr, &json, &offset));
1226 PUSHs (sv_2mortal (newSVuv (offset))); 1370 PUSHs (sv_2mortal (newSVuv (offset)));
1227} 1371}
1228 1372
1229PROTOTYPES: ENABLE 1373PROTOTYPES: ENABLE
1230 1374
1231void to_json (SV *scalar) 1375void to_json (SV *scalar)
1232 ALIAS:
1233 objToJson = 0
1234 PPCODE: 1376 PPCODE:
1377{
1378 struct json json = { F_DEFAULT | F_UTF8 };
1235 XPUSHs (encode_json (scalar, F_DEFAULT | F_UTF8)); 1379 XPUSHs (encode_json (scalar, &json));
1380}
1236 1381
1237void from_json (SV *jsonstr) 1382void from_json (SV *jsonstr)
1238 ALIAS:
1239 jsonToObj = 0
1240 PPCODE: 1383 PPCODE:
1384{
1385 struct json json = { F_DEFAULT | F_UTF8 };
1241 XPUSHs (decode_json (jsonstr, F_DEFAULT | F_UTF8, 0)); 1386 XPUSHs (decode_json (jsonstr, &json, 0));
1387}
1242 1388

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines