Scroll to navigation

Dancer2::Serializer::Mutable(3) User Contributed Perl Documentation Dancer2::Serializer::Mutable(3)

NAME

Dancer2::Serializer::Mutable - Serialize and deserialize content based on HTTP header

VERSION

version 2.3.0

SYNOPSIS

    # in config.yml
    serializer: Mutable
    engines:
        serializer:
            Mutable:
                mapping:
                    'text/x-yaml'        : YAML
                    'text/html'          : YAML
                    'text/x-json'        : JSON
                    'application/json'   : JSON
    # in the app
    put '/something' => sub {
        # deserialized from request
        my $name = param( 'name' );
        ...
        # will be serialized to the most
        # fitting format
        return { message => "user $name added" };
    };

DESCRIPTION

This serializer will try find the best (de)serializer for a given request. For this, it will pick the first valid content type found from a list, and use its related serializer. The list, and its order, is not the same in both directions:

  • When deserializing an incoming request body (that is, working out how to read it), the order is: the content_type from the request headers, then the accept from the request headers, then the default of application/json.
  • When serializing a response (that is, working out what to send back), the order is: the accept from the request headers, then the content_type from the request headers, then the default of application/json. Consulting "Accept" first is deliberate - it is the header a client uses to say what it wants back, which need not match the content type of what it sent.

In both directions, a header's value is matched against the mapping on its content type alone: any ";"-separated parameters (such as "; charset=utf-8") are stripped, surrounding whitespace is trimmed, and the result is lowercased before comparison. So "Content-Type: text/x-yaml", "Content-Type: text/x-yaml; charset=utf-8" and "Content-Type: TEXT/X-YAML" are all recognised as "text/x-yaml". An "Accept" header listing several comma-separated types is not split further - it is matched as a whole after parameter-stripping, so only a single-type "Accept" value is recognised; anything else falls through to the next header or the default.

The content-type/serializer mapping that "Dancer2::Serializer::Mutable" uses is

    serializer                  | content types
    ----------------------------------------------------------
    Dancer2::Serializer::YAML   | text/x-yaml, text/html
    Dancer2::Serializer::JSON   | text/x-json, application/json

The keys above are bare, lowercase content types - not raw header values - as described above.

A different mapping can be provided via the config file. For example, the default mapping would be configured as

    engines:
        serializer:
            Mutable:
                mapping:
                    'text/x-yaml'        : YAML
                    'text/html'          : YAML
                    'text/x-json'        : JSON
                    'application/json'   : JSON

The values are the serializers to use. Serialization for "YAML" and "JSON" are done using internal Dancer mechanisms. Any other serializer will be taken to be a Dancer2 serialization class (minus the "Dancer2::Serializer::" prefix) and an instance of it will be used to serialize/deserialize data. For example, adding Dancer2::Serializer::XML to the mapping would be:

    engines:
        serializer:
            Mutable:
                mapping:
                    'text/x-yaml'        : YAML
                    'text/html'          : YAML
                    'text/x-json'        : JSON
                    'text/xml'           : XML

Dumper

The Dumper serializer is not available by default, as it deserializes request content by evaluating it as Perl code, which is insecure and a bad idea. If you really want to use it, you must explicitly opt in, and you must have the "Dancer2-Serializer-Dumper" distribution installed. You can then enable it either by adding it to your mapping and setting "enable_dumper":

    engines:
        serializer:
            Mutable:
                enable_dumper: 1
                mapping:
                    'text/x-yaml'        : YAML
                    'text/html'          : YAML
                    'text/x-data-dumper' : Dumper
                    'text/x-json'        : JSON
                    'application/json'   : JSON

or by using the default mapping with "enable_dumper" set, which adds "text/x-data-dumper" to the default mapping:

    engines:
        serializer:
            Mutable:
                enable_dumper: 1

Attempting to use the Dumper serializer without setting "enable_dumper" to a true value will cause a fatal error.

INTERNAL METHODS

The following methods are used internally by "Dancer2" and are not made accessible via the DSL.

serialize

Serialize a data structure. The format it is serialized to is determined automatically as described above. It can be one of YAML, JSON, defaulting to JSON if there's no clear preference from the request.

deserialize

Deserialize the provided serialized data to a data structure. The type of serialization format depends on the request's content-type. For now, it can be one of YAML, JSON.

content_type

Returns the content-type that was used during the last "serialize" / "deserialize" call. WARNING : you must call "serialize" / "deserialize" before calling "content_type". Otherwise the return value will be "undef".

AUTHOR

Dancer Core Developers

COPYRIGHT AND LICENSE

This software is copyright (c) 2026 by Alexis Sukrieh.

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

2026-10-27 perl v5.44.0