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)
π 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. π«Ά
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
- fedora 44: perl-Dancer2 2.2.1-1.fc44 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms