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.55 by root, Mon Jul 23 22:57:40 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 {
311 SV **svp = av_fetch (av, i, 0); 325 SV **svp = av_fetch (av, i, 0);
312 326
313 encode_indent (enc); 327 encode_indent (enc);
314 328
315 if (svp) 329 if (svp)
316 encode_sv (enc, *svp); 330 encode_sv (enc, *svp);
317 else 331 else
318 encode_str (enc, "null", 4, 0); 332 encode_str (enc, "null", 4, 0);
319 333
320 if (i < len) 334 if (i < len)
321 encode_comma (enc); 335 encode_comma (enc);
322 } 336 }
323 337
338 encode_nl (enc); --enc->indent; encode_indent (enc);
339 }
340
324 encode_nl (enc); 341 encode_ch (enc, ']');
325
326 --enc->indent;
327 encode_indent (enc); encode_ch (enc, ']');
328} 342}
329 343
330static void 344static void
331encode_he (enc_t *enc, HE *he) 345encode_hk (enc_t *enc, HE *he)
332{ 346{
333 encode_ch (enc, '"'); 347 encode_ch (enc, '"');
334 348
335 if (HeKLEN (he) == HEf_SVKEY) 349 if (HeKLEN (he) == HEf_SVKEY)
336 { 350 {
349 encode_ch (enc, '"'); 363 encode_ch (enc, '"');
350 364
351 if (enc->json.flags & F_SPACE_BEFORE) encode_space (enc); 365 if (enc->json.flags & F_SPACE_BEFORE) encode_space (enc);
352 encode_ch (enc, ':'); 366 encode_ch (enc, ':');
353 if (enc->json.flags & F_SPACE_AFTER ) encode_space (enc); 367 if (enc->json.flags & F_SPACE_AFTER ) encode_space (enc);
354 encode_sv (enc, HeVAL (he));
355} 368}
356 369
357// compare hash entries, used when all keys are bytestrings 370// compare hash entries, used when all keys are bytestrings
358static int 371static int
359he_cmp_fast (const void *a_, const void *b_) 372he_cmp_fast (const void *a_, const void *b_)
364 HE *b = *(HE **)b_; 377 HE *b = *(HE **)b_;
365 378
366 STRLEN la = HeKLEN (a); 379 STRLEN la = HeKLEN (a);
367 STRLEN lb = HeKLEN (b); 380 STRLEN lb = HeKLEN (b);
368 381
369 if (!(cmp = memcmp (HeKEY (a), HeKEY (b), la < lb ? la : lb))) 382 if (!(cmp = memcmp (HeKEY (b), HeKEY (a), lb < la ? lb : la)))
370 cmp = la - lb; 383 cmp = lb - la;
371 384
372 return cmp; 385 return cmp;
373} 386}
374 387
375// 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
376static int 389static int
377he_cmp_slow (const void *a, const void *b) 390he_cmp_slow (const void *a, const void *b)
378{ 391{
379 return sv_cmp (HeSVKEY_force (*(HE **)a), HeSVKEY_force (*(HE **)b)); 392 return sv_cmp (HeSVKEY_force (*(HE **)b), HeSVKEY_force (*(HE **)a));
380} 393}
381 394
382static void 395static void
383encode_hv (enc_t *enc, HV *hv) 396encode_hv (enc_t *enc, HV *hv)
384{ 397{
398 HE *he;
385 int count, i; 399 int count;
386 400
387 if (enc->indent >= enc->maxdepth) 401 if (enc->indent >= enc->maxdepth)
388 croak ("data structure too deep (hit recursion limit)"); 402 croak ("data structure too deep (hit recursion limit)");
389 403
390 encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; 404 encode_ch (enc, '{');
391 405
392 if ((count = hv_iterinit (hv)))
393 {
394 // for canonical output we have to sort by keys first 406 // for canonical output we have to sort by keys first
395 // actually, this is mostly due to the stupid so-called 407 // actually, this is mostly due to the stupid so-called
396 // security workaround added somewhere in 5.8.x. 408 // security workaround added somewhere in 5.8.x.
397 // that randomises hash orderings 409 // that randomises hash orderings
398 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))
399 { 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 {
400 int fast = 1; 430 int i, fast = 1;
401 HE *he;
402#if defined(__BORLANDC__) || defined(_MSC_VER) 431#if defined(__BORLANDC__) || defined(_MSC_VER)
403 HE **hes = _alloca (count * sizeof (HE)); 432 HE **hes = _alloca (count * sizeof (HE));
404#else 433#else
405 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
406#endif 435#endif
433 462
434 FREETMPS; 463 FREETMPS;
435 LEAVE; 464 LEAVE;
436 } 465 }
437 466
438 for (i = 0; i < count; ++i) 467 encode_nl (enc); ++enc->indent;
468
469 while (count--)
439 { 470 {
440 encode_indent (enc); 471 encode_indent (enc);
472 he = hes [count];
441 encode_he (enc, hes [i]); 473 encode_hk (enc, he);
474 encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he));
442 475
443 if (i < count - 1) 476 if (count)
444 encode_comma (enc); 477 encode_comma (enc);
445 } 478 }
446 479
447 encode_nl (enc); 480 encode_nl (enc); --enc->indent; encode_indent (enc);
448 } 481 }
482 }
449 else 483 else
484 {
485 if (hv_iterinit (hv) || SvMAGICAL (hv))
486 if ((he = hv_iternext (hv)))
450 { 487 {
451 HE *he = hv_iternext (hv); 488 encode_nl (enc); ++enc->indent;
452 489
453 for (;;) 490 for (;;)
454 { 491 {
455 encode_indent (enc); 492 encode_indent (enc);
456 encode_he (enc, he); 493 encode_hk (enc, he);
494 encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he));
457 495
458 if (!(he = hv_iternext (hv))) 496 if (!(he = hv_iternext (hv)))
459 break; 497 break;
460 498
461 encode_comma (enc); 499 encode_comma (enc);
462 } 500 }
463 501
464 encode_nl (enc); 502 encode_nl (enc); --enc->indent; encode_indent (enc);
465 } 503 }
466 } 504 }
467 505
468 --enc->indent; encode_indent (enc); encode_ch (enc, '}'); 506 encode_ch (enc, '}');
469} 507}
470 508
471// encode objects, arrays and special \0=false and \1=true values. 509// encode objects, arrays and special \0=false and \1=true values.
472static void 510static void
473encode_rv (enc_t *enc, SV *sv) 511encode_rv (enc_t *enc, SV *sv)
477 SvGETMAGIC (sv); 515 SvGETMAGIC (sv);
478 svt = SvTYPE (sv); 516 svt = SvTYPE (sv);
479 517
480 if (expect_false (SvOBJECT (sv))) 518 if (expect_false (SvOBJECT (sv)))
481 { 519 {
520 HV *stash = !JSON_SLOW || json_boolean_stash
521 ? json_boolean_stash
522 : gv_stashpv ("JSON::XS::Boolean", 1);
523
482 if (SvSTASH (sv) == json_boolean_stash) 524 if (SvSTASH (sv) == stash)
483 { 525 {
484 if (SvIV (sv)) 526 if (SvIV (sv))
485 encode_str (enc, "true", 4, 0); 527 encode_str (enc, "true", 4, 0);
486 else 528 else
487 encode_str (enc, "false", 5, 0); 529 encode_str (enc, "false", 5, 0);
499 // we re-bless the reference to get overload and other niceties right 541 // we re-bless the reference to get overload and other niceties right
500 GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 0); 542 GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 0);
501 543
502 if (to_json) 544 if (to_json)
503 { 545 {
504 int count;
505 dSP; 546 dSP;
506 547
507 ENTER; SAVETMPS; PUSHMARK (SP); 548 ENTER; SAVETMPS; PUSHMARK (SP);
508 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); 549 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv)));
509 550
670 U32 depth; // recursion depth 711 U32 depth; // recursion depth
671 U32 maxdepth; // recursion depth limit 712 U32 maxdepth; // recursion depth limit
672} dec_t; 713} dec_t;
673 714
674inline 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
675decode_ws (dec_t *dec) 725decode_ws (dec_t *dec)
676{ 726{
677 for (;;) 727 for (;;)
678 { 728 {
679 char ch = *dec->cur; 729 char ch = *dec->cur;
680 730
681 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 }
682 || (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)) 743 else if (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)
683 break; 744 break; // parse error, but let higher level handle it, gives better error messages
684 745
685 ++dec->cur; 746 ++dec->cur;
686 } 747 }
687} 748}
688 749
937 is_nv = 1; 998 is_nv = 1;
938 } 999 }
939 1000
940 if (!is_nv) 1001 if (!is_nv)
941 { 1002 {
1003 int len = dec->cur - start;
1004
942 // 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
943 if (*start == '-') 1006 if (*start == '-')
944 switch (dec->cur - start) 1007 switch (len)
945 { 1008 {
946 case 2: return newSViv (-( start [1] - '0' * 1)); 1009 case 2: return newSViv (-( start [1] - '0' * 1));
947 case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); 1010 case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11));
948 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));
949 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));
950 } 1014 }
951 else 1015 else
952 switch (dec->cur - start) 1016 switch (len)
953 { 1017 {
954 case 1: return newSViv ( start [0] - '0' * 1); 1018 case 1: return newSViv ( start [0] - '0' * 1);
955 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); 1019 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11);
956 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);
957 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);
958 } 1023 }
959 1024
960 { 1025 {
961 UV uv; 1026 UV uv;
962 int numtype = grok_number (start, dec->cur - start, &uv); 1027 int numtype = grok_number (start, len, &uv);
963 if (numtype & IS_NUMBER_IN_UV) 1028 if (numtype & IS_NUMBER_IN_UV)
964 if (numtype & IS_NUMBER_NEG) 1029 if (numtype & IS_NUMBER_NEG)
965 { 1030 {
966 if (uv < (UV)IV_MIN) 1031 if (uv < (UV)IV_MIN)
967 return newSViv (-(IV)uv); 1032 return newSViv (-(IV)uv);
968 } 1033 }
969 else 1034 else
970 return newSVuv (uv); 1035 return newSVuv (uv);
971
972 // here would likely be the place for bigint support
973 } 1036 }
974 }
975 1037
976 // 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
977 return newSVnv (Atof (start)); 1054 return newSVnv (Atof (start));
978 1055
979fail: 1056fail:
980 return 0; 1057 return 0;
981} 1058}
1011 1088
1012 if (*dec->cur != ',') 1089 if (*dec->cur != ',')
1013 ERR (", or ] expected while parsing array"); 1090 ERR (", or ] expected while parsing array");
1014 1091
1015 ++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 }
1016 } 1101 }
1017 1102
1018 DEC_DEC_DEPTH; 1103 DEC_DEC_DEPTH;
1019 return newRV_noinc ((SV *)av); 1104 return newRV_noinc ((SV *)av);
1020 1105
1036 if (*dec->cur == '}') 1121 if (*dec->cur == '}')
1037 ++dec->cur; 1122 ++dec->cur;
1038 else 1123 else
1039 for (;;) 1124 for (;;)
1040 { 1125 {
1041 decode_ws (dec); EXPECT_CH ('"'); 1126 EXPECT_CH ('"');
1042 1127
1043 // heuristic: assume that 1128 // heuristic: assume that
1044 // a) decode_str + hv_store_ent are abysmally slow. 1129 // a) decode_str + hv_store_ent are abysmally slow.
1045 // b) most hash keys are short, simple ascii text. 1130 // b) most hash keys are short, simple ascii text.
1046 // => try to "fast-match" such strings to avoid 1131 // => try to "fast-match" such strings to avoid
1060 if (!key) 1145 if (!key)
1061 goto fail; 1146 goto fail;
1062 1147
1063 decode_ws (dec); EXPECT_CH (':'); 1148 decode_ws (dec); EXPECT_CH (':');
1064 1149
1150 decode_ws (dec);
1065 value = decode_sv (dec); 1151 value = decode_sv (dec);
1066 if (!value) 1152 if (!value)
1067 { 1153 {
1068 SvREFCNT_dec (key); 1154 SvREFCNT_dec (key);
1069 goto fail; 1155 goto fail;
1081 int len = p - key; 1167 int len = p - key;
1082 dec->cur = p + 1; 1168 dec->cur = p + 1;
1083 1169
1084 decode_ws (dec); EXPECT_CH (':'); 1170 decode_ws (dec); EXPECT_CH (':');
1085 1171
1172 decode_ws (dec);
1086 value = decode_sv (dec); 1173 value = decode_sv (dec);
1087 if (!value) 1174 if (!value)
1088 goto fail; 1175 goto fail;
1089 1176
1090 hv_store (hv, key, len, value, 0); 1177 hv_store (hv, key, len, value, 0);
1106 1193
1107 if (*dec->cur != ',') 1194 if (*dec->cur != ',')
1108 ERR (", or } expected while parsing object/hash"); 1195 ERR (", or } expected while parsing object/hash");
1109 1196
1110 ++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 }
1111 } 1206 }
1112 1207
1113 DEC_DEC_DEPTH; 1208 DEC_DEC_DEPTH;
1114 sv = newRV_noinc ((SV *)hv); 1209 sv = newRV_noinc ((SV *)hv);
1115 1210
1180} 1275}
1181 1276
1182static SV * 1277static SV *
1183decode_sv (dec_t *dec) 1278decode_sv (dec_t *dec)
1184{ 1279{
1185 decode_ws (dec);
1186
1187 // the beauty of JSON: you need exactly one character lookahead 1280 // the beauty of JSON: you need exactly one character lookahead
1188 // to parse anything. 1281 // to parse anything.
1189 switch (*dec->cur) 1282 switch (*dec->cur)
1190 { 1283 {
1191 case '"': ++dec->cur; return decode_str (dec); 1284 case '"': ++dec->cur; return decode_str (dec);
1199 1292
1200 case 't': 1293 case 't':
1201 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) 1294 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4))
1202 { 1295 {
1203 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
1204 return SvREFCNT_inc (json_true); 1300 return SvREFCNT_inc (json_true);
1205 } 1301 }
1206 else 1302 else
1207 ERR ("'true' expected"); 1303 ERR ("'true' expected");
1208 1304
1210 1306
1211 case 'f': 1307 case 'f':
1212 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) 1308 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5))
1213 { 1309 {
1214 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
1215 return SvREFCNT_inc (json_false); 1314 return SvREFCNT_inc (json_false);
1216 } 1315 }
1217 else 1316 else
1218 ERR ("'false' expected"); 1317 ERR ("'false' expected");
1219 1318
1269 1368
1270 if (dec.json.cb_object || dec.json.cb_sk_object) 1369 if (dec.json.cb_object || dec.json.cb_sk_object)
1271 dec.json.flags |= F_HOOK; 1370 dec.json.flags |= F_HOOK;
1272 1371
1273 *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);
1274 sv = decode_sv (&dec); 1375 sv = decode_sv (&dec);
1275 1376
1276 if (!(offset_return || !sv)) 1377 if (!(offset_return || !sv))
1277 { 1378 {
1278 // check for trailing garbage 1379 // check for trailing garbage
1346 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);
1347} 1448}
1348 1449
1349PROTOTYPES: DISABLE 1450PROTOTYPES: DISABLE
1350 1451
1452void CLONE (...)
1453 CODE:
1454 json_stash = 0;
1455 json_boolean_stash = 0;
1456
1351void new (char *klass) 1457void new (char *klass)
1352 PPCODE: 1458 PPCODE:
1353{ 1459{
1354 SV *pv = NEWSV (0, sizeof (JSON)); 1460 SV *pv = NEWSV (0, sizeof (JSON));
1355 SvPOK_only (pv); 1461 SvPOK_only (pv);
1356 Zero (SvPVX (pv), 1, JSON); 1462 Zero (SvPVX (pv), 1, JSON);
1357 ((JSON *)SvPVX (pv))->flags = F_DEFAULT; 1463 ((JSON *)SvPVX (pv))->flags = F_DEFAULT;
1358 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 )));
1359} 1468}
1360 1469
1361void ascii (JSON *self, int enable = 1) 1470void ascii (JSON *self, int enable = 1)
1362 ALIAS: 1471 ALIAS:
1363 ascii = F_ASCII 1472 ascii = F_ASCII
1370 pretty = F_PRETTY 1479 pretty = F_PRETTY
1371 allow_nonref = F_ALLOW_NONREF 1480 allow_nonref = F_ALLOW_NONREF
1372 shrink = F_SHRINK 1481 shrink = F_SHRINK
1373 allow_blessed = F_ALLOW_BLESSED 1482 allow_blessed = F_ALLOW_BLESSED
1374 convert_blessed = F_CONV_BLESSED 1483 convert_blessed = F_CONV_BLESSED
1484 relaxed = F_RELAXED
1375 PPCODE: 1485 PPCODE:
1376{ 1486{
1377 if (enable) 1487 if (enable)
1378 self->flags |= ix; 1488 self->flags |= ix;
1379 else 1489 else
1380 self->flags &= ~ix; 1490 self->flags &= ~ix;
1381 1491
1382 XPUSHs (ST (0)); 1492 XPUSHs (ST (0));
1383} 1493}
1384 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
1385void max_depth (JSON *self, UV max_depth = 0x80000000UL) 1512void max_depth (JSON *self, UV max_depth = 0x80000000UL)
1386 PPCODE: 1513 PPCODE:
1387{ 1514{
1388 UV log2 = 0; 1515 UV log2 = 0;
1389 1516
1395 self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); 1522 self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH);
1396 1523
1397 XPUSHs (ST (0)); 1524 XPUSHs (ST (0));
1398} 1525}
1399 1526
1527U32 get_max_depth (JSON *self)
1528 CODE:
1529 RETVAL = DEC_DEPTH (self->flags);
1530 OUTPUT:
1531 RETVAL
1532
1400void max_size (JSON *self, UV max_size = 0) 1533void max_size (JSON *self, UV max_size = 0)
1401 PPCODE: 1534 PPCODE:
1402{ 1535{
1403 UV log2 = 0; 1536 UV log2 = 0;
1404 1537
1410 1543
1411 self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE); 1544 self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE);
1412 1545
1413 XPUSHs (ST (0)); 1546 XPUSHs (ST (0));
1414} 1547}
1548
1549int get_max_size (JSON *self)
1550 CODE:
1551 RETVAL = DEC_SIZE (self->flags);
1552 OUTPUT:
1553 RETVAL
1415 1554
1416void filter_json_object (JSON *self, SV *cb = &PL_sv_undef) 1555void filter_json_object (JSON *self, SV *cb = &PL_sv_undef)
1417 PPCODE: 1556 PPCODE:
1418{ 1557{
1419 SvREFCNT_dec (self->cb_object); 1558 SvREFCNT_dec (self->cb_object);
1466 SvREFCNT_dec (self->cb_sk_object); 1605 SvREFCNT_dec (self->cb_sk_object);
1467 SvREFCNT_dec (self->cb_object); 1606 SvREFCNT_dec (self->cb_object);
1468 1607
1469PROTOTYPES: ENABLE 1608PROTOTYPES: ENABLE
1470 1609
1471void to_json (SV *scalar) 1610void encode_json (SV *scalar)
1472 PPCODE: 1611 PPCODE:
1473{ 1612{
1474 JSON json = { F_DEFAULT | F_UTF8 }; 1613 JSON json = { F_DEFAULT | F_UTF8 };
1475 XPUSHs (encode_json (scalar, &json)); 1614 XPUSHs (encode_json (scalar, &json));
1476} 1615}
1477 1616
1478void from_json (SV *jsonstr) 1617void decode_json (SV *jsonstr)
1479 PPCODE: 1618 PPCODE:
1480{ 1619{
1481 JSON json = { F_DEFAULT | F_UTF8 }; 1620 JSON json = { F_DEFAULT | F_UTF8 };
1482 XPUSHs (decode_json (jsonstr, &json, 0)); 1621 XPUSHs (decode_json (jsonstr, &json, 0));
1483} 1622}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines