Permalink
Browse files

improve Mojo::JSON by reusing JSON::PP boolean constants

  • Loading branch information...
kraih committed Sep 24, 2015
1 parent baa77ff commit 7a65c82cc4e3f22a348e27e1663ce02ff5390583
Showing with 13 additions and 15 deletions.
  1. +1 −0 Changes
  2. +2 −0 Makefile.PL
  3. +6 −12 lib/Mojo/JSON.pm
  4. +4 −3 t/mojolicious/restful_lite_app.t
View
@@ -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.
View
@@ -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(
@@ -33,6 +34,7 @@ WriteMakefile(
},
PREREQ_PM => {
'IO::Socket::IP' => '0.26',
'JSON::PP' => '2.27103',
'Pod::Simple' => '3.09',
'Time::Local' => '1.2'
},
View
@@ -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 = (
'"' => '"',
@@ -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);
@@ -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;
@@ -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;
@@ -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'))) {
@@ -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
@@ -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;
@@ -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';

0 comments on commit 7a65c82

Please sign in to comment.