File Coverage

blib/lib/Dancer/Serializer/JSON.pm
Criterion Covered Total %
statement 58 60 96.6
branch 7 12 58.3
condition 9 11 81.8
subroutine 18 18 100.0
pod 4 7 57.1
total 96 108 88.8


line stmt bran cond sub pod time code
1             package Dancer::Serializer::JSON;
2             our $AUTHORITY = 'cpan:SUKRIA';
3             #ABSTRACT: serializer for handling JSON data
4             $Dancer::Serializer::JSON::VERSION = '1.3514_04'; # TRIAL
5             $Dancer::Serializer::JSON::VERSION = '1.351404';
6 166     166   1398 use strict;
  166         324  
  166         4033  
7 166     166   736 use warnings;
  166         309  
  166         3375  
8 166     166   788 use Carp;
  166         357  
  166         9275  
9 166     166   1079 use Dancer::ModuleLoader;
  166         387  
  166         3887  
10 166     166   815 use Dancer::Deprecation;
  166         299  
  166         3856  
11 166     166   928 use Dancer::Config 'setting';
  166         353  
  166         6993  
12 166     166   961 use Dancer::Exception qw(:all);
  166         424  
  166         16482  
13 166     166   981 use base 'Dancer::Serializer::Abstract';
  166         335  
  166         62703  
14              
15              
16             # helpers
17              
18             sub from_json {
19 13     13 0 136 my $s = Dancer::Serializer::JSON->new;
20 13         49 $s->deserialize(@_);
21             }
22              
23             sub to_json {
24 8     8 0 53 my $s = Dancer::Serializer::JSON->new;
25 8         44 $s->serialize(@_);
26             }
27              
28             # class definition
29              
30 38     38 0 182 sub loaded { Dancer::ModuleLoader->load_with_params('JSON', '-support_by_pp') }
31              
32             sub init {
33 38     38 1 101 my ($self) = @_;
34 38 50       123 raise core_serializer => 'JSON is needed and is not installed'
35             unless $self->loaded;
36             }
37              
38             sub serialize {
39 32     32 1 1568 my $self = shift;
40 32         48 my $entity = shift;
41              
42 32   100     86 my $options = $self->_serialize_options_as_hashref(@_) || {};
43              
44 32   50     101 my $config = setting('engines') || {};
45 32   100     135 $config = $config->{JSON} || {};
46              
47             # straight pass through of config options to JSON
48 32         93 map { $options->{$_} = $config->{$_} } keys %$config;
  22         44  
49              
50             # pull in config from serializer init as well (and possibly override settings from the conf file)
51 32         53 map { $options->{$_} = $self->config->{$_} } keys %{$self->config};
  4         10  
  32         159  
52              
53 32 100 100     129 if (setting('environment') eq 'development' and not defined $options->{pretty}) {
54 26         56 $options->{pretty} = 1;
55             }
56              
57 32         114 JSON::to_json( $entity, $options );
58             }
59              
60             sub deserialize {
61 22     22 1 647 my $self = shift;
62 22         42 my $entity = shift;
63              
64 22         72 my $options = $self->_deserialize_options_as_hashref(@_);
65 22         77 JSON::from_json( $entity, $options );
66             }
67              
68             # Standard JSON behaviour is fine when serializing; we'll end up
69             # encoding as UTF8 later on.
70             sub _serialize_options_as_hashref {
71 32     32   92 return shift->_options_as_hashref(@_);
72             }
73              
74             # JSON should be UTF8 by default, so explicitly decode it as such
75             # on its way in.
76             sub _deserialize_options_as_hashref {
77 22     22   38 my $self = shift;
78 22   50     56 my $options = $self->_options_as_hashref(@_) || {};
79 22 50       80 $options->{utf8} = 1 if !exists $options->{utf8};
80 22         40 return $options;
81             }
82              
83             sub _options_as_hashref {
84 54     54   81 my $self = shift;
85              
86 54 100       343 return if scalar @_ == 0;
87              
88 2 50       7 if ( scalar @_ == 1 ) {
    0          
89 2         7 return shift;
90             }
91             elsif ( scalar @_ % 2 ) {
92 0         0 carp "options for to_json/from_json must be key value pairs (as a hashref)";
93             }
94             else {
95 0         0 Dancer::Deprecation->deprecated(
96             version => '1.3002',
97             fatal => 1,
98             message => 'options as hash for to_json/from_json is DEPRECATED. please pass a hashref',
99             );
100             }
101             }
102              
103 27     27 1 1362 sub content_type {'application/json'}
104              
105             1;
106              
107             __END__