| 1 | package Cake::Utils::Serializer; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 8 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 3 | 8 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 8 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 8 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 0 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 9 | 0 | return bless({data => shift},$class); | |||||||||||||||||||||||||||||||||||||||||
| 10 | } | ||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | 0 | ▶ | sub true {'true' } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
true: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 0 | ▶ | sub false { undef } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
false: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 0 | ▶ | sub null { 'null' } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
null: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | ##from json to perl | ||||||||||||||||||||||||||||||||||||||||||
| 17 | 0 | ▶ | sub to_perl { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
to_perl: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 19 | #remove comments | ||||||||||||||||||||||||||||||||||||||||||
| 20 | 0 | $self->{data} =~ s/\n+\s+/\n/g; | |||||||||||||||||||||||||||||||||||||||||
| 21 | 0 | $self->{data} =~ s/[\n\s+]\/\*.*?\*\/|[\n\s+]\/\/.*?\n/\n/gs; | |||||||||||||||||||||||||||||||||||||||||
| 22 | 0 | my $data = $self->{data}; | |||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | 0 | ▶ | if ($data){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 25 | 0 | $data =~ s/(["'])(?:\s?)+:/$1=>/g; | |||||||||||||||||||||||||||||||||||||||||
| 26 | 0 | $data =~ s/[^\\]([\@\$].*?\s*)/ \\$1/g; | |||||||||||||||||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||
| 29 | 0 | my $str = eval "$data"; | |||||||||||||||||||||||||||||||||||||||||
| 30 | 0 | ▶ | croak "invalid json" if $@; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | #return bless($str,'Cake::Utils::Serializer::Base'); | ||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||
| 34 | 0 | return $str; | |||||||||||||||||||||||||||||||||||||||||
| 35 | 0 | return _stringify($data); | |||||||||||||||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||
| 38 | 0 | ▶ | sub to_json { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
to_json: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 39 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 40 | 0 | my $trim = shift; | |||||||||||||||||||||||||||||||||||||||||
| 41 | 0 | my $perl_object = $self->{data}; | |||||||||||||||||||||||||||||||||||||||||
| 42 | 0 | my $dumper = Data::Dumper->new([ _stringify($perl_object,'encode') ]); | |||||||||||||||||||||||||||||||||||||||||
| 43 | 0 | $dumper->Purity(1)->Terse(1)->Indent(1)->Deparse(1)->Pair(' : '); | |||||||||||||||||||||||||||||||||||||||||
| 44 | 0 | my $json = $dumper->Dump; | |||||||||||||||||||||||||||||||||||||||||
| 45 | 0 | ▶ | $json =~ s/(?:'((.*?)[^\\'])?')/$1 ? '"'.$1.'"' : '""'/ge; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 46 | 0 | $json =~ s/\\'/'/g; | |||||||||||||||||||||||||||||||||||||||||
| 47 | 0 | $json =~ s/\\\\/\\/g; | |||||||||||||||||||||||||||||||||||||||||
| 48 | 0 | $json =~ s/(\\x\{(.*?)\})/chr(hex($2))/ge; | |||||||||||||||||||||||||||||||||||||||||
| 49 | 0 | ▶ | if ($trim){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 50 | 0 | $json =~ s/\n//g; | |||||||||||||||||||||||||||||||||||||||||
| 51 | 0 | $json =~ s/\s+//g; | |||||||||||||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||||||||||||
| 53 | 0 | return $json; | |||||||||||||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | 0 | ▶ | sub validate_json { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
validate_json: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 60 | 0 | my $data = $self->{data}; | |||||||||||||||||||||||||||||||||||||||||
| 61 | 0 | eval "$data"; | |||||||||||||||||||||||||||||||||||||||||
| 62 | ##fastest way to check valid json.. I guess!! | ||||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | $data =~ s/"(\\.|[^"\\])*"//g; | |||||||||||||||||||||||||||||||||||||||||
| 64 | 0 | ▶ | if ( $data =~ m/[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/g && $@){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$data =~ /[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/g and $@
MC/DC0%0/2
$data =~ /[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/g and $@
$data =~ /[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/g
$@
Truth table0%0/3
A and B
A= $data =~ /[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/gB= $@
| |||||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | croak('invalid json string'); | |||||||||||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||
| 70 | 0 | ▶ | sub _stringify { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_stringify: not called CC=8 SCAR=42.8
| |||||||||||||||||||||||||||||||||||||||||||
| 71 | 0 | my $hash = shift; | |||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | ▶ | my $type = shift || 'decode'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
shift() || 'decode'
MC/DC0%0/1
shift() || 'decode'
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | my $action = { | |||||||||||||||||||||||||||||||||||||||||
| 74 | decode => \&_decode_string, | ||||||||||||||||||||||||||||||||||||||||||
| 75 | encode => \&_encode_string, | ||||||||||||||||||||||||||||||||||||||||||
| 76 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | 0 | ▶ | if (!ref $hash){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | return $action->{$type}($hash); | |||||||||||||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | 0 | my $newhash = {}; | |||||||||||||||||||||||||||||||||||||||||
| 83 | 0 | my $array = 0; | |||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | my $loop; | |||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | #ref $hash eq 'ARRAY' ? $loop->{array} = $hash : $loop = $hash; | ||||||||||||||||||||||||||||||||||||||||||
| 87 | 0 | ▶ | if (ref $hash eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 88 | 0 | $loop->{array} = $hash; | |||||||||||||||||||||||||||||||||||||||||
| 89 | 0 | $array = 1; | |||||||||||||||||||||||||||||||||||||||||
| 90 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 91 | 0 | $loop = $hash | |||||||||||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | 0 | ▶ | while (my ($key,$value) = each (%{$loop}) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 95 | 0 | ▶ | if (ref $value eq 'HASH'){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 96 | 0 | $newhash->{$key} = _stringify($value,$type); | |||||||||||||||||||||||||||||||||||||||||
| 97 | } elsif (ref $value eq 'ARRAY'){ | ||||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | push @{$newhash->{$key}}, map { _stringify($_,$type) } @{$value}; | |||||||||||||||||||||||||||||||||||||||||
| 99 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 100 | 0 | $newhash->{$key} = $action->{$type}->($value); | |||||||||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||||||||||
| 103 | 0 | ▶ | return !$array ? $newhash : $newhash->{array}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||
| 107 | 0 | ▶ | sub _decode_string { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_decode_string: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | my $str = shift; | |||||||||||||||||||||||||||||||||||||||||
| 109 | 0 | ▶ | return '' if !$str; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 110 | 0 | my @search = ('\\\\', '\\n', '\\t', '\\r', '\\b', '\\f', '\"'); | |||||||||||||||||||||||||||||||||||||||||
| 111 | 0 | my @replace = ('\\', "\n", "\t", "\r", "\b", "\f", '"'); | |||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | map { $str =~ s/\Q$search[$_]/$replace[$_]/ } (0..$#search); | |||||||||||||||||||||||||||||||||||||||||
| 113 | 0 | return $str; | |||||||||||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||
| 117 | 0 | ▶ | sub _encode_string { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_encode_string: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||||||||||||||||
| 118 | 0 | my $str = shift; | |||||||||||||||||||||||||||||||||||||||||
| 119 | 0 | ▶ | return 0 if $str && $str =~ /^\d$/ && $str == 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$str and $str =~ /^\d$/ and $str == 0
$str and $str =~ /^\d$/
MC/DC0%0/3
$str and $str =~ /^\d$/ and $str == 0
$str
$str =~ /^\d$/
$str == 0
Truth table0%0/4
A and B and C
A= $strB= $str =~ /^\d$/C= $str == 0
| |||||||||||||||||||||||||||||||||||||||||||
| 120 | 0 | ▶ | return '' if !$str; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 121 | 0 | my @search = ('\\', "\n", "\t", "\r", "\b", "\f", '"'); | |||||||||||||||||||||||||||||||||||||||||
| 122 | 0 | my @replace = ('\\\\', '\\n', '\\t', '\\r', '\\b', '\\f', '\"'); | |||||||||||||||||||||||||||||||||||||||||
| 123 | 0 | map { $str =~ s/\Q$search[$_]/$replace[$_]/g } (0..$#search); | |||||||||||||||||||||||||||||||||||||||||
| 124 | 0 | return $str; | |||||||||||||||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||
| 127 | package Cake::Utils::Serializer::Base; | ||||||||||||||||||||||||||||||||||||||||||
| 128 | 8 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 129 | 8 | our $AUTOLOAD; | |||||||||||||||||||||||||||||||||||||||||
| 130 | 0 | ▶ | sub AUTOLOAD { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
AUTOLOAD: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||||||||||||||||
| 131 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 132 | 0 | my $index = shift; | |||||||||||||||||||||||||||||||||||||||||
| 133 | 0 | my $sub = $AUTOLOAD; | |||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||
| 135 | 0 | $sub =~ s/.*:://; | |||||||||||||||||||||||||||||||||||||||||
| 136 | 0 | my $val = $self->{$sub}; | |||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||
| 138 | 0 | ▶ | return undef if (!defined $val); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||||||
| 140 | 0 | ▶ | if ($index && ref $val eq 'ARRAY'){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$index and ref $val eq "ARRAY"
MC/DC0%0/2
$index and ref $val eq "ARRAY"
$index
ref $val eq "ARRAY"
Truth table0%0/3
A and B
A= $indexB= ref $val eq "ARRAY"
| |||||||||||||||||||||||||||||||||||||||||||
| 141 | 0 | $val = $val->[$index]; | |||||||||||||||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||
| 144 | 0 | ▶ | if (ref $val){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 145 | 0 | return bless($val,__PACKAGE__); | |||||||||||||||||||||||||||||||||||||||||
| 146 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 147 | 0 | return $val; | |||||||||||||||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||||||
| 151 | 8 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||||||
| 154 | __END__ | ||||||||||||||||||||||||||||||||||||||||||