blib/lib/Dancer2/Serializer/JSON.pm

filter: Statementstmt 89.8% 80 / 89 Branchbran 73.3% 22 / 30 Conditioncond 75.0% 3 / 4 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 94.4% 17 / 18 Podpod 66.6% 4 / 6 totaltotal 85.2% 127 / 149 CC 21 SCAR 14.3
CC
21
Cov
85%
_ensure_characters
8.1
serialize
6.0
_decode_utf8
6.0
CRAP 4.2
« blib/lib/Dancer2/Core/Cookie.pm ↑ Summary blib/lib/Dancer2/Core/Role/Hookable.pm »
1 package Dancer2::Serializer::JSON;
2 # ABSTRACT: Serializer for handling JSON data
3 29 $Dancer2::Serializer::JSON::VERSION = '2.2.2';
4 29 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 29 ▶use Ref::Util qw< is_plain_arrayref is_plain_hashref >;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 29 ▶use JSON::MaybeXS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 29 ▶use Encode qw(decode FB_CROAK);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 29 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 29 with 'Dancer2::Core::Role::Serializer';
11  
12 84 ▶has '+content_type' => ( default => sub {'application/json'} );
Subroutine __ANON__: called CC=1 SCAR=0.0
13  
14 # helpers
15 #
16 # These run outside a Dancer2 app, so there is no app logger for the engine to
17 # send diagnostics to. Without one they would be lost: a failed parse returns
18 # undef and says nothing, and _invalid_utf8 below would stop reaching STDERR
19 # the way it does when these run as class methods. Warn instead, so the caller
20 # still gets to see what went wrong.
21 2 ▶sub _warn_log_cb {
Subroutine _warn_log_cb: called CC=1 SCAR=0.0
22 2 my ( $level, $message ) = @_;
23  
24 # The message is whatever the parser left in $@, and whether it ends in a
25 # newline varies by backend, so strip the line ending it came with and
26 # supply exactly one: no blank line after a message that already ended in
27 # one, and no "at <this file> line <n>" appended by warn to one that did not.
28 2 $message =~ s/\r?\n\z//;
29 2 warn "$message\n";
30 2 return 1;
31 }
32  
33 12 ▶sub from_json { __PACKAGE__->new( log_cb => \&_warn_log_cb )->deserialize(@_) }
Subroutine from_json: called CC=1 SCAR=0.0
Pod documented
34  
35 39 ▶sub to_json { __PACKAGE__->new( log_cb => \&_warn_log_cb )->serialize(@_) }
Subroutine to_json: called CC=1 SCAR=0.0
Pod documented
36  
37 2 ▶sub decode_json {
Subroutine decode_json: called CC=1 SCAR=0.0
Pod undocumented
38 2 my ( $entity ) = @_;
39  
40 2 JSON::MaybeXS::decode_json($entity);
41 }
42  
43 2 ▶sub encode_json {
Subroutine encode_json: called CC=1 SCAR=0.0
Pod undocumented
44 2 my ( $entity ) = @_;
45  
46 2 JSON::MaybeXS::encode_json(_ensure_characters($entity));
47 }
48  
49 # class definition
50 97 ▶sub serialize {
Subroutine serialize: called CC=6 SCAR=18.0
Pod documented
51 97 my ( $self, $entity, $options ) = @_;
52  
53 97 ▶ my $config = blessed $self ? $self->config : {};
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($self) ? : 97 0 97
54 97 my $strict_utf8 = $config->{strict_utf8};
55 97 ▶ $options ||= {};
Condition100%2/2
$options ||= {}
l!l
394
MC/DC100%1/1
$options ||= {}
$options
Truth table100%2/2
A
A= $options
Aresult
11
00
56  
57 97 foreach (keys %$config) {
58 71 ▶ $options->{$_} = $config->{$_} unless exists $options->{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $options->{$_} 71 0 71
59 }
60  
61 97 $options->{utf8} = 1;
62 exists $options->{strict_utf8}
63 97 ▶ and $strict_utf8 = delete $options->{strict_utf8};
Branch: 1 branch
BranchTrueFalseTotal
exists $options->{"strict_utf8"} and $strict_utf8 = delete $options->{"strict_utf8"} 49 48 97
64 97 $entity = _ensure_characters( $entity, $strict_utf8, $self );
65 96 JSON::MaybeXS->new($options)->encode($entity);
66 }
67  
68 35 ▶sub deserialize {
Subroutine deserialize: called CC=2 SCAR=7.0
Pod documented
69 35 my ( $self, $entity, $options ) = @_;
70  
71 35 ▶ $options ||= {};
Condition50%1/2
$options ||= {}
l!l
035
MC/DC0%0/1
$options ||= {}
$options
Truth table50%1/2
A
A= $options
Aresult
11
00
72 35 $options->{utf8} = 1;
73 35 delete $options->{strict_utf8};
74 35 JSON::MaybeXS->new($options)->decode($entity);
75 }
76  
77 29 my $HAS_UNICODE_UTF8 = eval { require Unicode::UTF8; 1; };
78  
79 3 ▶sub _valid_utf8 {
Subroutine _valid_utf8: called CC=2 SCAR=10.1
80 3 my ($bytes) = @_;
81 3 ▶ return Unicode::UTF8::valid_utf8($bytes) if $HAS_UNICODE_UTF8;
Branch: 1 branch
BranchTrueFalseTotal
if $HAS_UNICODE_UTF8 3 0 3
82 0 return eval { decode( 'UTF-8', $bytes, FB_CROAK ); 1 };
83 }
84  
85 0 ▶sub _decode_utf8 {
Subroutine _decode_utf8: not called CC=2 SCAR=17.9
86 0 my ($bytes) = @_;
87 0 ▶ return Unicode::UTF8::decode_utf8($bytes) if $HAS_UNICODE_UTF8;
Branch: 1 branch
BranchTrueFalseTotal
if $HAS_UNICODE_UTF8 0 0 0
88 0 return decode( 'UTF-8', $bytes );
89 }
90  
91 298 ▶sub _ensure_characters {
Subroutine _ensure_characters: called CC=8 SCAR=20.9
92 298 my ( $entity, $strict_utf8, $self ) = @_;
93  
94 298 ▶ return $entity if !defined $entity;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $entity 1 297 298
95 297 ▶ return _ensure_scalar( $entity, $strict_utf8, $self ) if !ref $entity;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $entity 171 126 297
96  
97 126 ▶ if ( is_plain_arrayref($entity) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_plain_arrayref($entity)) 18 108 126
98 18 for my $i ( 0 .. $#{$entity} ) {
99 44 $entity->[$i] = _ensure_characters( $entity->[$i], $strict_utf8, $self );
100 }
101 18 return $entity;
102 }
103  
104 108 ▶ if ( is_plain_hashref($entity) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_plain_hashref($entity)) 107 1 108
105 107 my %ret;
106 107 for my $key ( keys %{$entity} ) {
107 155 my $value = $entity->{$key};
108 155 my $decoded_key = _ensure_scalar( $key, $strict_utf8, $self );
109 155 my $decoded_value =
110 _ensure_characters( $value, $strict_utf8, $self );
111  
112 154 ▶ if ( $decoded_key ne $key ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($decoded_key ne $key) { } 0 154 154
113 0 delete $entity->{$key};
114 0 $ret{$decoded_key} = $decoded_value;
115 } else {
116 154 $ret{$key} = $decoded_value;
117 }
118 }
119 106 return \%ret;
120 }
121  
122 1 return $entity;
123 }
124  
125 326 ▶sub _ensure_scalar {
Subroutine _ensure_scalar: called CC=4 SCAR=13.9
126 326 my ( $value, $strict_utf8, $self ) = @_;
127  
128 326 ▶ return $value if utf8::is_utf8($value);
Branch: 1 branch
BranchTrueFalseTotal
if utf8::is_utf8($value) 11 315 326
129 315 ▶ return $value if $value !~ /[\x80-\xFF]/;
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ /[\x80-\xFF]/ 312 3 315
130 3 ▶ return _decode_utf8($value) if _valid_utf8($value);
Branch: 1 branch
BranchTrueFalseTotal
if _valid_utf8($value) 0 3 3
131  
132 3 _invalid_utf8( $strict_utf8, $self );
133 2 return $value;
134 }
135  
136 3 ▶sub _invalid_utf8 {
Subroutine _invalid_utf8: called CC=3 SCAR=11.2
137 3 my ( $strict_utf8, $self ) = @_;
138 3 my $msg = 'Invalid UTF-8 in JSON data';
139  
140 3 ▶ $strict_utf8
Branch: 1 branch
BranchTrueFalseTotal
$strict_utf8 and die "$msg\n" 1 2 3
141 and die "$msg\n";
142  
143 2 ▶ if ( blessed($self) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($self)) { } 2 0 2
144 2 $self->log_cb->( warning => "$msg; leaving bytes unchanged" );
145 } else {
146 0 warn "$msg; leaving bytes unchanged\n";
147 }
148 }
149  
150 29 1;
151  
152 __END__
« blib/lib/Dancer2/Core/Cookie.pm ↑ Summary blib/lib/Dancer2/Core/Role/Hookable.pm »