Skip Menu |

This queue is for tickets about the MojoX-JSON-XS CPAN distribution.

Report information
The Basics
Id: 101904
Status: new
Priority: 0/
Queue: MojoX-JSON-XS

People
Owner: Nobody in particular
Requestors: oleg [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: (no value)
Broken in: (no value)
Fixed in: (no value)



Subject: Fixes for compatibility with new Mojo::JSON API [PATCH]
In the latest Mojolicious releases OOP API was completely removed from Mojo::JSON. It now has only decode_json/encode_json and from_json/to_json functions. In the attached patch I made changes to support new API. I also made this module to be not a plugin, to make it easier to use it even if you don't create full Mojolicious application with startup() method (all the same monkey patching is global). And finally I added few tests. This is mostly a copy of Mojo::JSON_XS (approved by Mojolicious author), but with JSON::XS as backend instead of Cpanel::JSON::XS. Would it be possible to include this changes?
Subject: mjxs.patch
diff -uNr MojoX-JSON-XS-0.01/lib/MojoX/JSON/XS.pm MojoX-JSON-XS-0.02/lib/MojoX/JSON/XS.pm --- MojoX-JSON-XS-0.01/lib/MojoX/JSON/XS.pm 2014-01-18 00:20:18.000000000 +0700 +++ MojoX-JSON-XS-0.02/lib/MojoX/JSON/XS.pm 2015-02-03 16:30:06.185594979 +0600 @@ -1,21 +1,26 @@ package MojoX::JSON::XS; -use Mojo::Base 'Mojolicious::Plugin'; use strict; - use Mojo::Util qw(monkey_patch); -use JSON::XS; +use Mojo::JSON (); +use JSON::XS (); +use Types::Serialiser; + +our $VERSION = "0.02"; + +my $Binary = JSON::XS->new->utf8(1)->allow_nonref(1) + ->allow_blessed(1)->convert_blessed(1); +my $Text = JSON::XS->new->utf8(0)->allow_nonref(1) + ->allow_blessed(1)->convert_blessed(1); + +monkey_patch 'Mojo::JSON', 'encode_json', sub { $Binary->encode($_[0]) }; +monkey_patch 'Mojo::JSON', 'decode_json', sub { $Binary->decode($_[0]) }; -our $VERSION = "0.01"; +monkey_patch 'Mojo::JSON', 'to_json', sub { $Text->encode($_[0]) }; +monkey_patch 'Mojo::JSON', 'from_json', sub { $Text->decode($_[0]) }; -sub register -{ - monkey_patch "Mojo::JSON", encode => sub { return encode_json( $_[1] ); }; - monkey_patch "Mojo::JSON", decode => sub { return decode_json( $_[1] ); }; - monkey_patch "Mojo::JSON", j => sub { if(ref $_[0]) { return encode_json( $_[0] ); } - else { return decode_json( $_[0] ); } - }; -} +monkey_patch 'Mojo::JSON', 'true', sub() { Types::Serialiser::true }; +monkey_patch 'Mojo::JSON', 'false', sub() { Types::Serialiser::false }; 1; @@ -27,23 +32,14 @@ =head1 SYNOPSIS - sub startup - { - # ... - - $self->plugin('MojoX::JSON::XS'); - - # ... - } + use MojoX::JSON::XS; # before exporting functions from Mojo::JSON + use Mojo::JSON 'decode_json'; + print decode_json('{"a": 12, "b": 14}')->{a}, "\n"; =head1 DESCRIPTION -Replaces Mojo::JSON methods encode, deocde and j with JSON::XS equivalient. +Replaces Mojo::JSON functions with JSON::XS equivalient. This gives faster processing, and removes the unnecessary encode of '/' chars in strings. -=head1 FEATURES - -It does not gracefully handle or skip blessed hashes - =cut diff -uNr MojoX-JSON-XS-0.01/Makefile.PL MojoX-JSON-XS-0.02/Makefile.PL --- MojoX-JSON-XS-0.01/Makefile.PL 2014-01-18 00:01:53.000000000 +0700 +++ MojoX-JSON-XS-0.02/Makefile.PL 2015-02-03 16:27:59.996978071 +0600 @@ -27,8 +27,8 @@ 'LICENSE' => 'perl', 'AUTHOR' => 'Companies House <chgovuk@cpan.org>', 'PREREQ_PM' => { - 'Mojolicious' => '4.00', - 'JSON:XS' => '3.01', + 'Mojolicious' => '4.82', + 'JSON:XS' => '3.0', }, 'dist' => { COMPRESS => "gzip -9", SUFFIX => "gz" }, # for ActivePerl: diff -uNr MojoX-JSON-XS-0.01/t/01_load.t MojoX-JSON-XS-0.02/t/01_load.t --- MojoX-JSON-XS-0.01/t/01_load.t 1970-01-01 07:00:00.000000000 +0700 +++ MojoX-JSON-XS-0.02/t/01_load.t 2015-02-03 16:24:51.048054355 +0600 @@ -0,0 +1,12 @@ +use strict; +use Test::More; +use Mojo::JSON; + +my $encode_ref = \&Mojo::JSON::encode_json; +my $decode_ref = \&Mojo::JSON::decode_json; + +require_ok('MojoX::JSON::XS'); +isnt(\&Mojo::JSON::encode_json, $encode_ref, 'encode_json properly replaced'); +isnt(\&Mojo::JSON::decode_json, $decode_ref, 'decode_json properly replaced'); + +done_testing; diff -uNr MojoX-JSON-XS-0.01/t/02_json.t MojoX-JSON-XS-0.02/t/02_json.t --- MojoX-JSON-XS-0.01/t/02_json.t 1970-01-01 07:00:00.000000000 +0700 +++ MojoX-JSON-XS-0.02/t/02_json.t 2015-02-03 16:40:09.952546645 +0600 @@ -0,0 +1,15 @@ +use strict; +use Test::More; +use MojoX::JSON::XS; +use Mojo::JSON ('encode_json', 'decode_json', 'false', 'true'); + +is_deeply( + decode_json('[1,[true], {"foo": "bar", "xxx": false}, "doom"]'), + [1, [true], {foo => 'bar', xxx => false}, 'doom'], + 'decode valid json' +); + +ok(!eval{decode_json('[1, {foo}, []]')}, 'decode invalid json'); +ok(encode_json([1, [true], {foo => 'bar', xxx => false}, 'doom']), 'encode json'); + +done_testing;