File Coverage

blib/lib/Dancer/Serializer/JSONPP.pm
Criterion Covered Total %
statement 42 46 91.3
branch 3 4 75.0
condition 3 7 42.8
subroutine 10 12 83.3
pod 3 4 75.0
total 61 73 83.5


line stmt bran cond sub pod time code
1             package Dancer::Serializer::JSONPP;
2              
3 1     1   199007 use strict;
  1         1  
  1         30  
4 1     1   3 use warnings;
  1         2  
  1         20  
5 1     1   15 use 5.008_005;
  1         5  
  1         49  
6             our $VERSION = '0.02';
7              
8 1     1   4 use Dancer::Config 'setting';
  1         1  
  1         41  
9 1     1   4 use Dancer::SharedData;
  1         1  
  1         15  
10 1     1   3 use base 'Dancer::Serializer::Abstract';
  1         1  
  1         63  
11 1     1   711 use JSON::PP;
  1         10672  
  1         334  
12              
13 0     0 0 0 sub loaded { 1 }
14              
15             sub _build_jsonpp {
16 1     1   2 my $self = shift;
17              
18 1         4 my $json = JSON::PP->new;
19              
20 1         8 my $options = {};
21              
22 1   50     4 my $config = setting('engines') || {};
23 1   50     14 $config = $config->{JSONPP} || $config->{JSON} || {};
24              
25             # straight pass through of config options to JSON
26 1         2 map { $options->{$_} = $config->{$_} } keys %$config;
  0         0  
27              
28             # pull in config from serializer init as well (and possibly override settings from the conf file)
29 1         2 map { $options->{$_} = $self->config->{$_} } keys %{$self->config};
  0         0  
  1         8  
30              
31 1 50 33     8 if (setting('environment') eq 'development' and not defined $options->{pretty}) {
32 1         11 $options->{pretty} = 1;
33             }
34              
35             # use live vars
36 1         9 my $vars = Dancer::SharedData->vars;
37 1         5 foreach my $k (%$vars) {
38 2 100       9 next unless $k =~ /^jsonpp_/;
39 1         1 my $k2 = $k; $k2 =~ s/^jsonpp_//;
  1         3  
40 1         3 $options->{$k2} = $vars->{$k};
41             }
42              
43 1         3 for my $method (keys %$options) {
44 2         12 $json->$method( $options->{$method} );
45             }
46              
47 1         88 return $json;
48             }
49              
50             sub serialize {
51 1     1 1 2210 (shift)->_build_jsonpp->encode(@_);
52             }
53              
54             sub deserialize {
55 0     0 1 0 (shift)->_build_jsonpp->decode(@_);
56             }
57              
58 1     1 1 192 sub content_type {'application/json'}
59              
60             1;
61             __END__