Dancer2...Mutable(3)

πŸ’– Dancer2::Serializer::Mutable πŸ’–

okay bestie, sit down, hydrate, because we're talking about Dancer2::Serializer::Mutable β€” the serializer that serializes and deserializes your content based on the HTTP header. yes. the header decides. it's giving adaptive ✨ (this is the Dancer2...Mutable(3) page from the User Contributed Perl Documentation, btw 🫢)


✨ VERSION ✨

version 2.2.1 β€” she's on 2.2.1 and she's thriving πŸ’…


πŸŽ€ 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" };
};

like… she reads your request, figures out the vibe, and hands back the format that matches. no cap. πŸ”₯


🌸 DESCRIPTION 🌸

So this serializer's whole thing is: she tries to find the best (de)serializer for a given request. To do that, she picks the first valid content type she finds from a list, and uses its related serializer. But here's the tea: the list β€” and the order of that list β€” is not the same in both directions. 😭

  • When deserializing an incoming request body (aka 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 (aka 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's the header a client uses to say what it wants back, which need not match the content type of what it sent. periodt πŸ’…

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. iconic. πŸ‘‘

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. so don't come at her with a whole list and expect magic bestie, she said no πŸ™ˆ

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

  • Dancer2::Serializer::YAML β€” content types: text/x-yaml, text/html
  • Dancer2::Serializer::JSON β€” content types: 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 πŸ¦‹

⚠️ bestie. listen. ⚠️

The Dumper serializer is not available by default, because it deserializes request content by evaluating it as Perl code β€” which is insecure and a bad idea. like genuinely bad. not the cute kind of bad. πŸ’€

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. so like. don't. 🫢


🌈 INTERNAL METHODS 🌈

The following methods are used internally by Dancer2 and are not made accessible via the DSL. iykyk β€” these are backstage, not main character. 🎭

✨ 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. she'll pick for you, don't stress πŸ’…

✨ 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. πŸ’€ order matters bestie!!

πŸ’œ AUTHOR πŸ’œ

Dancer Core Developers β€” shoutout to the icons πŸ‘‘


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.


and that's the whole page, bestie!! πŸŽ€ this was perl v5.42.3, dated 2026-09-20, and yes it's Dancer2...Mutable(3) β€” now go forth and serialize responsibly, mwah πŸ’–βœ¨πŸ¦‹

πŸ“¦ where this page comes from