File Coverage

blib/lib/Dancer2/Serializer/YAML.pm
Criterion Covered Total %
statement 17 17 100.0
branch n/a
condition n/a
subroutine 7 7 100.0
pod n/a
total 24 24 100.0


line stmt bran cond sub pod time code
1             package Dancer2::Serializer::YAML;
2             # ABSTRACT: Serializer for handling YAML data
3             $Dancer2::Serializer::YAML::VERSION = '1.0.0';
4 5     5   4250 use Moo;
  5         15  
  5         41  
5 5     5   2067 use Carp 'croak';
  5         12  
  5         352  
6 5     5   39 use Encode;
  5         11  
  5         489  
7 5     5   44 use Module::Runtime 'use_module';
  5         11  
  5         49  
8 5     5   258 use Sub::Defer;
  5         11  
  5         1678  
9              
10             with 'Dancer2::Core::Role::Serializer';
11              
12             has '+content_type' => ( default => sub {'text/x-yaml'} );
13              
14             # deferred helpers. These are called as class methods, but need to
15             # ensure YAML is loaded.
16              
17             my $_from_yaml = defer_sub 'Dancer2::Serializer::YAML::from_yaml' => sub {
18             use_module('YAML');
19 4     4   221 sub { __PACKAGE__->deserialize(@_) };
20             };
21              
22             my $_to_yaml = defer_sub 'Dancer2::Serializer::YAML::to_yaml' => sub {
23             use_module('YAML');
24 11     11   397 sub { __PACKAGE__->serialize(@_) };
25             };
26              
27             # class definition
28             sub BUILD { use_module('YAML') }
29              
30             sub serialize {
31             my ( $self, $entity ) = @_;
32             encode('UTF-8', YAML::Dump($entity));
33             }
34              
35             sub deserialize {
36             my ( $self, $content ) = @_;
37             YAML::Load(decode('UTF-8', $content));
38             }
39              
40             1;
41              
42             __END__
43              
44             =pod
45              
46             =encoding UTF-8
47              
48             =head1 NAME
49              
50             Dancer2::Serializer::YAML - Serializer for handling YAML data
51              
52             =head1 VERSION
53              
54             version 1.0.0
55              
56             =head1 DESCRIPTION
57              
58             This is a serializer engine that allows you to turn Perl data structures into
59             YAML output and vice-versa.
60              
61             =head1 ATTRIBUTES
62              
63             =head2 content_type
64              
65             Returns 'text/x-yaml'
66              
67             =head1 METHODS
68              
69             =head2 serialize($content)
70              
71             Serializes a data structure to a YAML structure.
72              
73             =head2 deserialize($content)
74              
75             Deserializes a YAML structure to a data structure.
76              
77             =head1 FUNCTIONS
78              
79             =head2 fom_yaml($content)
80              
81             This is an helper available to transform a YAML data structure to a Perl data structures.
82              
83             =head2 to_yaml($content)
84              
85             This is an helper available to transform a Perl data structure to YAML.
86              
87             Calling this function will B<not> trigger the serialization's hooks.
88              
89             =head1 AUTHOR
90              
91             Dancer Core Developers
92              
93             =head1 COPYRIGHT AND LICENSE
94              
95             This software is copyright (c) 2023 by Alexis Sukrieh.
96              
97             This is free software; you can redistribute it and/or modify it under
98             the same terms as the Perl 5 programming language system itself.
99              
100             =cut