blib/lib/WWW/Shopify/Liquid/Filter/JSON.pm untested

filter: Statementstmt 0.0% 0 / 31 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 30 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 87 CC 9 SCAR 30.7
CC
9
Cov
0%
operate
42.0
walk_data
20.0
BEGIN
2.0
CRAP 21.5
1 #!/usr/bin/perl
2 use strict;
3 use warnings;
4  
5 package WWW::Shopify::Liquid::Filter::JSON; use base 'WWW::Shopify::Liquid::Filter';
6 use JSON qw(encode_json decode_json from_json);
7 use Scalar::Util qw(blessed isweak);
8 use Clone qw(clone);
9  
10 sub walk_data {
11 my ($self) = @_;
12 if (ref($self) && ref($self) eq "ARRAY" && !isweak($self)) {
13 $self->[$_] = walk_data($self->[$_]) for (0..int(@$self)-1);
14 } elsif (ref($self) && ref($self) eq "HASH" && !isweak($self)) {
15 $self->{$_} = walk_data($self->{$_}) for (keys(%$self));
16 }
17 return defined $self && blessed($self) && $self->isa('DateTime') ? $self->iso8601 : $self;
18 }
19  
20 sub operate {
21 # We allow decoding with this filter as well.
22 if (defined $_[2] && !ref($_[2]) && length($_[2]) >= 2) {
23 my $object = eval { from_json($_[2]) };
24 return $object if !$@;
25 }
26 my $object = $_[2] ? (blessed($_[2]) && $_[2]->isa('WWW::Shopify::Model::Item') ? WWW::Shopify::Liquid->liquify_item($_[2]) : walk_data(clone($_[2]))) : $_[2];
27 return defined $object && ref($object) ? JSON->new->allow_blessed(1)->encode($object) : '{}';
28 }
29  
30 1;