File Coverage

blib/lib/Template/Plugin/JSON.pm
Criterion Covered Total %
statement 1 3 33.3
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 2 4 50.0


line stmt bran cond sub pod time code
1             #!/usr/bin/perl
2              
3             package Template::Plugin::JSON;
4 1     1   582291 use Moose;
  0            
  0            
5              
6             use JSON ();
7              
8             use Carp qw/croak/;
9              
10             extends qw(Moose::Object Template::Plugin);
11              
12             our $VERSION = "0.06";
13              
14              
15             has context => (
16             isa => "Object",
17             is => "ro",
18             );
19              
20             has json_converter => (
21             isa => "Object",
22             is => "ro",
23             lazy_build => 1,
24             );
25              
26             has json_args => (
27             isa => "HashRef",
28             is => "ro",
29             default => sub { {} },
30             );
31              
32             sub BUILDARGS {
33             my ( $class, $c, @args ) = @_;
34              
35             my $args;
36              
37             if ( @args == 1 and not ref $args[0] ) {
38             warn "Single argument form is deprecated, this module always uses JSON/JSON::XS now";
39             }
40              
41             $args = ref $args[0] ? $args[0] : {};
42              
43             return { %$args, context => $c, json_args => $args };
44             }
45              
46             sub _build_json_converter {
47             my $self = shift;
48              
49             my $json = JSON->new->allow_nonref(1);
50              
51             my $args = $self->json_args;
52              
53             for my $method (keys %$args) {
54             if ( $json->can($method) ) {
55             $json->$method( $args->{$method} );
56             }
57             }
58              
59             return $json;
60             }
61              
62             sub json {
63             my ( $self, $value ) = @_;
64              
65             $self->json_converter->encode($value);
66             }
67              
68             sub json_decode {
69             my ( $self, $value ) = @_;
70              
71             $self->json_converter->decode($value);
72             }
73              
74             sub BUILD {
75             my $self = shift;
76             $self->context->define_vmethod( $_ => json => sub { $self->json(@_) } ) for qw(hash list scalar);
77             }
78              
79             __PACKAGE__;
80              
81             __END__
82              
83             =pod
84              
85             =head1 NAME
86              
87             Template::Plugin::JSON - Adds a .json vmethod for all TT values.
88              
89             =head1 SYNOPSIS
90              
91             [% USE JSON ( pretty => 1 ) %];
92              
93             <script type="text/javascript">
94              
95             var foo = [% foo.json %];
96              
97             </script>
98              
99             or read in JSON
100              
101             [% USE JSON %]
102             [% data = JSON.json_decode(json) %]
103             [% data.thing %]
104              
105             =head1 DESCRIPTION
106              
107             This plugin provides a C<.json> vmethod to all value types when loaded. You
108             can also decode a json string back to a data structure.
109              
110             It will load the L<JSON> module (you probably want L<JSON::XS> installed for
111             automatic speed ups).
112              
113             Any options on the USE line are passed through to the JSON object, much like L<JSON/to_json>.
114              
115             =head1 SEE ALSO
116              
117             L<JSON>, L<Template::Plugin>
118              
119             =head1 VERSION CONTROL
120              
121             L<http://github.com/nothingmuch/template-plugin-json/>
122              
123             =head1 AUTHOR
124              
125             Yuval Kogman <nothingmuch@woobling.org>
126              
127             =head1 COPYRIGHT & LICENSE
128              
129             Copyright (c) 2006, 2008 Infinity Interactive, Yuval Kogman.
130              
131             Permission is hereby granted, free of charge, to any person obtaining a copy
132             of this software and associated documentation files (the "Software"), to deal
133             in the Software without restriction, including without limitation the rights
134             to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
135             copies of the Software, and to permit persons to whom the Software is
136             furnished to do so, subject to the following conditions:
137              
138             The above copyright notice and this permission notice shall be included in
139             all copies or substantial portions of the Software.
140              
141             THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
142             OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
143             FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
144             THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
145             LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
146             FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
147             DEALINGS IN THE SOFTWARE.
148              
149             =cut
150