lib/Dancer/Serializer/JSON.pm

filter: Statementstmt 96.8% 61 / 63 Branchbran 58.3% 7 / 12 Conditioncond 81.8% 9 / 11 MC/DCmcdc 66.6% 4 / 6 Subroutinesub 100% 18 / 18 Podpod 57.1% 4 / 7 totaltotal 88.0% 103 / 117 CC 12 SCAR 10.7
CC
12
Cov
90%
serialize
6.0
_options_as_hashref
5.2
_deserialize_options_as_hashref
3.1
CRAP 2.9
« lib/Dancer/Exception.pm ↑ Summary lib/Dancer/Exception/Base.pm »
1 package Dancer::Serializer::JSON;
2 178 our $AUTHORITY = 'cpan:SUKRIA';
3 #ABSTRACT: serializer for handling JSON data
4 178 $Dancer::Serializer::JSON::VERSION = '1.3522';
5 178 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 178 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 178 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 178 ▶use Dancer::ModuleLoader;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 178 ▶use Dancer::Deprecation;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 178 ▶use Dancer::Config 'setting';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 178 ▶use Dancer::Exception qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 178 ▶use base 'Dancer::Serializer::Abstract';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14  
15 # helpers
16  
17 13 ▶sub from_json {
Subroutine from_json: called CC=1 SCAR=0.0
Pod undocumented
18 13 my $s = Dancer::Serializer::JSON->new;
19 13 $s->deserialize(@_);
20 }
21  
22 8 ▶sub to_json {
Subroutine to_json: called CC=1 SCAR=0.0
Pod undocumented
23 8 my $s = Dancer::Serializer::JSON->new;
24 8 $s->serialize(@_);
25 }
26  
27 # class definition
28  
29 38 ▶sub loaded { Dancer::ModuleLoader->load_with_params('JSON', '-support_by_pp') }
Subroutine loaded: called CC=1 SCAR=0.0
Pod undocumented
30  
31 38 ▶sub init {
Subroutine init: called CC=2 SCAR=7.2
Pod documented
32 38 my ($self) = @_;
33 38 ▶ raise core_serializer => 'JSON is needed and is not installed'
Branch: 1 branch
BranchTrueFalseTotal
unless $self->loaded 0 38 38
34 unless $self->loaded;
35 }
36  
37 32 ▶sub serialize {
Subroutine serialize: called CC=6 SCAR=17.9
Pod documented
38 32 my $self = shift;
39 32 my $entity = shift;
40  
41 32 ▶ my $options = $self->_serialize_options_as_hashref(@_) || {};
Condition100%2/2
$self->_serialize_options_as_hashref(@_) || {}
l!l
230
MC/DC100%1/1
$self->_serialize_options_as_hashref(@_) || {}
$self->_serialize_options_as_hashref(@_)
Truth table100%2/2
A
A= $self->_serialize_options_as_hashref(@_)
Aresult
11
00
42  
43 32 ▶ my $config = setting('engines') || {};
Condition50%1/2
setting('engines') || {}
l!l
320
MC/DC0%0/1
setting('engines') || {}
setting('engines')
Truth table50%1/2
A
A= setting('engines')
Aresult
11
00
44 32 ▶ $config = $config->{JSON} || {};
Condition100%2/2
$config->{'JSON'} || {}
l!l
1121
MC/DC100%1/1
$config->{'JSON'} || {}
$config->{'JSON'}
Truth table100%2/2
A
A= $config->{'JSON'}
Aresult
11
00
45  
46 # straight pass through of config options to JSON
47 32 map { $options->{$_} = $config->{$_} } keys %$config;
48  
49 # pull in config from serializer init as well (and possibly override settings from the conf file)
50 32 map { $options->{$_} = $self->config->{$_} } keys %{$self->config};
51  
52 32 ▶ if (setting('environment') eq 'development' and not defined $options->{pretty}) {
Branch: 1 branch
BranchTrueFalseTotal
if (setting("environment") eq "development" and not defined $options->{"pretty"}) 26 6 32
Condition100%3/3
setting("environment") eq "development" and not defined $options->{"pretty"}
!ll&&!rl&&r
3326
MC/DC100%2/2
setting("environment") eq "development" and not defined $options->{"pretty"}
setting("environment") eq "development" not defined $options->{"pretty"}
Truth table100%3/3
A and not(B)
A= setting("environment") eq "development"B= not defined $options->{"pretty"}
ABresult
0X0
100
111
53 26 $options->{pretty} = 1;
54 }
55  
56 32 JSON::to_json( $entity, $options );
57 }
58  
59 22 ▶sub deserialize {
Subroutine deserialize: called CC=1 SCAR=0.0
Pod documented
60 22 my $self = shift;
61 22 my $entity = shift;
62  
63 22 my $options = $self->_deserialize_options_as_hashref(@_);
64 22 JSON::from_json( $entity, $options );
65 }
66  
67 # Standard JSON behaviour is fine when serializing; we'll end up
68 # encoding as UTF8 later on.
69 32 ▶sub _serialize_options_as_hashref {
Subroutine _serialize_options_as_hashref: called CC=1 SCAR=0.0
70 32 return shift->_options_as_hashref(@_);
71 }
72  
73 # JSON should be UTF8 by default, so explicitly decode it as such
74 # on its way in.
75 22 ▶sub _deserialize_options_as_hashref {
Subroutine _deserialize_options_as_hashref: called CC=3 SCAR=11.4
76 22 my $self = shift;
77 22 ▶ my $options = $self->_options_as_hashref(@_) || {};
Condition50%1/2
$self->_options_as_hashref(@_) || {}
l!l
022
MC/DC0%0/1
$self->_options_as_hashref(@_) || {}
$self->_options_as_hashref(@_)
Truth table50%1/2
A
A= $self->_options_as_hashref(@_)
Aresult
11
00
78 22 ▶ $options->{utf8} = 1 if !exists $options->{utf8};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $options->{"utf8"} 22 0 22
79 22 return $options;
80 }
81  
82 54 ▶sub _options_as_hashref {
Subroutine _options_as_hashref: called CC=4 SCAR=16.4
83 54 my $self = shift;
84  
85 54 ▶ return if scalar @_ == 0;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ == 0 52 2 54
86  
87 2 ▶ if ( scalar @_ == 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @_ == 1) { } 2 0 2
elsif (scalar @_ % 2) { } 0 0 0
88 2 return shift;
89 }
90 elsif ( scalar @_ % 2 ) {
91 0 carp "options for to_json/from_json must be key value pairs (as a hashref)";
92 }
93 else {
94 0 Dancer::Deprecation->deprecated(
95 version => '1.3002',
96 fatal => 1,
97 message => 'options as hash for to_json/from_json is DEPRECATED. please pass a hashref',
98 );
99 }
100 }
101  
102 27 ▶sub content_type {'application/json'}
Subroutine content_type: called CC=1 SCAR=0.0
Pod documented
103  
104 178 1;
105  
106 __END__
« lib/Dancer/Exception.pm ↑ Summary lib/Dancer/Exception/Base.pm »