Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
removed assigned_as_number and looks_like_bool functions again
  • Loading branch information
kraih committed Oct 25, 2012
1 parent 06a9bf2 commit 0a125bb
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 121 deletions.
4 changes: 0 additions & 4 deletions Changes
@@ -1,9 +1,5 @@

3.52 2012-10-25
- Deprecated Mojo::JSON->true and Mojo::JSON->false in favor of native Perl
booleans !!1 and !!0.
- Added assigned_as_number function to Mojo::Util.
- Added looks_like_bool function to Mojo::Util. (Adura, sri)
- Improved documentation.
- Improved tests.

Expand Down
60 changes: 38 additions & 22 deletions lib/Mojo/JSON.pm
@@ -1,11 +1,16 @@
package Mojo::JSON;
use Mojo::Base -base;

use Mojo::Util qw(assigned_as_number looks_like_bool);
use B;
use Mojo::Util;
use Scalar::Util 'blessed';

has 'error';

# Literal names
my $FALSE = bless \(my $false = 0), 'Mojo::JSON::_Bool';
my $TRUE = bless \(my $true = 1), 'Mojo::JSON::_Bool';

# Escaped special character map (with u2028 and u2029)
my %ESCAPE = (
'"' => '"',
Expand Down Expand Up @@ -93,18 +98,8 @@ sub encode {
return Mojo::Util::encode 'UTF-8', _encode_values($ref);
}

# DEPRECATED in Rainbow!
sub false {
warn qq/Mojo::JSON->false has been DEPRECATED in favor of "!!0"!\n/;
return !!0;
}

# DEPRECATED in Rainbow!
sub true {
warn qq/Mojo::JSON->true has been DEPRECATED in favor of "!!1"!\n/;
return !!1;
}

sub false {$FALSE}
sub true {$TRUE}

sub _decode_array {
my @array;
Expand Down Expand Up @@ -232,10 +227,10 @@ sub _decode_value {
if m/\G([-]?(?:0|[1-9][0-9]*)(?:\.[0-9]*)?(?:[eE][+-]?[0-9]+)?)/gc;

# True
return !!1 if m/\Gtrue/gc;
return $TRUE if m/\Gtrue/gc;

# False
return !!0 if m/\Gfalse/gc;
return $FALSE if m/\Gfalse/gc;

# Null
return undef if m/\Gnull/gc;
Expand Down Expand Up @@ -281,6 +276,9 @@ sub _encode_values {
# Object
return _encode_object($value) if $ref eq 'HASH';

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

# Blessed reference with TO_JSON method
if (blessed $value && (my $sub = $value->can('TO_JSON'))) {
return _encode_values($value->$sub);
Expand All @@ -290,11 +288,10 @@ sub _encode_values {
# Null
return 'null' unless defined $value;

# True or false
return $value ? 'true' : 'false' if looks_like_bool $value;

# Number
return $value if assigned_as_number $value;
my $flags = B::svref_2object(\$value)->FLAGS;
return $value
if $flags & (B::SVp_IOK | B::SVp_NOK) && !($flags & B::SVp_POK);

# String
return _encode_string($value);
Expand All @@ -317,6 +314,10 @@ sub _exception {
die "$context\n";
}

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

1;

=head1 NAME
Expand Down Expand Up @@ -344,10 +345,11 @@ stringify them if it doesn't exist.
[1, -2, 3] -> [1, -2, 3]
{"foo": "bar"} -> {foo => 'bar'}
Literal names will be translated to and from a similar native Perl value.
Literal names will be translated to and from L<Mojo::JSON> constants or a
similar native Perl value.
true -> !!1
false -> !!0
true -> Mojo::JSON->true
false -> Mojo::JSON->false
null -> undef
Decoding UTF-16 (LE/BE) and UTF-32 (LE/BE) will be handled transparently,
Expand Down Expand Up @@ -383,6 +385,20 @@ Decode JSON.
Encode Perl data structure.
=head2 C<false>
my $false = Mojo::JSON->false;
my $false = $json->false;
False value, used because Perl has no native equivalent.
=head2 C<true>
my $true = Mojo::JSON->true;
my $true = $json->true;
True value, used because Perl has no native equivalent.
=head1 SEE ALSO
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicio.us>.
Expand Down
61 changes: 0 additions & 61 deletions lib/Mojo/Util.pm
@@ -1,7 +1,6 @@
package Mojo::Util;
use Mojo::Base 'Exporter';

use B;
use Carp 'croak';
use Digest::MD5 qw(md5 md5_hex);
use Digest::SHA qw(sha1 sha1_hex);
Expand Down Expand Up @@ -38,14 +37,6 @@ $REVERSE{$ENTITIES{$_}} //= $_
for sort { @{[$a =~ /[A-Z]/g]} <=> @{[$b =~ /[A-Z]/g]} }
sort grep {/;/} keys %ENTITIES;

# Type and flags for looks_like_bool
my ($TYPE, $FLAGS);
{
my $obj = B::svref_2object(\(my $dummy = !!1));
$TYPE = $obj->SvTYPE;
$FLAGS = $obj->FLAGS;
}

our @EXPORT_OK = (
qw(assigned_as_number b64_decode b64_encode camelize class_to_file),
qw(class_to_path decamelize decode encode get_line hmac_md5_sum),
Expand All @@ -55,12 +46,6 @@ our @EXPORT_OK = (
qw(xml_escape xor_encode)
);

sub assigned_as_number {
my $value = shift;
my $flags = B::svref_2object(\$value)->FLAGS;
return !!($flags & (B::SVp_IOK | B::SVp_NOK) && !($flags & B::SVp_POK));
}

sub b64_decode { decode_base64(shift) }

sub b64_encode { encode_base64(shift, shift) }
Expand Down Expand Up @@ -140,26 +125,6 @@ sub html_unescape {
return $string;
}

sub looks_like_bool {
my $bool = shift;

# Compare type
my $obj = B::svref_2object(\$bool);
return !!0 unless $obj->SvTYPE == $TYPE;

# Compare flags
return !!0 unless $obj->FLAGS == $FLAGS;

# True value
return !!1 if $bool eq !!1;

# False value
return !!1 if $bool eq !!0;

# Not a boolean
return !!0;
}

sub md5_bytes { md5(@_) }
sub md5_sum { md5_hex(@_) }

Expand Down Expand Up @@ -442,18 +407,6 @@ L<Mojo::Util> provides portable utility functions for L<Mojo>.
L<Mojo::Util> implements the following functions.
=head2 C<assigned_as_number>
my $success = assigned_as_number $scalar;
Check if scalar value has been assigned as number.
# True
assigned_as_number 23;
# False
assigned_as_number "23";
=head2 C<b64_decode>
my $string = b64_decode $b64;
Expand Down Expand Up @@ -562,20 +515,6 @@ defaults to C<^\n\r\t !#$%(-;=?-~>.
Unescape all HTML entities in string.
=head2 C<looks_like_bool>
my $success = looks_like_bool $scalar;
Check if scalar looks like a native Perl boolean value.
# True
looks_like_bool !!1;
looks_like_bool !!0;
# False
looks_like_bool 1;
looks_like_bool 0;
=head2 C<md5_bytes>
my $checksum = md5_bytes $string;
Expand Down
21 changes: 11 additions & 10 deletions t/mojo/json.t
Expand Up @@ -51,11 +51,12 @@ cmp_ok $array->[0], '==', 1e3, 'value is 1e3';

# Decode name
$array = $json->decode('[true]');
is_deeply $array, [!!1], 'decode [true]';
is_deeply $array, [Mojo::JSON->true], 'decode [true]';
$array = $json->decode('[null]');
is_deeply $array, [undef], 'decode [null]';
$array = $json->decode('[true, false]');
is_deeply $array, [!!1, !!0], 'decode [true, false]';
is_deeply $array, [Mojo::JSON->true, Mojo::JSON->false],
'decode [true, false]';

# Decode string
$array = $json->decode('[" "]');
Expand Down Expand Up @@ -148,12 +149,12 @@ $string = $json->encode({foo => ['bar']});
is $string, '{"foo":["bar"]}', 'encode {foo => [\'bar\']}';

# Encode name
$string = $json->encode([!!1]);
is $string, '[true]', 'encode [!!1]';
$string = $json->encode([Mojo::JSON->true]);
is $string, '[true]', 'encode [Mojo::JSON->true]';
$string = $json->encode([undef]);
is $string, '[null]', 'encode [undef]';
$string = $json->encode([!!1, !!0]);
is $string, '[true,false]', 'encode [!!1, !!0]';
$string = $json->encode([Mojo::JSON->true, Mojo::JSON->false]);
is $string, '[true,false]', 'encode [Mojo::JSON->true, Mojo::JSON->false]';

# Encode number
$string = $json->encode([1]);
Expand Down Expand Up @@ -181,7 +182,7 @@ is_deeply $array, ["\x{10346}"], 'successful roundtrip';

# Decode UTF-16LE
$array = $json->decode(b("\x{feff}[true]")->encode('UTF-16LE'));
is_deeply $array, [!!1], 'decode \x{feff}[true]';
is_deeply $array, [Mojo::JSON->true], 'decode \x{feff}[true]';

# Decode UTF-16LE with faihu surrogate pair
$array = $json->decode(b("\x{feff}[\"\\ud800\\udf46\"]")->encode('UTF-16LE'));
Expand All @@ -199,11 +200,11 @@ is_deeply $array, ["\x{10346}"], 'decode \x{feff}[\"\\ud800\\udf46\"]';

# Decode UTF-32LE
$array = $json->decode(b("\x{feff}[true]")->encode('UTF-32LE'));
is_deeply $array, [!!1], 'decode \x{feff}[true]';
is_deeply $array, [Mojo::JSON->true], 'decode \x{feff}[true]';

# Decode UTF-32BE
$array = $json->decode(b("\x{feff}[true]")->encode('UTF-32BE'));
is_deeply $array, [!!1], 'decode \x{feff}[true]';
is_deeply $array, [Mojo::JSON->true], 'decode \x{feff}[true]';

# Decode UTF-16LE without BOM
$array
Expand All @@ -230,7 +231,7 @@ $string = '[null,false,true,"",0,1]';
$array = $json->decode($string);
isa_ok $array, 'ARRAY', 'decode [null,false,true,"",0,1]';
is $json->encode($array), $string, 'reencode';
$array = [undef, 0, 1, '', !!1, !!0];
$array = [undef, 0, 1, '', Mojo::JSON->true, Mojo::JSON->false];
$string = $json->encode($array);
ok $string, 'defined value';
is_deeply $json->decode($string), $array, 'successful roundtrip';
Expand Down
23 changes: 1 addition & 22 deletions t/mojo/util.t
Expand Up @@ -2,7 +2,7 @@ use Mojo::Base -strict;

use utf8;

use Test::More tests => 167;
use Test::More tests => 154;

use File::Spec::Functions qw(catfile splitdir);
use File::Temp 'tempdir';
Expand Down Expand Up @@ -59,27 +59,6 @@ is get_line(\$buffer), 'yada', 'right line';
is $buffer, '', 'no buffer content';
is get_line(\$buffer), undef, 'no line';

# assigned_as_number
ok assigned_as_number 23, 'has been assigned as number';
$buffer = 23;
ok assigned_as_number $buffer, 'has been assigned as number';
ok !assigned_as_number "23", 'has not been assigned as number';
$buffer = "23";
ok !assigned_as_number $buffer, 'has not been assigned as number';

# looks_like_bool
ok looks_like_bool !!1, 'boolean true';
$buffer = !!1;
ok looks_like_bool $buffer, 'boolean true';
ok looks_like_bool !!0, 'boolean false';
$buffer = !!0;
ok looks_like_bool $buffer, 'boolean false';
ok !looks_like_bool 1, 'not a boolean';
ok !looks_like_bool 0, 'not a boolean';
ok !looks_like_bool 'whatever', 'not a boolean';
ok !looks_like_bool 23, 'not a boolean';
ok !looks_like_bool 23.23, 'not a boolean';

# b64_encode
is b64_encode('foobar$%^&3217'), "Zm9vYmFyJCVeJjMyMTc=\n",
'right base64 encoded result';
Expand Down
3 changes: 1 addition & 2 deletions t/pod_coverage.t
Expand Up @@ -6,5 +6,4 @@ plan skip_all => 'set TEST_POD to enable this test (developer only!)'
plan skip_all => 'Test::Pod::Coverage 1.04 required for this test!'
unless eval 'use Test::Pod::Coverage 1.04; 1';

# DEPRECATED in Rainbow!
all_pod_coverage_ok({also_private => [qw(false true)]});
all_pod_coverage_ok();

0 comments on commit 0a125bb

Please sign in to comment.