Cataly...rialize(3pm)

πŸ’– Catalyst::Action::Serialize β€” bestie edition πŸ’–

okay so real talk, this is Catalyst::Action::Serialize, a Perl manual page from the 3pm section of the perl docs (yes the title got chopped to Cataly...rialize which is honestly so dramatic of it πŸ’…). It does one job and it does it gorgeously: it serializes the body of an HTTP Response. Let's get into it πŸ¦‹


✨ NAME ✨

Catalyst::Action::Serialize β€” Serialize Data in a Response πŸŽ€


🌸 SYNOPSIS 🌸

package Foo::Controller::Bar;

__PACKAGE__->config(
    'default'   => 'text/x-yaml',
    'stash_key' => 'rest',
    'map'       => {
        'text/html'          => [ 'View', 'TT', ],
        'text/x-yaml'        => 'YAML',
        'text/x-data-dumper' => [ 'Data::Serializer', 'Data::Dumper' ],
    }
);

sub end :ActionClass('Serialize') {}

like… look at it. that's the whole vibe. you set up a map, you slap :ActionClass('Serialize') on your end method, and the serializing just happens. ate. πŸ”₯


πŸ‘‘ DESCRIPTION πŸ‘‘

This action will serialize the body of an HTTP Response. The serializer is selected by introspecting the HTTP Requests content-type header. (so it literally looks at what the client asked for and goes "say less, I got you" 😌)

It requires that your Catalyst controller is properly configured to set up the mapping between Content Type's and Serialization classes. No map, no magic β€” the configuration is the whole plot, bestie.

The specifics of serializing each content-type is implemented as a plugin to Catalyst::Action::Serialize. So each content type gets its own lil specialist 🀝

Typically, you would use this ActionClass on your end method. However, nothing is stopping you from choosing specific methods to Serialize:

sub foo :Local :ActionClass('Serialize') {
   .. populate stash with data ..
}

main character energy honestly. pick whichever methods you want serialized, it's your world 🌈

When you use this module, the request class will be changed to Catalyst::Request::REST. not a suggestion, that's just what happens 🫢


πŸ’œ CONFIGURATION πŸ’œ

πŸŽ€ map

Takes a hashref, mapping Content-Types to a given serializer plugin. that's the whole thing. content type in, serializer plugin out πŸ’–

πŸŽ€ default

This is the 'fall-back' Content-Type if none of the requested or acceptable types is found in the map. It must be an entry in the map.

⚠️ bestie listen: it must be an entry in the map. don't be out here pointing default at something that isn't in there, okay? 😭

πŸŽ€ stash_key

Specifies the key of the stash entry holding the data that is to be serialized. So if the value is rest, we will serialize the data under:

$c->stash->{'rest'}

lowkey the most important setting and everyone forgets it πŸ’€

πŸŽ€ content_type_stash_key

Specifies the key of the stash entry that optionally holds an overriding Content-Type. If set, and if the specified stash entry has a valid value, then it takes priority over the requested content types.

This can be useful if you want to dynamically force a particular content type, perhaps for debugging. obsessed with a good override tbh πŸ™ˆ


🫢 HELPFUL PEOPLE 🫢

Daisuke Maki pointed out that early versions of this Action did not play well with others, or generally behave in a way that was very consistent with the rest of Catalyst.

(real ones remember. thank you Daisuke, genuinely iconic of you to say something πŸ™βœ¨)


😭 CUSTOM ERRORS 😭

For building custom error responses when serialization fails, you can create an ActionRole (and use Catalyst::Controller::ActionRole to apply it to the end action) which overrides unsupported_media_type and/or serialize_bad_request methods.

so when it all goes sideways β€” and sometimes it will, no cap β€” you can build your own cute error responses instead of vibing with the default ones πŸ’…


πŸ” SEE ALSO πŸ”

You likely want to look at Catalyst::Controller::REST, which implements a sensible set of defaults for doing a REST controller. fr fr, start there.

Catalyst::Action::Deserialize, Catalyst::Action::REST β€” the rest of the friend group 🌈


πŸ’– AUTHORS πŸ’–

See Catalyst::Action::REST for authors. (they're over there, go say hi πŸ‘‹)


πŸ“œ LICENSE πŸ“œ

You may distribute this code under the same terms as Perl itself. slay, generous, we love to see it ✨


aaaand that's the page!! this was shipped as part of perl v5.40.1, dated 2026-05-07, and it lives in the User Contributed Perl Documentation collection (the 3pm section, iykyk) πŸ’…πŸŒΈ mwah, go serialize something gorgeous bestie πŸ¦‹πŸ’–

πŸ“¦ where this page comes from