Debian Codegraph @

lib/Dancer2/Serializer/YAML.pm

raw
1 package Dancer2::Serializer::YAML;
2 # ABSTRACT: Serializer for handling YAML data
3 $Dancer2::Serializer::YAML::VERSION = '2.2.2';
4 use Moo;
5 use Carp 'croak';
6 use Encode;
7 use Module::Runtime 'use_module';
8 use Sub::Defer;
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 # They run outside a Dancer2 app, so there is no app logger for the engine to
18 # send diagnostics to, and a failed parse would otherwise return undef and say
19 # nothing at all. Warn instead, so the caller still gets to see what went
20 # wrong.
21 sub _warn_log_cb {
22 my ( $level, $message ) = @_;
23
24 # The message is whatever the parser left in $@, and whether it ends in a
25 # newline varies by backend, so strip the line ending it came with and
26 # supply exactly one: no blank line after a message that already ended in
27 # one, and no "at <this file> line <n>" appended by warn to one that did not.
28 $message =~ s/\r?\n\z//;
29 warn "$message\n";
30 return 1;
31 }
32
33 my $_from_yaml = defer_sub 'Dancer2::Serializer::YAML::from_yaml' => sub {
34 use_module('YAML');
35 sub { __PACKAGE__->new( log_cb => \&_warn_log_cb )->deserialize(@_) };
36 };
37
38 my $_to_yaml = defer_sub 'Dancer2::Serializer::YAML::to_yaml' => sub {
39 use_module('YAML');
40 sub { __PACKAGE__->new( log_cb => \&_warn_log_cb )->serialize(@_) };
41 };
42
43 # class definition
44 sub BUILD { use_module('YAML') }
45
46 sub serialize {
47 my ( $self, $entity ) = @_;
48 encode('UTF-8', YAML::Dump($entity));
49 }
50
51 sub deserialize {
52 my ( $self, $content ) = @_;
53
54 # Content reaching here is untrusted -- for an app with 'serializer: YAML'
55 # (or Serializer::Mutable, which maps both text/x-yaml and text/html to
56 # this class) it is the raw request body.
57 #
58 # YAML tags can ask the loader to build things that are not data.
59 # !!perl/hash:Some::Class instantiates an arbitrary blessed object, which
60 # is the entry point for DESTROY/AUTOLOAD gadget chains, and !!perl/code
61 # asks for a string eval. Both are refused here.
62 #
63 # These are set explicitly rather than left to YAML.pm's defaults so the
64 # behaviour does not depend on which YAML.pm the user resolved: LoadBlessed
65 # only defaults to 0 from YAML 1.30, and the variable itself only exists
66 # from 1.25 (which is why cpanfile floors YAML -- see the note there).
67 #
68 # UseCode must be zeroed as well: YAML::Loader::Base decides on code
69 # loading with a plain OR -- load_code($YAML::LoadCode || $YAML::UseCode) --
70 # so LoadCode = 0 alone is defeated by an ambient $YAML::UseCode = 1, and
71 # the !!perl/code string eval is not gated on LoadBlessed at all.
72 local $YAML::LoadBlessed = 0;
73 local $YAML::LoadCode = 0;
74 local $YAML::UseCode = 0;
75
76 YAML::Load(decode('UTF-8', $content));
77 }
78
79 1;
80
81 __END__
82
83 =pod
84
85 =encoding UTF-8
86
87 =head1 NAME
88
89 Dancer2::Serializer::YAML - Serializer for handling YAML data
90
91 =head1 VERSION
92
93 version 2.2.2
94
95 =head1 DESCRIPTION
96
97 This is a serializer engine that allows you to turn Perl data structures into
98 YAML output and vice-versa.
99
100 =head1 ATTRIBUTES
101
102 =head2 content_type
103
104 Returns 'text/x-yaml'
105
106 =head1 METHODS
107
108 =head2 serialize($content)
109
110 Serializes a data structure to a YAML structure.
111
112 =head2 deserialize($content)
113
114 Deserializes a YAML structure to a data structure.
115
116 =head1 FUNCTIONS
117
118 =head2 fom_yaml($content)
119
120 This is an helper available to transform a YAML data structure to a Perl data structures.
121
122 =head2 to_yaml($content)
123
124 This is an helper available to transform a Perl data structure to YAML.
125
126 Calling this function will B<not> trigger the serialization's hooks.
127
128 =head1 AUTHOR
129
130 Dancer Core Developers
131
132 =head1 COPYRIGHT AND LICENSE
133
134 This software is copyright (c) 2026 by Alexis Sukrieh.
135
136 This is free software; you can redistribute it and/or modify it under
137 the same terms as the Perl 5 programming language system itself.
138
139 =cut