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.45 by root, Mon Jun 25 06:57:42 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;
54 63
55///////////////////////////////////////////////////////////////////////////// 64/////////////////////////////////////////////////////////////////////////////
56// utility functions 65// utility functions
57 66
58static UV * 67static UV *
178 STRLEN clen; 187 STRLEN clen;
179 UV uch; 188 UV uch;
180 189
181 if (is_utf8) 190 if (is_utf8)
182 { 191 {
183 //uch = utf8n_to_uvuni (str, end - str, &clen, UTF8_CHECK_ONLY);
184 uch = decode_utf8 (str, end - str, &clen); 192 uch = decode_utf8 (str, end - str, &clen);
185 if (clen == (STRLEN)-1) 193 if (clen == (STRLEN)-1)
186 croak ("malformed or illegal unicode character in string [%.11s], cannot convert to JSON", str); 194 croak ("malformed or illegal unicode character in string [%.11s], cannot convert to JSON", str);
187 } 195 }
188 else 196 else
463 svtype svt; 471 svtype svt;
464 472
465 SvGETMAGIC (sv); 473 SvGETMAGIC (sv);
466 svt = SvTYPE (sv); 474 svt = SvTYPE (sv);
467 475
476 if (expect_false (SvOBJECT (sv)))
477 {
478 if (SvSTASH (sv) == json_boolean_stash)
479 {
480 if (SvIV (sv) == 0)
481 encode_str (enc, "false", 5, 0);
482 else
483 encode_str (enc, "true", 4, 0);
484 }
485 else
486 {
487#if 0
488 if (0 && sv_derived_from (rv, "JSON::Literal"))
489 {
490 // not yet
491 }
492#endif
493 if (enc->flags & F_CONV_BLESSED)
494 {
495 // we re-bless the reference to get overload and other niceties right
496 GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 1);
497
498 if (to_json)
499 {
500 dSP;
501 ENTER;
502 SAVETMPS;
503 PUSHMARK (SP);
504 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv)));
505
506 // calling with G_SCALAR ensures that we always get a 1 reutrn value
507 // check anyways.
508 PUTBACK;
509 assert (1 == call_sv ((SV *)GvCV (to_json), G_SCALAR));
510 SPAGAIN;
511
512 encode_sv (enc, POPs);
513
514 FREETMPS;
515 LEAVE;
516 }
517 else if (enc->flags & F_ALLOW_BLESSED)
518 encode_str (enc, "null", 4, 0);
519 else
520 croak ("encountered object '%s', but neither allow_blessed enabled nor TO_JSON method available on it",
521 SvPV_nolen (sv_2mortal (newRV_inc (sv))));
522 }
523 else if (enc->flags & F_ALLOW_BLESSED)
524 encode_str (enc, "null", 4, 0);
525 else
526 croak ("encountered object '%s', but neither allow_blessed nor convert_blessed settings are enabled",
527 SvPV_nolen (sv_2mortal (newRV_inc (sv))));
528 }
529 }
468 if (svt == SVt_PVHV) 530 else if (svt == SVt_PVHV)
469 encode_hv (enc, (HV *)sv); 531 encode_hv (enc, (HV *)sv);
470 else if (svt == SVt_PVAV) 532 else if (svt == SVt_PVAV)
471 encode_av (enc, (AV *)sv); 533 encode_av (enc, (AV *)sv);
472 else if (svt < SVt_PVAV) 534 else if (svt < SVt_PVAV)
473 { 535 {
631decode_4hex (dec_t *dec) 693decode_4hex (dec_t *dec)
632{ 694{
633 signed char d1, d2, d3, d4; 695 signed char d1, d2, d3, d4;
634 unsigned char *cur = (unsigned char *)dec->cur; 696 unsigned char *cur = (unsigned char *)dec->cur;
635 697
636 d1 = decode_hexdigit [cur [0]]; if (expect_false (d1 < 0)) ERR ("four hexadecimal digits expected"); 698 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"); 699 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"); 700 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"); 701 d4 = decode_hexdigit [cur [3]]; if (expect_false (d4 < 0)) ERR ("exactly four hexadecimal digits expected");
640 702
641 dec->cur += 4; 703 dec->cur += 4;
642 704
643 return ((UV)d1) << 12 705 return ((UV)d1) << 12
644 | ((UV)d2) << 8 706 | ((UV)d2) << 8
867 { 929 {
868 // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so 930 // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so
869 if (*start == '-') 931 if (*start == '-')
870 switch (dec->cur - start) 932 switch (dec->cur - start)
871 { 933 {
872 case 2: return newSViv (-( start [1] - '0' )); 934 case 2: return newSViv (-( start [1] - '0' * 1));
873 case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); 935 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)); 936 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)); 937 case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111));
876 } 938 }
877 else 939 else
878 switch (dec->cur - start) 940 switch (dec->cur - start)
879 { 941 {
880 case 1: return newSViv ( start [0] - '0' ); 942 case 1: return newSViv ( start [0] - '0' * 1);
881 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); 943 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); 944 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); 945 case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111);
884 } 946 }
885 947
892 if (uv < (UV)IV_MIN) 954 if (uv < (UV)IV_MIN)
893 return newSViv (-(IV)uv); 955 return newSViv (-(IV)uv);
894 } 956 }
895 else 957 else
896 return newSVuv (uv); 958 return newSVuv (uv);
959
960 // here would likely be the place for bigint support
897 } 961 }
898 } 962 }
899 963
964 // if we ever support bigint or bigfloat, this is the place for bigfloat
900 return newSVnv (Atof (start)); 965 return newSVnv (Atof (start));
901 966
902fail: 967fail:
903 return 0; 968 return 0;
904} 969}
1005 1070
1006static SV * 1071static SV *
1007decode_sv (dec_t *dec) 1072decode_sv (dec_t *dec)
1008{ 1073{
1009 decode_ws (dec); 1074 decode_ws (dec);
1075
1076 // the beauty of JSON: you need exactly one character lookahead
1077 // to parse anything.
1010 switch (*dec->cur) 1078 switch (*dec->cur)
1011 { 1079 {
1012 case '"': ++dec->cur; return decode_str (dec); 1080 case '"': ++dec->cur; return decode_str (dec);
1013 case '[': ++dec->cur; return decode_av (dec); 1081 case '[': ++dec->cur; return decode_av (dec);
1014 case '{': ++dec->cur; return decode_hv (dec); 1082 case '{': ++dec->cur; return decode_hv (dec);
1020 1088
1021 case 't': 1089 case 't':
1022 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) 1090 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4))
1023 { 1091 {
1024 dec->cur += 4; 1092 dec->cur += 4;
1025 return newSViv (1); 1093 return SvREFCNT_inc (json_true);
1026 } 1094 }
1027 else 1095 else
1028 ERR ("'true' expected"); 1096 ERR ("'true' expected");
1029 1097
1030 break; 1098 break;
1031 1099
1032 case 'f': 1100 case 'f':
1033 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) 1101 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5))
1034 { 1102 {
1035 dec->cur += 5; 1103 dec->cur += 5;
1036 return newSViv (0); 1104 return SvREFCNT_inc (json_false);
1037 } 1105 }
1038 else 1106 else
1039 ERR ("'false' expected"); 1107 ERR ("'false' expected");
1040 1108
1041 break; 1109 break;
1068 SV *sv; 1136 SV *sv;
1069 1137
1070 SvGETMAGIC (string); 1138 SvGETMAGIC (string);
1071 SvUPGRADE (string, SVt_PV); 1139 SvUPGRADE (string, SVt_PV);
1072 1140
1141 if (flags & F_MAXSIZE && SvCUR (string) > DEC_SIZE (flags))
1142 croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu",
1143 (unsigned long)SvCUR (string), (unsigned long)DEC_SIZE (flags));
1144
1073 if (flags & F_UTF8) 1145 if (flags & F_UTF8)
1074 sv_utf8_downgrade (string, 0); 1146 sv_utf8_downgrade (string, 0);
1075 else 1147 else
1076 sv_utf8_upgrade (string); 1148 sv_utf8_upgrade (string);
1077 1149
1144 1216
1145BOOT: 1217BOOT:
1146{ 1218{
1147 int i; 1219 int i;
1148 1220
1149 memset (decode_hexdigit, 0xff, 256);
1150
1151 for (i = 0; i < 256; ++i) 1221 for (i = 0; i < 256; ++i)
1152 decode_hexdigit [i] = 1222 decode_hexdigit [i] =
1153 i >= '0' && i <= '9' ? i - '0' 1223 i >= '0' && i <= '9' ? i - '0'
1154 : i >= 'a' && i <= 'f' ? i - 'a' + 10 1224 : i >= 'a' && i <= 'f' ? i - 'a' + 10
1155 : i >= 'A' && i <= 'F' ? i - 'A' + 10 1225 : i >= 'A' && i <= 'F' ? i - 'A' + 10
1156 : -1; 1226 : -1;
1157 1227
1158 json_stash = gv_stashpv ("JSON::XS", 1); 1228 json_stash = gv_stashpv ("JSON::XS" , 1);
1229 json_boolean_stash = gv_stashpv ("JSON::XS::Boolean", 1);
1230
1231 json_true = get_sv ("JSON::XS::true" , 1); SvREADONLY_on (json_true );
1232 json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false);
1159} 1233}
1160 1234
1161PROTOTYPES: DISABLE 1235PROTOTYPES: DISABLE
1162 1236
1163SV *new (char *dummy) 1237SV *new (char *dummy)
1166 OUTPUT: 1240 OUTPUT:
1167 RETVAL 1241 RETVAL
1168 1242
1169SV *ascii (SV *self, int enable = 1) 1243SV *ascii (SV *self, int enable = 1)
1170 ALIAS: 1244 ALIAS:
1171 ascii = F_ASCII 1245 ascii = F_ASCII
1172 latin1 = F_LATIN1 1246 latin1 = F_LATIN1
1173 utf8 = F_UTF8 1247 utf8 = F_UTF8
1174 indent = F_INDENT 1248 indent = F_INDENT
1175 canonical = F_CANONICAL 1249 canonical = F_CANONICAL
1176 space_before = F_SPACE_BEFORE 1250 space_before = F_SPACE_BEFORE
1177 space_after = F_SPACE_AFTER 1251 space_after = F_SPACE_AFTER
1178 pretty = F_PRETTY 1252 pretty = F_PRETTY
1179 allow_nonref = F_ALLOW_NONREF 1253 allow_nonref = F_ALLOW_NONREF
1180 shrink = F_SHRINK 1254 shrink = F_SHRINK
1255 allow_blessed = F_ALLOW_BLESSED
1256 convert_blessed = F_CONV_BLESSED
1181 CODE: 1257 CODE:
1182{ 1258{
1183 UV *uv = SvJSON (self); 1259 UV *uv = SvJSON (self);
1184 if (enable) 1260 if (enable)
1185 *uv |= ix; 1261 *uv |= ix;
1207 RETVAL = newSVsv (self); 1283 RETVAL = newSVsv (self);
1208} 1284}
1209 OUTPUT: 1285 OUTPUT:
1210 RETVAL 1286 RETVAL
1211 1287
1288SV *max_size (SV *self, UV max_size = 0)
1289 CODE:
1290{
1291 UV *uv = SvJSON (self);
1292 UV log2 = 0;
1293
1294 if (max_size > 0x80000000UL) max_size = 0x80000000UL;
1295 if (max_size == 1) max_size = 2;
1296
1297 while ((1UL << log2) < max_size)
1298 ++log2;
1299
1300 *uv = *uv & ~F_MAXSIZE | (log2 << S_MAXSIZE);
1301
1302 RETVAL = newSVsv (self);
1303}
1304 OUTPUT:
1305 RETVAL
1306
1212void encode (SV *self, SV *scalar) 1307void encode (SV *self, SV *scalar)
1213 PPCODE: 1308 PPCODE:
1214 XPUSHs (encode_json (scalar, *SvJSON (self))); 1309 XPUSHs (encode_json (scalar, *SvJSON (self)));
1215 1310
1216void decode (SV *self, SV *jsonstr) 1311void decode (SV *self, SV *jsonstr)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines