package Dancer::Serializer::JSON;
our $AUTHORITY = 'cpan:SUKRIA';
#ABSTRACT: serializer for handling JSON data
$Dancer::Serializer::JSON::VERSION = '1.3521';
use strict;
use warnings;
use Carp;
use Da
r::Abstract';
# helpers
sub from_json {
my $s = Dancer::Serializer::JSON->new;
$s->deserialize(@_);
}
sub to_json {
my $s = Dancer::Serializer::JSON->new;
$s->serialize(@_);
}
# c
sub loaded { Dancer::ModuleLoader->load_with_params('JSON', '-support_by_pp') }
sub init {
my ($self) = @_;
raise core_serializer => 'JSON is needed and is not installed'
unless $self->
text/html' => 'YAML',
'text/xml' => 'XML',
'text/x-json' => 'JSON',
'application/json' => 'JSON',
};
my $loaded_serializer = {};
my $_content_type;
sub template_or_se
}
if $params && $params->{content_type};
}
push @content_types, 'application/json';
# remove duplicates
my %seen;
return [ grep { not $seen{$_}++ } @content_types ];
from the URL
=item
the B<accept> from the request headers
=item
The default is B<application/json>
=back
The content-type/serializer mapping that C<Dancer::Serializer::Mutable>
uses is
ser
VERSION
version 1.3521
=head1 SYNOPSIS
my $engine = Dancer::Engine->build( Serializer => 'JSON', $configuration );
=head1 DESCRIPTION
Dancer has various engines such Serializer engines, Temp
t it inherits from this
class.
=head1 ATTRIBUTES
=head2 name
The name of the engine, such as I<JSON>, or I<Simple>.
=head2 type
The type of the engine, such as I<Serializer>, or I<Session>.
=hea
an example of a route handler that will return a HashRef
use Dancer;
set serializer => 'JSON';
get '/user/:id/' => sub {
{ foo => 42,
number => 100234,
list
ot
the case by default - Dancer renders the response via the current
serializer.
Hence, with the JSON serializer set, the route handler above would result in a
content like the following:
{"numb
ey dynamically depend on
Perl modules you may not have on your system.
=over 4
=item B<JSON>
requires L<JSON>
=item B<YAML>
requires L<YAML>
=item B<XML>
requires L<XML::Simple>
=item B<Mutabl
cer provides helpers to
serialize and deserialize for the following data formats:
=over 4
=item JSON
=item YAML
=item XML
=item Data::Dumper
=back
To activate this feature, you only have to set
he
format you require, for instance in your config.yml:
serializer: JSON
Or right in your code:
set serializer => 'JSON';
From now, all HashRefs or ArrayRefs returned by a route will be seri
# don't create a new serializer unless it's defined in the config
# (else it's created using json, and that's *not* what we want)
my $serializer_name = Dancer::App->current->setting('serializ
$serializer_name);
return;
}
sub init {
my ($class, $name, $config) = @_;
$name ||= 'JSON';
$_engine = Dancer::Engine->build('serializer' => $name, $config);
return $_engine;
}
#
e:
serializer: "YAML"
Or in the application code:
# setting JSON as the default serializer
set serializer => 'JSON';
In your routes you can access parameters just like any route.
When
}
sub _is_text {
my ($content_type) = @_;
return $content_type =~ /(\bx(?:ht)?ml\b|text|json|javascript)/;
}
# Fancy banner to print on startup
sub print_banner {
if (setting('startup_i
n the author tests (in F</xt>) against the built code.
=item $ dzil listdeps --json
List all the dependencies, in JSON.
=item $ dzil build --notgz
Build the code, but don't generate the tarball.
package Dancer::Serializer::JSONP;
our $AUTHORITY = 'cpan:SUKRIA';
$Dancer::Serializer::JSONP::VERSION = '1.3521';
# ABSTRACT: serializer for handling JSONP data
use strict;
use warnings;
use Dancer:
:JSON';
sub serialize {
my $self = shift;
my $callback = Dancer::SharedData->request->params('query')->{callback};
my $json = $self->SUPER::serialize(@_);
return $callback . '(' . $json
zer::JSONP - serializer for handling JSONP data
=head1 VERSION
version 1.3521
=head1 SYNOPSIS
=head1 DESCRIPTION
This class is a subclass of L<Dancer::Serializer::JSON> with support for JSONP.
I
ritten
with plugin setting 'content_type'.
Here is example to use JSON:
plugins:
'Ajax':
content_type: 'application/json'
=head1 AUTHOR
This module has been written by Alexis Sukrieh <
# set the status
$response->content_type('application/json');
Dancer::SharedData->response->content_type('application/json');
Set or get the status of the current response object.
=hea
g it into some 8bit
charset, for example).
Also, since automatically serialized JSON responses have
C<application/json> Content-Type, you should always encode them by
hand.
Can also be set with envi
ancer::Hook;
use Dancer::Logger;
use Dancer::Renderer;
use Dancer::Route;
use Dancer::Serializer::JSON;
use Dancer::Serializer::YAML;
use Dancer::Serializer::XML;
use Dancer::Serializer::Dumper;
use D
er_version
debug
del
dirname
info
error
engine
false
forward
from_dumper
from_json
from_yaml
from_xml
get
halt
header
headers
hook
layout
load
load_app
logger
error
set
setting
set_cookie
session
splat
status
start
template
to_dumper
to_json
to_yaml
to_xml
true
upload
captures
uri_for
var
vars
warning
);
# Dancer's syn