blib/lib/Dancer2/Serializer/JSON.pm

filter: Statementstmt 94.4% 85 / 90 Branchbran 83.3% 25 / 30 Conditioncond 66.6% 4 / 6 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 100% 18 / 18 Podpod 66.6% 4 / 6 totaltotal 89.5% 137 / 153 CC 21 SCAR 12.6
CC
21
Cov
90%
serialize
6.0
_ensure_characters
6.0
deserialize
4.0
CRAP 3.5
« lib/Dancer2/Core/Role/Hookable.pm ↑ Summary lib/Dancer2/Template/Tiny.pm »
1 package Dancer2::Serializer::JSON;
2 # ABSTRACT: Serializer for handling JSON data
3 32 $Dancer2::Serializer::JSON::VERSION = '2.3.0';
4 32 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 32 ▶use Ref::Util qw< is_plain_arrayref is_plain_hashref >;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 32 ▶use JSON::MaybeXS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 32 ▶use Encode qw(decode FB_CROAK);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 32 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 32 with 'Dancer2::Core::Role::Serializer';
11  
12 93 ▶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 47 ▶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 107 ▶sub serialize {
Subroutine serialize: called CC=6 SCAR=18.0
Pod documented
51 107 my ( $self, $entity, $options ) = @_;
52  
53 107 ▶ my $config = blessed $self ? $self->config : {};
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($self) ? : 107 0 107
54 107 my $strict_utf8 = $config->{strict_utf8};
55 107 ▶ $options ||= {};
Condition100%2/2
$options ||= {}
l!l
6101
MC/DC100%1/1
$options ||= {}
$options
Truth table100%2/2
A
A= $options
Aresult
11
00
56  
57 107 foreach (keys %$config) {
58 73 ▶ $options->{$_} = $config->{$_} unless exists $options->{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $options->{$_} 73 0 73
59 }
60  
61 107 $options->{utf8} = 1;
62 exists $options->{strict_utf8}
63 107 ▶ and $strict_utf8 = delete $options->{strict_utf8};
Branch: 1 branch
BranchTrueFalseTotal
exists $options->{"strict_utf8"} and $strict_utf8 = delete $options->{"strict_utf8"} 51 56 107
64 107 $entity = _ensure_characters( $entity, $strict_utf8, $self );
65 106 JSON::MaybeXS->new($options)->encode($entity);
66 }
67  
68 37 ▶sub deserialize {
Subroutine deserialize: called CC=4 SCAR=13.9
Pod documented
69 37 my ( $self, $entity, $options ) = @_;
70  
71 37 ▶ $options ||= {};
Condition50%1/2
$options ||= {}
l!l
037
MC/DC0%0/1
$options ||= {}
$options
Truth table50%1/2
A
A= $options
Aresult
11
00
72 37 $options->{utf8} = 1;
73 37 delete $options->{strict_utf8};
74 37 my $data;
75 eval {
76 37 $data = JSON::MaybeXS->new($options)->decode($entity);
77 30 1;
78 37 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $data = "JSON::MaybeXS"->new($options)->decode($entity); 1 } } or do { my $error = $@ || "Zombie Error"; $error =~ s/ at \S+ line \d+\.$//; die $error } 30 7 37
79 7 ▶ my $error = $@ || 'Zombie Error';
Condition50%1/2
$@ || 'Zombie Error'
l!l
70
MC/DC0%0/1
$@ || 'Zombie Error'
$@
Truth table50%1/2
A
A= $@
Aresult
11
00
80 # remove any "at our/source/code.pm line 67." from end of error
81 # and rethrow.
82 # Note the \S+ won't match against the heinous crime using paths with spaces
83 7 $error =~ s/ at \S+ line \d+\.$//;
84 7 die $error;
85 };
86 30 return $data;
87 }
88  
89 32 my $HAS_UNICODE_UTF8 = eval { require Unicode::UTF8; 1; };
90  
91 11 ▶sub _valid_utf8 {
Subroutine _valid_utf8: called CC=2 SCAR=10.1
92 11 my ($bytes) = @_;
93 11 ▶ return Unicode::UTF8::valid_utf8($bytes) if $HAS_UNICODE_UTF8;
Branch: 1 branch
BranchTrueFalseTotal
if $HAS_UNICODE_UTF8 11 0 11
94 0 return eval { decode( 'UTF-8', $bytes, FB_CROAK ); 1 };
95 }
96  
97 8 ▶sub _decode_utf8 {
Subroutine _decode_utf8: called CC=2 SCAR=8.1
98 8 my ($bytes) = @_;
99 8 ▶ return Unicode::UTF8::decode_utf8($bytes) if $HAS_UNICODE_UTF8;
Branch: 1 branch
BranchTrueFalseTotal
if $HAS_UNICODE_UTF8 8 0 8
100 0 return decode( 'UTF-8', $bytes );
101 }
102  
103 339 ▶sub _ensure_characters {
Subroutine _ensure_characters: called CC=6 SCAR=17.9
104 339 my ( $entity, $strict_utf8, $self ) = @_;
105  
106 339 ▶ return $entity if !defined $entity;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $entity 1 338 339
107 338 ▶ return _ensure_scalar( $entity, $strict_utf8, $self ) if !ref $entity;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $entity 190 148 338
108  
109 # Build new containers rather than writing decoded values back into the
110 # caller's. The entity belongs to the application, which may well hold on
111 # to it and serialize it again. GH #1863.
112 148 ▶ if ( is_plain_arrayref($entity) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_plain_arrayref($entity)) 25 123 148
113 52 return [ map { _ensure_characters( $_, $strict_utf8, $self ) }
114 25 @{$entity} ];
115 }
116  
117 123 ▶ if ( is_plain_hashref($entity) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_plain_hashref($entity)) 122 1 123
118 122 my %ret;
119 122 for my $key ( keys %{$entity} ) {
120 $ret{ _ensure_scalar( $key, $strict_utf8, $self ) } =
121 174 _ensure_characters( $entity->{$key}, $strict_utf8, $self );
122 }
123 121 return \%ret;
124 }
125  
126 1 return $entity;
127 }
128  
129 363 ▶sub _ensure_scalar {
Subroutine _ensure_scalar: called CC=4 SCAR=13.9
130 363 my ( $value, $strict_utf8, $self ) = @_;
131  
132 363 ▶ return $value if utf8::is_utf8($value);
Branch: 1 branch
BranchTrueFalseTotal
if utf8::is_utf8($value) 11 352 363
133  
134 # Probe a throwaway copy, not $value itself. Matching a regex against a
135 # number gives that SV a string form as well as its numeric one, and
136 # backends that decide "number or string?" by looking at the SV's flags
137 # -- JSON::XS, and JSON::PP before 2.97 -- then encode it as a string, so
138 # 42 comes back out as "42". GH #1861.
139 352 my $probe = $value;
140 352 ▶ return $value if $probe !~ /[\x80-\xFF]/;
Branch: 1 branch
BranchTrueFalseTotal
unless $probe =~ /[\x80-\xFF]/ 341 11 352
141 11 ▶ return _decode_utf8($value) if _valid_utf8($value);
Branch: 1 branch
BranchTrueFalseTotal
if _valid_utf8($value) 8 3 11
142  
143 3 _invalid_utf8( $strict_utf8, $self );
144 2 return $value;
145 }
146  
147 3 ▶sub _invalid_utf8 {
Subroutine _invalid_utf8: called CC=3 SCAR=11.2
148 3 my ( $strict_utf8, $self ) = @_;
149 3 my $msg = 'Invalid UTF-8 in JSON data';
150  
151 3 ▶ $strict_utf8
Branch: 1 branch
BranchTrueFalseTotal
$strict_utf8 and die "$msg\n" 1 2 3
152 and die "$msg\n";
153  
154 2 ▶ if ( blessed($self) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($self)) { } 2 0 2
155 2 $self->log_cb->( warning => "$msg; leaving bytes unchanged" );
156 } else {
157 0 warn "$msg; leaving bytes unchanged\n";
158 }
159 }
160  
161 32 1;
162  
163 __END__
« lib/Dancer2/Core/Role/Hookable.pm ↑ Summary lib/Dancer2/Template/Tiny.pm »