File Coverage

blib/lib/Dancer2/Core/Role/Serializer.pm
Criterion Covered Total %
statement 12 13 92.3
branch n/a
condition n/a
subroutine 5 6 83.3
pod 0 2 0.0
total 17 21 80.9


line stmt bran cond sub pod time code
1             # ABSTRACT: Role for Serializer engines
2             $Dancer2::Core::Role::Serializer::VERSION = '0.400000';
3             use Moo::Role;
4 23     23   59130 use Dancer2::Core::Types;
  23         52  
  23         157  
5 23     23   11447 use Scalar::Util 'blessed';
  23         78  
  23         275  
6 23     23   193318  
  23         57  
  23         10283  
7             with 'Dancer2::Core::Role::Engine';
8              
9             {
10             before_serializer => 'engine.serializer.before',
11             after_serializer => 'engine.serializer.after',
12 216     216 0 1004 }
13             }
14              
15              
16              
17 33     33 0 66 requires 'serialize';
  33         88  
18             requires 'deserialize';
19 0     0      
20             has log_cb => (
21             is => 'ro',
22             isa => CodeRef,
23             default => sub { sub {1} },
24             );
25              
26             has content_type => (
27             is => 'ro',
28             isa => Str,
29             required => 1,
30             writer => 'set_content_type'
31             );
32              
33             around serialize => sub {
34             my ( $orig, $self, $content, $options ) = @_;
35              
36             blessed $self && $self->execute_hook( 'engine.serializer.before', $content );
37              
38             $content or return $content;
39              
40             my $data;
41             eval {
42             $data = $self->$orig( $content, $options );
43             blessed $self
44             and $self->execute_hook( 'engine.serializer.after', $data );
45             1;
46             } or do {
47             my $error = $@ || 'Zombie Error';
48             blessed $self
49             and $self->log_cb->( core => "Failed to serialize content: $error" );
50             };
51              
52             return $data;
53             };
54              
55             around deserialize => sub {
56             my ( $orig, $self, $content, $options ) = @_;
57              
58             $content && length $content > 0
59             or return $content;
60              
61             my $data;
62             eval {
63             $data = $self->$orig($content, $options);
64             1;
65             } or do {
66             my $error = $@ || 'Zombie Error';
67             $self->log_cb->( core => "Failed to deserialize content: $error" );
68             };
69              
70             return $data;
71             };
72              
73             1;
74              
75              
76             =pod
77              
78             =encoding UTF-8
79              
80             =head1 NAME
81              
82             Dancer2::Core::Role::Serializer - Role for Serializer engines
83              
84             =head1 VERSION
85              
86             version 0.400000
87              
88             =head1 DESCRIPTION
89              
90             Any class that consumes this role will be able to be used as a
91             serializer under Dancer2.
92              
93             In order to implement this role, the consumer B<must> implement the
94             methods C<serialize> and C<deserialize>, and should define
95             the C<content_type> attribute value.
96              
97             =head1 ATTRIBUTES
98              
99             =head2 content_type
100              
101             The I<content type> of the object after being serialized. For example,
102             a JSON serializer would have a I<application/json> content type
103             defined.
104              
105             =head1 METHODS
106              
107             =head2 serialize($content, [\%options])
108              
109             The serialize method need to be implemented by the consumer. It
110             receives the serializer class object and a reference to the object to
111             be serialized. Should return the object after being serialized, in the
112             content type defined by the C<content_type> attribute.
113              
114             A third optional argument is a hash reference of options to the
115             serializer.
116              
117             The serialize method must return bytes and therefore has to handle any
118             encoding.
119              
120             =head2 deserialize($content, [\%options])
121              
122             The inverse method of C<serialize>. Receives the serializer class
123             object and a string that should be deserialized. The method should
124             return a reference to the deserialized Perl data structure.
125              
126             A third optional argument is a hash reference of options to the
127             serializer.
128              
129             The deserialize method receives encoded bytes and must therefore
130             handle any decoding required.
131              
132             =head1 CONFIGURATION
133              
134             The B<serializer> configuration variable tells Dancer2 which engine to use.
135              
136             You can change it either in your config.yml file:
137              
138             #Set JSON engine
139             serializer: "JSON"
140              
141             # Prettify JSON output
142             engines:
143             serializer:
144             JSON:
145             pretty: 1
146              
147             To know which engines are availables please see L<Dancer2::Manual/"Serializers">
148              
149             =head1 AUTHOR
150              
151             Dancer Core Developers
152              
153             =head1 COPYRIGHT AND LICENSE
154              
155             This software is copyright (c) 2022 by Alexis Sukrieh.
156              
157             This is free software; you can redistribute it and/or modify it under
158             the same terms as the Perl 5 programming language system itself.
159              
160             =cut