| 1 | package Template::Plugin::JSON::Escape; | ||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | 1 | ▶ | use base qw/Template::Plugin/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 1 | ▶ | use JSON (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 1 | our $VERSION = 0.02; | |||||||||
| 9 | |||||||||||
| 10 | 5 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 11 | 5 | my ($class, $context, $args) = @_; | |||||||||
| 12 | 5 | my $self = bless { json => undef, args => $args }, $class; | |||||||||
| 13 | |||||||||||
| 14 | 5 | ▶ | my $encode = sub { $self->json_encode( @_ ) }; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 15 | 5 | $context->define_vmethod( $_ => json => $encode ) for qw/hash list scalar/; | |||||||||
| 16 | 5 | $context->define_filter( json => \&json_filter ); | |||||||||
| 17 | |||||||||||
| 18 | 5 | return $self; | |||||||||
| 19 | } | ||||||||||
| 20 | |||||||||||
| 21 | 6 | ▶ | sub json { | ||||||||
|
Subroutine
json: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||
| 22 | 6 | my $self = shift; | |||||||||
| 23 | 6 | ▶ | return $self->{json} if $self->{json}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 24 | |||||||||||
| 25 | 4 | my $json = JSON->new->allow_nonref; | |||||||||
| 26 | 4 | my $args = $self->{args}; | |||||||||
| 27 | 4 | for ( keys %$args ) { | |||||||||
| 28 | 1 | ▶ | $json->$_( $args->{ $_ } ) if $json->can( $_ ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 29 | } | ||||||||||
| 30 | 4 | return $self->{json} = $json; | |||||||||
| 31 | } | ||||||||||
| 32 | |||||||||||
| 33 | 5 | ▶ | sub json_encode { | ||||||||
|
Subroutine
json_encode: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 34 | 5 | my ($self, $value) = @_; | |||||||||
| 35 | 5 | json_filter( $self->json->encode( $value ) ); | |||||||||
| 36 | } | ||||||||||
| 37 | |||||||||||
| 38 | 1 | ▶ | sub json_decode { | ||||||||
|
Subroutine
json_decode: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 39 | 1 | my ($self, $value) = @_; | |||||||||
| 40 | 1 | $self->json->decode( $value ); | |||||||||
| 41 | } | ||||||||||
| 42 | |||||||||||
| 43 | 6 | ▶ | sub json_filter { | ||||||||
|
Subroutine
json_filter: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 44 | 6 | my $value = shift; | |||||||||
| 45 | 6 | $value =~ s!&!\\u0026!g; | |||||||||
| 46 | 6 | $value =~ s!<!\\u003c!g; | |||||||||
| 47 | 6 | $value =~ s!>!\\u003e!g; | |||||||||
| 48 | 6 | $value =~ s!\+!\\u002b!g; | |||||||||
| 49 | 6 | $value =~ s!\x{2028}!\\u2028!g; | |||||||||
| 50 | 6 | $value =~ s!\x{2029}!\\u2029!g; | |||||||||
| 51 | 6 | $value; | |||||||||
| 52 | } | ||||||||||
| 53 | |||||||||||
| 54 | 1 | 1; | |||||||||
| 55 | |||||||||||
| 56 | __END__ | ||||||||||