Dancer2...er::JSON(3)

✨ NAME ✨

Dancer2::Serializer::JSON β€” it's the serializer for handling JSON data, bestie πŸ’–

(also hi, this is a User Contributed Perl Documentation page, so you know it's the real deal 🫢)


🌸 VERSION 🌸

version 2.2.1 ✨


πŸ¦‹ DESCRIPTION πŸ¦‹

Okay so listen. This is a serializer engine πŸ’… and what it does is it lets you flip Perl data structures into JSON output, and also flip them right back the other way. Two-way street, no cap. πŸ”₯


πŸŽ€ ATTRIBUTES πŸŽ€

content_type πŸ’–

Returns 'application/json' β€” that's it, that's the whole vibe ✨


πŸ‘‘ METHODS πŸ‘‘

serialize($content) 🌈

Takes a Perl data structure and serializes it into a JSON string. Glow up, but for data. πŸ’«

deserialize($content) πŸ¦‹

Takes a JSON string and deserializes it into a Perl data structure. Reverse uno card, but make it iconic. πŸŽ€


πŸ’œ FUNCTIONS πŸ’œ

from_json($content, \%options) πŸ“

This is a lil helper 🧚 that's available to transform a JSON data structure into a Perl data structure. So handy, so obsessed.

to_json($content, \%options) ⭐

This is a helper available to transform a Perl data structure into JSON.

Heads up bestie 🚨 calling this function will not trigger the serialization's hooks. Not me warning you twice. It's giving "no hooks, periodt."

Configuring the JSON Serializer using set engines πŸŽ›οΈ

The JSON serializer options can be configured via set engines. The most common settings are, and honestly they ate:

  • allow_nonref β€” ignore non-ref scalars returned from handlers. With this set, the "Hello, World!" handler returning a string will be dealt with properly. Slay. πŸ’…
  • strict_utf8 β€” if true, invalid UTF-8 bytes in data passed to the JSON encoder will cause an error 😭. If false (default), invalid bytes are left as-is and a warning is logged. Lowkey the chill option.
Note: the utf8 option is forced to true internally to ensure JSON output is UTF-8 encoded bytes. So don't even try it, it's non-negotiable. ✨

Set engines should be called prior to setting JSON as the serializer:

set engines =>
{
    serializer =>
    {
        JSON =>
        {
           allow_nonref => 1
        },
    }
};

set serializer      => 'JSON';
set content_type    => 'application/json';

Returning non-JSON data. πŸ™ˆ

Handlers can return non-JSON via send_as, which overrides the default serializer. Main character move, honestly:

get '/' =>
sub
{
    send_as html =>
    q{Welcome to the root of all evil...<br>step into my office.}
};

Any other non-JSON returned format supported by send_as can be used. So go off, pick your format. 🌈


🌸 AUTHOR 🌸

Dancer Core Developers πŸ’– (the icons who made this, we stan)


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. 🫢


okay that's a wrap, bestie πŸ’… β€” this was Dancer2::Serializer::JSON, shipped as part of perl v5.42.3, dated 2026-09-20. Go forth and serialize responsibly. no cap, bye πŸŽ€βœ¨
πŸ“¦ where this page comes from