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