/
JSON.pm
67 lines (43 loc) · 1.11 KB
/
JSON.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package SixteenColors::View::JSON;
use Moose;
use namespace::autoclean;
use JSON::XS ();
BEGIN {
extends 'Catalyst::View::JSON';
}
__PACKAGE__->config(
allow_callback => 1,
expose_stash => 'json'
);
sub process {
my( $self, $c ) = @_;
my $key = $c->stash->{ serialize_key };
my $data = $key ? $c->stash->{ $key } : undef;
unless( $data ) {
$c->response->body( 'Page not found' );
$c->response->status( 404 );
return;
}
$c->stash( $self->expose_stash => $data );
return $self->next::method( $c );
}
sub encode_json {
my ( $self, $c, $data ) = @_;
my $encoder = JSON::XS->new->convert_blessed;
$encoder = $encoder->pretty if $c->is_development_server;
$encoder->encode( $data );
}
1;
__END__
=head1 NAME
SixteenColors::View::JSON - JSON view for SixteenColors
=head1 DESCRIPTION
[enter your description here]
=head1 METHODS
=head2 encode_json
=head1 AUTHOR
Sixteen Colors <contact@sixteencolors.net>
=head1 LICENSE
This library is free software. You can redistribute it and/or modify
it under the same terms as Perl itself.
=cut