| 1 | package Dancer::Serializer::JSON; | ||||||||||||||||||||||||||||
| 2 | 171 | our $AUTHORITY = 'cpan:SUKRIA'; | |||||||||||||||||||||||||||
| 3 | #ABSTRACT: serializer for handling JSON data | ||||||||||||||||||||||||||||
| 4 | 171 | $Dancer::Serializer::JSON::VERSION = '1.3520'; | |||||||||||||||||||||||||||
| 5 | 171 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 171 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 171 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 171 | ▶ | use Dancer::ModuleLoader; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 171 | ▶ | use Dancer::Deprecation; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 171 | ▶ | use Dancer::Config 'setting'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 171 | ▶ | use Dancer::Exception qw(:all); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 171 | ▶ | use base 'Dancer::Serializer::Abstract'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | # helpers | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | sub from_json { | ||||||||||||||||||||||||||||
| 18 | 13 | ▶ | my $s = Dancer::Serializer::JSON->new; | ||||||||||||||||||||||||||
|
Subroutine
from_json: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 19 | 13 | $s->deserialize(@_); | |||||||||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | sub to_json { | ||||||||||||||||||||||||||||
| 23 | 8 | ▶ | my $s = Dancer::Serializer::JSON->new; | ||||||||||||||||||||||||||
|
Subroutine
to_json: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 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 | sub init { | ||||||||||||||||||||||||||||
| 32 | 38 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
init: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||||||||||
| 33 | 38 | ▶ | raise core_serializer => 'JSON is needed and is not installed' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | unless $self->loaded; | ||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | sub serialize { | ||||||||||||||||||||||||||||
| 38 | 32 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
serialize: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 39 | 32 | my $entity = shift; | |||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 32 | ▶ | my $options = $self->_serialize_options_as_hashref(@_) || {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->_serialize_options_as_hashref(@_) || {}
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(@_)
| |||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 32 | ▶ | my $config = setting('engines') || {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
setting('engines') || {}
MC/DC0%0/1
setting('engines') || {}
setting('engines')
Truth table50%1/2
A
A= setting('engines')
| |||||||||||||||||||||||||||||
| 44 | 32 | ▶ | $config = $config->{JSON} || {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
$config->{'JSON'} || {}
MC/DC100%1/1
$config->{'JSON'} || {}
$config->{'JSON'}
Truth table100%2/2
A
A= $config->{'JSON'}
| |||||||||||||||||||||||||||||
| 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
Condition100%3/3
setting("environment") eq "development" and not defined $options->{"pretty"}
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"}
| |||||||||||||||||||||||||||||
| 53 | 26 | $options->{pretty} = 1; | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 32 | JSON::to_json( $entity, $options ); | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | sub deserialize { | ||||||||||||||||||||||||||||
| 60 | 22 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
deserialize: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 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 | sub _serialize_options_as_hashref { | ||||||||||||||||||||||||||||
| 70 | 32 | ▶ | return shift->_options_as_hashref(@_); | ||||||||||||||||||||||||||
|
Subroutine
_serialize_options_as_hashref: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | # JSON should be UTF8 by default, so explicitly decode it as such | ||||||||||||||||||||||||||||
| 74 | # on its way in. | ||||||||||||||||||||||||||||
| 75 | sub _deserialize_options_as_hashref { | ||||||||||||||||||||||||||||
| 76 | 22 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_deserialize_options_as_hashref: called CC=3 SCAR=11.4
| |||||||||||||||||||||||||||||
| 77 | 22 | ▶ | my $options = $self->_options_as_hashref(@_) || {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->_options_as_hashref(@_) || {}
MC/DC0%0/1
$self->_options_as_hashref(@_) || {}
$self->_options_as_hashref(@_)
Truth table50%1/2
A
A= $self->_options_as_hashref(@_)
| |||||||||||||||||||||||||||||
| 78 | 22 | ▶ | $options->{utf8} = 1 if !exists $options->{utf8}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 22 | return $options; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | sub _options_as_hashref { | ||||||||||||||||||||||||||||
| 83 | 54 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_options_as_hashref: called CC=4 SCAR=16.4
| |||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 54 | ▶ | return if scalar @_ == 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 2 | ▶ | if ( scalar @_ == 1 ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 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 | 171 | 1; | |||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | __END__ | ||||||||||||||||||||||||||||