Split into new dist
[catagits/Catalyst-Action-Serialize-Data-Serializer.git] / lib / Catalyst / Action / Serialize / Data / Serializer.pm
CommitLineData
7ad87df9 1package Catalyst::Action::Serialize::Data::Serializer;
2
930013e6 3use Moose;
4use namespace::autoclean;
7ad87df9 5
930013e6 6extends 'Catalyst::Action';
7ad87df9 7use Data::Serializer;
8
f10c7e1c 9our $VERSION = '1.07';
f465980c 10$VERSION = eval $VERSION;
11
7ad87df9 12sub execute {
13 my $self = shift;
14 my ( $controller, $c, $serializer ) = @_;
15
faf5c20b 16 my $stash_key = (
07682cbc 17 $controller->{'serialize'} ?
18 $controller->{'serialize'}->{'stash_key'} :
79025f72 19 $controller->{'stash_key'}
faf5c20b 20 ) || 'rest';
e601adda 21 my $sp = $serializer;
22 $sp =~ s/::/\//g;
23 $sp .= ".pm";
24 eval {
25 require $sp
26 };
27 if ($@) {
faf5c20b 28 $c->log->info("Could not load $serializer, refusing to serialize: $@");
7f36b63e 29 return;
e601adda 30 }
eccb2137 31 my $dso = Data::Serializer->new( serializer => $serializer );
7f36b63e 32 my $data = $dso->raw_serialize($c->stash->{$stash_key});
e601adda 33 $c->response->output( $data );
7ad87df9 34 return 1;
eccb2137 35}
7ad87df9 36
24748286 37__PACKAGE__->meta->make_immutable;
38
7ad87df9 391;
79025f72 40
41__END__
42
43=pod
44
45=head1 NAME
46
47Catalyst::Action::Serialize::Data::Serializer - Serialize with Data::Serializer
48
49=head1 SYNOPSIS
50
51 package MyApp::Controller::Foo;
52
53 use Moose;
54 use namespace::autoclean;
55
56 BEGIN { extends 'Catalyst::Controller' }
57
58 __PACKAGE__->config(
59 'default' => 'text/x-yaml',
60 'stash_key' => 'rest',
61 'map' => {
62 'text/x-yaml' => 'YAML',
63 'application/json' => 'JSON',
64 'text/x-data-dumper' => [ 'Data::Serializer', 'Data::Dumper' ],
65 },
66 );
67
68=head1 DESCRIPTION
69
70This module implements a serializer for use with C<Data::Dumper> and others. It
71was factored out of L<Catalyst::Action::REST> because it is unlikely to be
72widely used and tends to break tests, be insecure, and is genreally weird. Use
73at your own risk.
74
75=head1 AUTHOR
76
77Adam Jacob E<lt>adam@stalecoffee.orgE<gt>, with lots of help from mst and jrockway
78
79Marchex, Inc. paid me while I developed this module. (L<http://www.marchex.com>)
80
81=head1 CONTRIBUTORS
82
83Tomas Doran (t0m) E<lt>bobtfish@bobtfish.netE<gt>
84
85John Goulah
86
87Christopher Laco
88
89Daisuke Maki E<lt>daisuke@endeworks.jpE<gt>
90
91Hans Dieter Pearcey
92
93Brian Phillips E<lt>bphillips@cpan.orgE<gt>
94
95Dave Rolsky E<lt>autarch@urth.orgE<gt>
96
97Luke Saunders
98
99Arthur Axel "fREW" Schmidt E<lt>frioux@gmail.comE<gt>
100
101J. Shirley E<lt>jshirley@gmail.comE<gt>
102
103Gavin Henry E<lt>ghenry@surevoip.co.ukE<gt>
104
105Gerv http://www.gerv.net/
106
107Colin Newell <colin@opusvl.com>
108
109Wallace Reis E<lt>wreis@cpan.orgE<gt>
110
111=head1 COPYRIGHT
112
113Copyright (c) 2006-2013 the above named AUTHOR and CONTRIBUTORS
114
115=head1 LICENSE
116
117You may distribute this code under the same terms as Perl itself.
118
119=cut