Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
added encode_json_text function to Mojo::JSON
  • Loading branch information
kraih committed Oct 4, 2014
1 parent 9efcd26 commit 8156b11
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
20 changes: 14 additions & 6 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 encode_json_text j);

# Literal names
my $FALSE = bless \(my $false = 0), 'Mojo::JSON::_Bool';
Expand Down Expand Up @@ -48,6 +48,8 @@ sub encode { encode_json($_[1]) }

sub encode_json { Mojo::Util::encode 'UTF-8', _encode_value(shift) }

sub encode_json_text { _encode_value(shift) }

sub false {$FALSE}

sub j {
Expand All @@ -64,12 +66,9 @@ sub _decode {
# 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);

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

# Value
my $value = _decode_value();
Expand Down Expand Up @@ -350,6 +349,7 @@ individually.
=head2 decode_json
my $value = decode_json($bytes);
my $value = decode_json($chars);
Decode JSON to Perl value and die if decoding fails.
Expand All @@ -359,11 +359,18 @@ Decode JSON to Perl value and die if decoding fails.
Encode Perl value to JSON.
=head2 encode_json_text
my $chars = encode_json_text({foo => 'bar'});
Encode Perl value to JSON text without C<UTF-8> encoding.
=head2 j
my $bytes = j([1, 2, 3]);
my $bytes = j({foo => 'bar'});
my $value = j($bytes);
my $value = j($chars);
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>
Expand All @@ -388,6 +395,7 @@ following new ones.
=head2 decode
my $value = $json->decode($bytes);
my $value = $json->decode($chars);
Decode JSON to Perl value and set L</"error"> if decoding failed.
Expand Down
8 changes: 5 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 encode_json_text j);
use Mojo::Util 'encode';
use Scalar::Util 'dualvar';

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

# Wide characters
is_deeply decode_json('["♥"]'), [''], 'wide characters decoded';
is encode_json_text(['']), '["♥"]', 'wide characters encoded';

# Blessed reference
$bytes = encode_json [b('test')];
is_deeply decode_json($bytes), ['test'], 'successful roundtrip';
Expand Down Expand Up @@ -326,8 +330,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

0 comments on commit 8156b11

Please sign in to comment.