Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

consolidate JSON handling around JSON::MaybeXS, to provide a sane backend everywhere #1341

Merged
merged 1 commit into from Sep 11, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions app.psgi
Expand Up @@ -18,7 +18,7 @@ use Config::JFDI;
use FindBin;
use lib "$FindBin::RealBin/lib";
use File::Path ();
use JSON ();
use JSON::MaybeXS ();
use MIME::Base64 ();
use MetaCPAN::Web;
use Plack::Builder;
Expand Down Expand Up @@ -78,7 +78,7 @@ my $app = Plack::App::URLMap->new;

# Pass $_[0] since the json subs may have a ($) protoype.
# Pass '' to base64 for a blank separator (instead of newlines).
MIME::Base64::encode( JSON::encode_json( $_[0] ), q[] );
MIME::Base64::encode( JSON::MaybeXS::encode_json( $_[0] ), q[] );
},
deserializer => sub {

Expand All @@ -87,7 +87,7 @@ my $app = Plack::App::URLMap->new;

# Use try/catch so JSON doesn't barf if the cookie is bad.
try {
JSON::decode_json( MIME::Base64::decode($cookie) );
JSON::MaybeXS::decode_json( MIME::Base64::decode($cookie) );
}

# No session.
Expand Down
2 changes: 1 addition & 1 deletion cpanfile
Expand Up @@ -39,7 +39,7 @@ requires 'HTTP::Request';
requires 'HTTP::Request::Common';
requires 'Hash::AsObject';
requires 'Hash::Merge';
requires 'JSON::XS', '3.01';
requires 'JSON::MaybeXS', '1.002000';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only problem I see here is that now we don't have any kind of XS JSON parser in the cpanfile, which will be a problem when we deploy to production.

requires 'List::MoreUtils';
requires 'List::Util';
requires 'Locale::Country','3.28';
Expand Down
4 changes: 2 additions & 2 deletions lib/MetaCPAN/Web/Controller/Account.pm
Expand Up @@ -3,7 +3,7 @@ package MetaCPAN::Web::Controller::Account;
use Moose;
use List::MoreUtils qw(pairwise);
use DateTime ();
use JSON::XS ();
use JSON::MaybeXS ();

BEGIN { extends 'MetaCPAN::Web::Controller' }

Expand Down Expand Up @@ -78,7 +78,7 @@ sub profile : Local {
for (qw(name asciiname gravatar_url city region country));
$data->{$_} = [ grep {$_} $req->param($_) ] for (qw(website email));
$data->{extra}
= eval { JSON->new->relaxed->utf8->decode( $req->params->{extra} ) };
= eval { JSON::MaybeXS->new->relaxed->utf8->decode( $req->params->{extra} ) };
$data->{donation} = undef unless ( $req->params->{donations} );

my $res
Expand Down
3 changes: 2 additions & 1 deletion lib/MetaCPAN/Web/Controller/Search/AutoComplete.pm
@@ -1,6 +1,7 @@
package MetaCPAN::Web::Controller::Search::AutoComplete;

use Moose;
use JSON::MaybeXS ();
use namespace::autoclean;

BEGIN { extends 'MetaCPAN::Web::Controller' }
Expand All @@ -11,7 +12,7 @@ sub index : Path {
my $query = join( q{ }, $c->req->param('q') );
my $data = $model->autocomplete($query)->recv;
$c->res->content_type('application/json');
$c->res->body( JSON::encode_json( $data->{results} ) );
$c->res->body( JSON::MaybeXS::encode_json( $data->{results} ) );
}

1;
2 changes: 1 addition & 1 deletion lib/MetaCPAN/Web/Model/API.pm
Expand Up @@ -6,7 +6,7 @@ extends 'Catalyst::Model';
has [qw(api api_secure)] => ( is => 'ro' );

use Encode ();
use JSON;
use JSON::MaybeXS;
use HTTP::Request ();
use AnyEvent::Curl::Multi;
use Try::Tiny 0.09;
Expand Down
6 changes: 3 additions & 3 deletions lib/MetaCPAN/Web/View/HTML.pm
Expand Up @@ -9,7 +9,7 @@ use Digest::MD5 qw(md5_hex);
use Digest::SHA1;
use URI;
use URI::QueryParam;
use JSON;
use JSON::MaybeXS;
use Gravatar::URL;
use Regexp::Common qw(time);
use Template::Plugin::DateTime;
Expand Down Expand Up @@ -90,7 +90,7 @@ Template::Alloy->define_vmethod( 'text',
Template::Alloy->define_vmethod(
'hash',
pretty_json => sub {
JSON->new->utf8->pretty->encode(shift);
JSON::MaybeXS->new->utf8->pretty->encode(shift);
}
);

Expand Down Expand Up @@ -138,7 +138,7 @@ Template::Alloy->define_vmethod(
Template::Alloy->define_vmethod(
'array',
json => sub {
JSON::encode_json(shift);
JSON::MaybeXS::encode_json(shift);
}
);

Expand Down
2 changes: 1 addition & 1 deletion t/controller/search/autocomplete.t
Expand Up @@ -4,7 +4,7 @@ use utf8;
use Encode qw(encode is_utf8);
use Test::More;
use MetaCPAN::Web::Test;
use JSON::XS;
use JSON::MaybeXS;

my @tests = (
[ moose => 'Moose' ],
Expand Down
2 changes: 1 addition & 1 deletion t/model/release.t
Expand Up @@ -2,7 +2,7 @@ use strict;
use warnings;

use Test::More;
use JSON;
use JSON::MaybeXS;
use MetaCPAN::Web;

sub search_release {
Expand Down