The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Catalyst::View::Base::JSON::_ClassInfo - Application Level Info for your View

SYNOPSIS

    NA - Internal use only.

DESCRIPTION

This is used by the main class Catalyst::View::JSON::PerRequest to hold application level information, mostly configuration and a few computations you would rather do once.

No real public reusably bits here, just for your inspection.

ATTRIBUTES

This View defines the following attributes that can be set during configuration

content_type

Sets the response content type. Defaults to 'application/json'.

returns_status

An optional arrayref of HTTP::Status codes that the view is allowed to generate. Setting this will injection helper methods into your view:

    $view->http_ok;
    $view->202;

Both 'friendly' names and numeric codes are generated (I recommend you stick with one style or the other in a project to avoid confusion. Helper methods return the view object to make common chained calls easier:

    $view->http_bad_request->detach;

callback_param

Optional. If set, we use this to get a method name for JSONP from the query parameters.

For example if 'callback_param' is 'callback' and the request is:

    localhost/foo/bar?callback=mymethod

Then the JSON response will be wrapped in a function call similar to:

    mymethod({
      'foo': 'bar',
      'baz': 'bin});

Which is a common technique for overcoming some cross-domain restrictions of XMLHttpRequest.

There are some restrictions to the value of the callback method, for security. For more see: http://ajaxian.com/archives/jsonp-json-with-padding

json_class

The class used to perform JSON encoding. Default is JSON::MaybeXS

json_init_args

Arguments used to initialize the "json_class". Defaults to:

    our %JSON_INIT_ARGS = (
      utf8 => 1,
      convert_blessed => 1);

json_extra_init_args

Allows you to 'tack on' some arguments to the JSON initialization without messing with the defaults. Unless you really need to override the defaults this is the method you should use.

handle_encode_error

A reference to a subroutine that is called when there is a failure to encode the data given into a JSON format. This can be used globally as an attribute on the defined configuration for the view, and you can set it or overide the global settings on a context basis.

Setting this optional attribute will capture and handle error conditions. We will NOT bubble the error up to the global Catalyst error handling (we don't set $c->error for example). If you want that you need to set it yourself in a custom handler, or don't define one.

The subroutine receives three arguments: the view object, the original reference that failed to encode and the exception. You must setup a new, valid response. For example:

    package MyApp::View::JSON;

    use Moo;
    extends 'Catalyst::View::Base::JSON';

    package MyApp;

    use Catalyst;

    MyApp->config(
      default_view =>'JSON',
      'View::JSON' => {
        handle_encode_error => sub {
          my ($view, $original_bad_ref, $err) = @_;
          $view->response(400, { error => "$err"})->detach;
        },
      },
    );

    MyApp->setup;

NOTE If you mess up the return value (you return something that can't be encoded) a second exception will occur which will NOT be handled and will then bubble up to the main application.

NOTE We define a rational default for this to get you started:

    sub HANDLE_ENCODE_ERROR {
      my ($view, $orginal_bad_ref, $err) = @_;
      $view->response(400, { error => "$err"})->detach;
    }

SEE ALSO

Catalyst, Catalyst::View, Catalyst::View::JSON, JSON::MaybeXS

AUTHOR

See Catalyst::View::Base::JSON

COPYRIGHT & LICENSE

See Catalyst::View::Base::JSON