Skip to content

Commit

Permalink
added json_class attribute to Mojo::UserAgent::Transactor
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jul 20, 2012
1 parent 21bb7a0 commit 2599c37
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
1 change: 1 addition & 0 deletions Changes
Expand Up @@ -3,6 +3,7 @@
- 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 n function to ojo.
- Improved text_field helper to always set the type attribute. (marty, sri)
- Improved documentation.
Expand Down
16 changes: 13 additions & 3 deletions lib/Mojo/UserAgent/Transactor.pm
Expand Up @@ -6,12 +6,15 @@ 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 @@ -95,9 +98,8 @@ sub form {

sub json {
my ($self, $url, $data) = (shift, shift, shift);
my $tx = $self->tx(POST => $url, @_);
my $req = $tx->req;
my $headers = $req->body($req->json_class->new->encode($data))->headers;
my $tx = $self->tx(POST => $url, @_, $self->json_class->new->encode($data));
my $headers = $tx->req->headers;
$headers->content_type('application/json') unless $headers->content_type;
return $tx;
}
Expand Down Expand Up @@ -274,6 +276,14 @@ 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

0 comments on commit 2599c37

Please sign in to comment.