1Dancer::Serializer::MutUasbelre(C3o)ntributed Perl DocumDeanntcaetri:o:nSerializer::Mutable(3)
2
3
4

NAME

6       Dancer::Serializer::Mutable - Serialize and deserialize content using
7       the appropriate HTTP header
8

VERSION

10       version 1.3513
11

SYNOPSIS

13           # in config.yml
14           serializer: Mutable
15
16           # in the app
17           put '/something' => sub {
18               # deserialized from request
19               my $name = param( 'name' );
20
21               ...
22
23               # will be serialized to the most
24               # fitting format
25               return { message => "user $name added" };
26           };
27

DESCRIPTION

29       This serializer will try find the best (de)serializer for a given
30       request.  For this, it will pick the first content type found from the
31       following list and use its related serializer.
32
33       •   The content_type from the request headers
34
35       •   the content_type parameter from the URL
36
37       •   the accept from the request headers
38
39       •   The default is application/json
40
41       The content-type/serializer mapping that "Dancer::Serializer::Mutable"
42       uses is
43
44           serializer               | content types
45           ----------------------------------------------------------
46           Dancer::Serializer::YAML | text/x-yaml, text/html
47           Dancer::Serializer::XML  | text/xml
48           Dancer::Serializer::JSON | text/x-json, application/json
49

EXPORTABLE FUNCTIONS

51   template_or_serialize( $template, $data, $options )
52       For instances where you want to render a template for normal browser
53       requests, and return serialized content for AJAX calls.
54
55       If the requested content-type is text/html, "template_or_serialize"
56       returns the rendered template, else it returns $data unmodified (which
57       will then be serialized as usual).
58
59       "template_or_serialize" is not exported by default.
60
61           use Dancer::Serializer::Mutable qw/ template_or_serialize /;
62
63           get '/greetings' => sub {
64               ...;
65
66               return template_or_serialize 'greetings' => {
67                   title => $title,
68                   name  => $name,
69               };
70           };
71
72   INTERNAL METHODS
73       The following methods are used internally by "Dancer" and are not made
74       accessible via the DSL.
75
76   serialize
77       Serialize a data structure. The format it is serialized to is
78       determined automatically as described above. It can be one of YAML,
79       XML, JSON, defaulting to JSON if there's no clear preference from the
80       request.
81
82   deserialize
83       Deserialize the provided serialized data to a data structure.  The type
84       of serialization format depends on the request's content-type. For now,
85       it can be one of YAML, XML, JSON.
86
87   content_type
88       Returns the content-type that was used during the last "serialize" /
89       "deserialize" call. WARNING : you must call "serialize" / "deserialize"
90       before calling "content_type". Otherwise the return value will be
91       "undef".
92

AUTHOR

94       Dancer Core Developers
95
97       This software is copyright (c) 2010 by Alexis Sukrieh.
98
99       This is free software; you can redistribute it and/or modify it under
100       the same terms as the Perl 5 programming language system itself.
101
102
103
104perl v5.34.0                      2021-07-22    Dancer::Serializer::Mutable(3)
Impressum