Skip to content

Commit

Permalink
improved Mojo::JSON to handle encoding errors more gracefully
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Feb 19, 2014
1 parent dfdb30f commit 97511b9
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 2 deletions.
3 changes: 3 additions & 0 deletions Changes
@@ -1,4 +1,7 @@

4.83 2014-02-20
- Improved Mojo::JSON to handle encoding errors more gracefully.

4.82 2014-02-19
- Added decode_json and encode_json functions to Mojo::JSON.
- Added data attribute to Mojo::JSON::Pointer.
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/JSON.pm
Expand Up @@ -65,7 +65,7 @@ sub decode_json {
# Detect and decode Unicode
my $encoding = 'UTF-8';
$bytes =~ $UTF_PATTERNS->{$_} and $encoding = $_ for keys %$UTF_PATTERNS;
local $_ = Mojo::Util::decode $encoding, $bytes;
local $_ = Mojo::Util::decode($encoding, $bytes) // '';

# Leading whitespace
m/\G$WHITESPACE_RE/gc;
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious.pm
Expand Up @@ -43,7 +43,7 @@ has types => sub { Mojolicious::Types->new };
has validator => sub { Mojolicious::Validator->new };

our $CODENAME = 'Top Hat';
our $VERSION = '4.82';
our $VERSION = '4.83';

sub AUTOLOAD {
my $self = shift;
Expand Down
5 changes: 5 additions & 0 deletions t/mojo/json.t
Expand Up @@ -11,6 +11,7 @@ use Mojo::Base -strict;
use Test::More;
use Mojo::ByteStream 'b';
use Mojo::JSON qw(decode_json encode_json j);
use Mojo::Util 'encode';

# Decode array
my $json = Mojo::JSON->new;
Expand Down Expand Up @@ -369,6 +370,10 @@ is $json->error,
'Malformed JSON: Unexpected data after array at line 3, offset 8',
'right error';
is $json->decode('0'), undef, 'syntax error';
is $json->error,
'Malformed JSON: Expected array or object at line 0, offset 0',
'right error';
is $json->decode(encode('Shift_JIS', 'やった')), undef, 'invalid encoding';
is $json->error,
'Malformed JSON: Expected array or object at line 0, offset 0',
'right error';
Expand Down

0 comments on commit 97511b9

Please sign in to comment.