Changeset 2493

Show
Ignore:
Timestamp:
02/23/08 07:35:41
Author:
miyagawa
Message:

added encode_json overrides

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • Catalyst-View-JSON/trunk/lib/Catalyst/View/JSON.pm

    r2413 r2493  
    88use NEXT; 
    99use Catalyst::Exception; 
    10 require JSON::Any; 
    1110 
    1211__PACKAGE__->mk_accessors(qw( allow_callback callback_param expose_stash encoding json_dumper no_x_json_header )); 
     
    2827    $driver =~ s/^JSON:://; #backward compatibility 
    2928 
    30     eval { 
    31         JSON::Any->import($driver); 
    32         my $json = JSON::Any->new; # create the copy of JSON handler 
    33         $self->json_dumper(sub { $json->objToJson($_[0]) }); 
    34     }; 
    35  
    36     if (my $error = $@) { 
    37         die $error; 
     29    if (my $method = $self->can('encode_json')) { 
     30        $self->json_dumper( sub { 
     31                                my($data, $self, $c) = @_; 
     32                                $method->($self, $c, $data); 
     33                            } ); 
     34    } else { 
     35        eval { 
     36            require JSON::Any; 
     37            JSON::Any->import($driver); 
     38            my $json = JSON::Any->new; # create the copy of JSON handler 
     39            $self->json_dumper(sub { $json->objToJson($_[0]) }); 
     40        }; 
     41 
     42        if (my $error = $@) { 
     43            die $error; 
     44        } 
    3845    } 
    3946 
     
    7481    $self->validate_callback_param($cb) if $cb; 
    7582 
    76     my $json = $self->json_dumper->($data); 
     83    my $json = $self->json_dumper->($data, $self, $c); # weird order to be backward compat 
    7784 
    7885    # When you set encoding option in View::JSON, this plugin DWIMs 
     
    205212 
    206213By default this plugin uses JSON to encode the object, but you can 
    207 switch to the other drivers like JSON::Syck. For now, JSON::Syck is 
    208 the only alternative encoding driver
     214switch to the other drivers like JSON::Syck, whichever JSON::Any 
     215supports
    209216 
    210217=item no_x_json_header 
     
    218225=back 
    219226 
    220 =head2 ENCODINGS 
     227=head1 OVERRIDING JSON ENCODER 
     228 
     229By default it uses JSON::Any to serialize perl data strucuture into 
     230JSON data format. If you want to avoid this and encode with your own 
     231encoder (like passing options to JSON::XS etc.), you can implement 
     232C<encode_json> method in your View class. 
     233 
     234  package MyApp::View::JSON; 
     235  use base qw( Catalyst::View::JSON ); 
     236 
     237  use JSON::XS; 
     238 
     239  sub encode_json { 
     240      my($self, $c, $data) = @_; 
     241      my $encoder = JSON::XS->new->ascii->pretty->allow_nonref; 
     242      $encoder->encode($data); 
     243  } 
     244 
     245  1; 
     246 
     247=head1 ENCODINGS 
    221248 
    222249Due to the browser gotchas like those of Safari and Opera, sometimes 
     
    242269L<Encode::JavaScript::UCS> to use the encoding. 
    243270 
    244 =head2 CALLBACKS 
     271=head1 CALLBACKS 
    245272 
    246273By default it returns raw JSON data so your JavaScript app can deal 
  • Catalyst-View-JSON/trunk/t/01_server.t

    r2412 r2493  
    1414} 
    1515 
    16 plan tests => 38
     16plan tests => 40
    1717 
    1818BEGIN { 
     
    135135} 
    136136 
     137{ 
     138    my $request = HTTP::Request->new( GET => "http://localhost/foo6" ); 
    137139 
     140    ok( my $response = request($request), 'Request' ); 
     141    my $data = JSON::from_json($response->content); 
     142    is $data->{foo}, "fake"; 
     143} 
     144 
     145 
  • Catalyst-View-JSON/trunk/t/lib/TestApp.pm

    r2186 r2493  
    6060} 
    6161 
     62sub foo6 : Global { 
     63    my( $self, $c ) = @_; 
     64    $c->stash->{json_foo} = "\x{5bae}\x{5ddd}"; 
     65    $c->forward('TestApp::View::JSON2'); 
     66} 
     67 
    6268sub finalize_error { 
    6369    my $c = shift;