Skip to content

Commit

Permalink
fixed Mojo::JSON to encode "inf" and "nan" values as strings
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed May 15, 2013
1 parent 47cd205 commit 7169454
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 3 deletions.
3 changes: 2 additions & 1 deletion Changes
Expand Up @@ -51,7 +51,7 @@
- Improved Mojolicious to not trap exceptions if the default exception
handling has been deactivated.
- Improved json_is and json_message_is methods in Test::Mojo by making the
JSON Pointer optional.
JSON Pointer optional. (jberger)
- Improved renderer performance.
- Improved Mojo::DOM::HTML performance.
- Improved Mojo::Reactor::Poll performance.
Expand All @@ -61,6 +61,7 @@
- Improved documentation.
- Improved tests.
- Fixed Perl 5.17.11+ compatibility.
- Fixed Mojo::JSON to encode "inf" and "nan" values as strings. (chansen)
- Fixed a few authority and rendering bugs in Mojo::URL.
- Fixed layout bugs in Mojolicious::Renderer.
- Fixed support for HEAD request method in Mojo::Server::CGI and
Expand Down
4 changes: 2 additions & 2 deletions lib/Mojo/JSON.pm
Expand Up @@ -293,8 +293,8 @@ sub _encode_value {
return 'null' unless defined $value;

# Number
return 0 + $value
if B::svref_2object(\$value)->FLAGS & (B::SVp_IOK | B::SVp_NOK);
my $flags = B::svref_2object(\$value)->FLAGS;
return 0 + $value if $flags & (B::SVp_IOK | B::SVp_NOK) && $value * 0 == 0;

# String
return _encode_string($value);
Expand Down
4 changes: 4 additions & 0 deletions t/mojo/json.t
Expand Up @@ -296,6 +296,10 @@ $num = 1 + $str;
is $json->encode({test => [$num, $str]}), '{"test":[1,0]}',
'upgraded number detected';

# "inf" and "nan"
is $json->encode({test => 9**9**9}), '{"test":"inf"}', 'encode "inf"';
is $json->encode({test => -sin(9**9**9)}), '{"test":"nan"}', 'encode "nan"';

# Errors
is $json->decode('["♥"]'), undef, 'wide character in input';
is $json->error, 'Wide character in input', 'right error';
Expand Down

0 comments on commit 7169454

Please sign in to comment.