From 931a07098fe5f4bfc01ac35e2e14da4ace26b37f Mon Sep 17 00:00:00 2001 From: Karen Etheridge Date: Fri, 26 Oct 2018 11:43:59 -0700 Subject: [PATCH] switch backends from JSON to JSON::MaybeXS This allows for more choices in backends, including Cpanel::JSON::XS, which has fewer incompatibilities. --- META.json | 4 ++-- cpanfile | 4 ++-- lib/JSON/RPC/Dispatch.pm | 4 ++-- lib/JSON/RPC/Legacy/Client.pm | 8 ++++---- lib/JSON/RPC/Legacy/Server.pm | 8 ++++---- t/002_basic.t | 7 ++++--- t/003_parser.t | 4 ++-- t/legacy/02_server.t | 3 ++- 8 files changed, 22 insertions(+), 20 deletions(-) diff --git a/META.json b/META.json index 0b01da7..39e01c5 100644 --- a/META.json +++ b/META.json @@ -52,14 +52,14 @@ }, "runtime" : { "recommends" : { - "JSON::XS" : "0" + "Cpanel::JSON::XS" : "0" }, "requires" : { "CGI" : "0", "Class::Accessor::Lite" : "0", "HTTP::Request" : "0", "HTTP::Response" : "0", - "JSON" : "0", + "JSON::MaybeXS" : "0", "LWP::UserAgent" : "0", "Plack" : "0", "Router::Simple" : "0", diff --git a/cpanfile b/cpanfile index 63e74d0..cee4dbf 100644 --- a/cpanfile +++ b/cpanfile @@ -2,12 +2,12 @@ requires 'CGI'; requires 'Class::Accessor::Lite'; requires 'HTTP::Request'; requires 'HTTP::Response'; -requires 'JSON'; +requires 'JSON::MaybeXS'; requires 'LWP::UserAgent'; requires 'Plack'; requires 'Router::Simple'; requires 'parent'; -recommends 'JSON::XS'; +recommends 'Cpanel::JSON::XS'; on build => sub { requires 'ExtUtils::MakeMaker', '6.36'; diff --git a/lib/JSON/RPC/Dispatch.pm b/lib/JSON/RPC/Dispatch.pm index c55437b..1b036ab 100644 --- a/lib/JSON/RPC/Dispatch.pm +++ b/lib/JSON/RPC/Dispatch.pm @@ -24,8 +24,8 @@ sub new { @args, }, $class; if (! $self->{coder}) { - require JSON; - $self->{coder} = JSON->new->utf8; + require JSON::MaybeXS; + $self->{coder} = JSON::MaybeXS::JSON()->new->utf8; } if (! $self->{parser}) { $self->{parser} = JSON::RPC::Parser->new( coder => $self->coder ) diff --git a/lib/JSON/RPC/Legacy/Client.pm b/lib/JSON/RPC/Legacy/Client.pm index b389c81..95f73b3 100644 --- a/lib/JSON/RPC/Legacy/Client.pm +++ b/lib/JSON/RPC/Legacy/Client.pm @@ -4,7 +4,7 @@ ############################################################################## use strict; -use JSON (); +use JSON::MaybeXS 'JSON'; use Carp (); ############################################################################## @@ -62,7 +62,7 @@ sub AUTOLOAD { sub create_json_coder { - JSON->new->allow_nonref->utf8; + JSON()->new->allow_nonref->utf8; } @@ -349,9 +349,9 @@ Setter/getter to L object. =item json Setter/getter to the JSON coder object. -Default is L, likes this: +The default is the backend returned by L, created like this: - $self->json( JSON->new->allow_nonref->utf8 ); + $self->json( JSON()->new->allow_nonref->utf8 ); $json = $self->json; diff --git a/lib/JSON/RPC/Legacy/Server.pm b/lib/JSON/RPC/Legacy/Server.pm index 1a63e23..41ee7b7 100644 --- a/lib/JSON/RPC/Legacy/Server.pm +++ b/lib/JSON/RPC/Legacy/Server.pm @@ -4,7 +4,6 @@ ############################################################################## use strict; -use JSON (); use Carp (); use HTTP::Request (); @@ -14,6 +13,7 @@ use HTTP::Response (); ############################################################################## package JSON::RPC::Legacy::Server; +use JSON::MaybeXS 'JSON'; my $JSONRPC_Procedure_Able; @@ -43,7 +43,7 @@ BEGIN { sub create_json_coder { - JSON->new->utf8; # assumes UTF8 + JSON()->new->utf8; # assumes UTF8 } @@ -569,9 +569,9 @@ An error code number in your procedure is an integer between 501 and 899. =item json Setter/Getter to json encoder/decoder object. -The default value is L object in the below way: +The default value is a JSON serializer object as returned by L in the following manner: - JSON->new->utf8 + JSON()->new->utf8 In your procedure, changes its behaviour. diff --git a/t/002_basic.t b/t/002_basic.t index fe46b62..c1d1a06 100644 --- a/t/002_basic.t +++ b/t/002_basic.t @@ -3,7 +3,7 @@ use warnings; use Test::More; use Plack::Test; use HTTP::Request; -use JSON; +use JSON::MaybeXS; BEGIN { use_ok "JSON::RPC::Dispatch"; @@ -15,7 +15,8 @@ BEGIN { subtest 'defaults' => sub { my $dispatch = JSON::RPC::Dispatch->new(); if (ok $dispatch->coder) { - isa_ok $dispatch->coder, 'JSON'; + my $json_backend = JSON::MaybeXS::JSON(); + isa_ok $dispatch->coder, $json_backend; } if (ok $dispatch->router) { @@ -28,7 +29,7 @@ subtest 'defaults' => sub { }; subtest 'normal dispatch' => sub { - my $coder = JSON->new; + my $coder = JSON()->new; my $router = Router::Simple->new; $router->connect( blowup => { handler => "Sum", diff --git a/t/003_parser.t b/t/003_parser.t index 792b83e..1cd3b7f 100644 --- a/t/003_parser.t +++ b/t/003_parser.t @@ -1,7 +1,7 @@ use strict; use Test::More; use Plack::Request; -use JSON; +use JSON::MaybeXS 'to_json', 'JSON'; use_ok "JSON::RPC::Parser"; use_ok "JSON::RPC::Procedure"; @@ -12,7 +12,7 @@ subtest 'basic' => sub { REQUEST_METHOD => "GET", } ); my $parser = JSON::RPC::Parser->new( - coder => JSON->new, + coder => JSON()->new, ); my $procedure = $parser->construct_from_req( $req ); diff --git a/t/legacy/02_server.t b/t/legacy/02_server.t index 5429c5e..ae48c9d 100644 --- a/t/legacy/02_server.t +++ b/t/legacy/02_server.t @@ -8,7 +8,8 @@ my $server = JSON::RPC::Legacy::Server->new; isa_ok($server, 'JSON::RPC::Legacy::Server'); -isa_ok($server->json, 'JSON'); +my $json_backend = JSON::MaybeXS::JSON(); +isa_ok($server->json, $json_backend); my $test = JSON::RPC::Legacy::Server::Test->new;