Skip to content

Commit

Permalink
added j function to Mojo::JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jan 11, 2013
1 parent 8e13768 commit 44dbf65
Show file tree
Hide file tree
Showing 17 changed files with 153 additions and 125 deletions.
1 change: 1 addition & 0 deletions Changes
@@ -1,5 +1,6 @@

3.77 2013-01-11
- Added j function to Mojo::JSON.
- Improved tests.
- Fixed aliasing bug in Mojo::EventEmitter.
- Fixed WebSocket cookie bug in Mojo::UserAgent.
Expand Down
4 changes: 2 additions & 2 deletions lib/Mojo/Asset.pm
Expand Up @@ -82,7 +82,7 @@ subclass.
=head2 get_chunk
my $chunk = $asset->get_chunk($offset);
my $bytes = $asset->get_chunk($offset);
Get chunk of data starting from a specific position. Meant to be overloaded
in a subclass.
Expand Down Expand Up @@ -113,7 +113,7 @@ Size of asset data in bytes. Meant to be overloaded in a subclass.
=head2 slurp
my $string = $asset->slurp;
my $bytes = $asset->slurp;
Read all asset data at once. Meant to be overloaded in a subclass.
Expand Down
4 changes: 2 additions & 2 deletions lib/Mojo/Asset/File.pm
Expand Up @@ -223,7 +223,7 @@ Check if asset contains a specific string.
=head2 get_chunk
my $chunk = $file->get_chunk($start);
my $bytes = $file->get_chunk($start);
Get chunk of data starting from a specific position.
Expand All @@ -247,7 +247,7 @@ Size of asset data in bytes.
=head2 slurp
my $string = $file->slurp;
my $bytes = $file->slurp;
Read all asset data at once.
Expand Down
4 changes: 2 additions & 2 deletions lib/Mojo/Asset/Memory.pm
Expand Up @@ -138,7 +138,7 @@ Check if asset contains a specific string.
=head2 get_chunk
my $chunk = $mem->get_chunk($offset);
my $bytes = $mem->get_chunk($offset);
Get chunk of data starting from a specific position.
Expand All @@ -156,7 +156,7 @@ Size of asset data in bytes.
=head2 slurp
my $string = mem->slurp;
my $bytes = mem->slurp;
Read all asset data at once.
Expand Down
6 changes: 3 additions & 3 deletions lib/Mojo/Content.pm
Expand Up @@ -508,20 +508,20 @@ Clone content if possible, otherwise return C<undef>.
=head2 generate_body_chunk
my $chunk = $content->generate_body_chunk(0);
my $bytes = $content->generate_body_chunk(0);
Generate dynamic content.
=head2 get_body_chunk
my $chunk = $content->get_body_chunk(0);
my $bytes = $content->get_body_chunk(0);
Get a chunk of content starting from a specfic position. Meant to be
overloaded in a subclass.
=head2 get_header_chunk
my $chunk = $content->get_header_chunk(13);
my $bytes = $content->get_header_chunk(13);
Get a chunk of the headers starting from a specfic position.
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Content/MultiPart.pm
Expand Up @@ -290,7 +290,7 @@ Clone content if possible, otherwise return C<undef>.
=head2 get_body_chunk
my $chunk = $multi->get_body_chunk(0);
my $bytes = $multi->get_body_chunk(0);
Get a chunk of content starting from a specfic position.
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Content/Single.pm
Expand Up @@ -147,7 +147,7 @@ Clone content if possible, otherwise return C<undef>.
=head2 get_body_chunk
my $chunk = $single->get_body_chunk(0);
my $bytes = $single->get_body_chunk(0);
Get a chunk of content starting from a specfic position.
Expand Down
36 changes: 34 additions & 2 deletions lib/Mojo/JSON.pm
Expand Up @@ -2,11 +2,14 @@ package Mojo::JSON;
use Mojo::Base -base;

use B;
use Exporter 'import';
use Mojo::Util;
use Scalar::Util 'blessed';

has 'error';

our @EXPORT_OK = ('j');

# Literal names
my $FALSE = bless \(my $false = 0), 'Mojo::JSON::_Bool';
my $TRUE = bless \(my $true = 1), 'Mojo::JSON::_Bool';
Expand Down Expand Up @@ -99,7 +102,14 @@ sub encode {
}

sub false {$FALSE}
sub true {$TRUE}

sub j {
my $d = shift;
return __PACKAGE__->new->encode($d) if ref $d eq 'ARRAY' || ref $d eq 'HASH';
return __PACKAGE__->new->decode($d);
}

sub true {$TRUE}

sub _decode_array {
my @array;
Expand Down Expand Up @@ -328,12 +338,22 @@ Mojo::JSON - Minimalistic JSON
=head1 SYNOPSIS
# Encode and decode JSON
use Mojo::JSON;
my $json = Mojo::JSON->new;
my $bytes = $json->encode({foo => [1, 2], bar => 'hello!', baz => \1});
my $hash = $json->decode($bytes);
# Check for errors
my $json = Mojo::JSON->new;
if (defined(my $hash = $json->decode($bytes))) { say $hash->{message} }
else { say "Error: ", $json->error }
# Use alternative interface
use Mojo::JSON 'j';
my $bytes = j({foo => [1, 2], bar => 'hello!', baz => \1});
my $hash = j($bytes);
=head1 DESCRIPTION
L<Mojo::JSON> is a minimalistic and relaxed implementation of RFC 4627. While
Expand All @@ -359,6 +379,18 @@ Decoding UTF-16 (LE/BE) and UTF-32 (LE/BE) will be handled transparently,
encoding will only generate UTF-8. The two Unicode whitespace characters
C<u2028> and C<u2029> will always be escaped to make JSONP easier.
=head1 FUNCTIONS
L<Mojo::JSON> implements the following functions.
=head2 j
my $array = j($bytes);
my $hash = j($bytes);
my $bytes = j({foo => 'bar'});
Decode JSON or encode Perl data structure.
=head1 ATTRIBUTES
L<Mojo::JSON> implements the following attributes.
Expand Down
18 changes: 9 additions & 9 deletions lib/Mojo/Message.pm
Expand Up @@ -486,9 +486,9 @@ implements the following new ones.
=head2 body
my $string = $msg->body;
$msg = $msg->body('Hello!');
my $cb = $msg->body(sub {...});
my $bytes = $msg->body;
$msg = $msg->body('Hello!');
my $cb = $msg->body(sub {...});
Access C<content> data or replace all subscribers of the C<read> event.
Expand Down Expand Up @@ -517,19 +517,19 @@ Content size in bytes.
=head2 build_body
my $string = $msg->build_body;
my $bytes = $msg->build_body;
Render whole body.
=head2 build_headers
my $string = $msg->build_headers;
my $bytes = $msg->build_headers;
Render all headers.
=head2 build_start_line
my $string = $msg->build_start_line;
my $bytes = $msg->build_start_line;
Render start line.
Expand Down Expand Up @@ -597,19 +597,19 @@ Make sure message has all required headers.
=head2 get_body_chunk
my $string = $msg->get_body_chunk($offset);
my $bytes = $msg->get_body_chunk($offset);
Get a chunk of body data starting from a specific position.
=head2 get_header_chunk
my $string = $msg->get_header_chunk($offset);
my $bytes = $msg->get_header_chunk($offset);
Get a chunk of header data, starting from a specific position.
=head2 get_start_line_chunk
my $string = $msg->get_start_line_chunk($offset);
my $bytes = $msg->get_start_line_chunk($offset);
Get a chunk of start line data starting from a specific position. Meant to be
overloaded in a subclass.
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Message/Request.pm
Expand Up @@ -369,7 +369,7 @@ Make sure request has all required headers.
=head2 get_start_line_chunk
my $string = $req->get_start_line_chunk($offset);
my $bytes = $req->get_start_line_chunk($offset);
Get a chunk of request line data starting from a specific position.
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Message/Response.pm
Expand Up @@ -230,7 +230,7 @@ Make sure response has all required headers.
=head2 get_start_line_chunk
my $string = $res->get_start_line_chunk($offset);
my $bytes = $res->get_start_line_chunk($offset);
Get a chunk of status line data starting from a specific position.
Expand Down
4 changes: 2 additions & 2 deletions lib/Mojo/Transaction.pm
Expand Up @@ -207,7 +207,7 @@ in a subclass.
=head2 client_write
my $chunk = $tx->client_write;
my $bytes = $tx->client_write;
Write data client-side, used to implement user agents. Meant to be overloaded
in a subclass.
Expand Down Expand Up @@ -272,7 +272,7 @@ in a subclass.
=head2 server_write
my $chunk = $tx->server_write;
my $bytes = $tx->server_write;
Write data server-side, used to implement web servers. Meant to be overloaded
in a subclass.
Expand Down
4 changes: 2 additions & 2 deletions lib/Mojo/Transaction/HTTP.pm
Expand Up @@ -255,7 +255,7 @@ Read data client-side, used to implement user agents.
=head2 client_write
my $chunk = $tx->client_write;
my $bytes = $tx->client_write;
Write data client-side, used to implement user agents.
Expand All @@ -273,7 +273,7 @@ Read data server-side, used to implement web servers.
=head2 server_write
my $chunk = $tx->server_write;
my $bytes = $tx->server_write;
Write data server-side, used to implement web servers.
Expand Down
4 changes: 2 additions & 2 deletions lib/Mojo/Transaction/WebSocket.pm
Expand Up @@ -450,7 +450,7 @@ Read data client-side, used to implement user agents.
=head2 client_write
my $chunk = $ws->client_write;
my $bytes = $ws->client_write;
Write data client-side, used to implement user agents.
Expand Down Expand Up @@ -563,7 +563,7 @@ Read data server-side, used to implement web servers.
=head2 server_write
my $chunk = $ws->server_write;
my $bytes = $ws->server_write;
Write data server-side, used to implement web servers.
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Upload.pm
Expand Up @@ -87,7 +87,7 @@ Size of uploaded data in bytes.
=head2 slurp
my $string = $upload->slurp;
my $bytes = $upload->slurp;
Read all uploaded data at once.
Expand Down
21 changes: 8 additions & 13 deletions lib/ojo.pm
Expand Up @@ -4,7 +4,7 @@ use Mojo::Base -strict;
use Mojo::ByteStream 'b';
use Mojo::Collection 'c';
use Mojo::DOM;
use Mojo::JSON;
use Mojo::JSON 'j';
use Mojo::UserAgent;
use Mojo::Util 'monkey_patch';

Expand Down Expand Up @@ -34,23 +34,18 @@ sub import {
a => sub { $caller->can('any')->(@_) and return $UA->app },
b => \&b,
c => \&c,
d => sub { _request($UA->build_tx(DELETE => @_)) },
d => sub { _request($UA->build_tx(DELETE => @_)) },
f => sub { _request($UA->build_form_tx(@_)) },
g => sub { _request($UA->build_tx(GET => @_)) },
h => sub { _request($UA->build_tx(HEAD => @_)) },
g => sub { _request($UA->build_tx(GET => @_)) },
h => sub { _request($UA->build_tx(HEAD => @_)) },
j => \&j,
n => sub { _request($UA->build_json_tx(@_)) },
o => sub { _request($UA->build_tx(OPTIONS => @_)) },
p => sub { _request($UA->build_tx(POST => @_)) },
p => sub { _request($UA->build_tx(POST => @_)) },
r => sub { $UA->app->dumper(@_) },
t => sub { _request($UA->build_tx(PATCH => @_)) },
u => sub { _request($UA->build_tx(PUT => @_)) },
t => sub { _request($UA->build_tx(PATCH => @_)) },
u => sub { _request($UA->build_tx(PUT => @_)) },
x => sub { Mojo::DOM->new(@_) };
monkey_patch $caller, j => sub {
my $d = shift;
my $j = Mojo::JSON->new;
return $j->encode($d) if ref $d eq 'ARRAY' || ref $d eq 'HASH';
return $j->decode($d);
};
}

sub _request {
Expand Down

0 comments on commit 44dbf65

Please sign in to comment.