2 # Catlyst::Action::Serialize.pm
3 # Created by: Adam Jacob, Marchex, <adam@marchex.com>
7 package Catalyst::Action::Serialize;
12 use base 'Catalyst::Action::SerializeBase';
13 use Module::Pluggable::Object;
14 use Data::Dump qw(dump);
18 my ( $controller, $c ) = @_;
20 $self->NEXT::execute( @_ );
22 return 1 if $c->req->method eq 'HEAD';
23 return 1 if length( $c->response->body );
24 return 1 if scalar @{ $c->error };
25 return 1 if $c->response->status =~ /^(?:204|3\d\d)$/;
27 my ($sclass, $sarg, $content_type) = $self->_load_content_plugins("Catalyst::Action::Serialize", $controller, $c);
28 return 1 unless defined $sclass;
31 if ( defined($sarg) ) {
32 $rc = $sclass->execute( $controller, $c, $sarg );
34 $rc = $sclass->execute( $controller, $c );
37 return $self->_unsupported_media_type($c, $content_type);
39 return $self->_serialize_bad_request($c, $content_type, $rc);
49 Catalyst::Action::Serialize - Serialize Data in a Response
53 package Foo::Controller::Bar;
58 'stash_key' => 'rest',
60 'text/x-yaml' => 'YAML',
61 'text/x-data-dumper' => [ 'Data::Serializer', 'Data::Dumper' ],
66 sub end :ActionClass('Serialize') {}
70 This action will serialize the body of an HTTP Response. The serializer is
71 selected by introspecting the HTTP Requests content-type header.
73 It requires that your Catalyst controller have a "serialize" entry
74 in it's configuration, which sets up the mapping between Content Type's
75 and Serialization classes.
77 The specifics of serializing each content-type is implemented as
78 a plugin to L<Catalyst::Action::Serialize>.
80 Typically, you would use this ActionClass on your C<end> method. However,
81 nothing is stopping you from choosing specific methods to Serialize:
83 sub foo :Local :ActionClass('Serialize') {
84 .. populate stash with data ..
93 The default Serialization format. See the next section for
94 available options. This is used if a requested content-type
99 We will serialize the data that lives in this location in the stash. So
100 if the value is "rest", we will serialize the data under:
106 Takes a hashref, mapping Content-Types to a given plugin.
110 =head1 HELPFUL PEOPLE
112 Daisuke Maki pointed out that early versions of this Action did not play
113 well with others, or generally behave in a way that was very consistent
114 with the rest of Catalyst.
118 You likely want to look at L<Catalyst::Controller::REST>, which implements
119 a sensible set of defaults for doing a REST controller.
121 L<Catalyst::Action::Deserialize>, L<Catalyst::Action::REST>
125 Adam Jacob <adam@stalecoffee.org>, with lots of help from mst and jrockway
127 Marchex, Inc. paid me while I developed this module. (http://www.marchex.com)
131 You may distribute this code under the same terms as Perl itself.