Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
deprecated Object-Oriented Mojo::JSON API
  • Loading branch information
kraih committed Oct 23, 2014
1 parent 27a3518 commit e9e69af
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 104 deletions.
1 change: 1 addition & 0 deletions Changes
@@ -1,5 +1,6 @@

5.54 2014-10-23
- Deprecated Object-Oriented Mojo::JSON API.
- Added auto_decompress attribute to Mojo::Content.
- Fixed bug where Mojo::UserAgent would try to follow redirects for
protocols other than HTTP and HTTPS.
Expand Down
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -17,7 +17,7 @@
templates, content negotiation, session management, form validation,
testing framework, static file server, first class Unicode support and
much more for you to discover.
* Very clean, portable and Object Oriented pure-Perl API with no hidden
* Very clean, portable and Object-Oriented pure-Perl API with no hidden
magic and no requirements besides Perl 5.18.0 (versions as old as 5.10.1
can be used too, but may require additional CPAN modules to be installed)
* Full stack HTTP and WebSocket client/server implementation with IPv6, TLS,
Expand Down
64 changes: 18 additions & 46 deletions lib/Mojo/JSON.pm
Expand Up @@ -4,12 +4,13 @@ use Mojo::Base -base;
use B;
use Carp 'croak';
use Exporter 'import';
use Mojo::Util;
use Mojo::Util 'deprecated';
use Scalar::Util 'blessed';

# DEPRECATED in Tiger Face!
has 'error';

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

# Literal names
my $FALSE = bless \(my $false = 0), 'Mojo::JSON::_Bool';
Expand All @@ -31,6 +32,7 @@ my %ESCAPE = (
my %REVERSE = map { $ESCAPE{$_} => "\\$_" } keys %ESCAPE;
for (0x00 .. 0x1f) { $REVERSE{pack 'C', $_} //= sprintf '\u%.4X', $_ }

# DEPRECATED in Tiger Face!
sub decode {
shift->error(my $err = _catch(\my $value, pop));
return defined $err ? undef : $value;
Expand All @@ -41,6 +43,7 @@ sub decode_json {
return defined $err ? croak $err : $value;
}

# DEPRECATED in Tiger Face!
sub encode { encode_json($_[1]) }

sub encode_json { Mojo::Util::encode 'UTF-8', _encode_value(shift) }
Expand All @@ -57,6 +60,12 @@ sub j {
return eval { _decode($_[0]) };
}

# DEPRECATED in Tiger Face!
sub new {
deprecated 'Object-Oriented Mojo::JSON API is DEPRECATED';
return shift->SUPER::new(@_);
}

sub to_json { _encode_value(shift) }

sub true {$TRUE}
Expand Down Expand Up @@ -306,16 +315,9 @@ 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;
# Handle errors
my $json = Mojo::JSON->new;
my $hash = $json->decode($bytes);
my $err = $json->error;
say $err ? "Error: $err" : $hash->{message};
=head1 DESCRIPTION
L<Mojo::JSON> is a minimalistic and possibly the fastest pure-Perl
Expand Down Expand Up @@ -364,6 +366,12 @@ Decode JSON to Perl value and die if decoding fails.
Encode Perl value to JSON.
=head2 false
my $false = false;
False value, used because Perl has no native equivalent.
=head2 from_json
my $value = from_json $chars;
Expand All @@ -387,45 +395,9 @@ or that decoding failed.
Encode Perl value to JSON text without C<UTF-8> encoding it.
=head1 ATTRIBUTES
L<Mojo::JSON> implements the following attributes.
=head2 error
my $err = $json->error;
$json = $json->error('Parser error');
Parser error.
=head1 METHODS
L<Mojo::JSON> inherits all methods from L<Mojo::Base> and implements the
following new ones.
=head2 decode
my $value = $json->decode($bytes);
Decode JSON to Perl value and set L</"error"> if decoding failed.
=head2 encode
my $bytes = $json->encode({i => '♥ mojolicious'});
Encode Perl value to JSON.
=head2 false
my $false = Mojo::JSON->false;
my $false = $json->false;
False value, used because Perl has no native equivalent.
=head2 true
my $true = Mojo::JSON->true;
my $true = $json->true;
my $true = true;
True value, used because Perl has no native equivalent.
Expand Down
4 changes: 2 additions & 2 deletions lib/Mojolicious/Guides.pod
Expand Up @@ -274,8 +274,6 @@ This is the class hierarchy of the L<Mojolicious> distribution.

=item * L<Mojo::IOLoop>

=item * L<Mojo::JSON>

=item * L<Mojo::JSON::Pointer>

=item * L<Mojo::Loader>
Expand Down Expand Up @@ -412,6 +410,8 @@ This is the class hierarchy of the L<Mojolicious> distribution.

=item * L<Mojo::DOM>

=item * L<Mojo::JSON>

=item * L<Mojo::Util>

=item * L<ojo>
Expand Down
111 changes: 57 additions & 54 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 from_json j to_json);
use Mojo::JSON qw(decode_json encode_json false from_json j to_json true);
use Mojo::Util 'encode';
use Scalar::Util 'dualvar';

Expand Down Expand Up @@ -54,8 +54,7 @@ is_deeply $array, [Mojo::JSON->true], 'decode [true]';
$array = decode_json '[null]';
is_deeply $array, [undef], 'decode [null]';
$array = decode_json '[true, false]';
is_deeply $array, [Mojo::JSON->true, Mojo::JSON->false],
'decode [true, false]';
is_deeply $array, [true, false], 'decode [true, false]';
$value = decode_json 'true';
is $value, Mojo::JSON->true, 'decode true';
$value = decode_json 'false';
Expand Down Expand Up @@ -322,70 +321,74 @@ like encode_json({test => -sin(9**9**9)}), qr/^{"test":".*"}$/,
'encode "nan" as string';

# "null"
my $json = Mojo::JSON->new;
is $json->decode('null'), undef, 'decode null';
ok !$json->error, 'no error';
is j('null'), undef, 'decode null';

# Errors
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(b('["\\ud800"]')->encode), undef, 'syntax error';
is $json->error, 'Malformed JSON: Missing low-surrogate at line 1, offset 8',
eval { decode_json 'test' };
like $@, qr/Malformed JSON: Expected string, array, object/, 'right error';
like $@, qr/object, number, boolean or null at line 0, offset 0/,
'right error';
is $json->decode(b('["\\udf46"]')->encode), undef, 'syntax error';
is $json->error, 'Malformed JSON: Missing high-surrogate at line 1, offset 8',
eval { decode_json b('["\\ud800"]')->encode };
like $@, qr/Malformed JSON: Missing low-surrogate at line 1, offset 8/,
'right error';
is $json->decode('[[]'), undef, 'syntax error';
is $json->error, 'Malformed JSON: Expected comma or right square bracket while'
. ' parsing array at line 1, offset 3', 'right error';
is $json->decode('{{}'), undef, 'syntax error';
is $json->error, 'Malformed JSON: Expected string while'
. ' parsing object at line 1, offset 1', 'right error';
is $json->decode("[\"foo\x00]"), undef, 'syntax error';
is $json->error, 'Malformed JSON: Unexpected character or invalid escape while'
. ' parsing string at line 1, offset 5', 'right error';
is $json->decode('{"foo":"bar"{'), undef, 'syntax error';
is $json->error, 'Malformed JSON: Expected comma or right curly bracket while'
. ' parsing object at line 1, offset 12', 'right error';
is $json->decode('{"foo""bar"}'), undef, 'syntax error';
is $json->error,
'Malformed JSON: Expected colon while parsing object at line 1, offset 6',
eval { decode_json b('["\\udf46"]')->encode };
like $@, qr/Malformed JSON: Missing high-surrogate at line 1, offset 8/,
'right error';
is $json->decode('[[]...'), undef, 'syntax error';
is $json->error, 'Malformed JSON: Expected comma or right square bracket while'
. ' parsing array at line 1, offset 3', 'right error';
is $json->decode('{{}...'), undef, 'syntax error';
is $json->error, 'Malformed JSON: Expected string while'
. ' parsing object at line 1, offset 1', 'right error';
is $json->decode('[nan]'), undef, 'syntax error';
is $json->error, 'Malformed JSON: Expected string, array, object, number,'
. ' boolean or null at line 1, offset 1', 'right error';
is $json->decode('["foo]'), undef, 'syntax error';
is $json->error, 'Malformed JSON: Unterminated string at line 1, offset 6',
eval { decode_json '[[]' };
like $@, qr/Malformed JSON: Expected comma or right square bracket/,
'right error';
is $json->decode('{"foo":"bar"}lala'), undef, 'syntax error';
is $json->error, 'Malformed JSON: Unexpected data at line 1, offset 13',
like $@, qr/bracket while parsing array at line 1, offset 3/, 'right error';
eval { decode_json '{{}' };
like $@,
qr/Malformed JSON: Expected string while parsing object at line 1, offset 1/,
'right error';
is $json->decode(''), undef, 'missing input';
is $json->error, 'Missing or empty input', 'right error';
is $json->decode("[\"foo\",\n\"bar\"]lala"), undef, 'syntax error';
is $json->error, 'Malformed JSON: Unexpected data at line 2, offset 6',
eval { decode_json "[\"foo\x00]" };
like $@, qr/Malformed JSON: Unexpected character or invalid escape/,
'right error';
is $json->decode("[\"foo\",\n\"bar\",\n\"bazra\"]lalala"), undef,
'syntax error';
is $json->error, 'Malformed JSON: Unexpected data at line 3, offset 8',
like $@, qr/escape while parsing string at line 1, offset 5/, 'right error';
eval { decode_json '{"foo":"bar"{' };
like $@, qr/Malformed JSON: Expected comma or right curly bracket/,
'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';
like $@, qr/bracket while parsing object at line 1, offset 12/, 'right error';
eval { decode_json '{"foo""bar"}' };
like $@,
qr/Malformed JSON: Expected colon while parsing object at line 1, offset 6/,
'right error';
eval { decode_json '[[]...' };
like $@, qr/Malformed JSON: Expected comma or right square bracket/,
'right error';
like $@, qr/bracket while parsing array at line 1, offset 3/, 'right error';
eval { decode_json '{{}...' };
like $@,
qr/Malformed JSON: Expected string while parsing object at line 1, offset 1/,
'right error';
eval { decode_json '[nan]' };
like $@, qr/Malformed JSON: Expected string, array, object, number/,
'right error';
like $@, qr/number, boolean or null at line 1, offset 1/, 'right error';
eval { decode_json '["foo]' };
like $@, qr/Malformed JSON: Unterminated string at line 1, offset 6/,
'right error';
eval { decode_json '{"foo":"bar"}lala' };
like $@, qr/Malformed JSON: Unexpected data at line 1, offset 13/,
'right error';
eval { decode_json '' };
like $@, qr/Missing or empty input/, 'right error';
eval { decode_json "[\"foo\",\n\"bar\"]lala" };
like $@, qr/Malformed JSON: Unexpected data at line 2, offset 6/,
'right error';
eval { decode_json "[\"foo\",\n\"bar\",\n\"bazra\"]lalala" };
like $@, qr/Malformed JSON: Unexpected data at line 3, offset 8/,
'right error';
eval { decode_json '["♥"]' };
like $@, qr/Input is not UTF-8 encoded/, 'right error';
eval { decode_json encode('Shift_JIS', 'やった') };
like $@, qr/Input is not UTF-8 encoded/, 'right error';
is j('{'), undef, 'syntax error';
eval { decode_json("[\"foo\",\n\"bar\",\n\"bazra\"]lalala") };
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") };
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
3 changes: 2 additions & 1 deletion t/pod_coverage.t
Expand Up @@ -8,7 +8,8 @@ plan skip_all => 'Test::Pod::Coverage 1.04 required for this test!'
unless eval 'use Test::Pod::Coverage 1.04; 1';

# DEPRECATED in Tiger Face!
my @tiger = (qw(emit_safe has_conditions render_static));
my @tiger
= (qw(decode emit_safe encode error has_conditions new render_static));

# False positive constants
all_pod_coverage_ok({also_private => [qw(IPV6 TLS), @tiger]});

0 comments on commit e9e69af

Please sign in to comment.