… | |
… | |
408 | |
408 | |
409 | encode_nl (enc); |
409 | encode_nl (enc); |
410 | } |
410 | } |
411 | else |
411 | else |
412 | { |
412 | { |
413 | SV *sv; |
|
|
414 | HE *he = hv_iternext (hv); |
413 | HE *he = hv_iternext (hv); |
415 | |
414 | |
416 | for (;;) |
415 | for (;;) |
417 | { |
416 | { |
418 | encode_indent (enc); |
417 | encode_indent (enc); |
… | |
… | |
433 | |
432 | |
434 | // encode objects, arrays and special \0=false and \1=true values. |
433 | // encode objects, arrays and special \0=false and \1=true values. |
435 | static void |
434 | static void |
436 | encode_rv (enc_t *enc, SV *sv) |
435 | encode_rv (enc_t *enc, SV *sv) |
437 | { |
436 | { |
|
|
437 | svtype svt; |
|
|
438 | |
438 | SvGETMAGIC (sv); |
439 | SvGETMAGIC (sv); |
439 | |
|
|
440 | svtype svt = SvTYPE (sv); |
440 | svt = SvTYPE (sv); |
441 | |
441 | |
442 | if (svt == SVt_PVHV) |
442 | if (svt == SVt_PVHV) |
443 | encode_hv (enc, (HV *)sv); |
443 | encode_hv (enc, (HV *)sv); |
444 | else if (svt == SVt_PVAV) |
444 | else if (svt == SVt_PVAV) |
445 | encode_av (enc, (AV *)sv); |
445 | encode_av (enc, (AV *)sv); |
… | |
… | |
495 | } |
495 | } |
496 | |
496 | |
497 | static SV * |
497 | static SV * |
498 | encode_json (SV *scalar, U32 flags) |
498 | encode_json (SV *scalar, U32 flags) |
499 | { |
499 | { |
|
|
500 | enc_t enc; |
|
|
501 | |
500 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
502 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
501 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
503 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
502 | |
504 | |
503 | enc_t enc; |
|
|
504 | enc.flags = flags; |
505 | enc.flags = flags; |
505 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
506 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
506 | enc.cur = SvPVX (enc.sv); |
507 | enc.cur = SvPVX (enc.sv); |
507 | enc.end = SvEND (enc.sv); |
508 | enc.end = SvEND (enc.sv); |
508 | enc.indent = 0; |
509 | enc.indent = 0; |
… | |
… | |
670 | } |
671 | } |
671 | else if (ch >= 0x20 && ch <= 0x7f) |
672 | else if (ch >= 0x20 && ch <= 0x7f) |
672 | *cur++ = ch; |
673 | *cur++ = ch; |
673 | else if (ch >= 0x80) |
674 | else if (ch >= 0x80) |
674 | { |
675 | { |
|
|
676 | STRLEN clen; |
|
|
677 | UV uch; |
|
|
678 | |
675 | --dec->cur; |
679 | --dec->cur; |
676 | |
680 | |
677 | STRLEN clen; |
|
|
678 | UV uch = decode_utf8 (dec->cur, dec->end - dec->cur, &clen); |
681 | uch = decode_utf8 (dec->cur, dec->end - dec->cur, &clen); |
679 | if (clen == (STRLEN)-1) |
682 | if (clen == (STRLEN)-1) |
680 | ERR ("malformed UTF-8 character in JSON string"); |
683 | ERR ("malformed UTF-8 character in JSON string"); |
681 | |
684 | |
682 | do |
685 | do |
683 | *cur++ = *dec->cur++; |
686 | *cur++ = *dec->cur++; |
… | |
… | |
695 | ERR ("invalid character encountered while parsing JSON string"); |
698 | ERR ("invalid character encountered while parsing JSON string"); |
696 | } |
699 | } |
697 | } |
700 | } |
698 | while (cur < buf + SHORT_STRING_LEN); |
701 | while (cur < buf + SHORT_STRING_LEN); |
699 | |
702 | |
|
|
703 | { |
700 | STRLEN len = cur - buf; |
704 | STRLEN len = cur - buf; |
701 | |
705 | |
702 | if (sv) |
706 | if (sv) |
703 | { |
707 | { |
704 | SvGROW (sv, SvCUR (sv) + len + 1); |
708 | SvGROW (sv, SvCUR (sv) + len + 1); |
705 | memcpy (SvPVX (sv) + SvCUR (sv), buf, len); |
709 | memcpy (SvPVX (sv) + SvCUR (sv), buf, len); |
706 | SvCUR_set (sv, SvCUR (sv) + len); |
710 | SvCUR_set (sv, SvCUR (sv) + len); |
707 | } |
711 | } |
708 | else |
712 | else |
709 | sv = newSVpvn (buf, len); |
713 | sv = newSVpvn (buf, len); |
|
|
714 | } |
710 | } |
715 | } |
711 | while (*dec->cur != '"'); |
716 | while (*dec->cur != '"'); |
712 | |
717 | |
713 | ++dec->cur; |
718 | ++dec->cur; |
714 | |
719 | |
… | |
… | |
969 | } |
974 | } |
970 | |
975 | |
971 | static SV * |
976 | static SV * |
972 | decode_json (SV *string, U32 flags) |
977 | decode_json (SV *string, U32 flags) |
973 | { |
978 | { |
|
|
979 | dec_t dec; |
974 | SV *sv; |
980 | SV *sv; |
975 | |
981 | |
976 | SvUPGRADE (string, SVt_PV); |
982 | SvUPGRADE (string, SVt_PV); |
977 | |
983 | |
978 | if (flags & F_UTF8) |
984 | if (flags & F_UTF8) |
… | |
… | |
980 | else |
986 | else |
981 | sv_utf8_upgrade (string); |
987 | sv_utf8_upgrade (string); |
982 | |
988 | |
983 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
989 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
984 | |
990 | |
985 | dec_t dec; |
|
|
986 | dec.flags = flags; |
991 | dec.flags = flags; |
987 | dec.cur = SvPVX (string); |
992 | dec.cur = SvPVX (string); |
988 | dec.end = SvEND (string); |
993 | dec.end = SvEND (string); |
989 | dec.err = 0; |
994 | dec.err = 0; |
990 | dec.depth = 0; |
995 | dec.depth = 0; |
… | |
… | |
1074 | RETVAL = newSVsv (self); |
1079 | RETVAL = newSVsv (self); |
1075 | } |
1080 | } |
1076 | OUTPUT: |
1081 | OUTPUT: |
1077 | RETVAL |
1082 | RETVAL |
1078 | |
1083 | |
1079 | SV *max_depth (SV *self, int max_depth = 0x80000000UL) |
1084 | SV *max_depth (SV *self, UV max_depth = 0x80000000UL) |
1080 | CODE: |
1085 | CODE: |
1081 | { |
1086 | { |
1082 | UV *uv = SvJSON (self); |
1087 | UV *uv = SvJSON (self); |
1083 | UV log2 = 0; |
1088 | UV log2 = 0; |
1084 | |
1089 | |