The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Dancer2::Serializer::JsonApi

VERSION

version 0.0.1

SYNOPSIS

As part of a Dancer2 App:

    # in config.yaml
    
    serializer: JsonApi  

As a standalone module:

    use Dancer2::Serializer::JsonApi;
    use Dancer2::Plugin::JsonApi::Registry;

    my $registry = Dancer2::Plugin::JsonApi::Registry->new;

    $registry->add_type( 'spaceship' => {
        relationships => {
            crew => { type => 'person' }
        }
    } );

    $registry->add_type( 'person' );

    my $serializer = Dancer2::Serializer::JsonApi->new( 
        registry => $registry 
    );

    my $serialized = $serializer->serialize([ 
        'spaceship', {
            id => 1,
            name => 'Unrequited Retribution',
            crew => [
                { id => 2, name => 'One-eye Flanagan', species => 'human' },
                { id => 3, name => 'Flabgor', species => 'Moisterian' },
            ]
        }
    ]);

DESCRIPTION

Serializer for JSON:API. Takes in a data structure, munge it to conforms to the JSON:API format (potentially based on a provided registry of JSON:API schemas), and encode it as JSON.

Note that using Dancer2::Plugin::JsonApi in an app will automatically set Dancer2::Serializer::JsonApi as its serializer if it's not already defined.

ATTRIBUTES

content_type

Returns the content type used by the serializer, which is application/vnd.api+json;

registry

The Dancer2::Plugin::JsonApi::Registry to use.

json_serializer

The underlying JSON serializer. Defaults to Dancer2::Serializer::JSON.

METHODS

$self->serialize( [ $type, $data, $xtra ])

Serializes the $data using the $type from the registry. The returned value will be a JSON string.

$self->deserialize( $json_string )

Takes in the serialized $json_string and recreate data out of it.

AUTHOR

Yanick Champoux <yanick@babyl.ca>

COPYRIGHT AND LICENSE

This software is copyright (c) 2023 by Yanick Champoux.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.