From: Alexander Hartmaier Date: Wed, 13 Jun 2018 23:20:28 +0000 (+0200) Subject: Use JSON::MaybeXS for improved performance X-Git-Tag: 2.007001~1 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Controller-DBIC-API.git;a=commitdiff_plain;h=a5949bfde189427d34ccd19f15d8656156454936 Use JSON::MaybeXS for improved performance --- diff --git a/Changes b/Changes index aa3b4ed..10ff9ae 100644 --- a/Changes +++ b/Changes @@ -4,6 +4,7 @@ Revision history for Catalyst-Controller-DBIC-API: {{ $dist->version }} - Improve data_root & item_root documentation - Fix grouped_by request argument documentation - Don't overwrite already set response data when return_object is enabled + - Use JSON::MaybeXS for improved performance 2.006002 2014-08-26 12:31:27+02:00 Europe/Vienna - Fix missing Data::Printer test requirement diff --git a/dist.ini b/dist.ini index 7175d31..4fda33d 100644 --- a/dist.ini +++ b/dist.ini @@ -40,7 +40,7 @@ DBIx::Class = 0.08103 Catalyst::Runtime = 5.90020 Catalyst::Action::Serialize = 0.83 CGI::Expand = 2.02 -JSON = 2.50 +JSON::MaybeXS = 1.004000 Data::DPath::Validator = 0.093411 Catalyst::Model::DBIC::Schema = 0.20 MooseX::Types::Structured = 0.20 diff --git a/lib/Catalyst/Controller/DBIC/API.pm b/lib/Catalyst/Controller/DBIC/API.pm index 058e790..fc929cb 100644 --- a/lib/Catalyst/Controller/DBIC/API.pm +++ b/lib/Catalyst/Controller/DBIC/API.pm @@ -6,7 +6,7 @@ BEGIN { extends 'Catalyst::Controller'; } use CGI::Expand (); use DBIx::Class::ResultClass::HashRefInflator; -use JSON (); +use JSON::MaybeXS (); use Test::Deep::NoTest('eq_deeply'); use MooseX::Types::Moose(':all'); use Moose::Util; @@ -17,14 +17,14 @@ use namespace::autoclean; has '_json' => ( is => 'ro', - isa => 'JSON', + isa => JSON::MaybeXS::JSON(), lazy_build => 1, ); sub _build__json { # no ->utf8 here because the request params get decoded by Catalyst - return JSON->new; + return JSON::MaybeXS->new; } with 'Catalyst::Controller::DBIC::API::StoredResultSource', @@ -721,7 +721,7 @@ sub validate_object { } # check for multiple values - if ( ref($value) && !( reftype($value) eq reftype(JSON::true) ) ) + if ( ref($value) && !( reftype($value) eq reftype(JSON::MaybeXS::true) ) ) { require Data::Dumper; die @@ -815,7 +815,7 @@ sub update_object_from_params { foreach my $key ( keys %$params ) { my $value = $params->{$key}; - if ( ref($value) && !( reftype($value) eq reftype(JSON::true) ) ) { + if ( ref($value) && !( reftype($value) eq reftype(JSON::MaybeXS::true) ) ) { $self->update_object_relation( $c, $object, delete $params->{$key}, $key ); } @@ -850,7 +850,7 @@ sub update_object_relation { if ($row) { foreach my $key ( keys %$related_params ) { my $value = $related_params->{$key}; - if ( ref($value) && !( reftype($value) eq reftype(JSON::true) ) ) + if ( ref($value) && !( reftype($value) eq reftype(JSON::MaybeXS::true) ) ) { $self->update_object_relation( $c, $row, delete $related_params->{$key}, $key ); @@ -888,7 +888,7 @@ sub insert_object_from_params { my %rels; while ( my ( $key, $value ) = each %{$params} ) { - if ( ref($value) && !( reftype($value) eq reftype(JSON::true) ) ) { + if ( ref($value) && !( reftype($value) eq reftype(JSON::MaybeXS::true) ) ) { $rels{$key} = $value; } @@ -963,7 +963,7 @@ sub end : Private { if ( $c->res->status == 200 ) { $c->stash->{ $self->stash_key }->{success} = - $self->use_json_boolean ? JSON::true : 'true'; + $self->use_json_boolean ? JSON::MaybeXS::true : 'true'; if ( $self->return_object && $c->req->has_objects && ! exists $c->stash->{ $self->stash_key }->{ $self->data_root } ) { @@ -978,7 +978,7 @@ sub end : Private { } else { $c->stash->{ $self->stash_key }->{success} = - $self->use_json_boolean ? JSON::false : 'false'; + $self->use_json_boolean ? JSON::MaybeXS::false : 'false'; $c->stash->{ $self->stash_key }->{messages} = $self->get_errors($c) if $self->has_errors($c); @@ -1157,8 +1157,9 @@ $c->stash->{$self->stash_key}->{$self->item_root} and item_root default to =head3 use_json_boolean By default, the response success status is set to a string value of "true" or -"false". If this attribute is true, JSON's true() and false() will be used -instead. Note, this does not effect other internal processing of boolean values. +"false". If this attribute is true, JSON::MaybeXS's true() and false() will be +used instead. Note, this does not effect other internal processing of boolean +values. =head3 count_arg, page_arg, select_arg, search_arg, grouped_by_arg, ordered_by_arg, prefetch_arg, as_arg, total_entries_arg @@ -1318,9 +1319,10 @@ status quo. The internals were revamped to use more modern tools such as Moose and its role system to refactor functionality out into self-contained roles. To this end, internally, this module now understands JSON boolean values (as -represented by the JSON module) and will Do The Right Thing in handling those -values. This means you can have ColumnInflators installed that can covert -between JSON booleans and whatever your database wants for boolean values. +represented by the JSON::MaybeXS module) and will Do The Right Thing in +handling those values. This means you can have ColumnInflators installed that +can covert between JSON booleans and whatever your database wants for boolean +values. Validation for various *_allows or *_exposes is now accomplished via Data::DPath::Validator with a lightly simplified, via a subclass of diff --git a/t/rest/create.t b/t/rest/create.t index eff1bfa..06d3b59 100644 --- a/t/rest/create.t +++ b/t/rest/create.t @@ -10,9 +10,9 @@ use DBICTest; use Test::More; use Test::WWW::Mechanize::Catalyst 'RestTest'; use HTTP::Request::Common; -use JSON; +use JSON::MaybeXS; -my $json = JSON->new->utf8; +my $json = JSON::MaybeXS->new(utf8 => 1); my $mech = Test::WWW::Mechanize::Catalyst->new; ok( my $schema = DBICTest->init_schema(), 'got schema' ); @@ -138,7 +138,7 @@ my $track_create_url = "$base/api/rest/track"; is( $rs->count, 3, 'no records created' ); my $response = $json->decode( $mech->content ); - is( $response->{success}, JSON::false, + is( $response->{success}, JSON::MaybeXS::false, 'success property returns unquoted false' ); like( $response->{messages}->[0], diff --git a/t/rest/delete.t b/t/rest/delete.t index 0cd4502..0df60d5 100644 --- a/t/rest/delete.t +++ b/t/rest/delete.t @@ -11,9 +11,9 @@ use DBICTest; use Test::More; use Test::WWW::Mechanize::Catalyst 'RestTest'; use HTTP::Request::Common qw/ DELETE /; -use JSON; +use JSON::MaybeXS; -my $json = JSON->new->utf8; +my $json = JSON::MaybeXS->new(utf8 => 1); my $mech = Test::WWW::Mechanize::Catalyst->new; ok( my $schema = DBICTest->init_schema(), 'got schema' ); diff --git a/t/rest/item.t b/t/rest/item.t index 01203bd..845bc1a 100644 --- a/t/rest/item.t +++ b/t/rest/item.t @@ -11,9 +11,9 @@ use URI; use Test::More; use Test::WWW::Mechanize::Catalyst 'RestTest'; use HTTP::Request::Common; -use JSON; +use JSON::MaybeXS; -my $json = JSON->new->utf8; +my $json = JSON::MaybeXS->new(utf8 => 1); my $mech = Test::WWW::Mechanize::Catalyst->new; ok( my $schema = DBICTest->init_schema(), 'got schema' ); @@ -70,7 +70,7 @@ my $track_view_url = "$base/api/rest/track/"; $response, # track does set use_json_boolean - { data => \%expected_response, success => JSON::true }, + { data => \%expected_response, success => JSON::MaybeXS::true }, 'correct data returned for track with datetime' ); } diff --git a/t/rest/list.t b/t/rest/list.t index b63cc98..033411a 100644 --- a/t/rest/list.t +++ b/t/rest/list.t @@ -11,10 +11,10 @@ use URI; use Test::More; use Test::WWW::Mechanize::Catalyst 'RestTest'; use HTTP::Request::Common; -use JSON; +use JSON::MaybeXS; use Data::Printer; -my $json = JSON->new->utf8; +my $json = JSON::MaybeXS->new(utf8 => 1); my $mech = Test::WWW::Mechanize::Catalyst->new; ok( my $schema = DBICTest->init_schema(), 'got schema' ); @@ -206,7 +206,7 @@ my $track_list_url = "$base/api/rest/track"; $response, # track does set use_json_boolean - { list => \@expected_response, success => JSON::true, totalcount => 15 }, + { list => \@expected_response, success => JSON::MaybeXS::true, totalcount => 15 }, 'correct data returned for static configured paging' ); } @@ -326,7 +326,7 @@ my $track_list_url = "$base/api/rest/track"; is_deeply( $response, # track does set use_json_boolean - { list => \@expected_response, success => JSON::true, totalcount => 2 }, + { list => \@expected_response, success => JSON::MaybeXS::true, totalcount => 2 }, 'correct data returned for -and|-or search param' ) or diag p($case) . p($response); diff --git a/t/rest/stashedclass.t b/t/rest/stashedclass.t index f28fc41..79dd174 100644 --- a/t/rest/stashedclass.t +++ b/t/rest/stashedclass.t @@ -11,9 +11,9 @@ use URI; use Test::More; use Test::WWW::Mechanize::Catalyst 'RestTest'; use HTTP::Request::Common; -use JSON; +use JSON::MaybeXS; -my $json = JSON->new->utf8; +my $json = JSON::MaybeXS->new(utf8 => 1); my $mech = Test::WWW::Mechanize::Catalyst->new; ok( my $schema = DBICTest->init_schema(), 'got schema' ); diff --git a/t/rest/update.t b/t/rest/update.t index d0a916e..3d8bae2 100644 --- a/t/rest/update.t +++ b/t/rest/update.t @@ -11,9 +11,9 @@ use DBICTest; use Test::More; use Test::WWW::Mechanize::Catalyst 'RestTest'; use HTTP::Request::Common; -use JSON; +use JSON::MaybeXS; -my $json = JSON->new->utf8; +my $json = JSON::MaybeXS->new(utf8 => 1); my $mech = Test::WWW::Mechanize::Catalyst->new; ok( my $schema = DBICTest->init_schema(), 'got schema' ); @@ -119,7 +119,7 @@ my $tracks_update_url = $track_url; $mech->request($req); cmp_ok( $mech->status, '==', 400, 'Attempt to update three tracks fails' ); my $response = $json->decode( $mech->content ); - is( $response->{success}, JSON::false, + is( $response->{success}, JSON::MaybeXS::false, 'success property returns unquoted false' ); like( $response->{messages}->[0], @@ -175,7 +175,7 @@ my $tracks_update_url = $track_url; cmp_ok( $mech->status, '==', 400, 'Attempt to update three nonexisting tracks fails' ); my $response = $json->decode( $mech->content ); - is( $response->{success}, JSON::false, + is( $response->{success}, JSON::MaybeXS::false, 'success property returns unquoted false' ); like( $response->{messages}->[0], diff --git a/t/rpc/create.t b/t/rpc/create.t index f2757a3..7f46b2d 100644 --- a/t/rpc/create.t +++ b/t/rpc/create.t @@ -11,9 +11,9 @@ use DBICTest; use Test::More; use Test::WWW::Mechanize::Catalyst 'RestTest'; use HTTP::Request::Common; -use JSON; +use JSON::MaybeXS; -my $json = JSON->new->utf8; +my $json = JSON::MaybeXS->new(utf8 => 1); my $mech = Test::WWW::Mechanize::Catalyst->new; ok( my $schema = DBICTest->init_schema(), 'got schema' ); diff --git a/t/rpc/delete.t b/t/rpc/delete.t index 030addd..877168b 100644 --- a/t/rpc/delete.t +++ b/t/rpc/delete.t @@ -11,9 +11,9 @@ use DBICTest; use Test::More; use Test::WWW::Mechanize::Catalyst 'RestTest'; use HTTP::Request::Common; -use JSON; +use JSON::MaybeXS; -my $json = JSON->new->utf8; +my $json = JSON::MaybeXS->new(utf8 => 1); my $mech = Test::WWW::Mechanize::Catalyst->new; ok( my $schema = DBICTest->init_schema(), 'got schema' ); diff --git a/t/rpc/item.t b/t/rpc/item.t index 12eb5bb..417bffb 100644 --- a/t/rpc/item.t +++ b/t/rpc/item.t @@ -11,9 +11,9 @@ use URI; use Test::More; use Test::WWW::Mechanize::Catalyst 'RestTest'; use HTTP::Request::Common; -use JSON; +use JSON::MaybeXS; -my $json = JSON->new->utf8; +my $json = JSON::MaybeXS->new(utf8 => 1); my $mech = Test::WWW::Mechanize::Catalyst->new; ok( my $schema = DBICTest->init_schema(), 'got schema' ); diff --git a/t/rpc/list.t b/t/rpc/list.t index 44b781a..93200a4 100644 --- a/t/rpc/list.t +++ b/t/rpc/list.t @@ -11,9 +11,9 @@ use URI; use Test::More; use Test::WWW::Mechanize::Catalyst 'RestTest'; use HTTP::Request::Common; -use JSON; +use JSON::MaybeXS; -my $json = JSON->new->utf8; +my $json = JSON::MaybeXS->new(utf8 => 1); my $mech = Test::WWW::Mechanize::Catalyst->new; ok( my $schema = DBICTest->init_schema(), 'got schema' ); diff --git a/t/rpc/list_json_search.t b/t/rpc/list_json_search.t index e8db665..dab6b7e 100644 --- a/t/rpc/list_json_search.t +++ b/t/rpc/list_json_search.t @@ -11,9 +11,9 @@ use URI; use Test::More; use Test::WWW::Mechanize::Catalyst 'RestTest'; use HTTP::Request::Common; -use JSON; +use JSON::MaybeXS; -my $json = JSON->new->utf8; +my $json = JSON::MaybeXS->new(utf8 => 1); my $mech = Test::WWW::Mechanize::Catalyst->new; ok( my $schema = DBICTest->init_schema(), 'got schema' ); diff --git a/t/rpc/list_prefetch.t b/t/rpc/list_prefetch.t index 118bdc3..3c8b1a4 100644 --- a/t/rpc/list_prefetch.t +++ b/t/rpc/list_prefetch.t @@ -11,9 +11,9 @@ use URI; use Test::More tests => 17; use Test::WWW::Mechanize::Catalyst 'RestTest'; use HTTP::Request::Common; -use JSON; +use JSON::MaybeXS; -my $json = JSON->new->utf8; +my $json = JSON::MaybeXS->new(utf8 => 1); my $mech = Test::WWW::Mechanize::Catalyst->new; ok( my $schema = DBICTest->init_schema(), 'got schema' ); diff --git a/t/rpc/list_search_allows.t b/t/rpc/list_search_allows.t index 64d1249..4742421 100644 --- a/t/rpc/list_search_allows.t +++ b/t/rpc/list_search_allows.t @@ -11,9 +11,9 @@ use URI; use Test::More; use Test::WWW::Mechanize::Catalyst 'RestTest'; use HTTP::Request::Common; -use JSON; +use JSON::MaybeXS; -my $json = JSON->new->utf8; +my $json = JSON::MaybeXS->new(utf8 => 1); my $mech = Test::WWW::Mechanize::Catalyst->new; ok( my $schema = DBICTest->init_schema(), 'got schema' ); diff --git a/t/rpc/setup_dbic_args.t b/t/rpc/setup_dbic_args.t index 57bdf60..9cb00eb 100644 --- a/t/rpc/setup_dbic_args.t +++ b/t/rpc/setup_dbic_args.t @@ -11,9 +11,9 @@ use URI; use Test::More; use Test::WWW::Mechanize::Catalyst 'RestTest'; use HTTP::Request::Common; -use JSON; +use JSON::MaybeXS; -my $json = JSON->new->utf8; +my $json = JSON::MaybeXS->new(utf8 => 1); my $mech = Test::WWW::Mechanize::Catalyst->new; ok( my $schema = DBICTest->init_schema(), 'got schema' ); diff --git a/t/rpc/update.t b/t/rpc/update.t index c0fcc77..2e36a6d 100644 --- a/t/rpc/update.t +++ b/t/rpc/update.t @@ -11,9 +11,9 @@ use DBICTest; use Test::More; use Test::WWW::Mechanize::Catalyst 'RestTest'; use HTTP::Request::Common; -use JSON; +use JSON::MaybeXS; -my $json = JSON->new->utf8; +my $json = JSON::MaybeXS->new(utf8 => 1); my $mech = Test::WWW::Mechanize::Catalyst->new; ok( my $schema = DBICTest->init_schema(), 'got schema' );