Changeset 1718

Show
Ignore:
Timestamp:
01/10/06 17:53:30
Author:
miyagawa
Message:

0.05: support encoding option. Content-Type changed to text/javascript

Files:

Legend:

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

    r1702 r1718  
    11Revision history for Perl extension Catalyst::View::JSON 
     2 
     30.05  Tue Jan 10 08:51:02 UTC 2006 
     4        - Added encoding option (default utf-8) to tackle with Safari 
     5          multibytes problem. Content-Type is now text/javascript (no +json) 
     6          (Thanks to kazeburo) 
    27 
    380.04  Sun Jan  8 17:06:56 UTC 2006 
  • Catalyst-View-JSON/trunk/lib/Catalyst/View/JSON.pm

    r1702 r1718  
    22 
    33use strict; 
    4 our $VERSION = '0.04'; 
     4our $VERSION = '0.05'; 
    55 
    66use base qw( Catalyst::View ); 
     7use Encode (); 
    78use NEXT; 
    89use JSON (); 
    910use Catalyst::Exception; 
    1011 
    11 __PACKAGE__->mk_accessors(qw( allow_callback callback_param expose_stash __json )); 
     12__PACKAGE__->mk_accessors(qw( allow_callback callback_param expose_stash encoding __json )); 
    1213 
    1314sub new { 
     
    6162    $self->validate_callback_param($cb) if $cb; 
    6263 
    63     $c->res->content_type('text/javascript+json'); # xxx 
     64    my $json = $self->_jsonize($data); 
     65 
     66    # When you set encoding option in View::JSON, this plugin DWIMs 
     67    my $encoding = $self->encoding || 'utf-8'; 
     68 
     69    # if you pass a valid Unicode flagged string in the stash, 
     70    # this view automatically transcodes to the encoding you set. 
     71    # Otherwise it just by passed the stash data in JSON format 
     72    if ( Encode::is_utf8($json) ) { 
     73        $json = Encode::encode($json, $encoding); 
     74    } 
     75 
     76    $c->res->content_type("text/javascript; charset=$encoding"); 
    6477 
    6578    my $output; 
    6679    $output .= "$cb(" if $cb; 
    67     $output .= $self->_jsonize($data)
     80    $output .= $json
    6881    $output .= ");"   if $cb; 
    6982 
     
    154167  {"foo":[1,2],"bar":2} 
    155168 
    156 But when you set C<< expose_stash => 'foo' >>, it'll just return 
     169When you set C<< expose_stash => [ 'foo' ] >>, it'll return 
     170 
     171  {"foo":[1,2]} 
     172 
     173and in the case of C<< expose_stash => 'foo' >>, it'll just return 
    157174 
    158175  [1,2] 
     
    163180 
    164181=back 
     182 
     183=head2 ENCODINGS 
     184 
     185Due to the browser gotchas like those of Safari and Opera, sometimes 
     186you have to specify a valid charset value in the response's 
     187Content-Type header, e.g. C<text/javascript; charset=utf-8>. 
     188 
     189Catalyst::View::JSON comes with the configuration variable C<encoding> 
     190which defaults to utf-8. You can change it via C<< YourApp->config >> 
     191or even runtime, using C<component>. 
     192 
     193  $c->component('View::JSON')->encoding('euc-jp'); 
     194 
     195This assumes you set your stash data in raw euc-jp bytes, or Unicode 
     196flagged variable. In case of Unicode flagged variable, 
     197Catalyst::View::JSON automatically encodes the data into your 
     198C<encoding> value (euc-jp in this case) before emitting the data to 
     199the browser. 
    165200 
    166201=head2 CALLBACKS