Changeset 2186

Show
Ignore:
Timestamp:
04/16/07 22:52:53
Author:
miyagawa
Message:

now use JSON::Any for json driver dispatching

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • Catalyst-View-JSON/trunk/Changes

    r2119 r2186  
    11Revision history for Perl extension Catalyst::View::JSON 
     2 
     30.15  Fri Apr 13 19:25:04 PDT 2007 
     4        - Switch to use JSON::Any to abstract JSON drivers imlementations 
     5          (Thanks to Florian Ragwitz for the patch) 
    26 
    370.14  Wed Dec 20 17:20:26 PST 2006 
  • Catalyst-View-JSON/trunk/lib/Catalyst/View/JSON.pm

    r2126 r2186  
    22 
    33use strict; 
    4 our $VERSION = '0.14'; 
     4our $VERSION = '0.15'; 
    55 
    66use base qw( Catalyst::View ); 
     
    88use NEXT; 
    99use Catalyst::Exception; 
     10require JSON::Any; 
    1011 
    1112__PACKAGE__->mk_accessors(qw( allow_callback callback_param expose_stash encoding json_dumper no_x_json_header )); 
     
    2425    } 
    2526 
    26     my $driver = $arguments->{json_driver} || 'JSON'; 
    27     if ($driver eq 'JSON::Syck') { 
    28         require JSON::Syck; 
    29         $self->json_dumper(sub { JSON::Syck::Dump($_[0]) }); 
    30     } elsif ($driver eq 'JSON') { 
    31         require JSON::Converter; 
    32         my $conv   = JSON::Converter->new; 
    33         my $dumper = sub { 
    34             my $data = shift; 
    35             ref $data ? $conv->objToJson($data) : $conv->valueToJson($data); 
    36         }; 
    37         $self->json_dumper($dumper); 
    38     } else { 
    39         Catalyst::Exception->throw("Don't know json_driver $driver"); 
    40     } 
    41  
    42     $self; 
     27    my $driver = $arguments->{json_driver} || 'Syck'; 
     28    $driver =~ s/^JSON:://; #backward compatibility 
     29 
     30    eval { 
     31        JSON::Any->import($driver); 
     32#        my $json = JSON::Any->new; 
     33        $self->json_dumper(sub { JSON::Any->objToJson($_[0]) }); 
     34    }; 
     35 
     36    if (my $error = $@) { 
     37        die $error; 
     38    } 
     39 
     40    return $self; 
    4341} 
    4442 
  • Catalyst-View-JSON/trunk/t/01_server.t

    r2120 r2186  
    5050 
    5151    my $data = JSON::jsonToObj($response->content); 
    52     is $data, "bar"
     52    is_deeply( $data, [1, 2, 3] )
    5353} 
    5454 
  • Catalyst-View-JSON/trunk/t/lib/TestApp.pm

    r2120 r2186  
    3232    my( $self, $c ) = @_; 
    3333 
    34     $c->component('View::JSON')->expose_stash('json_foo'); 
     34    $c->component('View::JSON')->expose_stash('json_baz'); 
    3535    $c->stash->{json_foo} = "bar"; 
    3636    $c->stash->{json_baz} = [ 1, 2, 3 ];