From: Yuval Kogman Date: Fri, 5 Oct 2007 03:31:06 +0000 (+0000) Subject: if json is valid utf8 mark it as such X-Git-Tag: 0_09~9 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=c9ff362a2d76f097ae83e473d0c31f05857ec5e4;p=gitmo%2FMooseX-Storage.git if json is valid utf8 mark it as such --- diff --git a/lib/MooseX/Storage/Format/JSON.pm b/lib/MooseX/Storage/Format/JSON.pm index e26cadb..e5b4817 100644 --- a/lib/MooseX/Storage/Format/JSON.pm +++ b/lib/MooseX/Storage/Format/JSON.pm @@ -5,6 +5,7 @@ use Moose::Role; no warnings 'once'; use JSON::Any; +use utf8 (); our $VERSION = '0.02'; our $AUTHORITY = 'cpan:STEVAN'; @@ -15,13 +16,16 @@ requires 'unpack'; sub thaw { my ( $class, $json, @args ) = @_; local $JSON::UnMapping = 1; + utf8::encode($json) if utf8::is_utf8($json); $class->unpack( JSON::Any->jsonToObj($json), @args ); } sub freeze { my ( $self, @args ) = @_; local $JSON::UnMapping = 1; - JSON::Any->objToJson( $self->pack(@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; } 1;