Nick Kostirya has asked for the wisdom of the Perl Monks concerning the following question:

Why we have octets with the utf8 flag on output?
> perl -MJSON::XS -MData::Dumper -E 'say Dumper(JSON::XS->new()->decod +e(qq(["☺"])))' $VAR1 = [ "\x{e2}\x{98}\x{ba}" ];
I want to get octets without the utf8 flag... I don't want to downgrade the utf8 flag manually...

Replies are listed 'Best First'.
Re: JSON and utf8 flag
by choroba (Cardinal) on Sep 03, 2017 at 20:04 UTC
    -Mutf8 is missing on the command line:
    perl -Mutf8 -MJSON::XS -MData::Dumper -E 'say Dumper(JSON::XS->new->decode(qq(["☻"])))'
    $VAR1 = [
              "\x{263b}"
            ];
    
    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
      I get "☻" from external source. I want octets. I need max speed. :-)
Re: JSON and utf8 flag
by Nick Kostirya (Sexton) on Sep 04, 2017 at 07:17 UTC
    For a quick solution:
    > diff -u XS.xs_orig XS.xs --- XS.xs_orig 2017-09-05 13:30:07.933454000 +0300 +++ XS.xs 2017-09-06 08:35:35.224749000 +0300 @@ -1214,7 +1214,7 @@ SvPOK_only (sv); *SvEND (sv) = 0; - if (utf8) + if (utf8 && !(dec->json.flags & F_LATIN1)) SvUTF8_on (sv); } else

      Don't break JSON::XS because of a bug in your code!

        Where?

        use Devel::Peek; use JSON::XS; my $smile = "☻"; my $j = JSON::XS->new->latin1(1)->encode(["$smile"]); my $d = JSON::XS->new->utf8(1)->latin1(1)->decode($j); Dump $$d[0];
        Output:
        SV = PV(0x8f19788) at 0x8f323bc REFCNT = 1 FLAGS = (POK,pPOK,UTF8) PV = 0x8f4be68 "\342\230\273"\0 [UTF8 "\x{263b}"] CUR = 3 LEN = 10

        I put data without UTF8 flag to JSON and I want to get data without UTF8 flag from this JSON.

        How to do it?