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.3520';
5 165     165   1219 use strict;
  165         371  
  165         5266  
6 165     165   935 use warnings;
  165         433  
  165         4065  
7 165     165   883 use Carp;
  165         443  
  165         9792  
8 165     165   1160 use Dancer::ModuleLoader;
  165         448  
  165         5618  
9 165     165   1034 use Dancer::Deprecation;
  165         449  
  165         5098  
10 165     165   1127 use Dancer::Config 'setting';
  165         520  
  165         9089  
11 165     165   1193 use Dancer::Exception qw(:all);
  165         451  
  165         20700  
12 165     165   1231 use base 'Dancer::Serializer::Abstract';
  165         444  
  165         77235  
13              
14              
15             # helpers
16              
17             sub from_json {
18 13     13 0 153 my $s = Dancer::Serializer::JSON->new;
19 13         57 $s->deserialize(@_);
20             }
21              
22             sub to_json {
23 8     8 0 71 my $s = Dancer::Serializer::JSON->new;
24 8         42 $s->serialize(@_);
25             }
26              
27             # class definition
28              
29 38     38 0 223 sub loaded { Dancer::ModuleLoader->load_with_params('JSON', '-support_by_pp') }
30              
31             sub init {
32 38     38 1 104 my ($self) = @_;
33 38 50       186 raise core_serializer => 'JSON is needed and is not installed'
34             unless $self->loaded;
35             }
36              
37             sub serialize {
38 32     32 1 1932 my $self = shift;
39 32         56 my $entity = shift;
40              
41 32   100     109 my $options = $self->_serialize_options_as_hashref(@_) || {};
42              
43 32   50     133 my $config = setting('engines') || {};
44 32   100     156 $config = $config->{JSON} || {};
45              
46             # straight pass through of config options to JSON
47 32         102 map { $options->{$_} = $config->{$_} } keys %$config;
  22         73  
48              
49             # pull in config from serializer init as well (and possibly override settings from the conf file)
50 32         54 map { $options->{$_} = $self->config->{$_} } keys %{$self->config};
  4         9  
  32         180  
51              
52 32 100 100     92 if (setting('environment') eq 'development' and not defined $options->{pretty}) {
53 26         68 $options->{pretty} = 1;
54             }
55              
56 32         281 JSON::to_json( $entity, $options );
57             }
58              
59             sub deserialize {
60 22     22 1 773 my $self = shift;
61 22         49 my $entity = shift;
62              
63 22         74 my $options = $self->_deserialize_options_as_hashref(@_);
64 22         103 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     32   104 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             sub _deserialize_options_as_hashref {
76 22     22   43 my $self = shift;
77 22   50     67 my $options = $self->_options_as_hashref(@_) || {};
78 22 50       89 $options->{utf8} = 1 if !exists $options->{utf8};
79 22         53 return $options;
80             }
81              
82             sub _options_as_hashref {
83 54     54   86 my $self = shift;
84              
85 54 100       369 return if scalar @_ == 0;
86              
87 2 50       16 if ( scalar @_ == 1 ) {
    0          
88 2         8 return shift;
89             }
90             elsif ( scalar @_ % 2 ) {
91 0         0 carp "options for to_json/from_json must be key value pairs (as a hashref)";
92             }
93             else {
94 0         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     27 1 1440 sub content_type {'application/json'}
103              
104             1;
105              
106             __END__