Commit | Line | Data |
7ad87df9 |
1 | package Catalyst::Action::Serialize::Data::Serializer; |
2 | |
930013e6 |
3 | use Moose; |
4 | use namespace::autoclean; |
7ad87df9 |
5 | |
930013e6 |
6 | extends 'Catalyst::Action'; |
7ad87df9 |
7 | use Data::Serializer; |
8 | |
e4d7f27e |
9 | our $VERSION = '1.08'; |
f465980c |
10 | $VERSION = eval $VERSION; |
11 | |
7ad87df9 |
12 | sub 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 |
39 | 1; |
79025f72 |
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 |
87ed2faa |
72 | widely used and tends to break tests, be insecure, and is generally weird. Use |
79025f72 |
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 |