… | |
… | |
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 |
|
|
32 | #define SHORT_STRING_LEN 512 // special-case strings of up to this size |
36 | #define SHORT_STRING_LEN 512 // special-case strings of up to this size |
33 | |
37 | |
34 | #define SB do { |
38 | #define SB do { |
35 | #define SE } while (0) |
39 | #define SE } while (0) |
36 | |
40 | |
… | |
… | |
76 | { |
80 | { |
77 | *clen = 2; |
81 | *clen = 2; |
78 | return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); |
82 | return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); |
79 | } |
83 | } |
80 | else |
84 | else |
|
|
85 | { |
|
|
86 | *clen = (STRLEN)-1; |
81 | return (UV)-1; |
87 | return (UV)-1; |
|
|
88 | } |
82 | } |
89 | } |
83 | |
90 | |
84 | ///////////////////////////////////////////////////////////////////////////// |
91 | ///////////////////////////////////////////////////////////////////////////// |
85 | // encoder |
92 | // encoder |
86 | |
93 | |
… | |
… | |
101 | if (enc->cur + len >= enc->end) |
108 | if (enc->cur + len >= enc->end) |
102 | { |
109 | { |
103 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
110 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
104 | SvGROW (enc->sv, cur + len + 1); |
111 | SvGROW (enc->sv, cur + len + 1); |
105 | enc->cur = SvPVX (enc->sv) + cur; |
112 | enc->cur = SvPVX (enc->sv) + cur; |
106 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv); |
113 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
107 | } |
114 | } |
108 | } |
115 | } |
109 | |
116 | |
110 | static void |
117 | static void |
111 | encode_ch (enc_t *enc, char ch) |
118 | encode_ch (enc_t *enc, char ch) |
… | |
… | |
208 | } |
215 | } |
209 | while (--clen); |
216 | while (--clen); |
210 | } |
217 | } |
211 | else |
218 | else |
212 | { |
219 | { |
213 | need (enc, len += UTF8_MAX_LEN - 1); // never more than 11 bytes needed |
220 | need (enc, len += UTF8_MAXBYTES - 1); // never more than 11 bytes needed |
214 | enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
221 | enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
215 | ++str; |
222 | ++str; |
216 | } |
223 | } |
217 | } |
224 | } |
218 | } |
225 | } |
… | |
… | |
268 | static void |
275 | static void |
269 | encode_av (enc_t *enc, AV *av) |
276 | encode_av (enc_t *enc, AV *av) |
270 | { |
277 | { |
271 | int i, len = av_len (av); |
278 | int i, len = av_len (av); |
272 | |
279 | |
|
|
280 | if (enc->indent >= enc->maxdepth) |
|
|
281 | croak ("data structure too deep (hit recursion limit)"); |
|
|
282 | |
273 | encode_ch (enc, '['); encode_nl (enc); |
283 | encode_ch (enc, '['); encode_nl (enc); |
274 | ++enc->indent; |
284 | ++enc->indent; |
275 | |
285 | |
276 | for (i = 0; i <= len; ++i) |
286 | for (i = 0; i <= len; ++i) |
277 | { |
287 | { |
… | |
… | |
342 | |
352 | |
343 | static void |
353 | static void |
344 | encode_hv (enc_t *enc, HV *hv) |
354 | encode_hv (enc_t *enc, HV *hv) |
345 | { |
355 | { |
346 | int count, i; |
356 | int count, i; |
|
|
357 | |
|
|
358 | if (enc->indent >= enc->maxdepth) |
|
|
359 | croak ("data structure too deep (hit recursion limit)"); |
347 | |
360 | |
348 | encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; |
361 | encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; |
349 | |
362 | |
350 | if ((count = hv_iterinit (hv))) |
363 | if ((count = hv_iterinit (hv))) |
351 | { |
364 | { |
… | |
… | |
399 | |
412 | |
400 | encode_nl (enc); |
413 | encode_nl (enc); |
401 | } |
414 | } |
402 | else |
415 | else |
403 | { |
416 | { |
404 | SV *sv; |
|
|
405 | HE *he = hv_iternext (hv); |
417 | HE *he = hv_iternext (hv); |
406 | |
418 | |
407 | for (;;) |
419 | for (;;) |
408 | { |
420 | { |
409 | encode_indent (enc); |
421 | encode_indent (enc); |
… | |
… | |
418 | encode_nl (enc); |
430 | encode_nl (enc); |
419 | } |
431 | } |
420 | } |
432 | } |
421 | |
433 | |
422 | --enc->indent; encode_indent (enc); encode_ch (enc, '}'); |
434 | --enc->indent; encode_indent (enc); encode_ch (enc, '}'); |
|
|
435 | } |
|
|
436 | |
|
|
437 | // encode objects, arrays and special \0=false and \1=true values. |
|
|
438 | static void |
|
|
439 | encode_rv (enc_t *enc, SV *sv) |
|
|
440 | { |
|
|
441 | svtype svt; |
|
|
442 | |
|
|
443 | SvGETMAGIC (sv); |
|
|
444 | svt = SvTYPE (sv); |
|
|
445 | |
|
|
446 | if (svt == SVt_PVHV) |
|
|
447 | encode_hv (enc, (HV *)sv); |
|
|
448 | else if (svt == SVt_PVAV) |
|
|
449 | encode_av (enc, (AV *)sv); |
|
|
450 | else if (svt < SVt_PVAV) |
|
|
451 | { |
|
|
452 | if (SvNIOK (sv) && SvIV (sv) == 0) |
|
|
453 | encode_str (enc, "false", 5, 0); |
|
|
454 | else if (SvNIOK (sv) && SvIV (sv) == 1) |
|
|
455 | encode_str (enc, "true", 4, 0); |
|
|
456 | else |
|
|
457 | croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", |
|
|
458 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
|
|
459 | } |
|
|
460 | else |
|
|
461 | croak ("encountered %s, but JSON can only represent references to arrays or hashes", |
|
|
462 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
423 | } |
463 | } |
424 | |
464 | |
425 | static void |
465 | static void |
426 | encode_sv (enc_t *enc, SV *sv) |
466 | encode_sv (enc_t *enc, SV *sv) |
427 | { |
467 | { |
… | |
… | |
448 | SvIsUV(sv) |
488 | SvIsUV(sv) |
449 | ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv)) |
489 | ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv)) |
450 | : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv)); |
490 | : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv)); |
451 | } |
491 | } |
452 | else if (SvROK (sv)) |
492 | else if (SvROK (sv)) |
453 | { |
493 | 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)) |
494 | else if (!SvOK (sv)) |
470 | encode_str (enc, "null", 4, 0); |
495 | encode_str (enc, "null", 4, 0); |
471 | else |
496 | else |
472 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
497 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
473 | SvPV_nolen (sv), SvFLAGS (sv)); |
498 | SvPV_nolen (sv), SvFLAGS (sv)); |
474 | } |
499 | } |
475 | |
500 | |
476 | static SV * |
501 | static SV * |
477 | encode_json (SV *scalar, U32 flags) |
502 | encode_json (SV *scalar, U32 flags) |
478 | { |
503 | { |
|
|
504 | enc_t enc; |
|
|
505 | |
479 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
506 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
480 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
507 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
481 | |
508 | |
482 | enc_t enc; |
|
|
483 | enc.flags = flags; |
509 | enc.flags = flags; |
484 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
510 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
485 | enc.cur = SvPVX (enc.sv); |
511 | enc.cur = SvPVX (enc.sv); |
486 | enc.end = SvEND (enc.sv); |
512 | enc.end = SvEND (enc.sv); |
487 | enc.indent = 0; |
513 | enc.indent = 0; |
… | |
… | |
492 | |
518 | |
493 | if (!(flags & (F_ASCII | F_UTF8))) |
519 | if (!(flags & (F_ASCII | F_UTF8))) |
494 | SvUTF8_on (enc.sv); |
520 | SvUTF8_on (enc.sv); |
495 | |
521 | |
496 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
522 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
|
|
523 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
497 | |
524 | |
498 | if (enc.flags & F_SHRINK) |
525 | if (enc.flags & F_SHRINK) |
499 | shrink (enc.sv); |
526 | shrink (enc.sv); |
500 | |
527 | |
501 | return enc.sv; |
528 | return enc.sv; |
… | |
… | |
573 | SV *sv = 0; |
600 | SV *sv = 0; |
574 | int utf8 = 0; |
601 | int utf8 = 0; |
575 | |
602 | |
576 | do |
603 | do |
577 | { |
604 | { |
578 | char buf [SHORT_STRING_LEN + UTF8_MAX_LEN]; |
605 | char buf [SHORT_STRING_LEN + UTF8_MAXBYTES]; |
579 | char *cur = buf; |
606 | char *cur = buf; |
580 | |
607 | |
581 | do |
608 | do |
582 | { |
609 | { |
583 | unsigned char ch = *(unsigned char *)dec->cur++; |
610 | unsigned char ch = *(unsigned char *)dec->cur++; |
… | |
… | |
649 | } |
676 | } |
650 | else if (ch >= 0x20 && ch <= 0x7f) |
677 | else if (ch >= 0x20 && ch <= 0x7f) |
651 | *cur++ = ch; |
678 | *cur++ = ch; |
652 | else if (ch >= 0x80) |
679 | else if (ch >= 0x80) |
653 | { |
680 | { |
|
|
681 | STRLEN clen; |
|
|
682 | UV uch; |
|
|
683 | |
654 | --dec->cur; |
684 | --dec->cur; |
655 | |
685 | |
656 | STRLEN clen; |
|
|
657 | UV uch = decode_utf8 (dec->cur, dec->end - dec->cur, &clen); |
686 | uch = decode_utf8 (dec->cur, dec->end - dec->cur, &clen); |
658 | if (clen == (STRLEN)-1) |
687 | if (clen == (STRLEN)-1) |
659 | ERR ("malformed UTF-8 character in JSON string"); |
688 | ERR ("malformed UTF-8 character in JSON string"); |
660 | |
689 | |
661 | do |
690 | do |
662 | { |
|
|
663 | *cur++ = *dec->cur++; |
691 | *cur++ = *dec->cur++; |
664 | } |
|
|
665 | while (--clen); |
692 | while (--clen); |
666 | |
693 | |
667 | utf8 = 1; |
694 | utf8 = 1; |
668 | } |
695 | } |
669 | else if (!ch) |
|
|
670 | ERR ("unexpected end of string while parsing json string"); |
|
|
671 | else |
696 | else |
|
|
697 | { |
|
|
698 | --dec->cur; |
|
|
699 | |
|
|
700 | if (!ch) |
|
|
701 | ERR ("unexpected end of string while parsing JSON string"); |
|
|
702 | else |
672 | ERR ("invalid character encountered"); |
703 | ERR ("invalid character encountered while parsing JSON string"); |
673 | |
704 | } |
674 | } |
705 | } |
675 | while (cur < buf + SHORT_STRING_LEN); |
706 | while (cur < buf + SHORT_STRING_LEN); |
676 | |
707 | |
|
|
708 | { |
677 | STRLEN len = cur - buf; |
709 | STRLEN len = cur - buf; |
678 | |
710 | |
679 | if (sv) |
711 | if (sv) |
680 | { |
712 | { |
681 | SvGROW (sv, SvCUR (sv) + len + 1); |
713 | SvGROW (sv, SvCUR (sv) + len + 1); |
682 | memcpy (SvPVX (sv) + SvCUR (sv), buf, len); |
714 | memcpy (SvPVX (sv) + SvCUR (sv), buf, len); |
683 | SvCUR_set (sv, SvCUR (sv) + len); |
715 | SvCUR_set (sv, SvCUR (sv) + len); |
684 | } |
716 | } |
685 | else |
717 | else |
686 | sv = newSVpvn (buf, len); |
718 | sv = newSVpvn (buf, len); |
|
|
719 | } |
687 | } |
720 | } |
688 | while (*dec->cur != '"'); |
721 | while (*dec->cur != '"'); |
689 | |
722 | |
690 | ++dec->cur; |
723 | ++dec->cur; |
691 | |
724 | |
… | |
… | |
935 | ERR ("'null' expected"); |
968 | ERR ("'null' expected"); |
936 | |
969 | |
937 | break; |
970 | break; |
938 | |
971 | |
939 | default: |
972 | default: |
940 | ERR ("malformed json string, neither array, object, number, string or atom"); |
973 | ERR ("malformed JSON string, neither array, object, number, string or atom"); |
941 | break; |
974 | break; |
942 | } |
975 | } |
943 | |
976 | |
944 | fail: |
977 | fail: |
945 | return 0; |
978 | return 0; |
946 | } |
979 | } |
947 | |
980 | |
948 | static SV * |
981 | static SV * |
949 | decode_json (SV *string, U32 flags) |
982 | decode_json (SV *string, U32 flags) |
950 | { |
983 | { |
|
|
984 | dec_t dec; |
951 | SV *sv; |
985 | SV *sv; |
|
|
986 | |
|
|
987 | SvGETMAGIC (string); |
|
|
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 | |