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.52 by root, Mon Jul 2 02:57:11 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
306 encode_ch (enc, '['); encode_nl (enc); 307 encode_ch (enc, '['); encode_nl (enc);
307 ++enc->indent; 308 ++enc->indent;
308 309
309 for (i = 0; i <= len; ++i) 310 for (i = 0; i <= len; ++i)
310 { 311 {
312 SV **svp = av_fetch (av, i, 0);
313
311 encode_indent (enc); 314 encode_indent (enc);
312 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);
313 320
314 if (i < len) 321 if (i < len)
315 encode_comma (enc); 322 encode_comma (enc);
316 } 323 }
317 324
489 } 496 }
490#endif 497#endif
491 if (enc->json.flags & F_CONV_BLESSED) 498 if (enc->json.flags & F_CONV_BLESSED)
492 { 499 {
493 // 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
494 GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 1); 501 GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 0);
495 502
496 if (to_json) 503 if (to_json)
497 { 504 {
505 int count;
506 dSP;
507
498 dSP; ENTER; SAVETMPS; PUSHMARK (SP); 508 ENTER; SAVETMPS; PUSHMARK (SP);
499 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); 509 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv)));
500 510
501 // 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
502 // check anyways.
503 PUTBACK; 512 PUTBACK;
504 assert (1 == call_sv ((SV *)GvCV (to_json), G_SCALAR)); 513 call_sv ((SV *)GvCV (to_json), G_SCALAR);
505 SPAGAIN; 514 SPAGAIN;
506 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
507 encode_sv (enc, POPs); 523 encode_sv (enc, sv);
508 524
509 FREETMPS; LEAVE; 525 FREETMPS; LEAVE;
510 } 526 }
511 else if (enc->json.flags & F_ALLOW_BLESSED) 527 else if (enc->json.flags & F_ALLOW_BLESSED)
512 encode_str (enc, "null", 4, 0); 528 encode_str (enc, "null", 4, 0);
922 is_nv = 1; 938 is_nv = 1;
923 } 939 }
924 940
925 if (!is_nv) 941 if (!is_nv)
926 { 942 {
943 int len = dec->cur - start;
944
927 // 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
928 if (*start == '-') 946 if (*start == '-')
929 switch (dec->cur - start) 947 switch (len)
930 { 948 {
931 case 2: return newSViv (-( start [1] - '0' * 1)); 949 case 2: return newSViv (-( start [1] - '0' * 1));
932 case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); 950 case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11));
933 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));
934 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));
935 } 953 }
936 else 954 else
937 switch (dec->cur - start) 955 switch (len)
938 { 956 {
939 case 1: return newSViv ( start [0] - '0' * 1); 957 case 1: return newSViv ( start [0] - '0' * 1);
940 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); 958 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11);
941 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);
942 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);
943 } 961 }
944 962
945 { 963 {
946 UV uv; 964 UV uv;
947 int numtype = grok_number (start, dec->cur - start, &uv); 965 int numtype = grok_number (start, len, &uv);
948 if (numtype & IS_NUMBER_IN_UV) 966 if (numtype & IS_NUMBER_IN_UV)
949 if (numtype & IS_NUMBER_NEG) 967 if (numtype & IS_NUMBER_NEG)
950 { 968 {
951 if (uv < (UV)IV_MIN) 969 if (uv < (UV)IV_MIN)
952 return newSViv (-(IV)uv); 970 return newSViv (-(IV)uv);
953 } 971 }
954 else 972 else
955 return newSVuv (uv); 973 return newSVuv (uv);
956
957 // here would likely be the place for bigint support
958 } 974 }
959 }
960 975
961 // 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
962 return newSVnv (Atof (start)); 992 return newSVnv (Atof (start));
963 993
964fail: 994fail:
965 return 0; 995 return 0;
966} 996}
1113 // might want to optimise this for common cases. 1143 // might want to optimise this for common cases.
1114 cb = hv_fetch_ent (dec->json.cb_sk_object, hv_iterkeysv (he), 0, 0); 1144 cb = hv_fetch_ent (dec->json.cb_sk_object, hv_iterkeysv (he), 0, 0);
1115 1145
1116 if (cb) 1146 if (cb)
1117 { 1147 {
1148 dSP;
1118 int count; 1149 int count;
1119 ENTER; SAVETMPS;
1120 1150
1121 dSP; PUSHMARK (SP); 1151 ENTER; SAVETMPS; PUSHMARK (SP);
1122 XPUSHs (HeVAL (he)); 1152 XPUSHs (HeVAL (he));
1123 1153
1124 PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; 1154 PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN;
1125 1155
1126 if (count == 1) 1156 if (count == 1)
1134 } 1164 }
1135 } 1165 }
1136 1166
1137 if (dec->json.cb_object) 1167 if (dec->json.cb_object)
1138 { 1168 {
1169 dSP;
1139 int count; 1170 int count;
1140 ENTER; SAVETMPS;
1141 1171
1142 dSP; ENTER; SAVETMPS; PUSHMARK (SP); 1172 ENTER; SAVETMPS; PUSHMARK (SP);
1143 XPUSHs (sv_2mortal (sv)); 1173 XPUSHs (sv_2mortal (sv));
1144 1174
1145 PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN; 1175 PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN;
1146 1176
1147 if (count == 1) 1177 if (count == 1)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines