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