1Dancer2::Serializer::MuUtsaebrleC(o3n)tributed Perl DocuDmaenncteart2i:o:nSerializer::Mutable(3)
2
3
4

NAME

6       Dancer2::Serializer::Mutable - Serialize and deserialize content based
7       on HTTP header
8

VERSION

10       version 0.207000
11

SYNOPSIS

13           # in config.yml
14           serializer: Mutable
15
16           engines:
17               serializer:
18                   Mutable:
19                       mapping:
20                           'text/x-yaml'        : YAML
21                           'text/html'          : YAML
22                           'text/x-data-dumper' : Dumper
23                           'text/x-json'        : JSON
24                           'application/json'   : JSON
25
26           # in the app
27           put '/something' => sub {
28               # deserialized from request
29               my $name = param( 'name' );
30
31               ...
32
33               # will be serialized to the most
34               # fitting format
35               return { message => "user $name added" };
36           };
37

DESCRIPTION

39       This serializer will try find the best (de)serializer for a given
40       request.  For this, it will pick the first valid content type found
41       from the following list and use its related serializer.
42
43       ·   The content_type from the request headers
44
45       ·   the accept from the request headers
46
47       ·   The default is application/json
48
49       The content-type/serializer mapping that "Dancer2::Serializer::Mutable"
50       uses is
51
52           serializer                  | content types
53           ----------------------------------------------------------
54           Dancer2::Serializer::YAML   | text/x-yaml, text/html
55           Dancer2::Serializer::Dumper | text/x-data-dumper
56           Dancer2::Serializer::JSON   | text/x-json, application/json
57
58       A different mapping can be provided via the config file. For example,
59       the default mapping would be configured as
60
61           engines:
62               serializer:
63                   Mutable:
64                       mapping:
65                           'text/x-yaml'        : YAML
66                           'text/html'          : YAML
67                           'text/x-data-dumper' : Dumper
68                           'text/x-json'        : JSON
69                           'application/json'   : JSON
70
71       The keys of the mapping are the content-types to serialize, and the
72       values the serializers to use. Serialization for "YAML", "Dumper" and
73       "JSON" are done using internal Dancer mechanisms. Any other serializer
74       will be taken to be as Dancer2 serialization class (minus the
75       "Dancer2::Serializer::" prefix) and an instance of it will be used to
76       serialize/deserialize data. For example, adding
77       Dancer2::Serializer::XML to the mapping would be:
78
79           engines:
80               serializer:
81                   Mutable:
82                       mapping:
83                           'text/x-yaml'        : YAML
84                           'text/html'          : YAML
85                           'text/x-data-dumper' : Dumper
86                           'text/x-json'        : JSON
87                           'text/xml'           : XML
88
89   INTERNAL METHODS
90       The following methods are used internally by "Dancer2" and are not made
91       accessible via the DSL.
92
93   serialize
94       Serialize a data structure. The format it is serialized to is
95       determined automatically as described above. It can be one of YAML,
96       Dumper, JSON, defaulting to JSON if there's no clear preference from
97       the request.
98
99   deserialize
100       Deserialize the provided serialized data to a data structure.  The type
101       of serialization format depends on the request's content-type. For now,
102       it can be one of YAML, Dumper, JSON.
103
104   content_type
105       Returns the content-type that was used during the last "serialize" /
106       "deserialize" call. WARNING : you must call "serialize" / "deserialize"
107       before calling "content_type". Otherwise the return value will be
108       "undef".
109

AUTHOR

111       Dancer Core Developers
112
114       This software is copyright (c) 2018 by Alexis Sukrieh.
115
116       This is free software; you can redistribute it and/or modify it under
117       the same terms as the Perl 5 programming language system itself.
118
119
120
121perl v5.28.1                      2018-11-14   Dancer2::Serializer::Mutable(3)
Impressum