Skip to content

Commit

Permalink
fix a few reference encoding bugs in Mojo::JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Dec 10, 2015
1 parent 782df33 commit 7d5a82a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
3 changes: 2 additions & 1 deletion Changes
@@ -1,5 +1,6 @@

6.37 2015-12-09
6.37 2015-12-10
- Fixed a few reference encoding bugs in Mojo::JSON.

6.36 2015-12-08
- Improved Mojo::JSON performance slightly. (haarg)
Expand Down
8 changes: 4 additions & 4 deletions lib/Mojo/JSON.pm
Expand Up @@ -239,10 +239,10 @@ sub _encode_value {
return $$value ? 'true' : 'false' if $ref eq 'SCALAR';
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'))) {
return _encode_value($value->$sub);
}
# Everything else
return _encode_string($value)
unless blessed $value && (my $sub = $value->can('TO_JSON'));
return _encode_value($value->$sub);
}

# Null
Expand Down
6 changes: 6 additions & 0 deletions t/mojo/json.t
Expand Up @@ -13,6 +13,7 @@ use Mojo::ByteStream 'b';
use Mojo::JSON qw(decode_json encode_json false from_json j to_json true);
use Mojo::Util 'encode';
use Scalar::Util 'dualvar';
use version;

# Decode array
my $array = decode_json '[]';
Expand Down Expand Up @@ -311,6 +312,11 @@ is encode_json({test => [$num, $str]}), '{"test":[23,"bar"]}',
my $dual = dualvar 23, 'twenty three';
is encode_json([$dual]), '["twenty three"]', 'dualvar stringified';

# Other reference types
my $sub = sub { };
is encode_json([$sub]), "[\"$sub\"]", 'encoded code reference';
is encode_json([version->new('1.0')]), "[\"1.0\"]", 'encoded version object';

# Ensure numbers and strings are not upgraded
my $mixed = [3, 'three', '3', 0, "0"];
is encode_json($mixed), '[3,"three","3",0,"0"]',
Expand Down

0 comments on commit 7d5a82a

Please sign in to comment.