Sledge::Plugin::JSON - JSON plugin for Sledge


Sledge-Plugin-JSON documentation Contained in the Sledge-Plugin-JSON distribution.

Index


Code Index:

NAME

Top

Sledge::Plugin::JSON - JSON plugin for Sledge

SYNOPSIS

Top

    package Your::Pages;
    use Sledge::Plugin::JSON;

    $self->output_json(
        {
            data     => \@data,
            encoding => 'euc-jp',
        }
    );

DESCRIPTION

Top

Sledge::Plugin::JSON is easy to implement JSON plugin for Sledge.

BUGS

Top

Please report any bugs or feature requests to bug-sledge-plugin-json at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Sledge-Plugin-JSON. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

AUTHORS

Top

Atsushi Kobayashi <atsushi __at__ mobilefactory.jp> hi-rocks

THANKS TO

Top

tokurirom Kensuke Kaneko

SUPPORT

Top

You can find documentation for this module with the perldoc command.

    perldoc Sledge::Plugin::JSON

You can also look for information at:

* AnnoCPAN: Annotated CPAN documentation

http://annocpan.org/dist/Sledge-Plugin-JSON

* CPAN Ratings

http://cpanratings.perl.org/d/Sledge-Plugin-JSON

* RT: CPAN's request tracker

http://rt.cpan.org/NoAuth/Bugs.html?Dist=Sledge-Plugin-JSON

* Search CPAN

http://search.cpan.org/dist/Sledge-Plugin-JSON

ACKNOWLEDGEMENTS

Top

COPYRIGHT & LICENSE

Top


Sledge-Plugin-JSON documentation Contained in the Sledge-Plugin-JSON distribution.

package Sledge::Plugin::JSON;
use warnings;
use strict;

use JSON::Syck;

our $VERSION = '0.01';
our $ConformToRFC4627 = 0;

sub import {
    my $self = shift;
    my $pkg  = caller;

    no strict 'refs';
    *{"$pkg\::output_json"} = \&_output_json;
}

sub _output_json {
    my ($self, $args) = @_;

    my $encoding = $args->{encoding} ? $args->{encoding} : 'utf-8';

    my $content_type =
        ( $self->debug_level && $self->r->param('debug') )
        ? "text/plain; charset=$encoding"
        : _content_type($self, $encoding );

    my $json = JSON::Syck::Dump($args->{data});
    my $output = _add_callback($self, _validate_callback_param($self, $self->r->param('callback')), $json );

    $self->r->content_type($content_type);
    $self->set_content_length(length $output);
    $self->send_http_header;
    $self->r->print($output);
    $self->invoke_hook('AFTER_OUTPUT');
    $self->finished(1);
}

# copy from Catalyst::View::JSON
sub _content_type {
    my ($self, $encoding) = @_;

    my $user_agent = $self->can('mobile') ? $self->mobile->agent->user_agent
                                          : $self->r->header_in('User-Agent');

    if ( $ConformToRFC4627 ) {
        return "application/json; charset=$encoding";
    } elsif (($user_agent || '') =~ /Opera/) {
        return "application/x-javascript; charset=$encoding";
    } else {
        return "application/javascript; charset=$encoding";
    }
}

sub _add_callback {
    my ($self, $cb, $json) = @_;

    my $output;
    $output .= "$cb(" if $cb;
    $output .= $json;
    $output .= ");"   if $cb;

    return $output;
}

sub _validate_callback_param {
   my ($self, $param) = @_;
   return ( $param =~ /^[a-zA-Z0-9\.\_\[\]]+$/ ) ? $param : undef;
}

1; # End of Sledge::Plugin::JSON