Skip to content

Commit

Permalink
removed json_class attributes again
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jul 20, 2012
1 parent 84755b8 commit 21bb7a0
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 35 deletions.
2 changes: 0 additions & 2 deletions Changes
Expand Up @@ -3,8 +3,6 @@
- Added json method to Mojo::UserAgent::Transactor.
- Added build_json_tx and post_json methods to Mojo::UserAgent.
- Added post_json_ok method to Test::Mojo.
- Added json_class attribute to Mojo::UserAgent::Transactor.
- Added json_class attribute to Mojolicious::Sessions.
- Added n function to ojo.
- Improved text_field helper to always set the type attribute. (marty, sri)
- Improved documentation.
Expand Down
16 changes: 3 additions & 13 deletions lib/Mojo/UserAgent/Transactor.pm
Expand Up @@ -6,15 +6,12 @@ use Mojo::Asset::File;
use Mojo::Asset::Memory;
use Mojo::Content::MultiPart;
use Mojo::Content::Single;
use Mojo::JSON;
use Mojo::Parameters;
use Mojo::Transaction::HTTP;
use Mojo::Transaction::WebSocket;
use Mojo::URL;
use Mojo::Util qw(encode url_escape);

has json_class => 'Mojo::JSON';

sub endpoint {
my ($self, $tx) = @_;

Expand Down Expand Up @@ -98,8 +95,9 @@ sub form {

sub json {
my ($self, $url, $data) = (shift, shift, shift);
my $tx = $self->tx(POST => $url, @_, $self->json_class->new->encode($data));
my $headers = $tx->req->headers;
my $tx = $self->tx(POST => $url, @_);
my $req = $tx->req;
my $headers = $req->body($req->json_class->new->encode($data))->headers;
$headers->content_type('application/json') unless $headers->content_type;
return $tx;
}
Expand Down Expand Up @@ -276,14 +274,6 @@ framework used by L<Mojo::UserAgent>.
L<Mojo::UserAgent::Transactor> implements the following attributes.
=head2 C<json_class>
my $class = $t->json_class;
$t = $t->json_class('Mojo::JSON');
Class to be used for JSON serialization with the C<json> method, defaults to
L<Mojo::JSON>.
=head1 METHODS
L<Mojo::UserAgent::Transactor> inherits all methods from L<Mojo::Base> and
Expand Down
11 changes: 5 additions & 6 deletions lib/Mojolicious/Command/get.pm
Expand Up @@ -144,18 +144,17 @@ sub run {

# JSON Pointer
return unless $selector;
return $self->_json($buffer, $selector)
return _json($buffer, $selector)
if ($tx->res->headers->content_type || '') =~ /JSON/i;

# Selector
$self->_select($buffer, $charset // $tx->res->content->charset, $selector);
_select($buffer, $charset // $tx->res->content->charset, $selector);
}

sub _json {
my ($self, $buffer, $pointer) = @_;
my $json = Mojo::JSON->new;
return unless my $data = $json->decode($buffer);
return unless defined($data = Mojo::JSON::Pointer->get($data, $pointer));
return unless my $data = $json->decode(shift);
return unless defined($data = Mojo::JSON::Pointer->get($data, shift));
ref $data ~~ ['HASH', 'ARRAY'] ? say($json->encode($data)) : _say($data);
}

Expand All @@ -165,7 +164,7 @@ sub _say {
}

sub _select {
my ($self, $buffer, $charset, $selector) = @_;
my ($buffer, $charset, $selector) = @_;

# Find
my $dom = Mojo::DOM->new->charset($charset)->parse($buffer);
Expand Down
3 changes: 1 addition & 2 deletions lib/Mojolicious/Renderer.pm
Expand Up @@ -4,7 +4,6 @@ use Mojo::Base -base;
use File::Spec::Functions 'catfile';
use Mojo::Cache;
use Mojo::Home;
use Mojo::JSON;
use Mojo::Loader;
use Mojo::Util 'encode';

Expand Down Expand Up @@ -33,7 +32,7 @@ sub new {
$self->add_handler(
json => sub {
my ($r, $c, $output, $options) = @_;
$$output = Mojo::JSON->new->encode($options->{json});
$$output = $c->req->json_class->new->encode($options->{json});
}
);

Expand Down
14 changes: 2 additions & 12 deletions lib/Mojolicious/Sessions.pm
@@ -1,14 +1,12 @@
package Mojolicious::Sessions;
use Mojo::Base -base;

use Mojo::JSON;
use Mojo::Util qw(b64_decode b64_encode);

has [qw(cookie_domain secure)];
has cookie_name => 'mojolicious';
has cookie_path => '/';
has default_expiration => 3600;
has json_class => 'Mojo::JSON';

# "Bender, quit destroying the universe!"
sub load {
Expand All @@ -20,7 +18,7 @@ sub load {
# Deserialize
$value =~ s/-/=/g;
return
unless my $session = $self->json_class->new->decode(b64_decode $value);
unless my $session = $c->req->json_class->new->decode(b64_decode $value);

# Expiration
my $expiration = $self->default_expiration;
Expand Down Expand Up @@ -58,7 +56,7 @@ sub store {
if $expiration || $default;

# Serialize
my $value = b64_encode($self->json_class->new->encode($session), '');
my $value = b64_encode $c->req->json_class->new->encode($session), '';
$value =~ s/=/-/g;

# Session cookie
Expand Down Expand Up @@ -134,14 +132,6 @@ epoch seconds.
# Expire a long long time ago
$c->session(expires => 1);
=head2 C<json_class>
my $class = $sessions->json_class;
$sessions = $sessions->json_class('Mojo::JSON');
Class to be used for JSON serialization and deserialization of session data,
defaults to L<Mojo::JSON>.
=head2 C<secure>
my $secure = $sessions->secure;
Expand Down

0 comments on commit 21bb7a0

Please sign in to comment.