Changeset 1727

Show
Ignore:
Timestamp:
01/10/06 22:01:28
Author:
miyagawa
Message:

0.07: able to use JSON::Syck

Files:

Legend:

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

    r1719 r1727  
    11Revision history for Perl extension Catalyst::View::JSON 
     2 
     30.07  Tue Jan 10 13:00:19 UTC 2006 
     4        - Now able to use JSON::Syck as a converter by passing 
     5          json_driver as 'JSON::Syck' 
    26 
    370.06  Tue Jan 10 09:04:34 UTC 2006 
  • Catalyst-View-JSON/trunk/lib/Catalyst/View/JSON.pm

    r1719 r1727  
    22 
    33use strict; 
    4 our $VERSION = '0.06'; 
     4our $VERSION = '0.07'; 
    55 
    66use base qw( Catalyst::View ); 
    77use Encode (); 
    88use NEXT; 
    9 use JSON (); 
    109use Catalyst::Exception; 
    1110 
    12 __PACKAGE__->mk_accessors(qw( allow_callback callback_param expose_stash encoding __json )); 
     11__PACKAGE__->mk_accessors(qw( allow_callback callback_param expose_stash encoding json_dumper )); 
    1312 
    1413sub new { 
     
    2019            $self->$field($arguments->{$field}); 
    2120        } else { 
    22             $c->log->debug("Unkown config parameter '$field'") 
    23 #                if $c->debug; 
     21            $c->log->debug("Unkown config parameter '$field'"); 
    2422        } 
    2523    } 
    2624 
    27     $self->__json( JSON::Converter->new ); 
     25    my $driver = $arguments->{json_driver} || 'JSON'; 
     26    if ($driver eq 'JSON::Syck') { 
     27        require JSON::Syck; 
     28        $self->json_dumper(sub { JSON::Syck::Dump($_[0]) }); 
     29    } elsif ($driver eq 'JSON') { 
     30        require JSON::Converter; 
     31        my $conv   = JSON::Converter->new; 
     32        my $dumper = sub { 
     33            my $data = shift; 
     34            ref $data ? $conv->objToJson($data) : $conv->valueToJson($data); 
     35        }; 
     36        $self->json_dumper($dumper); 
     37    } else { 
     38        Catalyst::Exception->throw("Don't know json_driver $driver"); 
     39    } 
     40 
    2841    $self; 
    2942} 
     
    6275    $self->validate_callback_param($cb) if $cb; 
    6376 
    64     my $json = $self->_jsonize($data); 
     77    my $json = $self->json_dumper->($data); 
    6578 
    6679    # When you set encoding option in View::JSON, this plugin DWIMs 
     
    8295 
    8396    $c->res->output($output); 
    84 } 
    85  
    86 sub _jsonize { 
    87     my($self, $data) = @_; 
    88     ref $data ? $self->__json->objToJson($data) : $self->__json->valueToJson($data); 
    8997} 
    9098