MooseX::Storage::Format::JSON and utf8 encoding backwards??\rr

[email protected] (Bill Moseley)
Newsgroups perl.moose
Message-ID <CAKhN_m7njTeDO8c0VDxYq1moPuGGKT5OPs9YHqC=-MHyRVEG4w@mail.gmail.com>
This code looks wrong to me.  I don't think that utf8 line should be there.

sub freeze {
    my ( $self, @args ) = @_;
    my $json = JSON::Any->objToJson( $self->pack(@args) );
    utf8::decode($json) if !utf8::is_utf8($json) and
utf8::valid($json); # if it's valid utf8 mark it as such
    return $json;
}


Normally, I would want to take a structure that might include character
data (i.e utf8 flag is on) and serialize it into (utf8) encoded bytes.   So
JSON::Any->objToJson does that.    In other words:

my %utf8_char = ( foo => 'hello ' . "\N{U+263A}" );
my $return_trip = JSON::Any->jsonToObj( JSON::Any->objToJson( \%utf8_char )
);
print Encode::is_utf8( $return_trip->{foo} ) ? "Yes utf8\n" : "Nope, not
ut8\n";


Works as expected:

Yes utf8


So, the "freeze" method quoted above first returns the utf8-encoded string
then does this:

    utf8::decode($json) if !utf8::is_utf8($json) and
utf8::valid($json); # if it's valid utf8 mark it as such


turns the string back into utf8 characters.

As a result when I send that frozen serialized string some place I get Wide
Character errors.



-- 
Bill Moseley
[email protected]
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.