Group
Extension

Matches 15

Dancer2 ( C/CR/CROMEDOME/Dancer2-2.0.1.tar.gz, CROMEDOME, 2025; MetaCPAN )
Dancer2/lib/Dancer2/Serializer/JSON.pm ( view source; MetaCPAN )
package Dancer2::Serializer::JSON;
# ABSTRACT: Serializer for handling JSON data
$Dancer2::Serializer::JSON::VERSION = '2.0.1';
use Moo;
use JSON::MaybeXS ();
use Scalar::Util 'blessed';

with 'Dancer
cation/json'} );

# helpers
sub from_json { __PACKAGE__->deserialize(@_) }

sub to_json { __PACKAGE__->serialize(@_) }

sub decode_json {
    my ( $entity ) = @_;

    JSON::MaybeXS::decode_json($enti
ty);
}

sub encode_json {
    my ( $entity ) = @_;

    JSON::MaybeXS::encode_json($entity);
}

# class definition
sub serialize {
    my ( $self, $entity, $options ) = @_;

    my $config = blessed $
Dancer2 ( C/CR/CROMEDOME/Dancer2-2.0.1.tar.gz, CROMEDOME, 2025; MetaCPAN )
Dancer2/lib/Dancer2/Manual/Extending.pod ( view source; MetaCPAN )
handle serializing and deserializing data between
Perl data structures and different formats like JSON, YAML, or XML.

All serializer engines must implement the L<Dancer2::Core::Role::Serializer>
role
Dancer2 ( C/CR/CROMEDOME/Dancer2-2.0.1.tar.gz, CROMEDOME, 2025; MetaCPAN )
Dancer2/lib/Dancer2/Core/Request.pm ( view source; MetaCPAN )
umerical; decoding an SV that is an IV
    # converts that to a PVIV. Some serializers are picky (JSON)..
    $self->{_body_params} = $data;

    # Set body parameters (decoded HMV)
    $self->{'body_
Dancer2 ( C/CR/CROMEDOME/Dancer2-2.0.1.tar.gz, CROMEDOME, 2025; MetaCPAN )
Dancer2/lib/Dancer2/Core/DSL.pm ( view source; MetaCPAN )
version => { is_global => 1 },
        debug                => { is_global => 1 },
        decode_json          => { is_global => 1 },
        del                  => { is_global => 1 },
        delay
        => { is_global => 0 },
        dsl                  => { is_global => 1 },
        encode_json          => { is_global => 1 },
        engine               => { is_global => 1 },
        error
          => { is_global => 0 },
        from_dumper          => { is_global => 1 },
        from_json            => { is_global => 1 },
        from_yaml            => { is_global => 1 },
        get
Dancer2 ( C/CR/CROMEDOME/Dancer2-2.0.1.tar.gz, CROMEDOME, 2025; MetaCPAN )
Dancer2/lib/Dancer2/Config.pod ( view source; MetaCPAN )
ile F<config.EXT> (where F<EXT> is the
type of configuration file you are using; e.g. F<ini> or F<json> or
F<yml>) in the root directory of your application. This is considered
your global Dancer2 con
ted by
L<Config::Any>.  At the time of this writing, that includes YAML (.yml and
.yaml), JSON (.jsn and .json), INI (.ini), Apache-style configurations (.cnf
and .conf), XML (.xml), and Perl-style ha
session: "YAML"
    serializer: "JSON"

    plugins:
      DBIC:
        default:
          dsn: dbi:SQLite:db/mydata.db
          schema_class: Hello::Schema

If JSON is more your thing, your file mi
Dancer2 ( C/CR/CROMEDOME/Dancer2-2.0.1.tar.gz, CROMEDOME, 2025; MetaCPAN )
Dancer2/lib/Dancer2/Serializer/Mutable.pm ( view source; MetaCPAN )
dule';
with 'Dancer2::Core::Role::Serializer';

use constant DEFAULT_CONTENT_TYPE => 'application/json';

has '+content_type' => ( default => DEFAULT_CONTENT_TYPE() );

my $serializer = {
    'YAML'  
DSL::from_dumper(@_) },
    },
    'JSON'   => {
        to      => sub { Dancer2::Core::DSL::to_json(@_)   },
        from    => sub { Dancer2::Core::DSL::from_json(@_) },
    },
};

has mapping => (
,
            'text/x-data-dumper' => 'Dumper',
            'text/x-json'        => 'JSON',
            'application/json'   => 'JSON',
        }
    },
);

sub serialize {
    my ( $self, $entity ) =
Dancer2 ( C/CR/CROMEDOME/Dancer2-2.0.1.tar.gz, CROMEDOME, 2025; MetaCPAN )
Dancer2/lib/Dancer2/Manual.pod ( view source; MetaCPAN )

Templates help separate your display logic from your programming logic.
The same code that sends JSON to satisfy an API quest could also be used
to display an HTML page containing park information to
y HTML is intertwined with the code to gather the
data needed, the code necessary to just produce JSON becomes unnecessarily
complicated. Templates take the HTML out of your Perl code.

=head2 Views

insert that HTML
dynamically in the page, and the browser then takes over rendering it)

=item *

JSON/XML/etc. content that the JS decides what to do with, possibly
rendering some front-end template 
Dancer2 ( C/CR/CROMEDOME/Dancer2-2.0.1.tar.gz, CROMEDOME, 2025; MetaCPAN )
Dancer2/lib/Dancer2/CLI/Gen.pm ( view source; MetaCPAN )
tion
$Dancer2::CLI::Gen::VERSION = '2.0.1';
use Moo;
use URI;
use HTTP::Tiny;
use Path::Tiny;
use JSON::MaybeXS;
use Dancer2::Template::Tiny;
use Module::Runtime qw( use_module is_module_name );
use C
 5 )->get( 'https://fastapi.metacpan.org/release/Dancer2' );
    if( $resp->{ success } && decode_json( $resp->{ content } )->{ version } =~ /(\d\.\d+)/ ) {
        $latest_version = $1;
        if ($
Dancer2 ( C/CR/CROMEDOME/Dancer2-2.0.1.tar.gz, CROMEDOME, 2025; MetaCPAN )
Dancer2/lib/Dancer2/Manual/Testing.pod ( view source; MetaCPAN )
 that we want to test:

    # MyApp.pm
    package MyApp;
    use Dancer2;
    set serializer => 'JSON';

    my %users = (
        jason => {
            name  => 'Jason',
            likes => 'plane
arnings;

    use Test::More;
    use Plack::Test;
    use HTTP::Request::Common;
    use JSON qw/ decode_json /;

    use MyApp;

    # swap 'null' for 'note' to see the logs in your TAP output
    M
uccess, 'user not found';
        is $res->code => 404, 'ressource not found';

        is decode_json($res->content)->{message}, 'user not found', 'error message';
    };

    subtest 'Request with v
Dancer2 ( C/CR/CROMEDOME/Dancer2-2.0.1.tar.gz, CROMEDOME, 2025; MetaCPAN )
Dancer2/lib/Dancer2/Manual/Config.pod ( view source; MetaCPAN )
ile F<config.EXT> (where F<EXT> is the
type of configuration file you are using; e.g. F<ini> or F<json> or
F<yml>) in the root directory of your application. This is considered
your global Dancer2 con
ted by
L<Config::Any>.  At the time of this writing, that includes YAML (.yml and
.yaml), JSON (.jsn and .json), INI (.ini), Apache-style configurations (.cnf
and .conf), XML (.xml), and Perl-style ha
session: "YAML"
    serializer: "JSON"

    plugins:
      DBIC:
        default:
          dsn: dbi:SQLite:db/mydata.db
          schema_class: Hello::Schema

If JSON is more your thing, your file mi
Dancer2 ( C/CR/CROMEDOME/Dancer2-2.0.1.tar.gz, CROMEDOME, 2025; MetaCPAN )
Dancer2/lib/Dancer2/Core/Role/SessionFactory/File.pm ( view source; MetaCPAN )
--------------------------------------------------#

requires '_suffix';              # '.yml', '.json', etc.
requires '_thaw_from_handle';    # given handle, return session 'data' field
requires '_fr
Dancer2 ( C/CR/CROMEDOME/Dancer2-2.0.1.tar.gz, CROMEDOME, 2025; MetaCPAN )
Dancer2/lib/Dancer2/Manual/Cookbook.pod ( view source; MetaCPAN )
direct...
         template login => { error => 'Invalid username or password' };
     };

=head1 JSON/Serialization/API Recipes

=head2 Writing a REST application

With Dancer2, it's easy to write RE
er2 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
 format you require, for instance in your config file:

   serializer: JSON

Or directly in your code:

   set serializer => 'JSON';

From now, all hashrefs or arrayrefs returned by a route will be se
Dancer2 ( C/CR/CROMEDOME/Dancer2-2.0.1.tar.gz, CROMEDOME, 2025; MetaCPAN )
Dancer2/lib/Dancer2/Core/Role/Serializer.pm ( view source; MetaCPAN )
pe

The I<content type> of the object after being serialized. For example,
a JSON serializer would have a I<application/json> content type
defined.

=head1 METHODS

=head2 serialize($content, [\%optio
e it either in your config.yml file:

    #Set JSON engine
    serializer: "JSON"

    # Prettify JSON output
    engines:
      serializer:
        JSON:
          pretty: 1

To know which engines ar
Dancer2 ( C/CR/CROMEDOME/Dancer2-2.0.1.tar.gz, CROMEDOME, 2025; MetaCPAN )
Dancer2/lib/Dancer2/Manual/Keywords.pod ( view source; MetaCPAN )
L<decode_json|Dancer2::Manual/decode_json>

Deserializes a JSON string to a Perl data structure. Does not trigger
serialization hooks.

B<When to use>: When you need to translate incoming JSON to a Pe
rl hashref.

B<Related Keywords>: L</encode_json>, L</from_json>

=head2 L<delayed|Dancer2::Manual/delayed>

Initiates an asynchronous response. Requires a Plack/PSGI server capable
of serving request

B<See also>: L</delayed>, L</flush>

=head2 L<encode_json|Dancer2::Manual/encode_json>

Serializes a data structure to a UTF-8 encoded binary JSON string. Calling
this keyword will not trigger the ap
Dancer2 ( C/CR/CROMEDOME/Dancer2-2.0.1.tar.gz, CROMEDOME, 2025; MetaCPAN )
Dancer2/lib/Dancer2/Cookbook.pod ( view source; MetaCPAN )
ph, we need the url I</data> to return a JSON representation
of the word count data. Dancer in fact has a C<encode_json()> function that takes
care of the JSON encapsulation.

     get '/data' => sub 
ount;
         }

         my @json;  # data structure that is going to be JSONified

         while ( my ( $peep, $data ) = each %contestant ) {
             push @json, {
                 label     
my $end       = DateTime::Format::Flexible->parse_datetime( "2010-12-01")->epoch;

         push @json, {
             label => 'de par',
             data => [
                 [$beginning * 1000, 0]

Powered by Groonga
Maintained by Kenichi Ishigaki <ishigaki@cpan.org>. If you find anything, submit it on GitHub.