From: Graham Knop Date: Sat, 4 Feb 2017 06:45:54 +0000 (-0500) Subject: handle UTF-8 data in JSON correctly X-Git-Tag: v0.28~6 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=p5sagit%2FConfig-Any.git;a=commitdiff_plain;h=3a27e96d3f96ae99fa6bc1a78ec420b0a6392f0d handle UTF-8 data in JSON correctly --- diff --git a/lib/Config/Any/JSON.pm b/lib/Config/Any/JSON.pm index 950424f..785f396 100644 --- a/lib/Config/Any/JSON.pm +++ b/lib/Config/Any/JSON.pm @@ -45,7 +45,8 @@ sub load { my $class = shift; my $file = shift; - open( my $fh, $file ) or die $!; + open( my $fh, '<', $file ) or die $!; + binmode $fh; my $content = do { local $/; <$fh> }; close $fh; @@ -56,19 +57,20 @@ sub load { return $data; } elsif ( eval { require JSON::XS } ) { - my $decoder = JSON::XS->new->relaxed; + my $decoder = JSON::XS->new->utf8->relaxed; return $decoder->decode( $content ); } elsif ( eval { require JSON::Syck } ) { - return JSON::Syck::Load( $content ); + require Encode; + return JSON::Syck::Load( Encode::decode('UTF-8', $content ) ); } elsif ( eval { require JSON::PP; JSON::PP->VERSION( 2 ); } ) { - my $decoder = JSON::PP->new->relaxed; + my $decoder = JSON::PP->new->utf8->relaxed; return $decoder->decode( $content ); } require JSON; if ( eval { JSON->VERSION( 2 ) } ) { - return JSON::from_json( $content ); + return JSON::decode_json( $content ); } else { return JSON::jsonToObj( $content );