… | |
… | |
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 |
|
|
19 | |
|
|
20 | #define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) |
|
|
21 | |
|
|
22 | // F_SELFCONVERT? <=> to_json/toJson |
|
|
23 | // F_BLESSED? <=> { $__class__$ => } |
17 | |
24 | |
18 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
25 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
19 | #define F_DEFAULT 0 |
26 | #define F_DEFAULT (12UL << S_MAXDEPTH) |
20 | |
27 | |
21 | #define INIT_SIZE 32 // initial scalar size to be allocated |
28 | #define INIT_SIZE 32 // initial scalar size to be allocated |
22 | #define INDENT_STEP 3 // spaces per indentation level |
29 | #define INDENT_STEP 3 // spaces per indentation level |
23 | |
30 | |
24 | #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 |
… | |
… | |
81 | typedef struct |
88 | typedef struct |
82 | { |
89 | { |
83 | char *cur; // SvPVX (sv) + current output position |
90 | char *cur; // SvPVX (sv) + current output position |
84 | char *end; // SvEND (sv) |
91 | char *end; // SvEND (sv) |
85 | SV *sv; // result scalar |
92 | SV *sv; // result scalar |
86 | UV flags; // F_* |
93 | U32 flags; // F_* |
87 | int indent; // indentation level |
94 | U32 indent; // indentation level |
88 | int max_depth; // max. recursion level |
95 | U32 maxdepth; // max. indentation/recursion level |
89 | } enc_t; |
96 | } enc_t; |
90 | |
97 | |
91 | static void |
98 | static void |
92 | need (enc_t *enc, STRLEN len) |
99 | need (enc_t *enc, STRLEN len) |
93 | { |
100 | { |
… | |
… | |
261 | static void |
268 | static void |
262 | encode_av (enc_t *enc, AV *av) |
269 | encode_av (enc_t *enc, AV *av) |
263 | { |
270 | { |
264 | int i, len = av_len (av); |
271 | int i, len = av_len (av); |
265 | |
272 | |
|
|
273 | if (enc->indent >= enc->maxdepth) |
|
|
274 | croak ("data structure too deep (hit recursion limit)"); |
|
|
275 | |
266 | encode_ch (enc, '['); encode_nl (enc); |
276 | encode_ch (enc, '['); encode_nl (enc); |
267 | ++enc->indent; |
277 | ++enc->indent; |
268 | |
278 | |
269 | for (i = 0; i <= len; ++i) |
279 | for (i = 0; i <= len; ++i) |
270 | { |
280 | { |
… | |
… | |
335 | |
345 | |
336 | static void |
346 | static void |
337 | encode_hv (enc_t *enc, HV *hv) |
347 | encode_hv (enc_t *enc, HV *hv) |
338 | { |
348 | { |
339 | int count, i; |
349 | int count, i; |
|
|
350 | |
|
|
351 | if (enc->indent >= enc->maxdepth) |
|
|
352 | croak ("data structure too deep (hit recursion limit)"); |
340 | |
353 | |
341 | encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; |
354 | encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; |
342 | |
355 | |
343 | if ((count = hv_iterinit (hv))) |
356 | if ((count = hv_iterinit (hv))) |
344 | { |
357 | { |
… | |
… | |
413 | } |
426 | } |
414 | |
427 | |
415 | --enc->indent; encode_indent (enc); encode_ch (enc, '}'); |
428 | --enc->indent; encode_indent (enc); encode_ch (enc, '}'); |
416 | } |
429 | } |
417 | |
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 | |
418 | static void |
458 | static void |
419 | encode_sv (enc_t *enc, SV *sv) |
459 | encode_sv (enc_t *enc, SV *sv) |
420 | { |
460 | { |
421 | SvGETMAGIC (sv); |
461 | SvGETMAGIC (sv); |
422 | |
462 | |
… | |
… | |
441 | SvIsUV(sv) |
481 | SvIsUV(sv) |
442 | ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv)) |
482 | ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv)) |
443 | : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv)); |
483 | : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv)); |
444 | } |
484 | } |
445 | else if (SvROK (sv)) |
485 | else if (SvROK (sv)) |
446 | { |
486 | encode_rv (enc, SvRV (sv)); |
447 | SV *rv = SvRV (sv); |
|
|
448 | |
|
|
449 | if (enc->indent >= enc->max_depth) |
|
|
450 | croak ("data structure too deep (hit recursion limit)"); |
|
|
451 | |
|
|
452 | switch (SvTYPE (rv)) |
|
|
453 | { |
|
|
454 | case SVt_PVAV: encode_av (enc, (AV *)rv); break; |
|
|
455 | case SVt_PVHV: encode_hv (enc, (HV *)rv); break; |
|
|
456 | |
|
|
457 | default: |
|
|
458 | croak ("encountered %s, but JSON can only represent references to arrays or hashes", |
|
|
459 | SvPV_nolen (sv)); |
|
|
460 | } |
|
|
461 | } |
|
|
462 | else if (!SvOK (sv)) |
487 | else if (!SvOK (sv)) |
463 | encode_str (enc, "null", 4, 0); |
488 | encode_str (enc, "null", 4, 0); |
464 | else |
489 | else |
465 | 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", |
466 | SvPV_nolen (sv), SvFLAGS (sv)); |
491 | SvPV_nolen (sv), SvFLAGS (sv)); |
467 | } |
492 | } |
468 | |
493 | |
469 | static SV * |
494 | static SV * |
470 | encode_json (SV *scalar, UV flags) |
495 | encode_json (SV *scalar, U32 flags) |
471 | { |
496 | { |
472 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
497 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
473 | 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)"); |
474 | |
499 | |
475 | enc_t enc; |
500 | enc_t enc; |
476 | enc.flags = flags; |
501 | enc.flags = flags; |
477 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
502 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
478 | enc.cur = SvPVX (enc.sv); |
503 | enc.cur = SvPVX (enc.sv); |
479 | enc.end = SvEND (enc.sv); |
504 | enc.end = SvEND (enc.sv); |
480 | enc.indent = 0; |
505 | enc.indent = 0; |
481 | enc.max_depth = 0x7fffffffUL; |
506 | enc.maxdepth = DEC_DEPTH (flags); |
482 | |
507 | |
483 | SvPOK_only (enc.sv); |
508 | SvPOK_only (enc.sv); |
484 | encode_sv (&enc, scalar); |
509 | encode_sv (&enc, scalar); |
485 | |
510 | |
486 | if (!(flags & (F_ASCII | F_UTF8))) |
511 | if (!(flags & (F_ASCII | F_UTF8))) |
… | |
… | |
501 | typedef struct |
526 | typedef struct |
502 | { |
527 | { |
503 | char *cur; // current parser pointer |
528 | char *cur; // current parser pointer |
504 | char *end; // end of input string |
529 | char *end; // end of input string |
505 | const char *err; // parse error, if != 0 |
530 | const char *err; // parse error, if != 0 |
506 | UV flags; // F_* |
531 | U32 flags; // F_* |
|
|
532 | U32 depth; // recursion depth |
|
|
533 | U32 maxdepth; // recursion depth limit |
507 | } dec_t; |
534 | } dec_t; |
508 | |
535 | |
509 | static void |
536 | static void |
510 | decode_ws (dec_t *dec) |
537 | decode_ws (dec_t *dec) |
511 | { |
538 | { |
… | |
… | |
520 | ++dec->cur; |
547 | ++dec->cur; |
521 | } |
548 | } |
522 | } |
549 | } |
523 | |
550 | |
524 | #define ERR(reason) SB dec->err = reason; goto fail; SE |
551 | #define ERR(reason) SB dec->err = reason; goto fail; SE |
|
|
552 | |
525 | #define EXPECT_CH(ch) SB \ |
553 | #define EXPECT_CH(ch) SB \ |
526 | if (*dec->cur != ch) \ |
554 | if (*dec->cur != ch) \ |
527 | ERR (# ch " expected"); \ |
555 | ERR (# ch " expected"); \ |
528 | ++dec->cur; \ |
556 | ++dec->cur; \ |
529 | 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 |
530 | |
561 | |
531 | static SV *decode_sv (dec_t *dec); |
562 | static SV *decode_sv (dec_t *dec); |
532 | |
563 | |
533 | static signed char decode_hexdigit[256]; |
564 | static signed char decode_hexdigit[256]; |
534 | |
565 | |
… | |
… | |
778 | static SV * |
809 | static SV * |
779 | decode_av (dec_t *dec) |
810 | decode_av (dec_t *dec) |
780 | { |
811 | { |
781 | AV *av = newAV (); |
812 | AV *av = newAV (); |
782 | |
813 | |
|
|
814 | DEC_INC_DEPTH; |
783 | decode_ws (dec); |
815 | decode_ws (dec); |
|
|
816 | |
784 | if (*dec->cur == ']') |
817 | if (*dec->cur == ']') |
785 | ++dec->cur; |
818 | ++dec->cur; |
786 | else |
819 | else |
787 | for (;;) |
820 | for (;;) |
788 | { |
821 | { |
… | |
… | |
806 | ERR (", or ] expected while parsing array"); |
839 | ERR (", or ] expected while parsing array"); |
807 | |
840 | |
808 | ++dec->cur; |
841 | ++dec->cur; |
809 | } |
842 | } |
810 | |
843 | |
|
|
844 | DEC_DEC_DEPTH; |
811 | return newRV_noinc ((SV *)av); |
845 | return newRV_noinc ((SV *)av); |
812 | |
846 | |
813 | fail: |
847 | fail: |
814 | SvREFCNT_dec (av); |
848 | SvREFCNT_dec (av); |
|
|
849 | DEC_DEC_DEPTH; |
815 | return 0; |
850 | return 0; |
816 | } |
851 | } |
817 | |
852 | |
818 | static SV * |
853 | static SV * |
819 | decode_hv (dec_t *dec) |
854 | decode_hv (dec_t *dec) |
820 | { |
855 | { |
821 | HV *hv = newHV (); |
856 | HV *hv = newHV (); |
822 | |
857 | |
|
|
858 | DEC_INC_DEPTH; |
823 | decode_ws (dec); |
859 | decode_ws (dec); |
|
|
860 | |
824 | if (*dec->cur == '}') |
861 | if (*dec->cur == '}') |
825 | ++dec->cur; |
862 | ++dec->cur; |
826 | else |
863 | else |
827 | for (;;) |
864 | for (;;) |
828 | { |
865 | { |
… | |
… | |
841 | { |
878 | { |
842 | SvREFCNT_dec (key); |
879 | SvREFCNT_dec (key); |
843 | goto fail; |
880 | goto fail; |
844 | } |
881 | } |
845 | |
882 | |
846 | //TODO: optimise |
|
|
847 | hv_store_ent (hv, key, value, 0); |
883 | hv_store_ent (hv, key, value, 0); |
|
|
884 | SvREFCNT_dec (key); |
848 | |
885 | |
849 | decode_ws (dec); |
886 | decode_ws (dec); |
850 | |
887 | |
851 | if (*dec->cur == '}') |
888 | if (*dec->cur == '}') |
852 | { |
889 | { |
… | |
… | |
858 | ERR (", or } expected while parsing object/hash"); |
895 | ERR (", or } expected while parsing object/hash"); |
859 | |
896 | |
860 | ++dec->cur; |
897 | ++dec->cur; |
861 | } |
898 | } |
862 | |
899 | |
|
|
900 | DEC_DEC_DEPTH; |
863 | return newRV_noinc ((SV *)hv); |
901 | return newRV_noinc ((SV *)hv); |
864 | |
902 | |
865 | fail: |
903 | fail: |
866 | SvREFCNT_dec (hv); |
904 | SvREFCNT_dec (hv); |
|
|
905 | DEC_DEC_DEPTH; |
867 | return 0; |
906 | return 0; |
868 | } |
907 | } |
869 | |
908 | |
870 | static SV * |
909 | static SV * |
871 | decode_sv (dec_t *dec) |
910 | decode_sv (dec_t *dec) |
… | |
… | |
923 | fail: |
962 | fail: |
924 | return 0; |
963 | return 0; |
925 | } |
964 | } |
926 | |
965 | |
927 | static SV * |
966 | static SV * |
928 | decode_json (SV *string, UV flags) |
967 | decode_json (SV *string, U32 flags) |
929 | { |
968 | { |
930 | SV *sv; |
969 | SV *sv; |
931 | |
970 | |
932 | if (flags & F_UTF8) |
971 | if (flags & F_UTF8) |
933 | sv_utf8_downgrade (string, 0); |
972 | sv_utf8_downgrade (string, 0); |
… | |
… | |
935 | sv_utf8_upgrade (string); |
974 | sv_utf8_upgrade (string); |
936 | |
975 | |
937 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
976 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
938 | |
977 | |
939 | dec_t dec; |
978 | dec_t dec; |
940 | dec.flags = flags; |
979 | dec.flags = flags; |
941 | dec.cur = SvPVX (string); |
980 | dec.cur = SvPVX (string); |
942 | dec.end = SvEND (string); |
981 | dec.end = SvEND (string); |
943 | dec.err = 0; |
982 | dec.err = 0; |
|
|
983 | dec.depth = 0; |
|
|
984 | dec.maxdepth = DEC_DEPTH (dec.flags); |
944 | |
985 | |
|
|
986 | *dec.end = 0; // this should basically be a nop, too, but make sure its there |
945 | sv = decode_sv (&dec); |
987 | sv = decode_sv (&dec); |
946 | |
988 | |
947 | if (!sv) |
989 | if (!sv) |
948 | { |
990 | { |
949 | IV offset = dec.flags & F_UTF8 |
991 | IV offset = dec.flags & F_UTF8 |
… | |
… | |
982 | BOOT: |
1024 | BOOT: |
983 | { |
1025 | { |
984 | int i; |
1026 | int i; |
985 | |
1027 | |
986 | memset (decode_hexdigit, 0xff, 256); |
1028 | memset (decode_hexdigit, 0xff, 256); |
|
|
1029 | |
987 | for (i = 10; i--; ) |
1030 | for (i = 0; i < 256; ++i) |
988 | decode_hexdigit ['0' + i] = i; |
1031 | decode_hexdigit [i] = |
989 | |
1032 | i >= '0' && i <= '9' ? i - '0' |
990 | for (i = 7; i--; ) |
1033 | : i >= 'a' && i <= 'f' ? i - 'a' + 10 |
991 | { |
1034 | : i >= 'A' && i <= 'F' ? i - 'A' + 10 |
992 | decode_hexdigit ['a' + i] = 10 + i; |
1035 | : -1; |
993 | decode_hexdigit ['A' + i] = 10 + i; |
|
|
994 | } |
|
|
995 | |
1036 | |
996 | json_stash = gv_stashpv ("JSON::XS", 1); |
1037 | json_stash = gv_stashpv ("JSON::XS", 1); |
997 | } |
1038 | } |
998 | |
1039 | |
999 | PROTOTYPES: DISABLE |
1040 | PROTOTYPES: DISABLE |
… | |
… | |
1026 | RETVAL = newSVsv (self); |
1067 | RETVAL = newSVsv (self); |
1027 | } |
1068 | } |
1028 | OUTPUT: |
1069 | OUTPUT: |
1029 | RETVAL |
1070 | RETVAL |
1030 | |
1071 | |
|
|
1072 | SV *max_depth (SV *self, int max_depth = 0x80000000UL) |
|
|
1073 | CODE: |
|
|
1074 | { |
|
|
1075 | UV *uv = SvJSON (self); |
|
|
1076 | UV log2 = 0; |
|
|
1077 | |
|
|
1078 | if (max_depth > 0x80000000UL) max_depth = 0x80000000UL; |
|
|
1079 | |
|
|
1080 | while ((1UL << log2) < max_depth) |
|
|
1081 | ++log2; |
|
|
1082 | |
|
|
1083 | *uv = *uv & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); |
|
|
1084 | |
|
|
1085 | RETVAL = newSVsv (self); |
|
|
1086 | } |
|
|
1087 | OUTPUT: |
|
|
1088 | RETVAL |
|
|
1089 | |
1031 | void encode (SV *self, SV *scalar) |
1090 | void encode (SV *self, SV *scalar) |
1032 | PPCODE: |
1091 | PPCODE: |
1033 | XPUSHs (encode_json (scalar, *SvJSON (self))); |
1092 | XPUSHs (encode_json (scalar, *SvJSON (self))); |
1034 | |
1093 | |
1035 | void decode (SV *self, SV *jsonstr) |
1094 | void decode (SV *self, SV *jsonstr) |
… | |
… | |
1037 | XPUSHs (decode_json (jsonstr, *SvJSON (self))); |
1096 | XPUSHs (decode_json (jsonstr, *SvJSON (self))); |
1038 | |
1097 | |
1039 | PROTOTYPES: ENABLE |
1098 | PROTOTYPES: ENABLE |
1040 | |
1099 | |
1041 | void to_json (SV *scalar) |
1100 | void to_json (SV *scalar) |
|
|
1101 | ALIAS: |
|
|
1102 | objToJson = 0 |
1042 | PPCODE: |
1103 | PPCODE: |
1043 | XPUSHs (encode_json (scalar, F_UTF8)); |
1104 | XPUSHs (encode_json (scalar, F_DEFAULT | F_UTF8)); |
1044 | |
1105 | |
1045 | void from_json (SV *jsonstr) |
1106 | void from_json (SV *jsonstr) |
|
|
1107 | ALIAS: |
|
|
1108 | jsonToObj = 0 |
1046 | PPCODE: |
1109 | PPCODE: |
1047 | XPUSHs (decode_json (jsonstr, F_UTF8)); |
1110 | XPUSHs (decode_json (jsonstr, F_DEFAULT | F_UTF8)); |
1048 | |
1111 | |