2 |
2 |
|
3 |
3 |
use JSON ();
|
4 |
4 |
use Carp qw(croak);
|
5 |
|
use parent qw(Template::Plugin);
|
|
5 |
use base qw(Template::Plugin);
|
6 |
6 |
|
7 |
7 |
our $VERSION = "0.06";
|
8 |
8 |
|
... | ... | |
11 |
11 |
|
12 |
12 |
my $self = bless {context => $context, json_args => $args }, $class;
|
13 |
13 |
|
14 |
|
$context->define_vmethod( $_ => json => sub { $self->json(@_) } ) for qw(hash list scalar);
|
|
14 |
$context->define_vmethod($_, json => sub { $self->json(@_) }) for qw(hash list scalar);
|
|
15 |
|
|
16 |
return $self;
|
15 |
17 |
}
|
16 |
18 |
|
17 |
19 |
sub json_converter {
|
... | ... | |
35 |
37 |
sub json {
|
36 |
38 |
my ($self, $value) = @_;
|
37 |
39 |
|
38 |
|
$self->json_converter->encode($value) . join '-', map { "'$_'" }@_;
|
|
40 |
$self->json_converter->encode($value);
|
39 |
41 |
}
|
40 |
42 |
|
41 |
43 |
sub json_decode {
|
42 |
|
my ( $self, $context, $value ) = @_;
|
|
44 |
my ( $self, $value ) = @_;
|
43 |
45 |
|
44 |
46 |
$self->json_converter->decode($value);
|
45 |
47 |
}
|
... | ... | |
47 |
49 |
1;
|
48 |
50 |
|
49 |
51 |
__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 |
|
|
Fixes vom portieren