| 1 |
package Dancer2::Serializer::JSON; |
| 2 |
# ABSTRACT: Serializer for handling JSON data |
| 3 |
$Dancer2::Serializer::JSON::VERSION = '2.2.2'; |
| 4 |
use Moo; |
| 5 |
use Ref::Util qw< is_plain_arrayref is_plain_hashref >; |
| 6 |
use JSON::MaybeXS (); |
| 7 |
use Encode qw(decode FB_CROAK); |
| 8 |
use Scalar::Util 'blessed'; |
| 9 |
|
| 10 |
with 'Dancer2::Core::Role::Serializer'; |
| 11 |
|
| 12 |
has '+content_type' => ( default => sub {'application/json'} ); |
| 13 |
|
| 14 |
# helpers |
| 15 |
# |
| 16 |
# These run outside a Dancer2 app, so there is no app logger for the engine to |
| 17 |
# send diagnostics to. Without one they would be lost: a failed parse returns |
| 18 |
# undef and says nothing, and _invalid_utf8 below would stop reaching STDERR |
| 19 |
# the way it does when these run as class methods. Warn instead, so the caller |
| 20 |
# still gets to see what went 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 |
sub from_json { __PACKAGE__->new( log_cb => \&_warn_log_cb )->deserialize(@_) } |
| 34 |
|
| 35 |
sub to_json { __PACKAGE__->new( log_cb => \&_warn_log_cb )->serialize(@_) } |
| 36 |
|
| 37 |
sub decode_json { |
| 38 |
my ( $entity ) = @_; |
| 39 |
|
| 40 |
JSON::MaybeXS::decode_json($entity); |
| 41 |
} |
| 42 |
|
| 43 |
sub encode_json { |
| 44 |
my ( $entity ) = @_; |
| 45 |
|
| 46 |
JSON::MaybeXS::encode_json(_ensure_characters($entity)); |
| 47 |
} |
| 48 |
|
| 49 |
# class definition |
| 50 |
sub serialize { |
| 51 |
my ( $self, $entity, $options ) = @_; |
| 52 |
|
| 53 |
my $config = blessed $self ? $self->config : {}; |
| 54 |
my $strict_utf8 = $config->{strict_utf8}; |
| 55 |
$options ||= {}; |
| 56 |
|
| 57 |
foreach (keys %$config) { |
| 58 |
$options->{$_} = $config->{$_} unless exists $options->{$_}; |
| 59 |
} |
| 60 |
|
| 61 |
$options->{utf8} = 1; |
| 62 |
exists $options->{strict_utf8} |
| 63 |
and $strict_utf8 = delete $options->{strict_utf8}; |
| 64 |
$entity = _ensure_characters( $entity, $strict_utf8, $self ); |
| 65 |
JSON::MaybeXS->new($options)->encode($entity); |
| 66 |
} |
| 67 |
|
| 68 |
sub deserialize { |
| 69 |
my ( $self, $entity, $options ) = @_; |
| 70 |
|
| 71 |
$options ||= {}; |
| 72 |
$options->{utf8} = 1; |
| 73 |
delete $options->{strict_utf8}; |
| 74 |
JSON::MaybeXS->new($options)->decode($entity); |
| 75 |
} |
| 76 |
|
| 77 |
my $HAS_UNICODE_UTF8 = eval { require Unicode::UTF8; 1; }; |
| 78 |
|
| 79 |
sub _valid_utf8 { |
| 80 |
my ($bytes) = @_; |
| 81 |
return Unicode::UTF8::valid_utf8($bytes) if $HAS_UNICODE_UTF8; |
| 82 |
return eval { decode( 'UTF-8', $bytes, FB_CROAK ); 1 }; |
| 83 |
} |
| 84 |
|
| 85 |
sub _decode_utf8 { |
| 86 |
my ($bytes) = @_; |
| 87 |
return Unicode::UTF8::decode_utf8($bytes) if $HAS_UNICODE_UTF8; |
| 88 |
return decode( 'UTF-8', $bytes ); |
| 89 |
} |
| 90 |
|
| 91 |
sub _ensure_characters { |
| 92 |
my ( $entity, $strict_utf8, $self ) = @_; |
| 93 |
|
| 94 |
return $entity if !defined $entity; |
| 95 |
return _ensure_scalar( $entity, $strict_utf8, $self ) if !ref $entity; |
| 96 |
|
| 97 |
if ( is_plain_arrayref($entity) ) { |
| 98 |
for my $i ( 0 .. $#{$entity} ) { |
| 99 |
$entity->[$i] = _ensure_characters( $entity->[$i], $strict_utf8, $self ); |
| 100 |
} |
| 101 |
return $entity; |
| 102 |
} |
| 103 |
|
| 104 |
if ( is_plain_hashref($entity) ) { |
| 105 |
my %ret; |
| 106 |
for my $key ( keys %{$entity} ) { |
| 107 |
my $value = $entity->{$key}; |
| 108 |
my $decoded_key = _ensure_scalar( $key, $strict_utf8, $self ); |
| 109 |
my $decoded_value = |
| 110 |
_ensure_characters( $value, $strict_utf8, $self ); |
| 111 |
|
| 112 |
if ( $decoded_key ne $key ) { |
| 113 |
delete $entity->{$key}; |
| 114 |
$ret{$decoded_key} = $decoded_value; |
| 115 |
} else { |
| 116 |
$ret{$key} = $decoded_value; |
| 117 |
} |
| 118 |
} |
| 119 |
return \%ret; |
| 120 |
} |
| 121 |
|
| 122 |
return $entity; |
| 123 |
} |
| 124 |
|
| 125 |
sub _ensure_scalar { |
| 126 |
my ( $value, $strict_utf8, $self ) = @_; |
| 127 |
|
| 128 |
return $value if utf8::is_utf8($value); |
| 129 |
return $value if $value !~ /[\x80-\xFF]/; |
| 130 |
return _decode_utf8($value) if _valid_utf8($value); |
| 131 |
|
| 132 |
_invalid_utf8( $strict_utf8, $self ); |
| 133 |
return $value; |
| 134 |
} |
| 135 |
|
| 136 |
sub _invalid_utf8 { |
| 137 |
my ( $strict_utf8, $self ) = @_; |
| 138 |
my $msg = 'Invalid UTF-8 in JSON data'; |
| 139 |
|
| 140 |
$strict_utf8 |
| 141 |
and die "$msg\n"; |
| 142 |
|
| 143 |
if ( blessed($self) ) { |
| 144 |
$self->log_cb->( warning => "$msg; leaving bytes unchanged" ); |
| 145 |
} else { |
| 146 |
warn "$msg; leaving bytes unchanged\n"; |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
1; |
| 151 |
|
| 152 |
__END__ |
| 153 |
|
| 154 |
=pod |
| 155 |
|
| 156 |
=encoding UTF-8 |
| 157 |
|
| 158 |
=head1 NAME |
| 159 |
|
| 160 |
Dancer2::Serializer::JSON - Serializer for handling JSON data |
| 161 |
|
| 162 |
=head1 VERSION |
| 163 |
|
| 164 |
version 2.2.2 |
| 165 |
|
| 166 |
=head1 DESCRIPTION |
| 167 |
|
| 168 |
This is a serializer engine that allows you to turn Perl data structures into |
| 169 |
JSON output and vice-versa. |
| 170 |
|
| 171 |
=head1 ATTRIBUTES |
| 172 |
|
| 173 |
=head2 content_type |
| 174 |
|
| 175 |
Returns 'application/json' |
| 176 |
|
| 177 |
=head1 METHODS |
| 178 |
|
| 179 |
=head2 serialize($content) |
| 180 |
|
| 181 |
Serializes a Perl data structure into a JSON string. |
| 182 |
|
| 183 |
=head2 deserialize($content) |
| 184 |
|
| 185 |
Deserializes a JSON string into a Perl data structure. |
| 186 |
|
| 187 |
=head1 FUNCTIONS |
| 188 |
|
| 189 |
=head2 from_json($content, \%options) |
| 190 |
|
| 191 |
This is an helper available to transform a JSON data structure to a Perl data structures. |
| 192 |
|
| 193 |
=head2 to_json($content, \%options) |
| 194 |
|
| 195 |
This is an helper available to transform a Perl data structure to JSON. |
| 196 |
|
| 197 |
Calling this function will B<not> trigger the serialization's hooks. |
| 198 |
|
| 199 |
=head2 Configuring the JSON Serializer using C<set engines> |
| 200 |
|
| 201 |
The JSON serializer options can be configured via C<set engines>. The most |
| 202 |
common settings are: |
| 203 |
|
| 204 |
=over 4 |
| 205 |
|
| 206 |
=item allow_nonref |
| 207 |
|
| 208 |
Ignore non-ref scalars returned from handlers. With this set the "Hello, World!" |
| 209 |
handler returning a string will be dealt with properly. |
| 210 |
|
| 211 |
=item strict_utf8 |
| 212 |
|
| 213 |
If true, invalid UTF-8 bytes in data passed to the JSON encoder will cause an |
| 214 |
error. If false (default), invalid bytes are left as-is and a warning is logged. |
| 215 |
|
| 216 |
=back |
| 217 |
|
| 218 |
Note: the C<utf8> option is forced to true internally to ensure JSON output |
| 219 |
is UTF-8 encoded bytes. |
| 220 |
|
| 221 |
Set engines should be called prior to setting JSON as the serializer: |
| 222 |
|
| 223 |
set engines => |
| 224 |
{ |
| 225 |
serializer => |
| 226 |
{ |
| 227 |
JSON => |
| 228 |
{ |
| 229 |
allow_nonref => 1 |
| 230 |
}, |
| 231 |
} |
| 232 |
}; |
| 233 |
|
| 234 |
set serializer => 'JSON'; |
| 235 |
set content_type => 'application/json'; |
| 236 |
|
| 237 |
=head2 Returning non-JSON data. |
| 238 |
|
| 239 |
Handlers can return non-JSON via C<send_as>, which overrides the default serializer: |
| 240 |
|
| 241 |
get '/' => |
| 242 |
sub |
| 243 |
{ |
| 244 |
send_as html => |
| 245 |
q{Welcome to the root of all evil...<br>step into my office.} |
| 246 |
}; |
| 247 |
|
| 248 |
Any other non-JSON returned format supported by 'send_as' can be used. |
| 249 |
|
| 250 |
=head1 AUTHOR |
| 251 |
|
| 252 |
Dancer Core Developers |
| 253 |
|
| 254 |
=head1 COPYRIGHT AND LICENSE |
| 255 |
|
| 256 |
This software is copyright (c) 2026 by Alexis Sukrieh. |
| 257 |
|
| 258 |
This is free software; you can redistribute it and/or modify it under |
| 259 |
the same terms as the Perl 5 programming language system itself. |
| 260 |
|
| 261 |
=cut |