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.53 by root, Tue Jul 10 15:45:34 2007 UTC vs.
Revision 1.70 by root, Wed Mar 19 00:44:54 2008 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
26#define F_SPACE_AFTER 0x00000040UL 27#define F_SPACE_AFTER 0x00000040UL
27#define F_ALLOW_NONREF 0x00000100UL 28#define F_ALLOW_NONREF 0x00000100UL
28#define F_SHRINK 0x00000200UL 29#define F_SHRINK 0x00000200UL
29#define F_ALLOW_BLESSED 0x00000400UL 30#define F_ALLOW_BLESSED 0x00000400UL
30#define F_CONV_BLESSED 0x00000800UL 31#define F_CONV_BLESSED 0x00000800UL
32#define F_RELAXED 0x00001000UL
33
31#define F_MAXDEPTH 0xf8000000UL 34#define F_MAXDEPTH 0xf8000000UL
32#define S_MAXDEPTH 27 35#define S_MAXDEPTH 27
33#define F_MAXSIZE 0x01f00000UL 36#define F_MAXSIZE 0x01f00000UL
34#define S_MAXSIZE 20 37#define S_MAXSIZE 20
35#define F_HOOK 0x00080000UL // some hooks exist, so slow-path processing 38#define F_HOOK 0x00080000UL // some hooks exist, so slow-path processing
56# define inline static 59# define inline static
57#endif 60#endif
58 61
59#define expect_false(expr) expect ((expr) != 0, 0) 62#define expect_false(expr) expect ((expr) != 0, 0)
60#define expect_true(expr) expect ((expr) != 0, 1) 63#define expect_true(expr) expect ((expr) != 0, 1)
64
65#ifdef USE_ITHREADS
66# define JSON_SLOW 1
67# define JSON_STASH (json_stash ? json_stash : gv_stashpv ("JSON::XS", 1))
68#else
69# define JSON_SLOW 0
70# define JSON_STASH json_stash
71#endif
61 72
62static HV *json_stash, *json_boolean_stash; // JSON::XS:: 73static HV *json_stash, *json_boolean_stash; // JSON::XS::
63static SV *json_true, *json_false; 74static SV *json_true, *json_false;
64 75
65typedef struct { 76typedef struct {
301 int i, len = av_len (av); 312 int i, len = av_len (av);
302 313
303 if (enc->indent >= enc->maxdepth) 314 if (enc->indent >= enc->maxdepth)
304 croak ("data structure too deep (hit recursion limit)"); 315 croak ("data structure too deep (hit recursion limit)");
305 316
306 encode_ch (enc, '['); encode_nl (enc); 317 encode_ch (enc, '[');
307 ++enc->indent; 318
319 if (len >= 0)
320 {
321 encode_nl (enc); ++enc->indent;
308 322
309 for (i = 0; i <= len; ++i) 323 for (i = 0; i <= len; ++i)
310 { 324 {
325 SV **svp = av_fetch (av, i, 0);
326
311 encode_indent (enc); 327 encode_indent (enc);
312 encode_sv (enc, *av_fetch (av, i, 0));
313 328
329 if (svp)
330 encode_sv (enc, *svp);
331 else
332 encode_str (enc, "null", 4, 0);
333
314 if (i < len) 334 if (i < len)
315 encode_comma (enc); 335 encode_comma (enc);
316 } 336 }
317 337
338 encode_nl (enc); --enc->indent; encode_indent (enc);
339 }
340
318 encode_nl (enc); 341 encode_ch (enc, ']');
319
320 --enc->indent;
321 encode_indent (enc); encode_ch (enc, ']');
322} 342}
323 343
324static void 344static void
325encode_he (enc_t *enc, HE *he) 345encode_hk (enc_t *enc, HE *he)
326{ 346{
327 encode_ch (enc, '"'); 347 encode_ch (enc, '"');
328 348
329 if (HeKLEN (he) == HEf_SVKEY) 349 if (HeKLEN (he) == HEf_SVKEY)
330 { 350 {
343 encode_ch (enc, '"'); 363 encode_ch (enc, '"');
344 364
345 if (enc->json.flags & F_SPACE_BEFORE) encode_space (enc); 365 if (enc->json.flags & F_SPACE_BEFORE) encode_space (enc);
346 encode_ch (enc, ':'); 366 encode_ch (enc, ':');
347 if (enc->json.flags & F_SPACE_AFTER ) encode_space (enc); 367 if (enc->json.flags & F_SPACE_AFTER ) encode_space (enc);
348 encode_sv (enc, HeVAL (he));
349} 368}
350 369
351// compare hash entries, used when all keys are bytestrings 370// compare hash entries, used when all keys are bytestrings
352static int 371static int
353he_cmp_fast (const void *a_, const void *b_) 372he_cmp_fast (const void *a_, const void *b_)
358 HE *b = *(HE **)b_; 377 HE *b = *(HE **)b_;
359 378
360 STRLEN la = HeKLEN (a); 379 STRLEN la = HeKLEN (a);
361 STRLEN lb = HeKLEN (b); 380 STRLEN lb = HeKLEN (b);
362 381
363 if (!(cmp = memcmp (HeKEY (a), HeKEY (b), la < lb ? la : lb))) 382 if (!(cmp = memcmp (HeKEY (b), HeKEY (a), lb < la ? lb : la)))
364 cmp = la - lb; 383 cmp = lb - la;
365 384
366 return cmp; 385 return cmp;
367} 386}
368 387
369// compare hash entries, used when some keys are sv's or utf-x 388// compare hash entries, used when some keys are sv's or utf-x
370static int 389static int
371he_cmp_slow (const void *a, const void *b) 390he_cmp_slow (const void *a, const void *b)
372{ 391{
373 return sv_cmp (HeSVKEY_force (*(HE **)a), HeSVKEY_force (*(HE **)b)); 392 return sv_cmp (HeSVKEY_force (*(HE **)b), HeSVKEY_force (*(HE **)a));
374} 393}
375 394
376static void 395static void
377encode_hv (enc_t *enc, HV *hv) 396encode_hv (enc_t *enc, HV *hv)
378{ 397{
398 HE *he;
379 int count, i; 399 int count;
380 400
381 if (enc->indent >= enc->maxdepth) 401 if (enc->indent >= enc->maxdepth)
382 croak ("data structure too deep (hit recursion limit)"); 402 croak ("data structure too deep (hit recursion limit)");
383 403
384 encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; 404 encode_ch (enc, '{');
385 405
386 if ((count = hv_iterinit (hv)))
387 {
388 // for canonical output we have to sort by keys first 406 // for canonical output we have to sort by keys first
389 // actually, this is mostly due to the stupid so-called 407 // actually, this is mostly due to the stupid so-called
390 // security workaround added somewhere in 5.8.x. 408 // security workaround added somewhere in 5.8.x.
391 // that randomises hash orderings 409 // that randomises hash orderings
392 if (enc->json.flags & F_CANONICAL) 410 if (enc->json.flags & F_CANONICAL)
411 {
412 int count = hv_iterinit (hv);
413
414 if (SvMAGICAL (hv))
393 { 415 {
416 // need to count by iterating. could improve by dynamically building the vector below
417 // but I don't care for the speed of this special case.
418 // note also that we will run into undefined behaviour when the two iterations
419 // do not result in the same count, something I might care for in some later release.
420
421 count = 0;
422 while (hv_iternext (hv))
423 ++count;
424
425 hv_iterinit (hv);
426 }
427
428 if (count)
429 {
394 int fast = 1; 430 int i, fast = 1;
395 HE *he;
396#if defined(__BORLANDC__) || defined(_MSC_VER) 431#if defined(__BORLANDC__) || defined(_MSC_VER)
397 HE **hes = _alloca (count * sizeof (HE)); 432 HE **hes = _alloca (count * sizeof (HE));
398#else 433#else
399 HE *hes [count]; // if your compiler dies here, you need to enable C99 mode 434 HE *hes [count]; // if your compiler dies here, you need to enable C99 mode
400#endif 435#endif
427 462
428 FREETMPS; 463 FREETMPS;
429 LEAVE; 464 LEAVE;
430 } 465 }
431 466
432 for (i = 0; i < count; ++i) 467 encode_nl (enc); ++enc->indent;
468
469 while (count--)
433 { 470 {
434 encode_indent (enc); 471 encode_indent (enc);
472 he = hes [count];
435 encode_he (enc, hes [i]); 473 encode_hk (enc, he);
474 encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he));
436 475
437 if (i < count - 1) 476 if (count)
438 encode_comma (enc); 477 encode_comma (enc);
439 } 478 }
440 479
441 encode_nl (enc); 480 encode_nl (enc); --enc->indent; encode_indent (enc);
442 } 481 }
482 }
443 else 483 else
484 {
485 if (hv_iterinit (hv) || SvMAGICAL (hv))
486 if ((he = hv_iternext (hv)))
444 { 487 {
445 HE *he = hv_iternext (hv); 488 encode_nl (enc); ++enc->indent;
446 489
447 for (;;) 490 for (;;)
448 { 491 {
449 encode_indent (enc); 492 encode_indent (enc);
450 encode_he (enc, he); 493 encode_hk (enc, he);
494 encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he));
451 495
452 if (!(he = hv_iternext (hv))) 496 if (!(he = hv_iternext (hv)))
453 break; 497 break;
454 498
455 encode_comma (enc); 499 encode_comma (enc);
456 } 500 }
457 501
458 encode_nl (enc); 502 encode_nl (enc); --enc->indent; encode_indent (enc);
459 } 503 }
460 } 504 }
461 505
462 --enc->indent; encode_indent (enc); encode_ch (enc, '}'); 506 encode_ch (enc, '}');
463} 507}
464 508
465// encode objects, arrays and special \0=false and \1=true values. 509// encode objects, arrays and special \0=false and \1=true values.
466static void 510static void
467encode_rv (enc_t *enc, SV *sv) 511encode_rv (enc_t *enc, SV *sv)
471 SvGETMAGIC (sv); 515 SvGETMAGIC (sv);
472 svt = SvTYPE (sv); 516 svt = SvTYPE (sv);
473 517
474 if (expect_false (SvOBJECT (sv))) 518 if (expect_false (SvOBJECT (sv)))
475 { 519 {
520 HV *stash = !JSON_SLOW || json_boolean_stash
521 ? json_boolean_stash
522 : gv_stashpv ("JSON::XS::Boolean", 1);
523
476 if (SvSTASH (sv) == json_boolean_stash) 524 if (SvSTASH (sv) == stash)
477 { 525 {
478 if (SvIV (sv)) 526 if (SvIV (sv))
479 encode_str (enc, "true", 4, 0); 527 encode_str (enc, "true", 4, 0);
480 else 528 else
481 encode_str (enc, "false", 5, 0); 529 encode_str (enc, "false", 5, 0);
499 547
500 ENTER; SAVETMPS; PUSHMARK (SP); 548 ENTER; SAVETMPS; PUSHMARK (SP);
501 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); 549 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv)));
502 550
503 // calling with G_SCALAR ensures that we always get a 1 return value 551 // calling with G_SCALAR ensures that we always get a 1 return value
504 // check anyways.
505 PUTBACK; 552 PUTBACK;
506 assert (1 == call_sv ((SV *)GvCV (to_json), G_SCALAR)); 553 call_sv ((SV *)GvCV (to_json), G_SCALAR);
507 SPAGAIN; 554 SPAGAIN;
508 555
509 // catch this surprisingly common error 556 // catch this surprisingly common error
510 if (SvROK (TOPs) && SvRV (TOPs) == sv) 557 if (SvROK (TOPs) && SvRV (TOPs) == sv)
511 croak ("%s::TO_JSON method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); 558 croak ("%s::TO_JSON method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv)));
664 U32 depth; // recursion depth 711 U32 depth; // recursion depth
665 U32 maxdepth; // recursion depth limit 712 U32 maxdepth; // recursion depth limit
666} dec_t; 713} dec_t;
667 714
668inline void 715inline void
716decode_comment (dec_t *dec)
717{
718 // only '#'-style comments allowed a.t.m.
719
720 while (*dec->cur && *dec->cur != 0x0a && *dec->cur != 0x0d)
721 ++dec->cur;
722}
723
724inline void
669decode_ws (dec_t *dec) 725decode_ws (dec_t *dec)
670{ 726{
671 for (;;) 727 for (;;)
672 { 728 {
673 char ch = *dec->cur; 729 char ch = *dec->cur;
674 730
675 if (ch > 0x20 731 if (ch > 0x20)
732 {
733 if (expect_false (ch == '#'))
734 {
735 if (dec->json.flags & F_RELAXED)
736 decode_comment (dec);
737 else
738 break;
739 }
740 else
741 break;
742 }
676 || (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)) 743 else if (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)
677 break; 744 break; // parse error, but let higher level handle it, gives better error messages
678 745
679 ++dec->cur; 746 ++dec->cur;
680 } 747 }
681} 748}
682 749
931 is_nv = 1; 998 is_nv = 1;
932 } 999 }
933 1000
934 if (!is_nv) 1001 if (!is_nv)
935 { 1002 {
1003 int len = dec->cur - start;
1004
936 // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so 1005 // special case the rather common 1..5-digit-int case
937 if (*start == '-') 1006 if (*start == '-')
938 switch (dec->cur - start) 1007 switch (len)
939 { 1008 {
940 case 2: return newSViv (-( start [1] - '0' * 1)); 1009 case 2: return newSViv (-( start [1] - '0' * 1));
941 case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); 1010 case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11));
942 case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); 1011 case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111));
943 case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); 1012 case 5: return newSViv (-( start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111));
1013 case 6: return newSViv (-(start [1] * 10000 + start [2] * 1000 + start [3] * 100 + start [4] * 10 + start [5] - '0' * 11111));
944 } 1014 }
945 else 1015 else
946 switch (dec->cur - start) 1016 switch (len)
947 { 1017 {
948 case 1: return newSViv ( start [0] - '0' * 1); 1018 case 1: return newSViv ( start [0] - '0' * 1);
949 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); 1019 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11);
950 case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); 1020 case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111);
951 case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); 1021 case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111);
1022 case 5: return newSViv ( start [0] * 10000 + start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 11111);
952 } 1023 }
953 1024
954 { 1025 {
955 UV uv; 1026 UV uv;
956 int numtype = grok_number (start, dec->cur - start, &uv); 1027 int numtype = grok_number (start, len, &uv);
957 if (numtype & IS_NUMBER_IN_UV) 1028 if (numtype & IS_NUMBER_IN_UV)
958 if (numtype & IS_NUMBER_NEG) 1029 if (numtype & IS_NUMBER_NEG)
959 { 1030 {
960 if (uv < (UV)IV_MIN) 1031 if (uv < (UV)IV_MIN)
961 return newSViv (-(IV)uv); 1032 return newSViv (-(IV)uv);
962 } 1033 }
963 else 1034 else
964 return newSVuv (uv); 1035 return newSVuv (uv);
965
966 // here would likely be the place for bigint support
967 } 1036 }
968 }
969 1037
970 // if we ever support bigint or bigfloat, this is the place for bigfloat 1038 len -= *start == '-' ? 1 : 0;
1039
1040 // does not fit into IV or UV, try NV
1041 if ((sizeof (NV) == sizeof (double) && DBL_DIG >= len)
1042 #if defined (LDBL_DIG)
1043 || (sizeof (NV) == sizeof (long double) && LDBL_DIG >= len)
1044 #endif
1045 )
1046 // fits into NV without loss of precision
1047 return newSVnv (Atof (start));
1048
1049 // everything else fails, convert it to a string
1050 return newSVpvn (start, dec->cur - start);
1051 }
1052
1053 // loss of precision here
971 return newSVnv (Atof (start)); 1054 return newSVnv (Atof (start));
972 1055
973fail: 1056fail:
974 return 0; 1057 return 0;
975} 1058}
1005 1088
1006 if (*dec->cur != ',') 1089 if (*dec->cur != ',')
1007 ERR (", or ] expected while parsing array"); 1090 ERR (", or ] expected while parsing array");
1008 1091
1009 ++dec->cur; 1092 ++dec->cur;
1093
1094 decode_ws (dec);
1095
1096 if (*dec->cur == ']' && dec->json.flags & F_RELAXED)
1097 {
1098 ++dec->cur;
1099 break;
1100 }
1010 } 1101 }
1011 1102
1012 DEC_DEC_DEPTH; 1103 DEC_DEC_DEPTH;
1013 return newRV_noinc ((SV *)av); 1104 return newRV_noinc ((SV *)av);
1014 1105
1030 if (*dec->cur == '}') 1121 if (*dec->cur == '}')
1031 ++dec->cur; 1122 ++dec->cur;
1032 else 1123 else
1033 for (;;) 1124 for (;;)
1034 { 1125 {
1035 decode_ws (dec); EXPECT_CH ('"'); 1126 EXPECT_CH ('"');
1036 1127
1037 // heuristic: assume that 1128 // heuristic: assume that
1038 // a) decode_str + hv_store_ent are abysmally slow. 1129 // a) decode_str + hv_store_ent are abysmally slow.
1039 // b) most hash keys are short, simple ascii text. 1130 // b) most hash keys are short, simple ascii text.
1040 // => try to "fast-match" such strings to avoid 1131 // => try to "fast-match" such strings to avoid
1054 if (!key) 1145 if (!key)
1055 goto fail; 1146 goto fail;
1056 1147
1057 decode_ws (dec); EXPECT_CH (':'); 1148 decode_ws (dec); EXPECT_CH (':');
1058 1149
1150 decode_ws (dec);
1059 value = decode_sv (dec); 1151 value = decode_sv (dec);
1060 if (!value) 1152 if (!value)
1061 { 1153 {
1062 SvREFCNT_dec (key); 1154 SvREFCNT_dec (key);
1063 goto fail; 1155 goto fail;
1075 int len = p - key; 1167 int len = p - key;
1076 dec->cur = p + 1; 1168 dec->cur = p + 1;
1077 1169
1078 decode_ws (dec); EXPECT_CH (':'); 1170 decode_ws (dec); EXPECT_CH (':');
1079 1171
1172 decode_ws (dec);
1080 value = decode_sv (dec); 1173 value = decode_sv (dec);
1081 if (!value) 1174 if (!value)
1082 goto fail; 1175 goto fail;
1083 1176
1084 hv_store (hv, key, len, value, 0); 1177 hv_store (hv, key, len, value, 0);
1100 1193
1101 if (*dec->cur != ',') 1194 if (*dec->cur != ',')
1102 ERR (", or } expected while parsing object/hash"); 1195 ERR (", or } expected while parsing object/hash");
1103 1196
1104 ++dec->cur; 1197 ++dec->cur;
1198
1199 decode_ws (dec);
1200
1201 if (*dec->cur == '}' && dec->json.flags & F_RELAXED)
1202 {
1203 ++dec->cur;
1204 break;
1205 }
1105 } 1206 }
1106 1207
1107 DEC_DEC_DEPTH; 1208 DEC_DEC_DEPTH;
1108 sv = newRV_noinc ((SV *)hv); 1209 sv = newRV_noinc ((SV *)hv);
1109 1210
1174} 1275}
1175 1276
1176static SV * 1277static SV *
1177decode_sv (dec_t *dec) 1278decode_sv (dec_t *dec)
1178{ 1279{
1179 decode_ws (dec);
1180
1181 // the beauty of JSON: you need exactly one character lookahead 1280 // the beauty of JSON: you need exactly one character lookahead
1182 // to parse anything. 1281 // to parse anything.
1183 switch (*dec->cur) 1282 switch (*dec->cur)
1184 { 1283 {
1185 case '"': ++dec->cur; return decode_str (dec); 1284 case '"': ++dec->cur; return decode_str (dec);
1193 1292
1194 case 't': 1293 case 't':
1195 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) 1294 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4))
1196 { 1295 {
1197 dec->cur += 4; 1296 dec->cur += 4;
1297#if JSON_SLOW
1298 json_true = get_sv ("JSON::XS::true", 1); SvREADONLY_on (json_true);
1299#endif
1198 return SvREFCNT_inc (json_true); 1300 return SvREFCNT_inc (json_true);
1199 } 1301 }
1200 else 1302 else
1201 ERR ("'true' expected"); 1303 ERR ("'true' expected");
1202 1304
1204 1306
1205 case 'f': 1307 case 'f':
1206 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) 1308 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5))
1207 { 1309 {
1208 dec->cur += 5; 1310 dec->cur += 5;
1311#if JSON_SLOW
1312 json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false);
1313#endif
1209 return SvREFCNT_inc (json_false); 1314 return SvREFCNT_inc (json_false);
1210 } 1315 }
1211 else 1316 else
1212 ERR ("'false' expected"); 1317 ERR ("'false' expected");
1213 1318
1263 1368
1264 if (dec.json.cb_object || dec.json.cb_sk_object) 1369 if (dec.json.cb_object || dec.json.cb_sk_object)
1265 dec.json.flags |= F_HOOK; 1370 dec.json.flags |= F_HOOK;
1266 1371
1267 *dec.end = 0; // this should basically be a nop, too, but make sure it's there 1372 *dec.end = 0; // this should basically be a nop, too, but make sure it's there
1373
1374 decode_ws (&dec);
1268 sv = decode_sv (&dec); 1375 sv = decode_sv (&dec);
1269 1376
1270 if (!(offset_return || !sv)) 1377 if (!(offset_return || !sv))
1271 { 1378 {
1272 // check for trailing garbage 1379 // check for trailing garbage
1340 json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false); 1447 json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false);
1341} 1448}
1342 1449
1343PROTOTYPES: DISABLE 1450PROTOTYPES: DISABLE
1344 1451
1452void CLONE (...)
1453 CODE:
1454 json_stash = 0;
1455 json_boolean_stash = 0;
1456
1345void new (char *klass) 1457void new (char *klass)
1346 PPCODE: 1458 PPCODE:
1347{ 1459{
1348 SV *pv = NEWSV (0, sizeof (JSON)); 1460 SV *pv = NEWSV (0, sizeof (JSON));
1349 SvPOK_only (pv); 1461 SvPOK_only (pv);
1350 Zero (SvPVX (pv), 1, JSON); 1462 Zero (SvPVX (pv), 1, JSON);
1351 ((JSON *)SvPVX (pv))->flags = F_DEFAULT; 1463 ((JSON *)SvPVX (pv))->flags = F_DEFAULT;
1352 XPUSHs (sv_2mortal (sv_bless (newRV_noinc (pv), json_stash))); 1464 XPUSHs (sv_2mortal (sv_bless (
1465 newRV_noinc (pv),
1466 strEQ (klass, "JSON::XS") ? JSON_STASH : gv_stashpv (klass, 1)
1467 )));
1353} 1468}
1354 1469
1355void ascii (JSON *self, int enable = 1) 1470void ascii (JSON *self, int enable = 1)
1356 ALIAS: 1471 ALIAS:
1357 ascii = F_ASCII 1472 ascii = F_ASCII
1364 pretty = F_PRETTY 1479 pretty = F_PRETTY
1365 allow_nonref = F_ALLOW_NONREF 1480 allow_nonref = F_ALLOW_NONREF
1366 shrink = F_SHRINK 1481 shrink = F_SHRINK
1367 allow_blessed = F_ALLOW_BLESSED 1482 allow_blessed = F_ALLOW_BLESSED
1368 convert_blessed = F_CONV_BLESSED 1483 convert_blessed = F_CONV_BLESSED
1484 relaxed = F_RELAXED
1369 PPCODE: 1485 PPCODE:
1370{ 1486{
1371 if (enable) 1487 if (enable)
1372 self->flags |= ix; 1488 self->flags |= ix;
1373 else 1489 else
1374 self->flags &= ~ix; 1490 self->flags &= ~ix;
1375 1491
1376 XPUSHs (ST (0)); 1492 XPUSHs (ST (0));
1377} 1493}
1378 1494
1495void get_ascii (JSON *self)
1496 ALIAS:
1497 get_ascii = F_ASCII
1498 get_latin1 = F_LATIN1
1499 get_utf8 = F_UTF8
1500 get_indent = F_INDENT
1501 get_canonical = F_CANONICAL
1502 get_space_before = F_SPACE_BEFORE
1503 get_space_after = F_SPACE_AFTER
1504 get_allow_nonref = F_ALLOW_NONREF
1505 get_shrink = F_SHRINK
1506 get_allow_blessed = F_ALLOW_BLESSED
1507 get_convert_blessed = F_CONV_BLESSED
1508 get_relaxed = F_RELAXED
1509 PPCODE:
1510 XPUSHs (boolSV (self->flags & ix));
1511
1379void max_depth (JSON *self, UV max_depth = 0x80000000UL) 1512void max_depth (JSON *self, UV max_depth = 0x80000000UL)
1380 PPCODE: 1513 PPCODE:
1381{ 1514{
1382 UV log2 = 0; 1515 UV log2 = 0;
1383 1516
1389 self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); 1522 self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH);
1390 1523
1391 XPUSHs (ST (0)); 1524 XPUSHs (ST (0));
1392} 1525}
1393 1526
1527U32 get_max_depth (JSON *self)
1528 CODE:
1529 RETVAL = DEC_DEPTH (self->flags);
1530 OUTPUT:
1531 RETVAL
1532
1394void max_size (JSON *self, UV max_size = 0) 1533void max_size (JSON *self, UV max_size = 0)
1395 PPCODE: 1534 PPCODE:
1396{ 1535{
1397 UV log2 = 0; 1536 UV log2 = 0;
1398 1537
1404 1543
1405 self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE); 1544 self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE);
1406 1545
1407 XPUSHs (ST (0)); 1546 XPUSHs (ST (0));
1408} 1547}
1548
1549int get_max_size (JSON *self)
1550 CODE:
1551 RETVAL = DEC_SIZE (self->flags);
1552 OUTPUT:
1553 RETVAL
1409 1554
1410void filter_json_object (JSON *self, SV *cb = &PL_sv_undef) 1555void filter_json_object (JSON *self, SV *cb = &PL_sv_undef)
1411 PPCODE: 1556 PPCODE:
1412{ 1557{
1413 SvREFCNT_dec (self->cb_object); 1558 SvREFCNT_dec (self->cb_object);
1460 SvREFCNT_dec (self->cb_sk_object); 1605 SvREFCNT_dec (self->cb_sk_object);
1461 SvREFCNT_dec (self->cb_object); 1606 SvREFCNT_dec (self->cb_object);
1462 1607
1463PROTOTYPES: ENABLE 1608PROTOTYPES: ENABLE
1464 1609
1465void to_json (SV *scalar) 1610void encode_json (SV *scalar)
1466 PPCODE: 1611 PPCODE:
1467{ 1612{
1468 JSON json = { F_DEFAULT | F_UTF8 }; 1613 JSON json = { F_DEFAULT | F_UTF8 };
1469 XPUSHs (encode_json (scalar, &json)); 1614 XPUSHs (encode_json (scalar, &json));
1470} 1615}
1471 1616
1472void from_json (SV *jsonstr) 1617void decode_json (SV *jsonstr)
1473 PPCODE: 1618 PPCODE:
1474{ 1619{
1475 JSON json = { F_DEFAULT | F_UTF8 }; 1620 JSON json = { F_DEFAULT | F_UTF8 };
1476 XPUSHs (decode_json (jsonstr, &json, 0)); 1621 XPUSHs (decode_json (jsonstr, &json, 0));
1477} 1622}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines