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

59#define expect_true(expr) expect ((expr) != 0, 1) 59#define expect_true(expr) expect ((expr) != 0, 1)
60 60
61static HV *json_stash, *json_boolean_stash; // JSON::XS:: 61static HV *json_stash, *json_boolean_stash; // JSON::XS::
62static SV *json_true, *json_false; 62static SV *json_true, *json_false;
63 63
64typedef struct json {
65 U32 flags;
66} JSON__XS;
67
64///////////////////////////////////////////////////////////////////////////// 68/////////////////////////////////////////////////////////////////////////////
65// utility functions 69// utility functions
66 70
67static UV * 71static UV *
68SvJSON (SV *sv) 72SvJSON (SV *sv)
116typedef struct 120typedef struct
117{ 121{
118 char *cur; // SvPVX (sv) + current output position 122 char *cur; // SvPVX (sv) + current output position
119 char *end; // SvEND (sv) 123 char *end; // SvEND (sv)
120 SV *sv; // result scalar 124 SV *sv; // result scalar
121 U32 flags; // F_* 125 struct json json;
122 U32 indent; // indentation level 126 U32 indent; // indentation level
123 U32 maxdepth; // max. indentation/recursion level 127 U32 maxdepth; // max. indentation/recursion level
124} enc_t; 128} enc_t;
125 129
126inline void 130inline void
200 } 204 }
201 205
202 if (uch > 0x10FFFFUL) 206 if (uch > 0x10FFFFUL)
203 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);
204 208
205 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))
206 { 210 {
207 if (uch > 0xFFFFUL) 211 if (uch > 0xFFFFUL)
208 { 212 {
209 need (enc, len += 11); 213 need (enc, len += 11);
210 sprintf (enc->cur, "\\u%04x\\u%04x", 214 sprintf (enc->cur, "\\u%04x\\u%04x",
224 *enc->cur++ = hexdigit [(uch >> 0) & 15]; 228 *enc->cur++ = hexdigit [(uch >> 0) & 15];
225 } 229 }
226 230
227 str += clen; 231 str += clen;
228 } 232 }
229 else if (enc->flags & F_LATIN1) 233 else if (enc->json.flags & F_LATIN1)
230 { 234 {
231 *enc->cur++ = uch; 235 *enc->cur++ = uch;
232 str += clen; 236 str += clen;
233 } 237 }
234 else if (is_utf8) 238 else if (is_utf8)
255} 259}
256 260
257inline void 261inline void
258encode_indent (enc_t *enc) 262encode_indent (enc_t *enc)
259{ 263{
260 if (enc->flags & F_INDENT) 264 if (enc->json.flags & F_INDENT)
261 { 265 {
262 int spaces = enc->indent * INDENT_STEP; 266 int spaces = enc->indent * INDENT_STEP;
263 267
264 need (enc, spaces); 268 need (enc, spaces);
265 memset (enc->cur, ' ', spaces); 269 memset (enc->cur, ' ', spaces);
275} 279}
276 280
277inline void 281inline void
278encode_nl (enc_t *enc) 282encode_nl (enc_t *enc)
279{ 283{
280 if (enc->flags & F_INDENT) 284 if (enc->json.flags & F_INDENT)
281 { 285 {
282 need (enc, 1); 286 need (enc, 1);
283 encode_ch (enc, '\n'); 287 encode_ch (enc, '\n');
284 } 288 }
285} 289}
287inline void 291inline void
288encode_comma (enc_t *enc) 292encode_comma (enc_t *enc)
289{ 293{
290 encode_ch (enc, ','); 294 encode_ch (enc, ',');
291 295
292 if (enc->flags & F_INDENT) 296 if (enc->json.flags & F_INDENT)
293 encode_nl (enc); 297 encode_nl (enc);
294 else if (enc->flags & F_SPACE_AFTER) 298 else if (enc->json.flags & F_SPACE_AFTER)
295 encode_space (enc); 299 encode_space (enc);
296} 300}
297 301
298static void encode_sv (enc_t *enc, SV *sv); 302static void encode_sv (enc_t *enc, SV *sv);
299 303
342 else 346 else
343 encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); 347 encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he));
344 348
345 encode_ch (enc, '"'); 349 encode_ch (enc, '"');
346 350
347 if (enc->flags & F_SPACE_BEFORE) encode_space (enc); 351 if (enc->json.flags & F_SPACE_BEFORE) encode_space (enc);
348 encode_ch (enc, ':'); 352 encode_ch (enc, ':');
349 if (enc->flags & F_SPACE_AFTER ) encode_space (enc); 353 if (enc->json.flags & F_SPACE_AFTER ) encode_space (enc);
350 encode_sv (enc, HeVAL (he)); 354 encode_sv (enc, HeVAL (he));
351} 355}
352 356
353// compare hash entries, used when all keys are bytestrings 357// compare hash entries, used when all keys are bytestrings
354static int 358static int
389 { 393 {
390 // for canonical output we have to sort by keys first 394 // for canonical output we have to sort by keys first
391 // actually, this is mostly due to the stupid so-called 395 // actually, this is mostly due to the stupid so-called
392 // security workaround added somewhere in 5.8.x. 396 // security workaround added somewhere in 5.8.x.
393 // that randomises hash orderings 397 // that randomises hash orderings
394 if (enc->flags & F_CANONICAL) 398 if (enc->json.flags & F_CANONICAL)
395 { 399 {
396 int fast = 1; 400 int fast = 1;
397 HE *he; 401 HE *he;
398#if defined(__BORLANDC__) || defined(_MSC_VER) 402#if defined(__BORLANDC__) || defined(_MSC_VER)
399 HE **hes = _alloca (count * sizeof (HE)); 403 HE **hes = _alloca (count * sizeof (HE));
488 if (0 && sv_derived_from (rv, "JSON::Literal")) 492 if (0 && sv_derived_from (rv, "JSON::Literal"))
489 { 493 {
490 // not yet 494 // not yet
491 } 495 }
492#endif 496#endif
493 if (enc->flags & F_CONV_BLESSED) 497 if (enc->json.flags & F_CONV_BLESSED)
494 { 498 {
495 // we re-bless the reference to get overload and other niceties right 499 // we re-bless the reference to get overload and other niceties right
496 GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 1); 500 GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 1);
497 501
498 if (to_json) 502 if (to_json)
512 encode_sv (enc, POPs); 516 encode_sv (enc, POPs);
513 517
514 FREETMPS; 518 FREETMPS;
515 LEAVE; 519 LEAVE;
516 } 520 }
517 else if (enc->flags & F_ALLOW_BLESSED) 521 else if (enc->json.flags & F_ALLOW_BLESSED)
518 encode_str (enc, "null", 4, 0); 522 encode_str (enc, "null", 4, 0);
519 else 523 else
520 croak ("encountered object '%s', but neither allow_blessed enabled nor TO_JSON method available on it", 524 croak ("encountered object '%s', but neither allow_blessed enabled nor TO_JSON method available on it",
521 SvPV_nolen (sv_2mortal (newRV_inc (sv)))); 525 SvPV_nolen (sv_2mortal (newRV_inc (sv))));
522 } 526 }
523 else if (enc->flags & F_ALLOW_BLESSED) 527 else if (enc->json.flags & F_ALLOW_BLESSED)
524 encode_str (enc, "null", 4, 0); 528 encode_str (enc, "null", 4, 0);
525 else 529 else
526 croak ("encountered object '%s', but neither allow_blessed nor convert_blessed settings are enabled", 530 croak ("encountered object '%s', but neither allow_blessed nor convert_blessed settings are enabled",
527 SvPV_nolen (sv_2mortal (newRV_inc (sv)))); 531 SvPV_nolen (sv_2mortal (newRV_inc (sv))));
528 } 532 }
614 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",
615 SvPV_nolen (sv), SvFLAGS (sv)); 619 SvPV_nolen (sv), SvFLAGS (sv));
616} 620}
617 621
618static SV * 622static SV *
619encode_json (SV *scalar, U32 flags) 623encode_json (SV *scalar, struct json *json)
620{ 624{
621 enc_t enc; 625 enc_t enc;
622 626
623 if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) 627 if (!(json->flags & F_ALLOW_NONREF) && !SvROK (scalar))
624 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)");
625 629
626 enc.flags = flags; 630 enc.json = *json;
627 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); 631 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE));
628 enc.cur = SvPVX (enc.sv); 632 enc.cur = SvPVX (enc.sv);
629 enc.end = SvEND (enc.sv); 633 enc.end = SvEND (enc.sv);
630 enc.indent = 0; 634 enc.indent = 0;
631 enc.maxdepth = DEC_DEPTH (flags); 635 enc.maxdepth = DEC_DEPTH (enc.json.flags);
632 636
633 SvPOK_only (enc.sv); 637 SvPOK_only (enc.sv);
634 encode_sv (&enc, scalar); 638 encode_sv (&enc, scalar);
635 639
636 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); 640 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv));
637 *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
638 642
639 if (!(flags & (F_ASCII | F_LATIN1 | F_UTF8))) 643 if (!(enc.json.flags & (F_ASCII | F_LATIN1 | F_UTF8)))
640 SvUTF8_on (enc.sv); 644 SvUTF8_on (enc.sv);
641 645
642 if (enc.flags & F_SHRINK) 646 if (enc.json.flags & F_SHRINK)
643 shrink (enc.sv); 647 shrink (enc.sv);
644 648
645 return enc.sv; 649 return enc.sv;
646} 650}
647 651
652typedef struct 656typedef struct
653{ 657{
654 char *cur; // current parser pointer 658 char *cur; // current parser pointer
655 char *end; // end of input string 659 char *end; // end of input string
656 const char *err; // parse error, if != 0 660 const char *err; // parse error, if != 0
657 U32 flags; // F_* 661 struct json json;
658 U32 depth; // recursion depth 662 U32 depth; // recursion depth
659 U32 maxdepth; // recursion depth limit 663 U32 maxdepth; // recursion depth limit
660} dec_t; 664} dec_t;
661 665
662inline void 666inline void
1023 if (*dec->cur == '}') 1027 if (*dec->cur == '}')
1024 ++dec->cur; 1028 ++dec->cur;
1025 else 1029 else
1026 for (;;) 1030 for (;;)
1027 { 1031 {
1028 SV *key, *value;
1029
1030 decode_ws (dec); EXPECT_CH ('"'); 1032 decode_ws (dec); EXPECT_CH ('"');
1031 1033
1032 key = decode_str (dec); 1034 // heuristic: assume that
1033 if (!key) 1035 // a) decode_str + hv_store_ent are abysmally slow.
1034 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
1035 1043
1036 decode_ws (dec); EXPECT_CH (':'); 1044 for (;;)
1037
1038 value = decode_sv (dec);
1039 if (!value)
1040 { 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);
1041 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)
1042 goto fail; 1079 goto fail;
1080
1081 hv_store (hv, key, len, value, 0);
1082
1083 break;
1084 }
1085
1086 ++p;
1043 } 1087 }
1044 1088 }
1045 hv_store_ent (hv, key, value, 0);
1046 SvREFCNT_dec (key);
1047 1089
1048 decode_ws (dec); 1090 decode_ws (dec);
1049 1091
1050 if (*dec->cur == '}') 1092 if (*dec->cur == '}')
1051 { 1093 {
1127fail: 1169fail:
1128 return 0; 1170 return 0;
1129} 1171}
1130 1172
1131static SV * 1173static SV *
1132decode_json (SV *string, U32 flags, UV *offset_return) 1174decode_json (SV *string, struct json *json, UV *offset_return)
1133{ 1175{
1134 dec_t dec; 1176 dec_t dec;
1135 UV offset; 1177 UV offset;
1136 SV *sv; 1178 SV *sv;
1137 1179
1138 SvGETMAGIC (string); 1180 SvGETMAGIC (string);
1139 SvUPGRADE (string, SVt_PV); 1181 SvUPGRADE (string, SVt_PV);
1140 1182
1141 if (flags & F_MAXSIZE && SvCUR (string) > DEC_SIZE (flags)) 1183 if (json->flags & F_MAXSIZE && SvCUR (string) > DEC_SIZE (json->flags))
1142 croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", 1184 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)); 1185 (unsigned long)SvCUR (string), (unsigned long)DEC_SIZE (json->flags));
1144 1186
1145 if (flags & F_UTF8) 1187 if (json->flags & F_UTF8)
1146 sv_utf8_downgrade (string, 0); 1188 sv_utf8_downgrade (string, 0);
1147 else 1189 else
1148 sv_utf8_upgrade (string); 1190 sv_utf8_upgrade (string);
1149 1191
1150 SvGROW (string, SvCUR (string) + 1); // should basically be a NOP 1192 SvGROW (string, SvCUR (string) + 1); // should basically be a NOP
1151 1193
1152 dec.flags = flags; 1194 dec.json = *json;
1153 dec.cur = SvPVX (string); 1195 dec.cur = SvPVX (string);
1154 dec.end = SvEND (string); 1196 dec.end = SvEND (string);
1155 dec.err = 0; 1197 dec.err = 0;
1156 dec.depth = 0; 1198 dec.depth = 0;
1157 dec.maxdepth = DEC_DEPTH (dec.flags); 1199 dec.maxdepth = DEC_DEPTH (dec.json.flags);
1158 1200
1159 *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
1160 sv = decode_sv (&dec); 1202 sv = decode_sv (&dec);
1161 1203
1162 if (!(offset_return || !sv)) 1204 if (!(offset_return || !sv))
1172 } 1214 }
1173 } 1215 }
1174 1216
1175 if (offset_return || !sv) 1217 if (offset_return || !sv)
1176 { 1218 {
1177 offset = dec.flags & F_UTF8 1219 offset = dec.json.flags & F_UTF8
1178 ? dec.cur - SvPVX (string) 1220 ? dec.cur - SvPVX (string)
1179 : utf8_distance (dec.cur, SvPVX (string)); 1221 : utf8_distance (dec.cur, SvPVX (string));
1180 1222
1181 if (offset_return) 1223 if (offset_return)
1182 *offset_return = offset; 1224 *offset_return = offset;
1201 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); 1243 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)");
1202 } 1244 }
1203 1245
1204 sv = sv_2mortal (sv); 1246 sv = sv_2mortal (sv);
1205 1247
1206 if (!(dec.flags & F_ALLOW_NONREF) && !SvROK (sv)) 1248 if (!(dec.json.flags & F_ALLOW_NONREF) && !SvROK (sv))
1207 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)");
1208 1250
1209 return sv; 1251 return sv;
1210} 1252}
1211 1253
1304 OUTPUT: 1346 OUTPUT:
1305 RETVAL 1347 RETVAL
1306 1348
1307void encode (SV *self, SV *scalar) 1349void encode (SV *self, SV *scalar)
1308 PPCODE: 1350 PPCODE:
1351{
1352 struct json json = { *SvJSON (self) };
1309 XPUSHs (encode_json (scalar, *SvJSON (self))); 1353 XPUSHs (encode_json (scalar, &json));
1354}
1310 1355
1311void decode (SV *self, SV *jsonstr) 1356void decode (SV *self, SV *jsonstr)
1312 PPCODE: 1357 PPCODE:
1358{
1359 struct json json = { *SvJSON (self) };
1313 XPUSHs (decode_json (jsonstr, *SvJSON (self), 0)); 1360 XPUSHs (decode_json (jsonstr, &json, 0));
1361}
1314 1362
1315void decode_prefix (SV *self, SV *jsonstr) 1363void decode_prefix (SV *self, SV *jsonstr)
1316 PPCODE: 1364 PPCODE:
1317{ 1365{
1318 UV offset; 1366 UV offset;
1367 struct json json = { *SvJSON (self) };
1319 EXTEND (SP, 2); 1368 EXTEND (SP, 2);
1320 PUSHs (decode_json (jsonstr, *SvJSON (self), &offset)); 1369 PUSHs (decode_json (jsonstr, &json, &offset));
1321 PUSHs (sv_2mortal (newSVuv (offset))); 1370 PUSHs (sv_2mortal (newSVuv (offset)));
1322} 1371}
1323 1372
1324PROTOTYPES: ENABLE 1373PROTOTYPES: ENABLE
1325 1374
1326void to_json (SV *scalar) 1375void to_json (SV *scalar)
1327 ALIAS:
1328 objToJson = 0
1329 PPCODE: 1376 PPCODE:
1377{
1378 struct json json = { F_DEFAULT | F_UTF8 };
1330 XPUSHs (encode_json (scalar, F_DEFAULT | F_UTF8)); 1379 XPUSHs (encode_json (scalar, &json));
1380}
1331 1381
1332void from_json (SV *jsonstr) 1382void from_json (SV *jsonstr)
1333 ALIAS:
1334 jsonToObj = 0
1335 PPCODE: 1383 PPCODE:
1384{
1385 struct json json = { F_DEFAULT | F_UTF8 };
1336 XPUSHs (decode_json (jsonstr, F_DEFAULT | F_UTF8, 0)); 1386 XPUSHs (decode_json (jsonstr, &json, 0));
1387}
1337 1388

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines