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.28 by root, Wed Apr 11 12:23:02 2007 UTC vs.
Revision 1.31 by root, Wed May 9 16:33:53 2007 UTC

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#define F_ASCII 0x00000001UL 14#define F_ASCII 0x00000001UL
15#define F_LATIN1 0x00000002UL
15#define F_UTF8 0x00000002UL 16#define F_UTF8 0x00000004UL
16#define F_INDENT 0x00000004UL 17#define F_INDENT 0x00000008UL
17#define F_CANONICAL 0x00000008UL 18#define F_CANONICAL 0x00000010UL
18#define F_SPACE_BEFORE 0x00000010UL 19#define F_SPACE_BEFORE 0x00000020UL
19#define F_SPACE_AFTER 0x00000020UL 20#define F_SPACE_AFTER 0x00000040UL
20#define F_ALLOW_NONREF 0x00000080UL 21#define F_ALLOW_NONREF 0x00000100UL
21#define F_SHRINK 0x00000100UL 22#define F_SHRINK 0x00000200UL
22#define F_MAXDEPTH 0xf8000000UL 23#define F_MAXDEPTH 0xf8000000UL
23#define S_MAXDEPTH 27 24#define S_MAXDEPTH 27
24 25
25#define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) 26#define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH))
26 27
180 } 181 }
181 182
182 if (uch > 0x10FFFFUL) 183 if (uch > 0x10FFFFUL)
183 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);
184 185
185 if (uch < 0x80 || enc->flags & F_ASCII) 186 if (uch < 0x80 || enc->flags & F_ASCII || (enc->flags & F_LATIN1 && uch > 0xFF))
186 { 187 {
187 if (uch > 0xFFFFUL) 188 if (uch > 0xFFFFUL)
188 { 189 {
189 need (enc, len += 11); 190 need (enc, len += 11);
190 sprintf (enc->cur, "\\u%04x\\u%04x", 191 sprintf (enc->cur, "\\u%04x\\u%04x",
204 *enc->cur++ = hexdigit [(uch >> 0) & 15]; 205 *enc->cur++ = hexdigit [(uch >> 0) & 15];
205 } 206 }
206 207
207 str += clen; 208 str += clen;
208 } 209 }
210 else if (enc->flags & F_LATIN1)
211 {
212 *enc->cur++ = uch;
213 str += clen;
214 }
209 else if (is_utf8) 215 else if (is_utf8)
210 { 216 {
211 need (enc, len += clen); 217 need (enc, len += clen);
212 do 218 do
213 { 219 {
514 enc.maxdepth = DEC_DEPTH (flags); 520 enc.maxdepth = DEC_DEPTH (flags);
515 521
516 SvPOK_only (enc.sv); 522 SvPOK_only (enc.sv);
517 encode_sv (&enc, scalar); 523 encode_sv (&enc, scalar);
518 524
519 if (!(flags & (F_ASCII | F_UTF8)))
520 SvUTF8_on (enc.sv);
521
522 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); 525 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv));
523 *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings 526 *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings
527
528 if (!(flags & (F_ASCII | F_LATIN1 | F_UTF8)))
529 SvUTF8_on (enc.sv);
524 530
525 if (enc.flags & F_SHRINK) 531 if (enc.flags & F_SHRINK)
526 shrink (enc.sv); 532 shrink (enc.sv);
527 533
528 return enc.sv; 534 return enc.sv;
977fail: 983fail:
978 return 0; 984 return 0;
979} 985}
980 986
981static SV * 987static SV *
982decode_json (SV *string, U32 flags) 988decode_json (SV *string, U32 flags, UV *offset_return)
983{ 989{
984 dec_t dec; 990 dec_t dec;
991 UV offset;
985 SV *sv; 992 SV *sv;
986 993
994 SvGETMAGIC (string);
987 SvUPGRADE (string, SVt_PV); 995 SvUPGRADE (string, SVt_PV);
988 996
989 if (flags & F_UTF8) 997 if (flags & F_UTF8)
990 sv_utf8_downgrade (string, 0); 998 sv_utf8_downgrade (string, 0);
991 else 999 else
998 dec.end = SvEND (string); 1006 dec.end = SvEND (string);
999 dec.err = 0; 1007 dec.err = 0;
1000 dec.depth = 0; 1008 dec.depth = 0;
1001 dec.maxdepth = DEC_DEPTH (dec.flags); 1009 dec.maxdepth = DEC_DEPTH (dec.flags);
1002 1010
1003 *dec.end = 0; // this should basically be a nop, too, but make sure its there 1011 *dec.end = 0; // this should basically be a nop, too, but make sure it's there
1004 sv = decode_sv (&dec); 1012 sv = decode_sv (&dec);
1005 1013
1014 if (offset_return || !sv)
1015 {
1016 offset = dec.flags & F_UTF8
1017 ? dec.cur - SvPVX (string)
1018 : utf8_distance (dec.cur, SvPVX (string));
1019
1020 if (offset_return)
1021 *offset_return = offset;
1022 }
1023 else
1024 {
1025 // check for trailing garbage
1026 decode_ws (&dec);
1027
1028 if (*dec.cur)
1029 {
1030 dec.err = "garbage after JSON object";
1031 SvREFCNT_dec (sv);
1032 sv = 0;
1033 }
1034 }
1035
1006 if (!sv) 1036 if (!sv)
1007 { 1037 {
1008 IV offset = dec.flags & F_UTF8
1009 ? dec.cur - SvPVX (string)
1010 : utf8_distance (dec.cur, SvPVX (string));
1011 SV *uni = sv_newmortal (); 1038 SV *uni = sv_newmortal ();
1012 1039
1013 // horrible hack to silence warning inside pv_uni_display 1040 // horrible hack to silence warning inside pv_uni_display
1014 COP cop = *PL_curcop; 1041 COP cop = *PL_curcop;
1015 cop.cop_warnings = pWARN_NONE; 1042 cop.cop_warnings = pWARN_NONE;
1063 RETVAL 1090 RETVAL
1064 1091
1065SV *ascii (SV *self, int enable = 1) 1092SV *ascii (SV *self, int enable = 1)
1066 ALIAS: 1093 ALIAS:
1067 ascii = F_ASCII 1094 ascii = F_ASCII
1095 latin1 = F_LATIN1
1068 utf8 = F_UTF8 1096 utf8 = F_UTF8
1069 indent = F_INDENT 1097 indent = F_INDENT
1070 canonical = F_CANONICAL 1098 canonical = F_CANONICAL
1071 space_before = F_SPACE_BEFORE 1099 space_before = F_SPACE_BEFORE
1072 space_after = F_SPACE_AFTER 1100 space_after = F_SPACE_AFTER
1108 PPCODE: 1136 PPCODE:
1109 XPUSHs (encode_json (scalar, *SvJSON (self))); 1137 XPUSHs (encode_json (scalar, *SvJSON (self)));
1110 1138
1111void decode (SV *self, SV *jsonstr) 1139void decode (SV *self, SV *jsonstr)
1112 PPCODE: 1140 PPCODE:
1113 XPUSHs (decode_json (jsonstr, *SvJSON (self))); 1141 XPUSHs (decode_json (jsonstr, *SvJSON (self), 0));
1142
1143void decode_prefix (SV *self, SV *jsonstr)
1144 PPCODE:
1145{
1146 UV offset;
1147 EXTEND (SP, 2);
1148 PUSHs (decode_json (jsonstr, *SvJSON (self), &offset));
1149 PUSHs (sv_2mortal (newSVuv (offset)));
1150}
1114 1151
1115PROTOTYPES: ENABLE 1152PROTOTYPES: ENABLE
1116 1153
1117void to_json (SV *scalar) 1154void to_json (SV *scalar)
1118 ALIAS: 1155 ALIAS:
1122 1159
1123void from_json (SV *jsonstr) 1160void from_json (SV *jsonstr)
1124 ALIAS: 1161 ALIAS:
1125 jsonToObj = 0 1162 jsonToObj = 0
1126 PPCODE: 1163 PPCODE:
1127 XPUSHs (decode_json (jsonstr, F_DEFAULT | F_UTF8)); 1164 XPUSHs (decode_json (jsonstr, F_DEFAULT | F_UTF8, 0));
1128 1165

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines