File Coverage

blib/lib/Dancer2/Serializer/JSONMaybeXS.pm
Criterion Covered Total %
statement 6 6 100.0
branch n/a
condition n/a
subroutine 2 2 100.0
pod n/a
total 8 8 100.0


line stmt bran cond sub pod time code
1             package Dancer2::Serializer::JSONMaybeXS;
2              
3 2     2   694944 use Moo;
  2         14564  
  2         18  
4 2     2   2158 use JSON::MaybeXS ();
  2         4  
  2         619  
5              
6             our $VERSION = '0.001';
7              
8             with 'Dancer2::Core::Role::Serializer';
9              
10             has '+content_type' => ( default => sub {'application/json;charset=UTF-8'} );
11              
12             sub serialize {
13             my ($self, $entity, $options) = @_;
14            
15             my $config = $self->config;
16            
17             foreach (keys %$config) {
18             $options->{$_} = $config->{$_} unless exists $options->{$_};
19             }
20            
21             $options->{utf8} = 1 if !defined $options->{utf8};
22            
23             JSON::MaybeXS->new($options)->encode($entity);
24             }
25              
26             sub deserialize {
27             my ($self, $entity, $options) = @_;
28            
29             $options->{utf8} = 1 if !defined $options->{utf8};
30             JSON::MaybeXS->new($options)->decode($entity);
31             }
32              
33             1;
34              
35             =head1 NAME
36              
37             Dancer2::Serializer::JSONMaybeXS - Serializer for handling JSON data
38              
39             =head1 SYNOPSIS
40              
41             use Dancer2;
42             set serializer => 'JSONMaybeXS';
43              
44             =head1 DESCRIPTION
45              
46             This is a serializer engine for the L web framework that allows you to
47             turn Perl data structures into JSON output and vice-versa. It uses
48             L, a faster and cleaner interface to XS and pure-perl JSON
49             modules than L.pm.
50              
51             =head1 METHODS
52              
53             =head2 deserialize
54              
55             my $data = $serializer->deserialize($string);
56              
57             Deserializes a JSON string into a Perl data structure.
58              
59             =head2 serialize
60              
61             my $string = $serializer->serialize($data);
62              
63             Serializes a Perl data structure into a JSON string.
64              
65             =head1 BUGS
66              
67             Report any issues on the public bugtracker.
68              
69             =head1 AUTHOR
70              
71             Dan Book
72              
73             =head1 COPYRIGHT AND LICENSE
74              
75             This software is Copyright (c) 2016 by Dan Book.
76              
77             This is free software, licensed under:
78              
79             The Artistic License 2.0 (GPL Compatible)
80              
81             =head1 SEE ALSO
82              
83             L