Dancer2::Serializer::Mutable3pm
Serialize and deserialize content based on http header
Usage
# in config.ymlserializer: Mutableengines:serializer:Mutable:mapping:'text/x-yaml' : YAML'text/html' : YAML'text/x-data-dumper' : Dumper'text/x-json' : JSON'application/json' : JSON# in the appput '/something' => sub {# deserialized from requestmy $name = param( 'name' );...# will be serialized to the most# fitting formatreturn { message => "user $name added" };};
Version
version 1.1.2
Overview
This serializer will try find the best (de)serializer for a given request. For this, it will pick the first valid content type found from the following list and use its related serializer.
•
|
The |
|---|---|
•
|
the |
•
|
The default is |
The content-type/serializer mapping that "Dancer2::Serializer::Mutable" uses is
serializer | content types----------------------------------------------------------Dancer2::Serializer::YAML | text/x-yaml, text/htmlDancer2::Serializer::Dumper | text/x-data-dumperDancer2::Serializer::JSON | text/x-json, application/json
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-data-dumper' : Dumper'text/x-json' : JSON'application/json' : JSON
The keys of the mapping are the content-types to serialize, and the values the serializers to use. Serialization for "YAML", "Dumper" and "JSON" are done using internal Dancer mechanisms. Any other serializer will be taken to be as 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-data-dumper' : Dumper'text/x-json' : JSON'text/xml' : XML
Internal methods
The following methods are used internally by "Dancer2" and are not made accessible via the DSL.
serialize
Serialize a data structure. The format it is serialized to is determined automatically as described above. It can be one of YAML, Dumper, JSON, defaulting to JSON if there's no clear preference from the request.
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, Dumper, 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".
Copyright and license
This software is copyright (c) 2024 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.
Releases
6 releases since Debian 8 (Jessie); the text changed 5 times.