… | |
… | |
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 | |
8 | |
9 | #define F_ASCII 0x00000001 |
9 | #define F_ASCII 0x00000001UL |
10 | #define F_UTF8 0x00000002 |
10 | #define F_UTF8 0x00000002UL |
11 | #define F_INDENT 0x00000004 |
11 | #define F_INDENT 0x00000004UL |
12 | #define F_CANONICAL 0x00000008 |
12 | #define F_CANONICAL 0x00000008UL |
13 | #define F_SPACE_BEFORE 0x00000010 |
13 | #define F_SPACE_BEFORE 0x00000010UL |
14 | #define F_SPACE_AFTER 0x00000020 |
14 | #define F_SPACE_AFTER 0x00000020UL |
15 | #define F_ALLOW_NONREF 0x00000080 |
15 | #define F_ALLOW_NONREF 0x00000080UL |
16 | #define F_SHRINK 0x00000100 |
16 | #define F_SHRINK 0x00000100UL |
|
|
17 | #define F_MAXDEPTH 0xf8000000UL |
|
|
18 | #define S_MAXDEPTH 27 |
17 | |
19 | |
18 | // F_SKIPINVALID? |
20 | #define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) |
19 | // F_EXECCODEREF? |
21 | |
20 | // F_SELFCONVERT? |
22 | // F_SELFCONVERT? <=> to_json/toJson |
|
|
23 | // F_BLESSED? <=> { $__class__$ => } |
21 | |
24 | |
22 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
25 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
23 | #define F_DEFAULT 0 |
26 | #define F_DEFAULT (12UL << S_MAXDEPTH) |
24 | |
27 | |
25 | #define INIT_SIZE 32 // initial scalar size to be allocated |
28 | #define INIT_SIZE 32 // initial scalar size to be allocated |
26 | #define INDENT_STEP 3 // spaces per indentation level |
29 | #define INDENT_STEP 3 // spaces per indentation level |
27 | |
30 | |
28 | #define UTF8_MAX_LEN 11 // for perls UTF-X: max. number of octets per character |
31 | #define UTF8_MAX_LEN 11 // for perls UTF-X: max. number of octets per character |
… | |
… | |
85 | typedef struct |
88 | typedef struct |
86 | { |
89 | { |
87 | char *cur; // SvPVX (sv) + current output position |
90 | char *cur; // SvPVX (sv) + current output position |
88 | char *end; // SvEND (sv) |
91 | char *end; // SvEND (sv) |
89 | SV *sv; // result scalar |
92 | SV *sv; // result scalar |
90 | UV flags; // F_* |
93 | U32 flags; // F_* |
91 | int indent; // indentation level |
94 | U32 indent; // indentation level |
92 | int max_depth; // max. recursion level |
95 | U32 maxdepth; // max. indentation/recursion level |
93 | } enc_t; |
96 | } enc_t; |
94 | |
97 | |
95 | static void |
98 | static void |
96 | need (enc_t *enc, STRLEN len) |
99 | need (enc_t *enc, STRLEN len) |
97 | { |
100 | { |
… | |
… | |
265 | static void |
268 | static void |
266 | encode_av (enc_t *enc, AV *av) |
269 | encode_av (enc_t *enc, AV *av) |
267 | { |
270 | { |
268 | int i, len = av_len (av); |
271 | int i, len = av_len (av); |
269 | |
272 | |
|
|
273 | if (enc->indent >= enc->maxdepth) |
|
|
274 | croak ("data structure too deep (hit recursion limit)"); |
|
|
275 | |
270 | encode_ch (enc, '['); encode_nl (enc); |
276 | encode_ch (enc, '['); encode_nl (enc); |
271 | ++enc->indent; |
277 | ++enc->indent; |
272 | |
278 | |
273 | for (i = 0; i <= len; ++i) |
279 | for (i = 0; i <= len; ++i) |
274 | { |
280 | { |
… | |
… | |
339 | |
345 | |
340 | static void |
346 | static void |
341 | encode_hv (enc_t *enc, HV *hv) |
347 | encode_hv (enc_t *enc, HV *hv) |
342 | { |
348 | { |
343 | int count, i; |
349 | int count, i; |
|
|
350 | |
|
|
351 | if (enc->indent >= enc->maxdepth) |
|
|
352 | croak ("data structure too deep (hit recursion limit)"); |
344 | |
353 | |
345 | encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; |
354 | encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; |
346 | |
355 | |
347 | if ((count = hv_iterinit (hv))) |
356 | if ((count = hv_iterinit (hv))) |
348 | { |
357 | { |
… | |
… | |
417 | } |
426 | } |
418 | |
427 | |
419 | --enc->indent; encode_indent (enc); encode_ch (enc, '}'); |
428 | --enc->indent; encode_indent (enc); encode_ch (enc, '}'); |
420 | } |
429 | } |
421 | |
430 | |
|
|
431 | // encode objects, arrays and special \0=false and \1=true values. |
|
|
432 | static void |
|
|
433 | encode_rv (enc_t *enc, SV *sv) |
|
|
434 | { |
|
|
435 | SvGETMAGIC (sv); |
|
|
436 | |
|
|
437 | svtype svt = SvTYPE (sv); |
|
|
438 | |
|
|
439 | if (svt == SVt_PVHV) |
|
|
440 | encode_hv (enc, (HV *)sv); |
|
|
441 | else if (svt == SVt_PVAV) |
|
|
442 | encode_av (enc, (AV *)sv); |
|
|
443 | else if (svt < SVt_PVAV) |
|
|
444 | { |
|
|
445 | if (SvNIOK (sv) && SvIV (sv) == 0) |
|
|
446 | encode_str (enc, "false", 5, 0); |
|
|
447 | else if (SvNIOK (sv) && SvIV (sv) == 1) |
|
|
448 | encode_str (enc, "true", 4, 0); |
|
|
449 | else |
|
|
450 | croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", |
|
|
451 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
|
|
452 | } |
|
|
453 | else |
|
|
454 | croak ("encountered %s, but JSON can only represent references to arrays or hashes", |
|
|
455 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
|
|
456 | } |
|
|
457 | |
422 | static void |
458 | static void |
423 | encode_sv (enc_t *enc, SV *sv) |
459 | encode_sv (enc_t *enc, SV *sv) |
424 | { |
460 | { |
425 | SvGETMAGIC (sv); |
461 | SvGETMAGIC (sv); |
426 | |
462 | |
… | |
… | |
445 | SvIsUV(sv) |
481 | SvIsUV(sv) |
446 | ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv)) |
482 | ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv)) |
447 | : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv)); |
483 | : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv)); |
448 | } |
484 | } |
449 | else if (SvROK (sv)) |
485 | else if (SvROK (sv)) |
450 | { |
486 | encode_rv (enc, SvRV (sv)); |
451 | SV *rv = SvRV (sv); |
|
|
452 | |
|
|
453 | if (enc->indent >= enc->max_depth) |
|
|
454 | croak ("data structure too deep (hit recursion limit)"); |
|
|
455 | |
|
|
456 | switch (SvTYPE (rv)) |
|
|
457 | { |
|
|
458 | case SVt_PVAV: encode_av (enc, (AV *)rv); break; |
|
|
459 | case SVt_PVHV: encode_hv (enc, (HV *)rv); break; |
|
|
460 | |
|
|
461 | default: |
|
|
462 | croak ("encountered %s, but JSON can only represent references to arrays or hashes", |
|
|
463 | SvPV_nolen (sv)); |
|
|
464 | } |
|
|
465 | } |
|
|
466 | else if (!SvOK (sv)) |
487 | else if (!SvOK (sv)) |
467 | encode_str (enc, "null", 4, 0); |
488 | encode_str (enc, "null", 4, 0); |
468 | else |
489 | else |
469 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
490 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
470 | SvPV_nolen (sv), SvFLAGS (sv)); |
491 | SvPV_nolen (sv), SvFLAGS (sv)); |
471 | } |
492 | } |
472 | |
493 | |
473 | static SV * |
494 | static SV * |
474 | encode_json (SV *scalar, UV flags) |
495 | encode_json (SV *scalar, U32 flags) |
475 | { |
496 | { |
476 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
497 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
477 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
498 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
478 | |
499 | |
479 | enc_t enc; |
500 | enc_t enc; |
480 | enc.flags = flags; |
501 | enc.flags = flags; |
481 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
502 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
482 | enc.cur = SvPVX (enc.sv); |
503 | enc.cur = SvPVX (enc.sv); |
483 | enc.end = SvEND (enc.sv); |
504 | enc.end = SvEND (enc.sv); |
484 | enc.indent = 0; |
505 | enc.indent = 0; |
485 | enc.max_depth = 0x7fffffffUL; |
506 | enc.maxdepth = DEC_DEPTH (flags); |
486 | |
507 | |
487 | SvPOK_only (enc.sv); |
508 | SvPOK_only (enc.sv); |
488 | encode_sv (&enc, scalar); |
509 | encode_sv (&enc, scalar); |
489 | |
510 | |
490 | if (!(flags & (F_ASCII | F_UTF8))) |
511 | if (!(flags & (F_ASCII | F_UTF8))) |
… | |
… | |
505 | typedef struct |
526 | typedef struct |
506 | { |
527 | { |
507 | char *cur; // current parser pointer |
528 | char *cur; // current parser pointer |
508 | char *end; // end of input string |
529 | char *end; // end of input string |
509 | const char *err; // parse error, if != 0 |
530 | const char *err; // parse error, if != 0 |
510 | UV flags; // F_* |
531 | U32 flags; // F_* |
|
|
532 | U32 depth; // recursion depth |
|
|
533 | U32 maxdepth; // recursion depth limit |
511 | } dec_t; |
534 | } dec_t; |
512 | |
535 | |
513 | static void |
536 | static void |
514 | decode_ws (dec_t *dec) |
537 | decode_ws (dec_t *dec) |
515 | { |
538 | { |
… | |
… | |
524 | ++dec->cur; |
547 | ++dec->cur; |
525 | } |
548 | } |
526 | } |
549 | } |
527 | |
550 | |
528 | #define ERR(reason) SB dec->err = reason; goto fail; SE |
551 | #define ERR(reason) SB dec->err = reason; goto fail; SE |
|
|
552 | |
529 | #define EXPECT_CH(ch) SB \ |
553 | #define EXPECT_CH(ch) SB \ |
530 | if (*dec->cur != ch) \ |
554 | if (*dec->cur != ch) \ |
531 | ERR (# ch " expected"); \ |
555 | ERR (# ch " expected"); \ |
532 | ++dec->cur; \ |
556 | ++dec->cur; \ |
533 | SE |
557 | SE |
|
|
558 | |
|
|
559 | #define DEC_INC_DEPTH if (++dec->depth > dec->maxdepth) ERR ("json datastructure exceeds maximum nesting level (set a higher max_depth)") |
|
|
560 | #define DEC_DEC_DEPTH --dec->depth |
534 | |
561 | |
535 | static SV *decode_sv (dec_t *dec); |
562 | static SV *decode_sv (dec_t *dec); |
536 | |
563 | |
537 | static signed char decode_hexdigit[256]; |
564 | static signed char decode_hexdigit[256]; |
538 | |
565 | |
… | |
… | |
782 | static SV * |
809 | static SV * |
783 | decode_av (dec_t *dec) |
810 | decode_av (dec_t *dec) |
784 | { |
811 | { |
785 | AV *av = newAV (); |
812 | AV *av = newAV (); |
786 | |
813 | |
|
|
814 | DEC_INC_DEPTH; |
787 | decode_ws (dec); |
815 | decode_ws (dec); |
|
|
816 | |
788 | if (*dec->cur == ']') |
817 | if (*dec->cur == ']') |
789 | ++dec->cur; |
818 | ++dec->cur; |
790 | else |
819 | else |
791 | for (;;) |
820 | for (;;) |
792 | { |
821 | { |
… | |
… | |
810 | ERR (", or ] expected while parsing array"); |
839 | ERR (", or ] expected while parsing array"); |
811 | |
840 | |
812 | ++dec->cur; |
841 | ++dec->cur; |
813 | } |
842 | } |
814 | |
843 | |
|
|
844 | DEC_DEC_DEPTH; |
815 | return newRV_noinc ((SV *)av); |
845 | return newRV_noinc ((SV *)av); |
816 | |
846 | |
817 | fail: |
847 | fail: |
818 | SvREFCNT_dec (av); |
848 | SvREFCNT_dec (av); |
|
|
849 | DEC_DEC_DEPTH; |
819 | return 0; |
850 | return 0; |
820 | } |
851 | } |
821 | |
852 | |
822 | static SV * |
853 | static SV * |
823 | decode_hv (dec_t *dec) |
854 | decode_hv (dec_t *dec) |
824 | { |
855 | { |
825 | HV *hv = newHV (); |
856 | HV *hv = newHV (); |
826 | |
857 | |
|
|
858 | DEC_INC_DEPTH; |
827 | decode_ws (dec); |
859 | decode_ws (dec); |
|
|
860 | |
828 | if (*dec->cur == '}') |
861 | if (*dec->cur == '}') |
829 | ++dec->cur; |
862 | ++dec->cur; |
830 | else |
863 | else |
831 | for (;;) |
864 | for (;;) |
832 | { |
865 | { |
… | |
… | |
845 | { |
878 | { |
846 | SvREFCNT_dec (key); |
879 | SvREFCNT_dec (key); |
847 | goto fail; |
880 | goto fail; |
848 | } |
881 | } |
849 | |
882 | |
850 | //TODO: optimise |
|
|
851 | hv_store_ent (hv, key, value, 0); |
883 | hv_store_ent (hv, key, value, 0); |
|
|
884 | SvREFCNT_dec (key); |
852 | |
885 | |
853 | decode_ws (dec); |
886 | decode_ws (dec); |
854 | |
887 | |
855 | if (*dec->cur == '}') |
888 | if (*dec->cur == '}') |
856 | { |
889 | { |
… | |
… | |
862 | ERR (", or } expected while parsing object/hash"); |
895 | ERR (", or } expected while parsing object/hash"); |
863 | |
896 | |
864 | ++dec->cur; |
897 | ++dec->cur; |
865 | } |
898 | } |
866 | |
899 | |
|
|
900 | DEC_DEC_DEPTH; |
867 | return newRV_noinc ((SV *)hv); |
901 | return newRV_noinc ((SV *)hv); |
868 | |
902 | |
869 | fail: |
903 | fail: |
870 | SvREFCNT_dec (hv); |
904 | SvREFCNT_dec (hv); |
|
|
905 | DEC_DEC_DEPTH; |
871 | return 0; |
906 | return 0; |
872 | } |
907 | } |
873 | |
908 | |
874 | static SV * |
909 | static SV * |
875 | decode_sv (dec_t *dec) |
910 | decode_sv (dec_t *dec) |
… | |
… | |
927 | fail: |
962 | fail: |
928 | return 0; |
963 | return 0; |
929 | } |
964 | } |
930 | |
965 | |
931 | static SV * |
966 | static SV * |
932 | decode_json (SV *string, UV flags) |
967 | decode_json (SV *string, U32 flags) |
933 | { |
968 | { |
934 | SV *sv; |
969 | SV *sv; |
|
|
970 | |
|
|
971 | SvUPGRADE (string, SVt_PV); |
935 | |
972 | |
936 | if (flags & F_UTF8) |
973 | if (flags & F_UTF8) |
937 | sv_utf8_downgrade (string, 0); |
974 | sv_utf8_downgrade (string, 0); |
938 | else |
975 | else |
939 | sv_utf8_upgrade (string); |
976 | sv_utf8_upgrade (string); |
940 | |
977 | |
941 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
978 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
942 | |
979 | |
943 | dec_t dec; |
980 | dec_t dec; |
944 | dec.flags = flags; |
981 | dec.flags = flags; |
945 | dec.cur = SvPVX (string); |
982 | dec.cur = SvPVX (string); |
946 | dec.end = SvEND (string); |
983 | dec.end = SvEND (string); |
947 | dec.err = 0; |
984 | dec.err = 0; |
|
|
985 | dec.depth = 0; |
|
|
986 | dec.maxdepth = DEC_DEPTH (dec.flags); |
948 | |
987 | |
|
|
988 | *dec.end = 0; // this should basically be a nop, too, but make sure its there |
949 | sv = decode_sv (&dec); |
989 | sv = decode_sv (&dec); |
950 | |
990 | |
951 | if (!sv) |
991 | if (!sv) |
952 | { |
992 | { |
953 | IV offset = dec.flags & F_UTF8 |
993 | IV offset = dec.flags & F_UTF8 |
… | |
… | |
986 | BOOT: |
1026 | BOOT: |
987 | { |
1027 | { |
988 | int i; |
1028 | int i; |
989 | |
1029 | |
990 | memset (decode_hexdigit, 0xff, 256); |
1030 | memset (decode_hexdigit, 0xff, 256); |
|
|
1031 | |
991 | for (i = 10; i--; ) |
1032 | for (i = 0; i < 256; ++i) |
992 | decode_hexdigit ['0' + i] = i; |
1033 | decode_hexdigit [i] = |
993 | |
1034 | i >= '0' && i <= '9' ? i - '0' |
994 | for (i = 7; i--; ) |
1035 | : i >= 'a' && i <= 'f' ? i - 'a' + 10 |
995 | { |
1036 | : i >= 'A' && i <= 'F' ? i - 'A' + 10 |
996 | decode_hexdigit ['a' + i] = 10 + i; |
1037 | : -1; |
997 | decode_hexdigit ['A' + i] = 10 + i; |
|
|
998 | } |
|
|
999 | |
1038 | |
1000 | json_stash = gv_stashpv ("JSON::XS", 1); |
1039 | json_stash = gv_stashpv ("JSON::XS", 1); |
1001 | } |
1040 | } |
1002 | |
1041 | |
1003 | PROTOTYPES: DISABLE |
1042 | PROTOTYPES: DISABLE |
… | |
… | |
1030 | RETVAL = newSVsv (self); |
1069 | RETVAL = newSVsv (self); |
1031 | } |
1070 | } |
1032 | OUTPUT: |
1071 | OUTPUT: |
1033 | RETVAL |
1072 | RETVAL |
1034 | |
1073 | |
|
|
1074 | SV *max_depth (SV *self, int max_depth = 0x80000000UL) |
|
|
1075 | CODE: |
|
|
1076 | { |
|
|
1077 | UV *uv = SvJSON (self); |
|
|
1078 | UV log2 = 0; |
|
|
1079 | |
|
|
1080 | if (max_depth > 0x80000000UL) max_depth = 0x80000000UL; |
|
|
1081 | |
|
|
1082 | while ((1UL << log2) < max_depth) |
|
|
1083 | ++log2; |
|
|
1084 | |
|
|
1085 | *uv = *uv & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); |
|
|
1086 | |
|
|
1087 | RETVAL = newSVsv (self); |
|
|
1088 | } |
|
|
1089 | OUTPUT: |
|
|
1090 | RETVAL |
|
|
1091 | |
1035 | void encode (SV *self, SV *scalar) |
1092 | void encode (SV *self, SV *scalar) |
1036 | PPCODE: |
1093 | PPCODE: |
1037 | XPUSHs (encode_json (scalar, *SvJSON (self))); |
1094 | XPUSHs (encode_json (scalar, *SvJSON (self))); |
1038 | |
1095 | |
1039 | void decode (SV *self, SV *jsonstr) |
1096 | void decode (SV *self, SV *jsonstr) |
… | |
… | |
1044 | |
1101 | |
1045 | void to_json (SV *scalar) |
1102 | void to_json (SV *scalar) |
1046 | ALIAS: |
1103 | ALIAS: |
1047 | objToJson = 0 |
1104 | objToJson = 0 |
1048 | PPCODE: |
1105 | PPCODE: |
1049 | XPUSHs (encode_json (scalar, F_UTF8)); |
1106 | XPUSHs (encode_json (scalar, F_DEFAULT | F_UTF8)); |
1050 | |
1107 | |
1051 | void from_json (SV *jsonstr) |
1108 | void from_json (SV *jsonstr) |
1052 | ALIAS: |
1109 | ALIAS: |
1053 | jsonToObj = 0 |
1110 | jsonToObj = 0 |
1054 | PPCODE: |
1111 | PPCODE: |
1055 | XPUSHs (decode_json (jsonstr, F_UTF8)); |
1112 | XPUSHs (decode_json (jsonstr, F_DEFAULT | F_UTF8)); |
1056 | |
1113 | |