Catalyst-View-JSON-0.36/000755 000765 000024 00000000000 12625356447 016760 5ustar00jnapiorkowskistaff000000 000000 Catalyst-View-JSON-0.36/Changes000644 000765 000024 00000013360 12625354550 020247 0ustar00jnapiorkowskistaff000000 000000 Revision history for Perl extension Catalyst::View::JSON 0.36 Wed Nov 25 12:00:00 CT 2015 - POD updates, improved warning messages, strictness (andyjack++) - Make the default BOM added for safari optional. Possible breaking change, please file issues if this causes trouble. - New render method to let you just get a JSON encoded version of some data (Added to make compatible with the unofficial Catalyst View API that has a render method). - 0.35 Wed Jan 07 12:00:00 CT 2014 - Specify a version of JSON::MaybXS in the Makefile to close a test failure (test case requires version '1.003000' so we made that the required version). 0.34 Mon Jan 05 12:00:00 CT 2014 - switched from JSON::Any to JSON::MaybeXS (json_driver option no longer supported) - Updated some documenation to note that in 2015 perhaps some of the given advice is no longer relevent. - Added a new configuration option 'json_encoder_args' which lets you pass arguments to JSON::MaybeXS. Useful for when you want to have more control on how your JSON serializer works. 0.33 Tue Apr 12 12:20:22 PDT 2011 - Fixed local_request monkey patching to support Catalyst >= 5.89 0.32 Tue Jan 4 10:03:05 PST 2011 - remove Opera special casing (omega) 0.31 Wed Sep 15 15:14:33 PDT 2010 - Chrome doesn't like UTF-8 BOM (Larry Leszczynski) 0.30 Tue Apr 13 01:14:07 CEST 2010 - Fix test application to call $c->forward('View::JSON') rather than (the incorrect) $c->forward('MyApp::View::JSON') - Fix documentation to match the above change - Turn off the regex fallback behavior in the test applications on newer Catalyst versions to ensure tests are correctly forwarding. - Move actions out of the test application classes into controller classes to avoid deprecation warnings on newer Catalyst releases. - 0.29 had unintended commits accidentally pulled from ap/master. Reverted 0.28 Wed Mar 10 11:19:35 JST 2010 - No code changes. Fixed packaging, added YAML dep for testing 0.27 Tue Feb 23 18:09:02 PST 2010 - Added repository info to META.yml to satisfy people 0.26 Mon Aug 24 16:11:37 PDT 2009 - Work around not to expose Catalyst specific stash variables (Chris Prather) 0.25 Sat Apr 18 19:09:46 CST 2009 - Use MRO::Compat instead of NEXT. (Florian Ragwitz) 0.24 Mon Mar 3 01:42:30 PST 2008 - removed t/02_ut8.t since it doesn't do anything useful 0.23 Fri Feb 22 14:35:28 PST 2008 - Implemented and documented how to override JSON encoder in your view class by implementing encode_json() method. 0.22 Wed Jan 9 14:29:20 PST 2008 - Updated the test to work with JSON 2.x and now unbundle JSON.pm in inc/ (Thanks to Daniel Westermann-Clark) 0.21 Thu Oct 18 13:53:47 PDT 2007 - Update JSON::Any dependency to 1.11 (Thanks to typester and perigrin) 0.20 Wed Aug 29 03:23:27 PDT 2007 - Fixed POD config 'V::JSON' to 'View::JSON' (Thanks to Matt S Trout) 0.19 Fri Aug 3 15:32:04 PDT 2007 - Store JSON::Any object into json_dumper to prevent other modules to change JSON::Any backend like WWW::Facebook::API does. (Thanks to J.Shirley) 0.18 Mon Apr 30 21:08:06 PDT 2007 - Make 'JSON' as a default driver to make it backward compatible (Thanks to Andy Hobbs) 0.17 Wed Apr 25 16:04:44 PDT 2007 - Switch to Module::Install - include JSON.pm in inc/ for testing. (Thanks to Matt S Trout) 0.16 Tue Apr 17 20:11:38 PDT 2007 - Added JSON::Any dependency 0.15 Fri Apr 13 19:25:04 PDT 2007 - Switch to use JSON::Any to abstract JSON drivers imlementations (Thanks to Florian Ragwitz for the patch) 0.14 Wed Dec 20 17:20:26 PST 2006 - Oops, no_x_json_header in 0.13 was broken 0.13 Wed Dec 20 17:12:32 PST 2006 - Added UTF-8 BOM if the User-Agent is Safari and encoding is utf-8 (Jun Kuriyama) - Added 'no_x_json_header' option to disable auto-setting X-JSON for Prototype.js (Jun Kuriyama) - Documented json_driver config 0.12 Thu Oct 26 17:37:58 JST 2006 - Support X-JSON for Prototype.js (John Wang) - Updated interoperability doc (John Wang) 0.11 Mon Jul 31 16:21:14 JST 2006 - Fix validator so 'json_driver' should just work (Thanks to typester) 0.10 Sun Jul 30 23:46:43 JST 2006 - Updated document (Thanks to John Wang) - Switch Content-Type to application/json the standard other than Opera - Mention Encode::UCS::JavaScript in POD 0.09 Thu Apr 20 22:16:19 JST 2006 - Added Helper class (Thanks to Komatsu) 0.08 Tue Jan 10 13:31:39 UTC 2006 - Added Opera hack (Content-Type: applcation/x-javascript) (Thanks to kazeburo) 0.07 Tue Jan 10 13:00:19 UTC 2006 - Now able to use JSON::Syck as a converter by passing json_driver as 'JSON::Syck' 0.06 Tue Jan 10 09:04:34 UTC 2006 - Oops, 0.05 was broken. Fixed encode() problem and added test cases for them. 0.05 Tue Jan 10 08:51:02 UTC 2006 - Added encoding option (default utf-8) to tackle with Safari multibytes problem. Content-Type is now text/javascript (no +json) (Thanks to kazeburo) 0.04 Sun Jan 8 17:06:56 UTC 2006 - Added scalar support to expose_hash and updated the document. 0.03 Sun Jan 8 16:36:36 UTC 2006 - Added sanitization of callback function names to avoid XSS thingy. 0.02 Wed Jan 4 10:41:28 UTC 2006 - Include the tests file on distribution 0.01 Wed Jan 4 09:27:33 2006 - original version Catalyst-View-JSON-0.36/inc/000755 000765 000024 00000000000 12625356447 017531 5ustar00jnapiorkowskistaff000000 000000 Catalyst-View-JSON-0.36/lib/000755 000765 000024 00000000000 12625356447 017526 5ustar00jnapiorkowskistaff000000 000000 Catalyst-View-JSON-0.36/Makefile.PL000644 000765 000024 00000000453 12625345565 020733 0ustar00jnapiorkowskistaff000000 000000 use inc::Module::Install; use Module::Install::Repository; name 'Catalyst-View-JSON'; all_from 'lib/Catalyst/View/JSON.pm'; build_requires 'Test::More'; requires 'JSON::MaybeXS', '1.003000'; requires 'Catalyst', 5.60; requires 'MRO::Compat'; test_requires 'YAML'; auto_set_repository; WriteAll; Catalyst-View-JSON-0.36/MANIFEST000644 000765 000024 00000001166 12625356434 020111 0ustar00jnapiorkowskistaff000000 000000 Changes inc/Module/Install.pm inc/Module/Install/Base.pm inc/Module/Install/Can.pm inc/Module/Install/Fetch.pm inc/Module/Install/Makefile.pm inc/Module/Install/Metadata.pm inc/Module/Install/Repository.pm inc/Module/Install/Win32.pm inc/Module/Install/WriteAll.pm lib/Catalyst/Helper/View/JSON.pm lib/Catalyst/View/JSON.pm Makefile.PL MANIFEST This list of files META.yml MYMETA.json MYMETA.yml t/00_compile.t t/01_server.t t/lib/TestApp.pm t/lib/TestApp/Controller/Root.pm t/lib/TestApp/View/JSON.pm t/lib/TestApp/View/JSON2.pm t/lib/TestAppUnicode.pm t/lib/TestAppUnicode/Controller/Root.pm t/lib/TestAppUnicode/View/JSON.pm Catalyst-View-JSON-0.36/META.yml000644 000765 000024 00000001266 12625356426 020233 0ustar00jnapiorkowskistaff000000 000000 --- abstract: 'JSON view for your data' author: - 'Tatsuhiko Miyagawa ' build_requires: ExtUtils::MakeMaker: 6.59 Test::More: 0 YAML: 0 configure_requires: ExtUtils::MakeMaker: 6.59 distribution_type: module dynamic_config: 1 generated_by: 'Module::Install version 1.16' license: perl meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: 1.4 name: Catalyst-View-JSON no_index: directory: - inc - t requires: Catalyst: 5.6 JSON::MaybeXS: '1.003000' MRO::Compat: 0 perl: 5.8.1 resources: license: http://dev.perl.org/licenses/ repository: git://github.com/perl-catalyst/Catalyst-View-JSON.git version: '0.36' Catalyst-View-JSON-0.36/MYMETA.json000644 000765 000024 00000002132 12625356426 020642 0ustar00jnapiorkowskistaff000000 000000 { "abstract" : "JSON view for your data", "author" : [ "Tatsuhiko Miyagawa " ], "dynamic_config" : 0, "generated_by" : "ExtUtils::MakeMaker version 7.1, CPAN::Meta::Converter version 2.150005", "license" : [ "perl_5" ], "meta-spec" : { "url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec", "version" : "2" }, "name" : "Catalyst-View-JSON", "no_index" : { "directory" : [ "t", "inc" ] }, "prereqs" : { "build" : { "requires" : { "ExtUtils::MakeMaker" : "6.59", "Test::More" : "0", "YAML" : "0" } }, "configure" : { "requires" : { "ExtUtils::MakeMaker" : "0" } }, "runtime" : { "requires" : { "Catalyst" : "5.6", "JSON::MaybeXS" : "1.003000", "MRO::Compat" : "0", "perl" : "5.008001" } } }, "release_status" : "stable", "version" : "0.36", "x_serialization_backend" : "JSON::PP version 2.27203" } Catalyst-View-JSON-0.36/MYMETA.yml000644 000765 000024 00000001231 12625356426 020471 0ustar00jnapiorkowskistaff000000 000000 --- abstract: 'JSON view for your data' author: - 'Tatsuhiko Miyagawa ' build_requires: ExtUtils::MakeMaker: '6.59' Test::More: '0' YAML: '0' configure_requires: ExtUtils::MakeMaker: '0' dynamic_config: 0 generated_by: 'ExtUtils::MakeMaker version 7.1, CPAN::Meta::Converter version 2.150005' license: perl meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: '1.4' name: Catalyst-View-JSON no_index: directory: - t - inc requires: Catalyst: '5.6' JSON::MaybeXS: '1.003000' MRO::Compat: '0' perl: '5.008001' version: '0.36' x_serialization_backend: 'CPAN::Meta::YAML version 0.012' Catalyst-View-JSON-0.36/t/000755 000765 000024 00000000000 12625356447 017223 5ustar00jnapiorkowskistaff000000 000000 Catalyst-View-JSON-0.36/t/00_compile.t000644 000765 000024 00000000120 12625345565 021327 0ustar00jnapiorkowskistaff000000 000000 use strict; use Test::More tests => 1; BEGIN { use_ok 'Catalyst::View::JSON' } Catalyst-View-JSON-0.36/t/01_server.t000644 000765 000024 00000012071 12625354550 021210 0ustar00jnapiorkowskistaff000000 000000 use strict; use warnings; use FindBin; use lib "$FindBin::Bin/lib"; use Encode; use Test::More; use Catalyst::Test 'TestApp'; use JSON::MaybeXS 1.003000 ':legacy'; plan tests => 47; BEGIN { no warnings 'redefine'; if ( $Catalyst::VERSION < 5.89 ) { *Catalyst::Test::local_request = sub { my ( $class, $request ) = @_; require HTTP::Request::AsCGI; my $cgi = HTTP::Request::AsCGI->new( $request, %ENV )->setup; $class->handle_request; return $cgi->restore->response; }; } else { *Catalyst::Test::local_request => sub { my ( $class, $request ) = @_; my $app = ref($class) eq "CODE" ? $class : $class->_finalized_psgi_app; my $ret; require Plack::Test; Plack::Test::test_psgi( app => sub { $app->(%{ $_[0] }) }, client => sub { $ret = shift->($request) }, ); return $ret; }; } } my $entrypoint = "http://localhost/foo"; { my $request = HTTP::Request->new( GET => $entrypoint ); ok( my $response = request($request), 'Request' ); ok( $response->is_success, 'Response Successful 2xx' ); is( $response->code, 200, 'Response Code' ); is_deeply( [ $response->content_type ], [ 'application/json', 'charset=utf-8' ] ); my $data = from_json($response->content); is $data->{json_foo}, "bar"; is_deeply $data->{json_baz}, [ 1, 2, 3 ]; ok ! $data->{foo}, "doesn't return stash that doesn't match json_"; } { my $request = HTTP::Request->new( GET => "http://localhost/foo2" ); ok( my $response = request($request), 'Request' ); ok( $response->is_success, 'Response Successful 2xx' ); is( $response->code, 200, 'Response Code' ); is_deeply( [ $response->content_type ], [ 'application/json', 'charset=utf-8' ] ); my $data = from_json($response->content); is_deeply( $data, [1, 2, 3] ); } { my $request = HTTP::Request->new( GET => $entrypoint . "?cb=foobar" ); ok( my $response = request($request), 'Request' ); ok( $response->is_success, 'Response Successful 2xx' ); is( $response->code, 200, 'Response Code' ); is_deeply( [ $response->content_type ], [ 'application/json', 'charset=utf-8' ] ); my $body = $response->content; ok $body =~ s/^foobar\((.*?)\);$/$1/sg, "wrapped in a callback"; my $data = from_json($body); is $data->{json_foo}, "bar"; is_deeply $data->{json_baz}, [ 1, 2, 3 ]; ok ! $data->{foo}, "doesn't return stash that doesn't match json_"; } { my $request = HTTP::Request->new( GET => $entrypoint . "?cb=foobar%28" ); ok( my $response = request($request), 'Request' ); like $response->header('X-Error'), qr/Invalid callback parameter/,; } { ## my $request = HTTP::Request->new( GET => "http://localhost/foo3" ); ok( my $response = request($request), 'Request' ); is_deeply( [ $response->content_type ], [ 'application/json', 'charset=utf-8' ] ); ok decode('utf-8', $response->content); } { my $request = HTTP::Request->new( GET => "http://localhost/foo4" ); ok( my $response = request($request), 'Request' ); is_deeply( [ $response->content_type ], [ 'application/json', 'charset=euc-jp' ] ); ok decode('euc-jp', $response->content); } { my $request = HTTP::Request->new( GET => "http://localhost/foo3" ); $request->header("User-Agent", "Safari"); ok( my $response = request($request), 'Request' ); is_deeply( [ $response->content_type ], [ 'application/json', 'charset=utf-8' ] ); my $bom = substr $response->content, 0, 3; is $bom, "\xEF\xBB\xBF"; } { my $request = HTTP::Request->new( GET => "http://localhost/foo3" ); $request->header("User-Agent", "Safari"); ok( my $response = request($request), 'Request' ); is_deeply( [ $response->content_type ], [ 'application/json', 'charset=utf-8' ] ); my $bom = substr $response->content, 0, 3; is $bom, "\xEF\xBB\xBF"; } { my $request = HTTP::Request->new( GET => "http://localhost/foo3" ); $request->header("X-Prototype-Version", "1.5"); ok( my $response = request($request), 'Request' ); ok $response->header('X-JSON'); } { my $request = HTTP::Request->new( GET => "http://localhost/foo5" ); $request->header("X-Prototype-Version", "1.5"); ok( my $response = request($request), 'Request' ); ok !$response->header('X-JSON'); } { my $request = HTTP::Request->new( GET => "http://localhost/foo6" ); ok( my $response = request($request), 'Request' ); my $data = from_json($response->content); is $data->{foo}, "fake"; } { my $request = HTTP::Request->new( GET => "http://localhost/warnmsg" ); ok( my $response = request($request), 'Request' ); ok( $response->is_success, 'Response Successful 2xx' ); is( $response->code, 200, 'Response Code' ); is_deeply( [ $response->content_type ], [ 'application/json', 'charset=utf-8' ] ); my $data = from_json($response->content); is $data->{json_foo}, "bar"; is_deeply $data->{json_baz}, [ 1, 2, 3 ]; is $data->{'foo'}, 'barbarbar'; } Catalyst-View-JSON-0.36/t/lib/000755 000765 000024 00000000000 12625356447 017771 5ustar00jnapiorkowskistaff000000 000000 Catalyst-View-JSON-0.36/t/lib/TestApp/000755 000765 000024 00000000000 12625356447 021351 5ustar00jnapiorkowskistaff000000 000000 Catalyst-View-JSON-0.36/t/lib/TestApp.pm000644 000765 000024 00000000761 12625354550 021704 0ustar00jnapiorkowskistaff000000 000000 package TestApp; use strict; use warnings; use MRO::Compat; use Catalyst; our $VERSION = '0.01'; __PACKAGE__->config({ name => 'TestApp', disable_component_resolution_regex_fallback => 1, 'View::JSON' => { use_force_bom => 1, expose_stash => qr/^json_/, allow_callback => 1, callback_param => 'cb', }, }); __PACKAGE__->setup; sub finalize_error { my $c = shift; $c->res->header('X-Error' => $c->error->[0]); $c->next::method; } 1; Catalyst-View-JSON-0.36/t/lib/TestAppUnicode/000755 000765 000024 00000000000 12625356447 022660 5ustar00jnapiorkowskistaff000000 000000 Catalyst-View-JSON-0.36/t/lib/TestAppUnicode.pm000644 000765 000024 00000000462 12625345565 023217 0ustar00jnapiorkowskistaff000000 000000 package TestAppUnicode; use strict; use warnings; use Catalyst qw( Unicode ); __PACKAGE__->config({ name => 'TestAppUnicode', disable_component_resolution_regex_fallback => 1, 'View::JSON' => { allow_callback => 1, callback_param => 'cb', }, }); __PACKAGE__->setup; 1; Catalyst-View-JSON-0.36/t/lib/TestAppUnicode/Controller/000755 000765 000024 00000000000 12625356447 025003 5ustar00jnapiorkowskistaff000000 000000 Catalyst-View-JSON-0.36/t/lib/TestAppUnicode/View/000755 000765 000024 00000000000 12625356447 023572 5ustar00jnapiorkowskistaff000000 000000 Catalyst-View-JSON-0.36/t/lib/TestAppUnicode/View/JSON.pm000644 000765 000024 00000000115 12625345565 024675 0ustar00jnapiorkowskistaff000000 000000 package TestAppUnicode::View::JSON; use base qw( Catalyst::View::JSON ); 1; Catalyst-View-JSON-0.36/t/lib/TestAppUnicode/Controller/Root.pm000644 000765 000024 00000000425 12625345565 026264 0ustar00jnapiorkowskistaff000000 000000 package TestAppUnicode::Controller::Root; use strict; use warnings; use base 'Catalyst::Controller'; __PACKAGE__->config(namespace => ''); sub foo : Global { my ( $self, $c ) = @_; $c->stash->{foo} = "\x{30c6}\x{30b9}\x{30c8}"; $c->forward('View::JSON'); } 1; Catalyst-View-JSON-0.36/t/lib/TestApp/Controller/000755 000765 000024 00000000000 12625356447 023474 5ustar00jnapiorkowskistaff000000 000000 Catalyst-View-JSON-0.36/t/lib/TestApp/View/000755 000765 000024 00000000000 12625356447 022263 5ustar00jnapiorkowskistaff000000 000000 Catalyst-View-JSON-0.36/t/lib/TestApp/View/JSON.pm000644 000765 000024 00000000106 12625345565 023366 0ustar00jnapiorkowskistaff000000 000000 package TestApp::View::JSON; use base qw( Catalyst::View::JSON ); 1; Catalyst-View-JSON-0.36/t/lib/TestApp/View/JSON2.pm000644 000765 000024 00000000244 12625345565 023453 0ustar00jnapiorkowskistaff000000 000000 package TestApp::View::JSON2; use base qw( Catalyst::View::JSON ); use YAML; sub encode_json { my($self, $c, $data) = @_; return qq({"foo":"fake"}); } 1; Catalyst-View-JSON-0.36/t/lib/TestApp/Controller/Root.pm000644 000765 000024 00000003240 12625354550 024745 0ustar00jnapiorkowskistaff000000 000000 package TestApp::Controller::Root; use strict; use warnings; use base 'Catalyst::Controller'; __PACKAGE__->config(namespace => ''); sub foo : Global { my ( $self, $c ) = @_; $c->component('View::JSON')->expose_stash(qr/^json_/); $c->stash->{json_foo} = "bar"; $c->stash->{json_baz} = [ 1, 2, 3 ]; $c->stash->{foo} = "barbarbar"; $c->forward('View::JSON'); } sub warnmsg : Global { my ( $self, $c ) = @_; # case where setting expose_stash returns everything in # the stash. Set a true value to enter to code path # that shows the warning message. $c->component('View::JSON')->expose_stash(\1); $c->stash->{json_foo} = "bar"; $c->stash->{json_baz} = [ 1, 2, 3 ]; $c->stash->{foo} = "barbarbar"; $c->forward('View::JSON'); } sub foo2 : Global { my( $self, $c ) = @_; $c->component('View::JSON')->expose_stash('json_baz'); $c->stash->{json_foo} = "bar"; $c->stash->{json_baz} = [ 1, 2, 3 ]; $c->forward('View::JSON'); } sub foo3 : Global { my( $self, $c ) = @_; $c->stash->{json_foo} = "\x{5bae}\x{5ddd}"; $c->component('View::JSON')->encoding('utf-8'); $c->forward('View::JSON'); } sub foo4 : Global { my( $self, $c ) = @_; $c->stash->{json_foo} = "\x{5bae}\x{5ddd}"; $c->component('View::JSON')->encoding('euc-jp'); $c->forward('View::JSON'); } sub foo5 : Global { my( $self, $c ) = @_; $c->stash->{json_foo} = "\x{5bae}\x{5ddd}"; $c->component('View::JSON')->no_x_json_header(1); $c->forward('View::JSON'); } sub foo6 : Global { my( $self, $c ) = @_; $c->stash->{json_foo} = "\x{5bae}\x{5ddd}"; $c->forward('View::JSON2'); } 1; Catalyst-View-JSON-0.36/lib/Catalyst/000755 000765 000024 00000000000 12625356447 021312 5ustar00jnapiorkowskistaff000000 000000 Catalyst-View-JSON-0.36/lib/Catalyst/Helper/000755 000765 000024 00000000000 12625356447 022531 5ustar00jnapiorkowskistaff000000 000000 Catalyst-View-JSON-0.36/lib/Catalyst/View/000755 000765 000024 00000000000 12625356447 022224 5ustar00jnapiorkowskistaff000000 000000 Catalyst-View-JSON-0.36/lib/Catalyst/View/JSON.pm000644 000765 000024 00000036502 12625355164 023334 0ustar00jnapiorkowskistaff000000 000000 package Catalyst::View::JSON; use strict; use warnings; our $VERSION = '0.36'; use 5.008_001; use base qw( Catalyst::View ); use Encode (); use MRO::Compat; use Catalyst::Exception; __PACKAGE__->mk_accessors(qw( allow_callback callback_param expose_stash encoding json_dumper no_x_json_header json_encoder_args use_force_bom)); sub new { my($class, $c, $arguments) = @_; my $self = $class->next::method($c); for my $field (keys %$arguments) { # Remove catalyst_component_name (and future Cat specific params) next if $field =~ /^catalyst/; # no longer supported warn('json_driver is no longer supported'), next if $field eq 'json_driver'; if ($self->can($field)) { $self->$field($arguments->{$field}); } else { $c->log->debug("Unknown config parameter '$field'"); } } if (my $method = $self->can('encode_json')) { $self->json_dumper( sub { my($data, $self, $c) = @_; $method->($self, $c, $data); } ); } else { require JSON::MaybeXS; my %args = (utf8=>1, %{$self->json_encoder_args ||+{}}); my $json = JSON::MaybeXS->new(%args); $self->json_dumper(sub { $json->encode($_[0]) }); } return $self; } sub process { my($self, $c) = @_; # get the response data from stash my $cond = sub { 1 }; my $single_key; if (my $expose = $self->expose_stash) { if (ref($expose) eq 'Regexp') { $cond = sub { $_[0] =~ $expose }; } elsif (ref($expose) eq 'ARRAY') { my %match = map { $_ => 1 } @$expose; $cond = sub { $match{$_[0]} }; } elsif (!ref($expose)) { $single_key = $expose; } else { $c->log->warn("expose_stash should be an array reference, Regexp object, or key for a single stash entry."); $c->log->warn("Returning all stash entries"); } } my $data; if ($single_key) { $data = $c->stash->{$single_key}; } else { $data = { map { $cond->($_) ? ($_ => $c->stash->{$_}) : () } keys %{$c->stash} }; } my $cb_param = $self->allow_callback ? ($self->callback_param || 'callback') : undef; my $cb = $cb_param ? $c->req->param($cb_param) : undef; $self->validate_callback_param($cb) if $cb; # When you set encoding option in View::JSON, this plugin DWIMs my $encoding = $self->encoding || 'utf-8'; $c->res->content_type("application/json; charset=$encoding"); if ($c->req->header('X-Prototype-Version') && !$self->no_x_json_header) { $c->res->header('X-JSON' => 'eval("("+this.transport.responseText+")")'); } my $json = $self->render($c, $data); my $output; ## add UTF-8 BOM if the client meets a test and the application wants it. if ($self->use_force_bom && $encoding eq 'utf-8') { my $user_agent = $c->req->user_agent || ''; if ($self->user_agent_bom_test($user_agent)) { $output = "\xEF\xBB\xBF"; } } $output .= "$cb(" if $cb; $output .= $json; $output .= ");" if $cb; $c->res->output($output); } # allow for called as $c, $template, $data || $c, $data so that we are compatible # with the semi standard render method that a lot of views use. sub render { my $self = shift; my $c = shift; my $data = pop; return $self->json_dumper->($data, $self, $c); # weird order to be backward compat } sub user_agent_bom_test { my ($self, $user_agent) = @_; return(($user_agent =~ m/\bSafari\b/) and ($user_agent !~ m/\bChrome\b/)); } sub validate_callback_param { my($self, $param) = @_; $param =~ /^[a-zA-Z0-9\.\_\[\]]+$/ or Catalyst::Exception->throw("Invalid callback parameter $param"); } 1; __END__ =head1 NAME Catalyst::View::JSON - JSON view for your data =head1 SYNOPSIS # lib/MyApp/View/JSON.pm package MyApp::View::JSON; use base qw( Catalyst::View::JSON ); 1; # configure in lib/MyApp.pm MyApp->config({ ... 'View::JSON' => { allow_callback => 1, # defaults to 0 callback_param => 'cb', # defaults to 'callback' expose_stash => [ qw(foo bar) ], # defaults to everything }, }); sub hello : Local { my($self, $c) = @_; $c->stash->{message} = 'Hello World!'; $c->forward('View::JSON'); } =head1 DESCRIPTION Catalyst::View::JSON is a Catalyst View handler that returns stash data in JSON format. =head1 CONFIG VARIABLES =over 4 =item allow_callback Flag to allow callbacks by adding C. Defaults to 0 (doesn't allow callbacks). See L for details. =item callback_param Name of URI parameter to specify JSON callback function name. Defaults to C. Only effective when C is turned on. =item expose_stash Scalar, List or regular expression object, to specify which stash keys are exposed as a JSON response. Defaults to everything. Examples configuration: # use 'json_data' value as a data to return expose_stash => 'json_data', # only exposes keys 'foo' and 'bar' expose_stash => [ qw( foo bar ) ], # only exposes keys that matches with /^json_/ expose_stash => qr/^json_/, Suppose you have data structure of the following. $c->stash->{foo} = [ 1, 2 ]; $c->stash->{bar} = 2; By default, this view will return: {"foo":[1,2],"bar":2} When you set C<< expose_stash => [ 'foo' ] >>, it'll return {"foo":[1,2]} and in the case of C<< expose_stash => 'foo' >>, it'll just return [1,2] instead of the whole object (hashref in perl). This option will be useful when you share the method with different views (e.g. TT) and don't want to expose non-irrelevant stash variables as in JSON. =item no_x_json_header no_x_json_header: 1 By default this plugin sets X-JSON header if the requested client is a Prototype.js with X-JSON support. By setting 1, you can opt-out this behavior so that you can do eval() by your own. Defaults to 0. =item json_encoder_args An optional hashref that supplies arguments to L used when creating a new object. =item use_force_bom If versions of this view older than 0.36, there was some code that added a UTF-8 BOM marker to the end of the JSON string when the user agent was Safari. After looking at a lot of existing code I don't think this is needed anymore so we removed it by default. However if this turns out to be a problem you can re enable it by setting this attribute to true. Possible a breaking change so we offer this workaround. You may also override the method 'user_agent_bom_test' which received the current request user agent string to try and better determine if this is needed. Patches for this welcomed. =back =head1 METHODS =head2 process Standard target of $c->forward used to prepare a response =head2 render The methods accepts either of the following argument signatures in order to promote compatibility with the semi standard render method as define in numerous L views on CPAN: my $json_string = $c->view('JSON')->render($c, undef, $data); my $json_string = $c->view('JSON')->render($c, $data); Given '$data' returns the JSON serialized version, or throws and error. =head1 OVERRIDING JSON ENCODER By default it uses L to serialize perl data structure into JSON data format. If you want to avoid this and encode with your own encoder (like passing different options to L etc.), you can implement the C method in your View class. package MyApp::View::JSON; use base qw( Catalyst::View::JSON ); use JSON::MaybeXS (); sub encode_json { my($self, $c, $data) = @_; my $encoder = JSON::MaybeXS->new->(ascii => 1, pretty => 1, allow_nonref => 1); $encoder->encode($data); } 1; =head1 ENCODINGS B Starting in release v5.90080 L encodes all text like body returns as UTF8. It however ignores content types like application/json and assumes that a correct JSON serializer is doing what it is supposed to do, which is encode UTF8 automatically. In general this is what this view does so you shoulding need to mess with the encoding flag here unless you have some odd case. Also, the comment aboe regard 'browser gotcha's' was written a number of years ago and I can't say one way or another if those gotchas continue to be common in the wild. B Setting this configuation has no bearing on how the actual serialized string is encoded. This ONLY sets the content type header in your response. By default we set the 'utf8' flag on L so that the string generated and set to your response body is proper UTF8 octets that can be transmitted over HTTP. If you are planning to do some alternative encoding you should turn off this default via the C: MyApp::View::JSON->config( json_encoder_args => +{utf8=>0} ); B In 2015 the use of UTF8 as encoding is widely standard so it is very likely you should need to do nothing to get the correct encoding. The following documention will remain for historical value and backcompat needs. Due to the browser gotchas like those of Safari and Opera, sometimes you have to specify a valid charset value in the response's Content-Type header, e.g. C. Catalyst::View::JSON comes with the configuration variable C which defaults to utf-8. You can change it via C<< YourApp->config >> or even runtime, using C. $c->component('View::JSON')->encoding('euc-jp'); This assumes you set your stash data in raw euc-jp bytes, or Unicode flagged variable. In case of Unicode flagged variable, Catalyst::View::JSON automatically encodes the data into your C value (euc-jp in this case) before emitting the data to the browser. Another option would be to use I as an encoding (and pass Unicode flagged string to the stash). That way all non-ASCII characters in the output JSON will be automatically encoded to JavaScript Unicode encoding like I<\uXXXX>. You have to install L to use the encoding. =head1 CALLBACKS By default it returns raw JSON data so your JavaScript app can deal with using XMLHttpRequest calls. Adding callbacks (JSONP) to the API gives more flexibility to the end users of the API: overcome the cross-domain restrictions of XMLHttpRequest. It can be done by appending I