Debian Codegraph @

lib/Dancer2/Core/Role/Serializer.pm

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