… | |
… | |
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" |
|
|
9 | |
|
|
10 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
|
|
11 | # define snprintf _snprintf // C compilers have this in stdio.h |
|
|
12 | #endif |
8 | |
13 | |
9 | #define F_ASCII 0x00000001UL |
14 | #define F_ASCII 0x00000001UL |
10 | #define F_UTF8 0x00000002UL |
15 | #define F_UTF8 0x00000002UL |
11 | #define F_INDENT 0x00000004UL |
16 | #define F_INDENT 0x00000004UL |
12 | #define F_CANONICAL 0x00000008UL |
17 | #define F_CANONICAL 0x00000008UL |
… | |
… | |
21 | |
26 | |
22 | // F_SELFCONVERT? <=> to_json/toJson |
27 | // F_SELFCONVERT? <=> to_json/toJson |
23 | // F_BLESSED? <=> { $__class__$ => } |
28 | // F_BLESSED? <=> { $__class__$ => } |
24 | |
29 | |
25 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
30 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
26 | #define F_DEFAULT (13UL << S_MAXDEPTH) |
31 | #define F_DEFAULT (9UL << S_MAXDEPTH) |
27 | |
32 | |
28 | #define INIT_SIZE 32 // initial scalar size to be allocated |
33 | #define INIT_SIZE 32 // initial scalar size to be allocated |
29 | #define INDENT_STEP 3 // spaces per indentation level |
34 | #define INDENT_STEP 3 // spaces per indentation level |
30 | |
35 | |
31 | #define UTF8_MAX_LEN 11 // for perls UTF-X: max. number of octets per character |
36 | #define UTF8_MAX_LEN 11 // for perls UTF-X: max. number of octets per character |
… | |
… | |
76 | { |
81 | { |
77 | *clen = 2; |
82 | *clen = 2; |
78 | return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); |
83 | return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); |
79 | } |
84 | } |
80 | else |
85 | else |
|
|
86 | { |
|
|
87 | *clen = (STRLEN)-1; |
81 | return (UV)-1; |
88 | return (UV)-1; |
|
|
89 | } |
82 | } |
90 | } |
83 | |
91 | |
84 | ///////////////////////////////////////////////////////////////////////////// |
92 | ///////////////////////////////////////////////////////////////////////////// |
85 | // encoder |
93 | // encoder |
86 | |
94 | |
… | |
… | |
101 | if (enc->cur + len >= enc->end) |
109 | if (enc->cur + len >= enc->end) |
102 | { |
110 | { |
103 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
111 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
104 | SvGROW (enc->sv, cur + len + 1); |
112 | SvGROW (enc->sv, cur + len + 1); |
105 | enc->cur = SvPVX (enc->sv) + cur; |
113 | enc->cur = SvPVX (enc->sv) + cur; |
106 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv); |
114 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
107 | } |
115 | } |
108 | } |
116 | } |
109 | |
117 | |
110 | static void |
118 | static void |
111 | encode_ch (enc_t *enc, char ch) |
119 | encode_ch (enc_t *enc, char ch) |
… | |
… | |
268 | static void |
276 | static void |
269 | encode_av (enc_t *enc, AV *av) |
277 | encode_av (enc_t *enc, AV *av) |
270 | { |
278 | { |
271 | int i, len = av_len (av); |
279 | int i, len = av_len (av); |
272 | |
280 | |
|
|
281 | if (enc->indent >= enc->maxdepth) |
|
|
282 | croak ("data structure too deep (hit recursion limit)"); |
|
|
283 | |
273 | encode_ch (enc, '['); encode_nl (enc); |
284 | encode_ch (enc, '['); encode_nl (enc); |
274 | ++enc->indent; |
285 | ++enc->indent; |
275 | |
286 | |
276 | for (i = 0; i <= len; ++i) |
287 | for (i = 0; i <= len; ++i) |
277 | { |
288 | { |
… | |
… | |
342 | |
353 | |
343 | static void |
354 | static void |
344 | encode_hv (enc_t *enc, HV *hv) |
355 | encode_hv (enc_t *enc, HV *hv) |
345 | { |
356 | { |
346 | int count, i; |
357 | int count, i; |
|
|
358 | |
|
|
359 | if (enc->indent >= enc->maxdepth) |
|
|
360 | croak ("data structure too deep (hit recursion limit)"); |
347 | |
361 | |
348 | encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; |
362 | encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; |
349 | |
363 | |
350 | if ((count = hv_iterinit (hv))) |
364 | if ((count = hv_iterinit (hv))) |
351 | { |
365 | { |
… | |
… | |
399 | |
413 | |
400 | encode_nl (enc); |
414 | encode_nl (enc); |
401 | } |
415 | } |
402 | else |
416 | else |
403 | { |
417 | { |
404 | SV *sv; |
|
|
405 | HE *he = hv_iternext (hv); |
418 | HE *he = hv_iternext (hv); |
406 | |
419 | |
407 | for (;;) |
420 | for (;;) |
408 | { |
421 | { |
409 | encode_indent (enc); |
422 | encode_indent (enc); |
… | |
… | |
418 | encode_nl (enc); |
431 | encode_nl (enc); |
419 | } |
432 | } |
420 | } |
433 | } |
421 | |
434 | |
422 | --enc->indent; encode_indent (enc); encode_ch (enc, '}'); |
435 | --enc->indent; encode_indent (enc); encode_ch (enc, '}'); |
|
|
436 | } |
|
|
437 | |
|
|
438 | // encode objects, arrays and special \0=false and \1=true values. |
|
|
439 | static void |
|
|
440 | encode_rv (enc_t *enc, SV *sv) |
|
|
441 | { |
|
|
442 | svtype svt; |
|
|
443 | |
|
|
444 | SvGETMAGIC (sv); |
|
|
445 | svt = SvTYPE (sv); |
|
|
446 | |
|
|
447 | if (svt == SVt_PVHV) |
|
|
448 | encode_hv (enc, (HV *)sv); |
|
|
449 | else if (svt == SVt_PVAV) |
|
|
450 | encode_av (enc, (AV *)sv); |
|
|
451 | else if (svt < SVt_PVAV) |
|
|
452 | { |
|
|
453 | if (SvNIOK (sv) && SvIV (sv) == 0) |
|
|
454 | encode_str (enc, "false", 5, 0); |
|
|
455 | else if (SvNIOK (sv) && SvIV (sv) == 1) |
|
|
456 | encode_str (enc, "true", 4, 0); |
|
|
457 | else |
|
|
458 | croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", |
|
|
459 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
|
|
460 | } |
|
|
461 | else |
|
|
462 | croak ("encountered %s, but JSON can only represent references to arrays or hashes", |
|
|
463 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
423 | } |
464 | } |
424 | |
465 | |
425 | static void |
466 | static void |
426 | encode_sv (enc_t *enc, SV *sv) |
467 | encode_sv (enc_t *enc, SV *sv) |
427 | { |
468 | { |
… | |
… | |
448 | SvIsUV(sv) |
489 | SvIsUV(sv) |
449 | ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv)) |
490 | ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv)) |
450 | : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv)); |
491 | : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv)); |
451 | } |
492 | } |
452 | else if (SvROK (sv)) |
493 | else if (SvROK (sv)) |
453 | { |
494 | encode_rv (enc, SvRV (sv)); |
454 | SV *rv = SvRV (sv); |
|
|
455 | |
|
|
456 | if (enc->indent >= enc->maxdepth) |
|
|
457 | croak ("data structure too deep (hit recursion limit)"); |
|
|
458 | |
|
|
459 | switch (SvTYPE (rv)) |
|
|
460 | { |
|
|
461 | case SVt_PVAV: encode_av (enc, (AV *)rv); break; |
|
|
462 | case SVt_PVHV: encode_hv (enc, (HV *)rv); break; |
|
|
463 | |
|
|
464 | default: |
|
|
465 | croak ("encountered %s, but JSON can only represent references to arrays or hashes", |
|
|
466 | SvPV_nolen (sv)); |
|
|
467 | } |
|
|
468 | } |
|
|
469 | else if (!SvOK (sv)) |
495 | else if (!SvOK (sv)) |
470 | encode_str (enc, "null", 4, 0); |
496 | encode_str (enc, "null", 4, 0); |
471 | else |
497 | else |
472 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
498 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
473 | SvPV_nolen (sv), SvFLAGS (sv)); |
499 | SvPV_nolen (sv), SvFLAGS (sv)); |
474 | } |
500 | } |
475 | |
501 | |
476 | static SV * |
502 | static SV * |
477 | encode_json (SV *scalar, U32 flags) |
503 | encode_json (SV *scalar, U32 flags) |
478 | { |
504 | { |
|
|
505 | enc_t enc; |
|
|
506 | |
479 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
507 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
480 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
508 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
481 | |
509 | |
482 | enc_t enc; |
|
|
483 | enc.flags = flags; |
510 | enc.flags = flags; |
484 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
511 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
485 | enc.cur = SvPVX (enc.sv); |
512 | enc.cur = SvPVX (enc.sv); |
486 | enc.end = SvEND (enc.sv); |
513 | enc.end = SvEND (enc.sv); |
487 | enc.indent = 0; |
514 | enc.indent = 0; |
… | |
… | |
492 | |
519 | |
493 | if (!(flags & (F_ASCII | F_UTF8))) |
520 | if (!(flags & (F_ASCII | F_UTF8))) |
494 | SvUTF8_on (enc.sv); |
521 | SvUTF8_on (enc.sv); |
495 | |
522 | |
496 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
523 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
|
|
524 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
497 | |
525 | |
498 | if (enc.flags & F_SHRINK) |
526 | if (enc.flags & F_SHRINK) |
499 | shrink (enc.sv); |
527 | shrink (enc.sv); |
500 | |
528 | |
501 | return enc.sv; |
529 | return enc.sv; |
… | |
… | |
649 | } |
677 | } |
650 | else if (ch >= 0x20 && ch <= 0x7f) |
678 | else if (ch >= 0x20 && ch <= 0x7f) |
651 | *cur++ = ch; |
679 | *cur++ = ch; |
652 | else if (ch >= 0x80) |
680 | else if (ch >= 0x80) |
653 | { |
681 | { |
|
|
682 | STRLEN clen; |
|
|
683 | UV uch; |
|
|
684 | |
654 | --dec->cur; |
685 | --dec->cur; |
655 | |
686 | |
656 | STRLEN clen; |
|
|
657 | UV uch = decode_utf8 (dec->cur, dec->end - dec->cur, &clen); |
687 | uch = decode_utf8 (dec->cur, dec->end - dec->cur, &clen); |
658 | if (clen == (STRLEN)-1) |
688 | if (clen == (STRLEN)-1) |
659 | ERR ("malformed UTF-8 character in JSON string"); |
689 | ERR ("malformed UTF-8 character in JSON string"); |
660 | |
690 | |
661 | do |
691 | do |
662 | { |
|
|
663 | *cur++ = *dec->cur++; |
692 | *cur++ = *dec->cur++; |
664 | } |
|
|
665 | while (--clen); |
693 | while (--clen); |
666 | |
694 | |
667 | utf8 = 1; |
695 | utf8 = 1; |
668 | } |
696 | } |
669 | else if (!ch) |
|
|
670 | ERR ("unexpected end of string while parsing json string"); |
|
|
671 | else |
697 | else |
|
|
698 | { |
|
|
699 | --dec->cur; |
|
|
700 | |
|
|
701 | if (!ch) |
|
|
702 | ERR ("unexpected end of string while parsing JSON string"); |
|
|
703 | else |
672 | ERR ("invalid character encountered"); |
704 | ERR ("invalid character encountered while parsing JSON string"); |
673 | |
705 | } |
674 | } |
706 | } |
675 | while (cur < buf + SHORT_STRING_LEN); |
707 | while (cur < buf + SHORT_STRING_LEN); |
676 | |
708 | |
|
|
709 | { |
677 | STRLEN len = cur - buf; |
710 | STRLEN len = cur - buf; |
678 | |
711 | |
679 | if (sv) |
712 | if (sv) |
680 | { |
713 | { |
681 | SvGROW (sv, SvCUR (sv) + len + 1); |
714 | SvGROW (sv, SvCUR (sv) + len + 1); |
682 | memcpy (SvPVX (sv) + SvCUR (sv), buf, len); |
715 | memcpy (SvPVX (sv) + SvCUR (sv), buf, len); |
683 | SvCUR_set (sv, SvCUR (sv) + len); |
716 | SvCUR_set (sv, SvCUR (sv) + len); |
684 | } |
717 | } |
685 | else |
718 | else |
686 | sv = newSVpvn (buf, len); |
719 | sv = newSVpvn (buf, len); |
|
|
720 | } |
687 | } |
721 | } |
688 | while (*dec->cur != '"'); |
722 | while (*dec->cur != '"'); |
689 | |
723 | |
690 | ++dec->cur; |
724 | ++dec->cur; |
691 | |
725 | |
… | |
… | |
935 | ERR ("'null' expected"); |
969 | ERR ("'null' expected"); |
936 | |
970 | |
937 | break; |
971 | break; |
938 | |
972 | |
939 | default: |
973 | default: |
940 | ERR ("malformed json string, neither array, object, number, string or atom"); |
974 | ERR ("malformed JSON string, neither array, object, number, string or atom"); |
941 | break; |
975 | break; |
942 | } |
976 | } |
943 | |
977 | |
944 | fail: |
978 | fail: |
945 | return 0; |
979 | return 0; |
946 | } |
980 | } |
947 | |
981 | |
948 | static SV * |
982 | static SV * |
949 | decode_json (SV *string, U32 flags) |
983 | decode_json (SV *string, U32 flags) |
950 | { |
984 | { |
|
|
985 | dec_t dec; |
951 | SV *sv; |
986 | SV *sv; |
|
|
987 | |
|
|
988 | SvUPGRADE (string, SVt_PV); |
952 | |
989 | |
953 | if (flags & F_UTF8) |
990 | if (flags & F_UTF8) |
954 | sv_utf8_downgrade (string, 0); |
991 | sv_utf8_downgrade (string, 0); |
955 | else |
992 | else |
956 | sv_utf8_upgrade (string); |
993 | sv_utf8_upgrade (string); |
957 | |
994 | |
958 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
995 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
959 | |
996 | |
960 | dec_t dec; |
|
|
961 | dec.flags = flags; |
997 | dec.flags = flags; |
962 | dec.cur = SvPVX (string); |
998 | dec.cur = SvPVX (string); |
963 | dec.end = SvEND (string); |
999 | dec.end = SvEND (string); |
964 | dec.err = 0; |
1000 | dec.err = 0; |
965 | dec.depth = 0; |
1001 | dec.depth = 0; |
966 | dec.maxdepth = DEC_DEPTH (dec.flags); |
1002 | dec.maxdepth = DEC_DEPTH (dec.flags); |
967 | |
1003 | |
968 | *SvEND (sv) = 0; // this shou[ld basically be a nop, too |
1004 | *dec.end = 0; // this should basically be a nop, too, but make sure its there |
969 | sv = decode_sv (&dec); |
1005 | sv = decode_sv (&dec); |
970 | |
1006 | |
971 | if (!sv) |
1007 | if (!sv) |
972 | { |
1008 | { |
973 | IV offset = dec.flags & F_UTF8 |
1009 | IV offset = dec.flags & F_UTF8 |
… | |
… | |
982 | SAVEVPTR (PL_curcop); |
1018 | SAVEVPTR (PL_curcop); |
983 | PL_curcop = &cop; |
1019 | PL_curcop = &cop; |
984 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
1020 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
985 | LEAVE; |
1021 | LEAVE; |
986 | |
1022 | |
987 | croak ("%s, at character offset %d (%s)", |
1023 | croak ("%s, at character offset %d [\"%s\"]", |
988 | dec.err, |
1024 | dec.err, |
989 | (int)offset, |
1025 | (int)offset, |
990 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1026 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
991 | } |
1027 | } |
992 | |
1028 | |
… | |
… | |
1049 | RETVAL = newSVsv (self); |
1085 | RETVAL = newSVsv (self); |
1050 | } |
1086 | } |
1051 | OUTPUT: |
1087 | OUTPUT: |
1052 | RETVAL |
1088 | RETVAL |
1053 | |
1089 | |
1054 | SV *max_depth (SV *self, int max_depth = 0x80000000UL) |
1090 | SV *max_depth (SV *self, UV max_depth = 0x80000000UL) |
1055 | CODE: |
1091 | CODE: |
1056 | { |
1092 | { |
1057 | UV *uv = SvJSON (self); |
1093 | UV *uv = SvJSON (self); |
1058 | UV log2 = 0; |
1094 | UV log2 = 0; |
1059 | |
1095 | |