1 package Catalyst::Action::Serialize;
4 use namespace::autoclean;
6 extends 'Catalyst::Action::SerializeBase';
7 use Module::Pluggable::Object;
10 our $VERSION = '0.88';
11 $VERSION = eval $VERSION;
16 default => sub { {} },
21 my ( $controller, $c ) = @_;
23 $self->maybe::next::method(@_);
25 return 1 if $c->req->method eq 'HEAD';
26 return 1 if $c->response->has_body;
27 return 1 if scalar @{ $c->error };
28 return 1 if $c->response->status =~ /^(?:204)$/;
30 my ( $sclass, $sarg, $content_type ) =
31 $self->_load_content_plugins( "Catalyst::Action::Serialize",
33 unless ( defined($sclass) ) {
34 if ( defined($content_type) ) {
35 $c->log->info("Could not find a serializer for $content_type");
38 "Could not find a serializer for an empty content-type");
43 "Serializing with $sclass" . ( $sarg ? " [$sarg]" : '' ) ) if $c->debug;
45 $self->_encoders->{$sclass} ||= $sclass->new;
46 my $sobj = $self->_encoders->{$sclass};
50 if ( defined($sarg) ) {
51 $rc = $sobj->execute( $controller, $c, $sarg );
53 $rc = $sobj->execute( $controller, $c );
57 return $self->_serialize_bad_request( $c, $content_type, $@ );
59 return $self->_unsupported_media_type( $c, $content_type );
65 __PACKAGE__->meta->make_immutable;
69 Catalyst::Action::Serialize - Serialize Data in a Response
73 package Foo::Controller::Bar;
76 'default' => 'text/x-yaml',
77 'stash_key' => 'rest',
79 'text/html' => [ 'View', 'TT', ],
80 'text/x-yaml' => 'YAML',
81 'text/x-data-dumper' => [ 'Data::Serializer', 'Data::Dumper' ],
85 sub end :ActionClass('Serialize') {}
89 This action will serialize the body of an HTTP Response. The serializer is
90 selected by introspecting the HTTP Requests content-type header.
92 It requires that your Catalyst controller is properly configured to set up the
93 mapping between Content Type's and Serialization classes.
95 The specifics of serializing each content-type is implemented as a plugin to
96 L<Catalyst::Action::Serialize>.
98 Typically, you would use this ActionClass on your C<end> method. However,
99 nothing is stopping you from choosing specific methods to Serialize:
101 sub foo :Local :ActionClass('Serialize') {
102 .. populate stash with data ..
105 When you use this module, the request class will be changed to
106 L<Catalyst::Request::REST>.
112 Takes a hashref, mapping Content-Types to a given serializer plugin.
116 This is the 'fall-back' Content-Type if none of the requested or acceptable
117 types is found in the L</map>. It must be an entry in the L</map>.
121 Specifies the key of the stash entry holding the data that is to be serialized.
122 So if the value is "rest", we will serialize the data under:
126 =head2 content_type_stash_key
128 Specifies the key of the stash entry that optionally holds an overriding
129 Content-Type. If set, and if the specified stash entry has a valid value,
130 then it takes priority over the requested content types.
132 This can be useful if you want to dynamically force a particular content type,
133 perhaps for debugging.
135 =head1 HELPFUL PEOPLE
137 Daisuke Maki pointed out that early versions of this Action did not play
138 well with others, or generally behave in a way that was very consistent
139 with the rest of Catalyst.
143 You likely want to look at L<Catalyst::Controller::REST>, which implements
144 a sensible set of defaults for doing a REST controller.
146 L<Catalyst::Action::Deserialize>, L<Catalyst::Action::REST>
150 See L<Catalyst::Action::REST> for authors.
154 You may distribute this code under the same terms as Perl itself.