line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
1
|
|
|
1
|
|
93221
|
use 5.006; |
|
1
|
|
|
|
|
13
|
|
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
package Template::Plugin::JSON; # git description: v0.07-6-g80fc733 |
4
|
|
|
|
|
|
|
# ABSTRACT: Adds a .json vmethod for all TT values. |
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
527
|
use Moose; |
|
1
|
|
|
|
|
459291
|
|
|
1
|
|
|
|
|
7
|
|
7
|
|
|
|
|
|
|
|
8
|
1
|
|
|
1
|
|
7849
|
use JSON::MaybeXS 'JSON'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
74
|
|
9
|
|
|
|
|
|
|
|
10
|
1
|
|
|
1
|
|
6
|
use Carp qw/croak/; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
506
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
extends qw(Moose::Object Template::Plugin); |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
our $VERSION = '0.08'; |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
has context => ( |
18
|
|
|
|
|
|
|
isa => "Object", |
19
|
|
|
|
|
|
|
is => "ro", |
20
|
|
|
|
|
|
|
weak_ref => 1, |
21
|
|
|
|
|
|
|
); |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
has json_converter => ( |
24
|
|
|
|
|
|
|
isa => "Object", |
25
|
|
|
|
|
|
|
is => "ro", |
26
|
|
|
|
|
|
|
lazy_build => 1, |
27
|
|
|
|
|
|
|
); |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
has json_args => ( |
30
|
|
|
|
|
|
|
isa => "HashRef", |
31
|
|
|
|
|
|
|
is => "ro", |
32
|
|
|
|
|
|
|
default => sub { {} }, |
33
|
|
|
|
|
|
|
); |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
sub BUILDARGS { |
36
|
3
|
|
|
3
|
1
|
66200
|
my ( $class, $c, @args ) = @_; |
37
|
|
|
|
|
|
|
|
38
|
3
|
|
|
|
|
7
|
my $args; |
39
|
|
|
|
|
|
|
|
40
|
3
|
50
|
66
|
|
|
16
|
if ( @args == 1 and not ref $args[0] ) { |
41
|
0
|
|
|
|
|
0
|
warn "Single argument form is deprecated, this module always uses JSON::PP/Cpanel::JSON::XS now"; |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
3
|
100
|
|
|
|
10
|
$args = ref $args[0] ? $args[0] : {}; |
45
|
|
|
|
|
|
|
|
46
|
3
|
|
|
|
|
17
|
return { %$args, context => $c, json_args => $args }; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
sub _build_json_converter { |
50
|
3
|
|
|
3
|
|
5
|
my $self = shift; |
51
|
|
|
|
|
|
|
|
52
|
3
|
|
|
|
|
11
|
my $json = JSON()->new->allow_nonref(1); |
53
|
|
|
|
|
|
|
|
54
|
3
|
|
|
|
|
113
|
my $args = $self->json_args; |
55
|
|
|
|
|
|
|
|
56
|
3
|
|
|
|
|
9
|
for my $method (keys %$args) { |
57
|
1
|
50
|
|
|
|
12
|
if ( $json->can($method) ) { |
58
|
1
|
|
|
|
|
5
|
$json->$method( $args->{$method} ); |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
3
|
|
|
|
|
86
|
return $json; |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
sub json { |
66
|
4
|
|
|
4
|
0
|
10
|
my ( $self, $value ) = @_; |
67
|
|
|
|
|
|
|
|
68
|
4
|
|
|
|
|
125
|
$self->json_converter->encode($value); |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
sub json_decode { |
72
|
1
|
|
|
1
|
0
|
8293
|
my ( $self, $value ) = @_; |
73
|
|
|
|
|
|
|
|
74
|
1
|
|
|
|
|
41
|
$self->json_converter->decode($value); |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
sub BUILD { |
78
|
3
|
|
|
3
|
0
|
4713
|
my $self = shift; |
79
|
3
|
|
|
4
|
|
107
|
$self->context->define_vmethod( $_ => json => sub { $self->json(@_) } ) for qw(hash list scalar); |
|
4
|
|
|
|
|
303
|
|
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
__PACKAGE__; |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
__END__ |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
=pod |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
=encoding UTF-8 |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
=head1 NAME |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
Template::Plugin::JSON - Adds a .json vmethod for all TT values. |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
=head1 VERSION |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
version 0.08 |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=head1 SYNOPSIS |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
[% USE JSON ( pretty => 1 ) %]; |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
<script type="text/javascript"> |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
var foo = [% foo.json %]; |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
</script> |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
or read in JSON |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
[% USE JSON %] |
111
|
|
|
|
|
|
|
[% data = JSON.json_decode(json) %] |
112
|
|
|
|
|
|
|
[% data.thing %] |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
=head1 DESCRIPTION |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
This plugin provides a C<.json> vmethod to all value types when loaded. You |
117
|
|
|
|
|
|
|
can also decode a json string back to a data structure. |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
It will load the L<JSON::MaybeXS> module, which will use L<Cpanel::JSON::XS> |
120
|
|
|
|
|
|
|
when possible and fall back to L<JSON::PP> otherwise. |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
Any options on the USE line are passed through to the JSON object, much like L<Cpanel::JSON::XS/to_json>. |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=head1 SEE ALSO |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
L<JSON::MaybeXS>, L<Template::Plugin> |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
=head1 SUPPORT |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
Bugs may be submitted through L<the RT bug tracker|https://rt.cpan.org/Public/Dist/Display.html?Name=Template-Plugin-JSON> |
131
|
|
|
|
|
|
|
(or L<bug-Template-Plugin-JSON@rt.cpan.org|mailto:bug-Template-Plugin-JSON@rt.cpan.org>). |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
=head1 AUTHOR |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
×××× ×§××'×× (Yuval Kogman) <nothingmuch@woobling.org> |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
=head1 CONTRIBUTORS |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
=for stopwords Neil Bowers Karen Etheridge Graham Barr Leo Lapworth perigrin |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
=over 4 |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
=item * |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
Neil Bowers <neil@bowers.com> |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
=item * |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
Karen Etheridge <ether@cpan.org> |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
=item * |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
Graham Barr <gbarr@pobox.com> |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
=item * |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
Leo Lapworth <leo@cuckoo.org> |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
=item * |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
perigrin <perigrin@cpan.org> |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
=back |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENCE |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
This software is Copyright (c) 2006 by Yuval Kogman. |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
This is free software, licensed under: |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
The MIT (X11) License |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
=cut |