Codebase list libmoosex-storage-perl / 71944de
convert from JSON::Any -> JSON::MaybeXS for now, keep the weird utf8 handling as it is Karen Etheridge 9 years ago
16 changed file(s) with 39 addition(s) and 58 deletion(s). Raw diff Collapse all Expand all
00 Revision history for {{$dist->name}}
11
22 {{$NEXT}}
3 - switched the JSON serializer from JSON::Any to JSON::MaybeXS
34
45 0.47 2014-08-23 22:34:21Z
56 - report the versions of all JSON and YAML backends during tests, in an
5454 -description = Serialize to JSON. You should have at least one serialization format.
5555 -always_recommend = 1
5656 -default = 1
57 JSON::Any = 1.15
57 JSON::MaybeXS = 1.001000
5858
5959 [OptionalFeature / JSON-RuntimeRecommends]
6060 -description = Serialize to JSON. You should have at least one serialization format.
11 # ABSTRACT: A JSON serialization role
22
33 use Moose::Role;
4 use JSON::Any;
4 use JSON::MaybeXS;
55 use namespace::autoclean;
66
77 requires 'pack';
1313 # TODO ugh! this is surely wrong and should be fixed.
1414 utf8::encode($json) if utf8::is_utf8($json);
1515
16 $class->unpack( JSON::Any->new->jsonToObj($json), @args );
16 $class->unpack( JSON::MaybeXS->new({ utf8 => 1 })->decode( $json), @args );
1717 }
1818
1919 sub freeze {
2020 my ( $self, @args ) = @_;
2121
22 my $json = JSON::Any->new(canonical => 1)->objToJson( $self->pack(@args) );
22 my $json = JSON::MaybeXS->new({ utf8 => 1, canonical => 1 })->encode($self->pack(@args));
2323
24 # if it's valid utf8 mark it as such
2425 # TODO ugh! this is surely wrong and should be fixed.
25 utf8::decode($json) if !utf8::is_utf8($json) and utf8::valid($json); # if it's valid utf8 mark it as such
26 utf8::decode($json) if !utf8::is_utf8($json) and utf8::valid($json);
2627
2728 return $json;
2829 }
3131 sub _inflate_json {
3232 my ($self, $json) = @_;
3333
34 eval { require JSON::Any; JSON::Any->import };
34 eval { require JSON::MaybeXS; JSON::MaybeXS->import };
3535 confess "Could not load JSON module because : $@" if $@;
3636
37 # this is actually a bad idea, but for consistency, we'll have to keep
38 # doing it...
3739 utf8::encode($json) if utf8::is_utf8($json);
3840
39 my $data = eval { JSON::Any->jsonToObj($json) };
41 my $data = eval { JSON::MaybeXS->new({ utf8 => 1 })->decode($json) };
4042 if ($@) {
4143 confess "There was an error when attempting to peek at JSON: $@";
4244 }
88
99 my $dir = tempdir;
1010
11 use Test::Requires 'JSON::Any';
12 diag 'using JSON backend: ', JSON::Any->handlerType;
11 use Test::Requires 'JSON::MaybeXS';
12 diag 'using JSON backend: ', JSON;
1313
1414 BEGIN {
1515 plan tests => 10;
44 use Test::Deep;
55
66 use Test::Requires qw(
7 JSON::Any
7 JSON::MaybeXS
88 Test::Deep::JSON
99 );
10 diag 'using JSON backend: ', JSON::Any->handlerType;
10 diag 'using JSON backend: ', JSON;
1111
1212 BEGIN {
1313 plan tests => 11;
22
33 use Test::More;
44
5 # NOTE:
6 # this is because JSON::XS (and Cpanel::JSON::XS) is
7 # the only one which really gets utf8 correct
8 # - SL
9 BEGIN {
10 $ENV{JSON_ANY_ORDER} = 'XS CPANEL';
11 $ENV{JSON_ANY_CONFIG} = "utf8=0,canonical=1";
12 }
13
145 use Test::Requires qw(
156 Encode
16 JSON::Any
7 JSON::MaybeXS
178 );
18 diag 'using JSON backend: ', JSON::Any->handlerType;
9 diag 'using JSON backend: ', JSON;
1910
2011 binmode $_, ':utf8' foreach map { Test::Builder->new->$_ } qw(output failure_output todo_output);
2112 binmode STDOUT, ':utf8';
33 use Test::More;
44 use Test::Deep;
55
6 use Test::Requires 'MooseX::Storage::Format::JSONpm';
6 use Test::Requires qw(MooseX::Storage::Format::JSONpm JSON::MaybeXS);
77
88 BEGIN {
99 plan tests => 6;
6060 my $json = eval { Bar->new(x => 10, y => 20)->freeze({ format => $p }) };
6161
6262 cmp_deeply(
63 JSON->new->decode($json),
63 decode_json($json),
6464 {
6565 '__CLASS__' => 'Bar-0.01',
6666 x => 10,
66
77 use Test::Requires qw(
88 Digest::HMAC_SHA1
9 JSON::Any
9 JSON::MaybeXS
1010 );
11 diag 'using JSON backend: ', JSON::Any->handlerType;
11 diag 'using JSON backend: ', JSON;
1212
1313 BEGIN {
1414 plan tests => 26;
77
88 use Test::Requires qw(
99 Test::Deep::JSON
10 JSON::Any
10 JSON::MaybeXS
1111 YAML::Any
1212 );
13 diag 'using JSON backend: ', JSON::Any->handlerType;
13 diag 'using JSON backend: ', JSON;
1414 diag 'using YAML backend: ', YAML::Any->implementation;
1515
1616 BEGIN {
99
1010 use Test::Requires qw(
1111 IO::AtomicFile
12 JSON::Any
12 JSON::MaybeXS
1313 );
14 diag 'using JSON backend: ', JSON::Any->handlerType;
14 diag 'using JSON backend: ', JSON;
1515
1616 BEGIN {
1717 plan tests => 24;
66 use File::Spec::Functions;
77 my $dir = tempdir;
88
9 use Test::Requires 'JSON::Any';
10 diag 'using JSON backend: ', JSON::Any->handlerType;
9 use Test::Requires 'JSON::MaybeXS';
10 diag 'using JSON backend: ', JSON;
1111
1212 BEGIN {
1313 plan tests => 14;
77 my $dir = tempdir( CLEANUP => 1 );
88
99 use Test::Requires qw(
10 JSON::Any
10 JSON::MaybeXS
1111 IO::AtomicFile
1212 );
13 diag 'using JSON backend: ', JSON::Any->handlerType;
13 diag 'using JSON backend: ', JSON;
1414
1515 BEGIN {
1616 plan tests => 14;
55 use File::Spec::Functions;
66 my $dir = tempdir( CLEANUP => 1 );
77
8 # NOTE:
9 # this is because JSON::XS (and Cpanel::JSON::XS) is
10 # the only one which really gets utf8 correct
11 # - SL
12 BEGIN {
13 $ENV{JSON_ANY_ORDER} = 'XS CPANEL';
14 $ENV{JSON_ANY_CONFIG} = "utf8=0,canonical=1";
15 }
16
178 use Test::Requires qw(
18 JSON::Any
9 JSON::MaybeXS
1910 IO::AtomicFile
2011 );
21 diag 'using JSON backend: ', JSON::Any->handlerType;
12 diag 'using JSON backend: ', JSON;
13
14 diag 'using JSON backend: ', JSON;
2215
2316 plan tests => 8;
2417 use_ok('MooseX::Storage');
55 use File::Spec::Functions;
66 my $dir = tempdir;
77
8 # NOTE:
9 # this is because JSON::XS (and Cpanel::JSON::XS) is
10 # the only one which really gets utf8 correct
11 # - SL
12 BEGIN {
13 $ENV{JSON_ANY_ORDER} = 'XS CPANEL';
14 $ENV{JSON_ANY_CONFIG} = "utf8=0,canonical=1";
15 }
16
178 use Test::Requires qw(
18 JSON::Any
9 JSON::MaybeXS
1910 IO::AtomicFile
2011 );
21 diag 'using JSON backend: ', JSON::Any->handlerType;
12 diag 'using JSON backend: ', JSON;
2213
2314 binmode $_, ':utf8' foreach map { Test::Builder->new->$_ } qw(output failure_output todo_output);
2415 binmode STDOUT, ':utf8';
2516 binmode STDERR, ':utf8';
17
18 diag 'using JSON backend: ', JSON;
2619
2720 plan tests => 8;
2821 use_ok('MooseX::Storage');
22 use Test::More;
33 use Test::Fatal;
44
5 use Test::Requires 'JSON::Any';
6 diag 'using JSON backend: ', JSON::Any->handlerType;
5 use Test::Requires 'JSON::MaybeXS';
6 diag 'using JSON backend: ', JSON;
77
88 {
99 package Thing;