Skip to content

Commit

Permalink
added from_json and to_json functions to Mojo::JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Oct 6, 2014
1 parent 5709239 commit 139ac44
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 18 deletions.
3 changes: 2 additions & 1 deletion Changes
@@ -1,5 +1,6 @@

5.48 2014-10-05
5.48 2014-10-06
- Added from_json and to_json functions to Mojo::JSON.

5.47 2014-09-28
- Improved url_for performance.
Expand Down
46 changes: 32 additions & 14 deletions lib/Mojo/JSON.pm
Expand Up @@ -9,7 +9,7 @@ use Scalar::Util 'blessed';

has 'error';

our @EXPORT_OK = qw(decode_json encode_json j);
our @EXPORT_OK = qw(decode_json encode_json from_json j to_json);

# Literal names
my $FALSE = bless \(my $false = 0), 'Mojo::JSON::_Bool';
Expand Down Expand Up @@ -47,11 +47,18 @@ sub encode_json { Mojo::Util::encode 'UTF-8', _encode_value(shift) }

sub false {$FALSE}

sub from_json {
my $err = _catch(\my $value, shift, 1);
return defined $err ? croak $err : $value;
}

sub j {
return encode_json($_[0]) if ref $_[0] eq 'ARRAY' || ref $_[0] eq 'HASH';
return eval { _decode($_[0]) };
}

sub to_json { _encode_value(shift) }

sub true {$TRUE}

sub _catch {
Expand All @@ -61,16 +68,14 @@ sub _catch {
}

sub _decode {
my ($json, $unencoded) = @_;

# Missing input
die "Missing or empty input\n" unless length(my $bytes = shift);

# Wide characters
die "Wide character in input\n" unless utf8::downgrade($bytes, 1);
die "Missing or empty input\n" unless length $json;

# UTF-8
die "Input is not UTF-8 encoded\n"
unless defined(local $_ = Mojo::Util::decode('UTF-8', $bytes));
local $_ = $unencoded ? $json : Mojo::Util::decode 'UTF-8', $json;
die "Input is not UTF-8 encoded\n" unless defined $_;

# Value
my $value = _decode_value();
Expand Down Expand Up @@ -303,8 +308,8 @@ Mojo::JSON - Minimalistic JSON
use Mojo::JSON qw(decode_json encode_json);
# Encode and decode JSON (die on errors)
my $bytes = encode_json({foo => [1, 2], bar => 'hello!', baz => \1});
my $hash = decode_json($bytes);
my $bytes = encode_json {foo => [1, 2], bar => 'hello!', baz => \1};
my $hash = decode_json $bytes;
# Handle errors
my $json = Mojo::JSON->new;
Expand Down Expand Up @@ -350,26 +355,39 @@ individually.
=head2 decode_json
my $value = decode_json($bytes);
my $value = decode_json $bytes;
Decode JSON to Perl value and die if decoding fails.
=head2 encode_json
my $bytes = encode_json({i => '♥ mojolicious'});
my $bytes = encode_json {i => '♥ mojolicious'};
Encode Perl value to JSON.
=head2 from_json
my $value = from_json $chars;
Decode JSON text without C<UTF-8> encoding to Perl value and die if decoding
fails.
=head2 j
my $bytes = j([1, 2, 3]);
my $bytes = j({i => '♥ mojolicious'});
my $value = j($bytes);
my $bytes = j [1, 2, 3];
my $bytes = j {i => '♥ mojolicious'};
my $value = j $bytes;
Encode Perl data structure (which may only be an array reference or hash
reference) or decode JSON, an C<undef> return value indicates a bare C<null>
or that decoding failed.
=head2 to_json
my $chars = to_json {i => '♥ mojolicious'};
Encode Perl value to JSON text without C<UTF-8> encoding.
=head1 ATTRIBUTES
L<Mojo::JSON> implements the following attributes.
Expand Down
14 changes: 11 additions & 3 deletions t/mojo/json.t
Expand Up @@ -10,7 +10,7 @@ use Mojo::Base -strict;

use Test::More;
use Mojo::ByteStream 'b';
use Mojo::JSON qw(decode_json encode_json j);
use Mojo::JSON qw(decode_json encode_json from_json j to_json);
use Mojo::Util 'encode';
use Scalar::Util 'dualvar';

Expand Down Expand Up @@ -250,6 +250,11 @@ is index($bytes, b("\x{2029}")->encode), -1, 'properly escaped';
is_deeply decode_json($bytes), ["\x{2028}test\x{2029}123"],
'successful roundtrip';

# JSON without UTF-8 encoding
is_deeply from_json('["♥"]'), [''], 'characters decoded';
is to_json(['']), '["♥"]', 'characters encoded';
is_deeply from_json(to_json(["\xe9"])), ["\xe9"], 'successful roundtrip';

# Blessed reference
$bytes = encode_json [b('test')];
is_deeply decode_json($bytes), ['test'], 'successful roundtrip';
Expand Down Expand Up @@ -326,8 +331,6 @@ is j('null'), undef, 'decode null';
is $json->decode('test'), undef, 'syntax error';
is $json->error, 'Malformed JSON: Expected string, array, object, number,'
. ' boolean or null at line 0, offset 0', 'right error';
is $json->decode('["♥"]'), undef, 'wide character in input';
is $json->error, 'Wide character in input', 'right error';
is $json->decode(b('["\\ud800"]')->encode), undef, 'syntax error';
is $json->error, 'Malformed JSON: Missing low-surrogate at line 1, offset 8',
'right error';
Expand Down Expand Up @@ -374,10 +377,15 @@ is $json->decode("[\"foo\",\n\"bar\",\n\"bazra\"]lalala"), undef,
'syntax error';
is $json->error, 'Malformed JSON: Unexpected data at line 3, offset 8',
'right error';
is $json->decode('["♥"]'), undef, 'wide character in input';
is $json->error, 'Input is not UTF-8 encoded', 'right error';
is $json->decode(encode('Shift_JIS', 'やった')), undef, 'invalid encoding';
is $json->error, 'Input is not UTF-8 encoded', 'right error';
is j('{'), undef, 'syntax error';
eval { decode_json("[\"foo\",\n\"bar\",\n\"bazra\"]lalala") };
like $@, qr/JSON: Unexpected data at line 3, offset 8 at.*json\.t/,
'right error';
eval { from_json("[\"foo\",\n\"bar\",\n\"bazra\"]lalala") };
like $@, qr/JSON: Unexpected data at line 3, offset 8 at.*json\.t/,
'right error';

Expand Down

0 comments on commit 139ac44

Please sign in to comment.