Dancer...Mutable(3pm)

πŸ’– Dancer::Serializer::Mutable πŸ’–

okay bestie, welcome to Dancer::Serializer::Mutable (yes that's the full name, she's from the Dancer::Serializer family, and she's a 3pm perl module πŸ’…). She's the one who figures out what format your content should be in, then serializes and deserializes it using the appropriate HTTP header. no cap. ✨


🌸 VERSION 🌸

so this is version 1.3522 πŸŽ€


✨ SYNOPSIS ✨

okay so here's the whole vibe:

# in config.yml
serializer: Mutable

# 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" };
};

that's literally it. you set it in config.yml, you go about your business, and she handles the rest 🫢


πŸ’« DESCRIPTION πŸ’«

so what she does is, she tries to find the best (de)serializer for any given request πŸ¦‹. to do that, she goes down this list and picks the first content type she finds, then uses its related serializer:

  • the content_type from the request headers
  • the content_type parameter from the URL
  • the accept from the request headers
  • and if nothing else shows up, the default is application/json

and here's the content-type to serializer mapping that Dancer::Serializer::Mutable uses, bestie, look at her:

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

πŸŽ€ EXPORTABLE FUNCTIONS πŸŽ€

🌈 template_or_serialize( $template, $data, $options ) 🌈

for those moments when you want to render a template for normal browser requests, but return serialized content for AJAX calls. two moods, one function, iconic 🧚

if the requested content-type is text/html, template_or_serialize returns the rendered template. otherwise it returns $data unmodified β€” and then that gets serialized as usual πŸ’œ

heads up: template_or_serialize is not exported by default. so you gotta ask for her by name, like:

use Dancer::Serializer::Mutable qw/ template_or_serialize /;

get '/greetings' => sub {
    ...;

    return template_or_serialize 'greetings' => {
        title => $title,
        name  => $name,
    };
};

slay πŸ“


πŸ”₯ INTERNAL METHODS πŸ”₯

these are the methods that Dancer uses internally, and they are not accessible via the DSL. so like, you can look, but they're not for you to call directly, iykyk πŸ™ˆ

⭐ serialize

serialize a data structure ✨. the format she serializes it to is determined automatically, as described above πŸ‘†. it can be one of YAML, XML… wait, YAML, XML, JSON β€” and she defaults to JSON if there's no clear preference from the request πŸ’…

⭐ deserialize

deserialize the provided serialized data into a data structure πŸ’–. the type of serialization format depends on the request's content-type. for now, it can be one of YAML, XML, 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 β€” legends, honestly πŸ‘‘


this software is copyright (c) 2010 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 doc shipped with perl v5.40.1 on 2026-05-05, so that's when this little icon got her glow-up πŸ¦‹πŸ’… mwah, go serialize something cute πŸ’–

πŸ“¦ where this page comes from