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.44 by root, Mon Jun 25 04:08:17 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
25 33
26#define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) 34#define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH))
27
28// F_SELFCONVERT? <=> to_json/toJson
29// F_BLESSED? <=> { $__class__$ => }
30 35
31#define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER 36#define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER
32#define F_DEFAULT (9UL << S_MAXDEPTH) 37#define F_DEFAULT (9UL << S_MAXDEPTH)
33 38
34#define INIT_SIZE 32 // initial scalar size to be allocated 39#define INIT_SIZE 32 // initial scalar size to be allocated
48#endif 53#endif
49 54
50#define expect_false(expr) expect ((expr) != 0, 0) 55#define expect_false(expr) expect ((expr) != 0, 0)
51#define expect_true(expr) expect ((expr) != 0, 1) 56#define expect_true(expr) expect ((expr) != 0, 1)
52 57
53static HV *json_stash; // JSON::XS:: 58static HV *json_stash, *json_boolean_stash; // JSON::XS::
59static SV *json_true, *json_false;
54 60
55///////////////////////////////////////////////////////////////////////////// 61/////////////////////////////////////////////////////////////////////////////
56// utility functions 62// utility functions
57 63
58static UV * 64static UV *
178 STRLEN clen; 184 STRLEN clen;
179 UV uch; 185 UV uch;
180 186
181 if (is_utf8) 187 if (is_utf8)
182 { 188 {
183 //uch = utf8n_to_uvuni (str, end - str, &clen, UTF8_CHECK_ONLY);
184 uch = decode_utf8 (str, end - str, &clen); 189 uch = decode_utf8 (str, end - str, &clen);
185 if (clen == (STRLEN)-1) 190 if (clen == (STRLEN)-1)
186 croak ("malformed or illegal unicode character in string [%.11s], cannot convert to JSON", str); 191 croak ("malformed or illegal unicode character in string [%.11s], cannot convert to JSON", str);
187 } 192 }
188 else 193 else
463 svtype svt; 468 svtype svt;
464 469
465 SvGETMAGIC (sv); 470 SvGETMAGIC (sv);
466 svt = SvTYPE (sv); 471 svt = SvTYPE (sv);
467 472
473 if (expect_false (SvOBJECT (sv)))
474 {
475 if (SvSTASH (sv) == json_boolean_stash)
476 {
477 if (SvIV (sv) == 0)
478 encode_str (enc, "false", 5, 0);
479 else
480 encode_str (enc, "true", 4, 0);
481 }
482 else
483 {
484#if 0
485 if (0 && sv_derived_from (rv, "JSON::Literal"))
486 {
487 // not yet
488 }
489#endif
490 if (enc->flags & F_CONV_BLESSED)
491 {
492 // we re-bless the reference to get overload and other niceties right
493 GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 1);
494
495 if (to_json)
496 {
497 dSP;
498 ENTER;
499 SAVETMPS;
500 PUSHMARK (SP);
501 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv)));
502
503 // calling with G_SCALAR ensures that we always get a 1 reutrn value
504 // check anyways.
505 PUTBACK;
506 assert (1 == call_sv ((SV *)GvCV (to_json), G_SCALAR));
507 SPAGAIN;
508
509 encode_sv (enc, POPs);
510
511 FREETMPS;
512 LEAVE;
513 }
514 else if (enc->flags & F_ALLOW_BLESSED)
515 encode_str (enc, "null", 4, 0);
516 else
517 croak ("encountered object '%s', but neither allow_blessed enabled nor TO_JSON method available on it",
518 SvPV_nolen (sv_2mortal (newRV_inc (sv))));
519 }
520 else if (enc->flags & F_ALLOW_BLESSED)
521 encode_str (enc, "null", 4, 0);
522 else
523 croak ("encountered object '%s', but neither allow_blessed nor convert_blessed settings are enabled",
524 SvPV_nolen (sv_2mortal (newRV_inc (sv))));
525 }
526 }
468 if (svt == SVt_PVHV) 527 else if (svt == SVt_PVHV)
469 encode_hv (enc, (HV *)sv); 528 encode_hv (enc, (HV *)sv);
470 else if (svt == SVt_PVAV) 529 else if (svt == SVt_PVAV)
471 encode_av (enc, (AV *)sv); 530 encode_av (enc, (AV *)sv);
472 else if (svt < SVt_PVAV) 531 else if (svt < SVt_PVAV)
473 { 532 {
631decode_4hex (dec_t *dec) 690decode_4hex (dec_t *dec)
632{ 691{
633 signed char d1, d2, d3, d4; 692 signed char d1, d2, d3, d4;
634 unsigned char *cur = (unsigned char *)dec->cur; 693 unsigned char *cur = (unsigned char *)dec->cur;
635 694
636 d1 = decode_hexdigit [cur [0]]; if (expect_false (d1 < 0)) ERR ("four hexadecimal digits expected"); 695 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"); 696 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"); 697 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"); 698 d4 = decode_hexdigit [cur [3]]; if (expect_false (d4 < 0)) ERR ("exactly four hexadecimal digits expected");
640 699
641 dec->cur += 4; 700 dec->cur += 4;
642 701
643 return ((UV)d1) << 12 702 return ((UV)d1) << 12
644 | ((UV)d2) << 8 703 | ((UV)d2) << 8
867 { 926 {
868 // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so 927 // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so
869 if (*start == '-') 928 if (*start == '-')
870 switch (dec->cur - start) 929 switch (dec->cur - start)
871 { 930 {
872 case 2: return newSViv (-( start [1] - '0' )); 931 case 2: return newSViv (-( start [1] - '0' * 1));
873 case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); 932 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)); 933 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)); 934 case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111));
876 } 935 }
877 else 936 else
878 switch (dec->cur - start) 937 switch (dec->cur - start)
879 { 938 {
880 case 1: return newSViv ( start [0] - '0' ); 939 case 1: return newSViv ( start [0] - '0' * 1);
881 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); 940 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); 941 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); 942 case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111);
884 } 943 }
885 944
892 if (uv < (UV)IV_MIN) 951 if (uv < (UV)IV_MIN)
893 return newSViv (-(IV)uv); 952 return newSViv (-(IV)uv);
894 } 953 }
895 else 954 else
896 return newSVuv (uv); 955 return newSVuv (uv);
956
957 // here would likely be the place for bigint support
897 } 958 }
898 } 959 }
899 960
961 // if we ever support bigint or bigfloat, this is the place for bigfloat
900 return newSVnv (Atof (start)); 962 return newSVnv (Atof (start));
901 963
902fail: 964fail:
903 return 0; 965 return 0;
904} 966}
1005 1067
1006static SV * 1068static SV *
1007decode_sv (dec_t *dec) 1069decode_sv (dec_t *dec)
1008{ 1070{
1009 decode_ws (dec); 1071 decode_ws (dec);
1072
1073 // the beauty of JSON: you need exactly one character lookahead
1074 // to parse anything.
1010 switch (*dec->cur) 1075 switch (*dec->cur)
1011 { 1076 {
1012 case '"': ++dec->cur; return decode_str (dec); 1077 case '"': ++dec->cur; return decode_str (dec);
1013 case '[': ++dec->cur; return decode_av (dec); 1078 case '[': ++dec->cur; return decode_av (dec);
1014 case '{': ++dec->cur; return decode_hv (dec); 1079 case '{': ++dec->cur; return decode_hv (dec);
1020 1085
1021 case 't': 1086 case 't':
1022 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) 1087 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4))
1023 { 1088 {
1024 dec->cur += 4; 1089 dec->cur += 4;
1025 return newSViv (1); 1090 return SvREFCNT_inc (json_true);
1026 } 1091 }
1027 else 1092 else
1028 ERR ("'true' expected"); 1093 ERR ("'true' expected");
1029 1094
1030 break; 1095 break;
1031 1096
1032 case 'f': 1097 case 'f':
1033 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) 1098 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5))
1034 { 1099 {
1035 dec->cur += 5; 1100 dec->cur += 5;
1036 return newSViv (0); 1101 return SvREFCNT_inc (json_false);
1037 } 1102 }
1038 else 1103 else
1039 ERR ("'false' expected"); 1104 ERR ("'false' expected");
1040 1105
1041 break; 1106 break;
1144 1209
1145BOOT: 1210BOOT:
1146{ 1211{
1147 int i; 1212 int i;
1148 1213
1149 memset (decode_hexdigit, 0xff, 256);
1150
1151 for (i = 0; i < 256; ++i) 1214 for (i = 0; i < 256; ++i)
1152 decode_hexdigit [i] = 1215 decode_hexdigit [i] =
1153 i >= '0' && i <= '9' ? i - '0' 1216 i >= '0' && i <= '9' ? i - '0'
1154 : i >= 'a' && i <= 'f' ? i - 'a' + 10 1217 : i >= 'a' && i <= 'f' ? i - 'a' + 10
1155 : i >= 'A' && i <= 'F' ? i - 'A' + 10 1218 : i >= 'A' && i <= 'F' ? i - 'A' + 10
1156 : -1; 1219 : -1;
1157 1220
1158 json_stash = gv_stashpv ("JSON::XS", 1); 1221 json_stash = gv_stashpv ("JSON::XS" , 1);
1222 json_boolean_stash = gv_stashpv ("JSON::XS::Boolean", 1);
1223
1224 json_true = get_sv ("JSON::XS::true" , 1); SvREADONLY_on (json_true );
1225 json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false);
1159} 1226}
1160 1227
1161PROTOTYPES: DISABLE 1228PROTOTYPES: DISABLE
1162 1229
1163SV *new (char *dummy) 1230SV *new (char *dummy)
1166 OUTPUT: 1233 OUTPUT:
1167 RETVAL 1234 RETVAL
1168 1235
1169SV *ascii (SV *self, int enable = 1) 1236SV *ascii (SV *self, int enable = 1)
1170 ALIAS: 1237 ALIAS:
1171 ascii = F_ASCII 1238 ascii = F_ASCII
1172 latin1 = F_LATIN1 1239 latin1 = F_LATIN1
1173 utf8 = F_UTF8 1240 utf8 = F_UTF8
1174 indent = F_INDENT 1241 indent = F_INDENT
1175 canonical = F_CANONICAL 1242 canonical = F_CANONICAL
1176 space_before = F_SPACE_BEFORE 1243 space_before = F_SPACE_BEFORE
1177 space_after = F_SPACE_AFTER 1244 space_after = F_SPACE_AFTER
1178 pretty = F_PRETTY 1245 pretty = F_PRETTY
1179 allow_nonref = F_ALLOW_NONREF 1246 allow_nonref = F_ALLOW_NONREF
1180 shrink = F_SHRINK 1247 shrink = F_SHRINK
1248 allow_blessed = F_ALLOW_BLESSED
1249 convert_blessed = F_CONV_BLESSED
1181 CODE: 1250 CODE:
1182{ 1251{
1183 UV *uv = SvJSON (self); 1252 UV *uv = SvJSON (self);
1184 if (enable) 1253 if (enable)
1185 *uv |= ix; 1254 *uv |= ix;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines