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.51 by root, Mon Jul 2 00:48:18 2007 UTC vs.
Revision 1.56 by root, Thu Jul 26 11:33:35 2007 UTC

1#include "EXTERN.h" 1#include "EXTERN.h"
2#include "perl.h" 2#include "perl.h"
3#include "XSUB.h" 3#include "XSUB.h"
4 4
5#include "assert.h" 5#include <assert.h>
6#include "string.h" 6#include <string.h>
7#include "stdlib.h" 7#include <stdlib.h>
8#include "stdio.h" 8#include <stdio.h>
9#include <float.h>
9 10
10#if defined(__BORLANDC__) || defined(_MSC_VER) 11#if defined(__BORLANDC__) || defined(_MSC_VER)
11# define snprintf _snprintf // C compilers have this in stdio.h 12# define snprintf _snprintf // C compilers have this in stdio.h
12#endif 13#endif
13 14
62static HV *json_stash, *json_boolean_stash; // JSON::XS:: 63static HV *json_stash, *json_boolean_stash; // JSON::XS::
63static SV *json_true, *json_false; 64static SV *json_true, *json_false;
64 65
65typedef struct { 66typedef struct {
66 U32 flags; 67 U32 flags;
67 SV *cb_object, *cb_sk_object; 68 SV *cb_object;
69 HV *cb_sk_object;
68} JSON; 70} JSON;
69 71
70///////////////////////////////////////////////////////////////////////////// 72/////////////////////////////////////////////////////////////////////////////
71// utility functions 73// utility functions
72 74
305 encode_ch (enc, '['); encode_nl (enc); 307 encode_ch (enc, '['); encode_nl (enc);
306 ++enc->indent; 308 ++enc->indent;
307 309
308 for (i = 0; i <= len; ++i) 310 for (i = 0; i <= len; ++i)
309 { 311 {
312 SV **svp = av_fetch (av, i, 0);
313
310 encode_indent (enc); 314 encode_indent (enc);
311 encode_sv (enc, *av_fetch (av, i, 0)); 315
316 if (svp)
317 encode_sv (enc, *svp);
318 else
319 encode_str (enc, "null", 4, 0);
312 320
313 if (i < len) 321 if (i < len)
314 encode_comma (enc); 322 encode_comma (enc);
315 } 323 }
316 324
488 } 496 }
489#endif 497#endif
490 if (enc->json.flags & F_CONV_BLESSED) 498 if (enc->json.flags & F_CONV_BLESSED)
491 { 499 {
492 // we re-bless the reference to get overload and other niceties right 500 // we re-bless the reference to get overload and other niceties right
493 GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 1); 501 GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 0);
494 502
495 if (to_json) 503 if (to_json)
496 { 504 {
505 int count;
506 dSP;
507
497 dSP; ENTER; SAVETMPS; PUSHMARK (SP); 508 ENTER; SAVETMPS; PUSHMARK (SP);
498 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); 509 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv)));
499 510
500 // calling with G_SCALAR ensures that we always get a 1 reutrn value 511 // calling with G_SCALAR ensures that we always get a 1 return value
501 // check anyways.
502 PUTBACK; 512 PUTBACK;
503 assert (1 == call_sv ((SV *)GvCV (to_json), G_SCALAR)); 513 call_sv ((SV *)GvCV (to_json), G_SCALAR);
504 SPAGAIN; 514 SPAGAIN;
505 515
516 // catch this surprisingly common error
517 if (SvROK (TOPs) && SvRV (TOPs) == sv)
518 croak ("%s::TO_JSON method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv)));
519
520 sv = POPs;
521 PUTBACK;
522
506 encode_sv (enc, POPs); 523 encode_sv (enc, sv);
507 524
508 FREETMPS; LEAVE; 525 FREETMPS; LEAVE;
509 } 526 }
510 else if (enc->json.flags & F_ALLOW_BLESSED) 527 else if (enc->json.flags & F_ALLOW_BLESSED)
511 encode_str (enc, "null", 4, 0); 528 encode_str (enc, "null", 4, 0);
921 is_nv = 1; 938 is_nv = 1;
922 } 939 }
923 940
924 if (!is_nv) 941 if (!is_nv)
925 { 942 {
943 int len = dec->cur - start;
944
926 // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so 945 // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so
927 if (*start == '-') 946 if (*start == '-')
928 switch (dec->cur - start) 947 switch (len)
929 { 948 {
930 case 2: return newSViv (-( start [1] - '0' * 1)); 949 case 2: return newSViv (-( start [1] - '0' * 1));
931 case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); 950 case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11));
932 case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); 951 case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111));
933 case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); 952 case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111));
934 } 953 }
935 else 954 else
936 switch (dec->cur - start) 955 switch (len)
937 { 956 {
938 case 1: return newSViv ( start [0] - '0' * 1); 957 case 1: return newSViv ( start [0] - '0' * 1);
939 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); 958 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11);
940 case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); 959 case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111);
941 case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); 960 case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111);
942 } 961 }
943 962
944 { 963 {
945 UV uv; 964 UV uv;
946 int numtype = grok_number (start, dec->cur - start, &uv); 965 int numtype = grok_number (start, len, &uv);
947 if (numtype & IS_NUMBER_IN_UV) 966 if (numtype & IS_NUMBER_IN_UV)
948 if (numtype & IS_NUMBER_NEG) 967 if (numtype & IS_NUMBER_NEG)
949 { 968 {
950 if (uv < (UV)IV_MIN) 969 if (uv < (UV)IV_MIN)
951 return newSViv (-(IV)uv); 970 return newSViv (-(IV)uv);
952 } 971 }
953 else 972 else
954 return newSVuv (uv); 973 return newSVuv (uv);
955
956 // here would likely be the place for bigint support
957 } 974 }
958 }
959 975
960 // if we ever support bigint or bigfloat, this is the place for bigfloat 976 len -= *start == '-' ? 1 : 0;
977
978 // does not fit into IV or UV, try NV
979 if ((sizeof (NV) == sizeof (double) && DBL_DIG >= len)
980 #if defined (LDBL_DIG)
981 || (sizeof (NV) == sizeof (long double) && LDBL_DIG >= len)
982 #endif
983 )
984 // fits into NV without loss of precision
985 return newSVnv (Atof (start));
986
987 // everything else fails, convert it to a string
988 return newSVpvn (start, dec->cur - start);
989 }
990
991 // loss of precision here
961 return newSVnv (Atof (start)); 992 return newSVnv (Atof (start));
962 993
963fail: 994fail:
964 return 0; 995 return 0;
965} 996}
1098 sv = newRV_noinc ((SV *)hv); 1129 sv = newRV_noinc ((SV *)hv);
1099 1130
1100 // check filter callbacks 1131 // check filter callbacks
1101 if (dec->json.flags & F_HOOK) 1132 if (dec->json.flags & F_HOOK)
1102 { 1133 {
1103 ENTER; SAVETMPS;
1104
1105 if (dec->json.cb_sk_object && HvKEYS (hv) == 1) 1134 if (dec->json.cb_sk_object && HvKEYS (hv) == 1)
1106 { 1135 {
1136 HE *cb, *he;
1137
1138 hv_iterinit (hv);
1139 he = hv_iternext (hv);
1140 hv_iterinit (hv);
1141
1142 // the next line creates a mortal sv each time its called.
1143 // might want to optimise this for common cases.
1144 cb = hv_fetch_ent (dec->json.cb_sk_object, hv_iterkeysv (he), 0, 0);
1145
1146 if (cb)
1147 {
1148 dSP;
1107 int count; 1149 int count;
1108 1150
1109 dSP; PUSHMARK (SP); 1151 ENTER; SAVETMPS; PUSHMARK (SP);
1110 XPUSHs (sv_2mortal (sv)); 1152 XPUSHs (HeVAL (he));
1111 1153
1112 PUTBACK; count = call_sv (dec->json.cb_sk_object, G_ARRAY); SPAGAIN; 1154 PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN;
1113 1155
1114 if (count == 1) 1156 if (count == 1)
1115 { 1157 {
1116 sv = newSVsv (POPs); 1158 sv = newSVsv (POPs);
1117 goto filter_ok; 1159 FREETMPS; LEAVE;
1160 return sv;
1118 } 1161 }
1119 1162
1120 SvREFCNT_inc (sv); 1163 FREETMPS; LEAVE;
1164 }
1121 } 1165 }
1122 1166
1123 if (dec->json.cb_object) 1167 if (dec->json.cb_object)
1124 { 1168 {
1169 dSP;
1125 int count; 1170 int count;
1126 1171
1127 dSP; ENTER; SAVETMPS; PUSHMARK (SP); 1172 ENTER; SAVETMPS; PUSHMARK (SP);
1128 XPUSHs (sv_2mortal (sv)); 1173 XPUSHs (sv_2mortal (sv));
1129 1174
1130 PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN; 1175 PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN;
1131 1176
1132 if (count == 1) 1177 if (count == 1)
1133 { 1178 {
1134 sv = newSVsv (POPs); 1179 sv = newSVsv (POPs);
1135 goto filter_ok; 1180 FREETMPS; LEAVE;
1181 return sv;
1136 } 1182 }
1137 1183
1138 SvREFCNT_inc (sv); 1184 SvREFCNT_inc (sv);
1185 FREETMPS; LEAVE;
1139 } 1186 }
1140
1141filter_ok:
1142 FREETMPS; LEAVE;
1143 } 1187 }
1144 1188
1145 return sv; 1189 return sv;
1146 1190
1147fail: 1191fail:
1383 1427
1384 XPUSHs (ST (0)); 1428 XPUSHs (ST (0));
1385} 1429}
1386 1430
1387void filter_json_object (JSON *self, SV *cb = &PL_sv_undef) 1431void filter_json_object (JSON *self, SV *cb = &PL_sv_undef)
1388 ALIAS:
1389 filter_json_single_key_object = 1
1390 PPCODE: 1432 PPCODE:
1391{ 1433{
1392 SV **svp; 1434 SvREFCNT_dec (self->cb_object);
1435 self->cb_object = SvOK (cb) ? newSVsv (cb) : 0;
1393 1436
1437 XPUSHs (ST (0));
1438}
1439
1440void filter_json_single_key_object (JSON *self, SV *key, SV *cb = &PL_sv_undef)
1441 PPCODE:
1442{
1443 if (!self->cb_sk_object)
1444 self->cb_sk_object = newHV ();
1445
1394 if (!SvOK (cb)) 1446 if (SvOK (cb))
1395 cb = 0; 1447 hv_store_ent (self->cb_sk_object, key, newSVsv (cb), 0);
1396 else 1448 else
1397 cb = newSVsv (cb);
1398
1399 switch (ix)
1400 { 1449 {
1401 case 0: svp = &self->cb_object ; break; 1450 hv_delete_ent (self->cb_sk_object, key, G_DISCARD, 0);
1402 case 1: svp = &self->cb_sk_object; break; 1451
1452 if (!HvKEYS (self->cb_sk_object))
1453 {
1454 SvREFCNT_dec (self->cb_sk_object);
1455 self->cb_sk_object = 0;
1456 }
1403 } 1457 }
1404
1405 if (*svp)
1406 SvREFCNT_dec (*svp);
1407
1408 *svp = cb;
1409 1458
1410 XPUSHs (ST (0)); 1459 XPUSHs (ST (0));
1411} 1460}
1412 1461
1413void encode (JSON *self, SV *scalar) 1462void encode (JSON *self, SV *scalar)
1425 EXTEND (SP, 2); 1474 EXTEND (SP, 2);
1426 PUSHs (decode_json (jsonstr, self, &offset)); 1475 PUSHs (decode_json (jsonstr, self, &offset));
1427 PUSHs (sv_2mortal (newSVuv (offset))); 1476 PUSHs (sv_2mortal (newSVuv (offset)));
1428} 1477}
1429 1478
1479void DESTROY (JSON *self)
1480 CODE:
1481 SvREFCNT_dec (self->cb_sk_object);
1482 SvREFCNT_dec (self->cb_object);
1483
1430PROTOTYPES: ENABLE 1484PROTOTYPES: ENABLE
1431 1485
1432void to_json (SV *scalar) 1486void to_json (SV *scalar)
1433 PPCODE: 1487 PPCODE:
1434{ 1488{

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines