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