Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
improve Mojo::JSON by reusing JSON::PP boolean constants
  • Loading branch information
kraih committed Sep 24, 2015
1 parent baa77ff commit 7a65c82
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 15 deletions.
1 change: 1 addition & 0 deletions Changes
@@ -1,5 +1,6 @@

6.22 2015-09-24
- Improved Mojo::JSON by reusing JSON::PP boolean constants.

6.21 2015-09-23
- Added val method to Mojo::DOM.
Expand Down
2 changes: 2 additions & 0 deletions Makefile.PL
Expand Up @@ -6,6 +6,7 @@ use warnings;
use ExtUtils::MakeMaker;

# Pod::Simple 3.09 first shipped with Perl 5.11.2
# JSON::PP 2.27103 first shipped with Perl 5.13.9
# Time::Local 1.2 first shipped with Perl 5.13.9
# IO::Socket::IP 0.26 first shipped with Perl 5.19.8
WriteMakefile(
Expand Down Expand Up @@ -33,6 +34,7 @@ WriteMakefile(
},
PREREQ_PM => {
'IO::Socket::IP' => '0.26',
'JSON::PP' => '2.27103',
'Pod::Simple' => '3.09',
'Time::Local' => '1.2'
},
Expand Down
18 changes: 6 additions & 12 deletions lib/Mojo/JSON.pm
Expand Up @@ -4,14 +4,12 @@ use Mojo::Base -strict;
use B;
use Carp 'croak';
use Exporter 'import';
use JSON::PP ();
use Mojo::Util;
use Scalar::Util 'blessed';

our @EXPORT_OK = qw(decode_json encode_json false from_json j to_json true);

# Booleans
my ($FALSE, $TRUE) = map { bless \(my $dummy = $_), 'Mojo::JSON::_Bool' } 0, 1;

# Escaped special character map (with u2028 and u2029)
my %ESCAPE = (
'"' => '"',
Expand All @@ -35,7 +33,7 @@ sub decode_json {

sub encode_json { Mojo::Util::encode 'UTF-8', _encode_value(shift) }

sub false () {$FALSE}
sub false () {JSON::PP::false}

sub from_json {
my $err = _decode(\my $value, shift, 1);
Expand All @@ -49,7 +47,7 @@ sub j {

sub to_json { _encode_value(shift) }

sub true () {$TRUE}
sub true () {JSON::PP::true}

sub _decode {
my $valueref = shift;
Expand Down Expand Up @@ -197,10 +195,10 @@ sub _decode_value {
if /\G([-]?(?:0|[1-9][0-9]*)(?:\.[0-9]*)?(?:[eE][+-]?[0-9]+)?)/gc;

# True
return $TRUE if /\Gtrue/gc;
return true() if /\Gtrue/gc;

# False
return $FALSE if /\Gfalse/gc;
return false() if /\Gfalse/gc;

# Null
return undef if /\Gnull/gc;
Expand Down Expand Up @@ -240,7 +238,7 @@ sub _encode_value {

# True or false
return $$value ? 'true' : 'false' if $ref eq 'SCALAR';
return $value ? 'true' : 'false' if $ref eq 'Mojo::JSON::_Bool';
return $value ? 'true' : 'false' if $ref eq 'JSON::PP::Boolean';

# Blessed reference with TO_JSON method
if (blessed $value && (my $sub = $value->can('TO_JSON'))) {
Expand Down Expand Up @@ -277,10 +275,6 @@ sub _throw {
die "$context\n";
}

# Emulate boolean type
package Mojo::JSON::_Bool;
use overload '""' => sub { ${$_[0]} }, fallback => 1;

1;

=encoding utf8
Expand Down
7 changes: 4 additions & 3 deletions t/mojolicious/restful_lite_app.t
Expand Up @@ -3,6 +3,7 @@ use Mojo::Base -strict;
BEGIN { $ENV{MOJO_REACTOR} = 'Mojo::Reactor::Poll' }

use Test::More;
use Mojo::JSON qw(false true);
use Mojolicious::Lite;
use Test::Mojo;

Expand Down Expand Up @@ -88,14 +89,14 @@ $t->get_ok('/accepts.html?format=json' => {Accept => 'text/plain'})
->status_is(200)->json_is({best => 'txt'});

# Nothing
$t->get_ok('/wants_json')->status_is(200)->json_is({wants_json => 0});
$t->get_ok('/wants_json')->status_is(200)->json_is({wants_json => false});

# Unsupported
$t->get_ok('/wants_json.xml')->status_is(200)->json_is({wants_json => 0});
$t->get_ok('/wants_json.xml')->status_is(200)->json_is({wants_json => false});

# Accept "json"
$t->get_ok('/wants_json' => {Accept => 'application/json'})->status_is(200)
->json_is({wants_json => 1});
->json_is({wants_json => true});

# Ajax
my $ajax = 'text/html;q=0.1,application/json';
Expand Down

0 comments on commit 7a65c82

Please sign in to comment.