1 package Catalyst::Action::Serialize;
4 use namespace::autoclean;
6 extends 'Catalyst::Action::SerializeBase';
7 use Module::Pluggable::Object;
12 my ( $controller, $c ) = @_;
14 $self->maybe::next::method(@_);
16 return 1 if $c->req->method eq 'HEAD';
17 return 1 if length( $c->response->body );
18 return 1 if scalar @{ $c->error };
19 return 1 if $c->response->status =~ /^(?:204|3\d\d)$/;
21 my ( $sclass, $sarg, $content_type ) =
22 $self->_load_content_plugins( "Catalyst::Action::Serialize",
24 unless ( defined($sclass) ) {
25 if ( defined($content_type) ) {
26 $c->log->info("Could not find a serializer for $content_type");
29 "Could not find a serializer for an empty content-type");
34 "Serializing with $sclass" . ( $sarg ? " [$sarg]" : '' ) ) if $c->debug;
38 if ( defined($sarg) ) {
39 $rc = $sclass->execute( $controller, $c, $sarg );
41 $rc = $sclass->execute( $controller, $c );
45 return $self->_serialize_bad_request( $c, $content_type, $@ );
47 return $self->_unsupported_media_type( $c, $content_type );
57 Catalyst::Action::Serialize - Serialize Data in a Response
61 package Foo::Controller::Bar;
64 'default' => 'text/x-yaml',
65 'stash_key' => 'rest',
67 'text/html' => [ 'View', 'TT', ],
68 'text/x-yaml' => 'YAML',
69 'text/x-data-dumper' => [ 'Data::Serializer', 'Data::Dumper' ],
73 sub end :ActionClass('Serialize') {}
77 This action will serialize the body of an HTTP Response. The serializer is
78 selected by introspecting the HTTP Requests content-type header.
80 It requires that your Catalyst controller is properly configured to set up the
81 mapping between Content Type's and Serialization classes.
83 The specifics of serializing each content-type is implemented as a plugin to
84 L<Catalyst::Action::Serialize>.
86 Typically, you would use this ActionClass on your C<end> method. However,
87 nothing is stopping you from choosing specific methods to Serialize:
89 sub foo :Local :ActionClass('Serialize') {
90 .. populate stash with data ..
93 When you use this module, the request class will be changed to
94 L<Catalyst::Request::REST>.
100 Takes a hashref, mapping Content-Types to a given serializer plugin.
104 This is the 'fall-back' Content-Type if none of the requested or acceptable
105 types is found in the L</map>. It must be an entry in the L</map>.
109 Specifies the key of the stash entry holding the data that is to be serialized.
110 So if the value is "rest", we will serialize the data under:
114 =head2 content_type_stash_key
116 Specifies the key of the stash entry that optionally holds an overriding
117 Content-Type. If set, and if the specified stash entry has a valid value,
118 then it takes priority over the requested content types.
120 This can be useful if you want to dynamically force a particular content type,
121 perhaps for debugging.
123 =head1 HELPFUL PEOPLE
125 Daisuke Maki pointed out that early versions of this Action did not play
126 well with others, or generally behave in a way that was very consistent
127 with the rest of Catalyst.
131 You likely want to look at L<Catalyst::Controller::REST>, which implements
132 a sensible set of defaults for doing a REST controller.
134 L<Catalyst::Action::Deserialize>, L<Catalyst::Action::REST>
138 See L<Catalyst::Action::REST> for authors.
142 You may distribute this code under the same terms as Perl itself.