Skip to content

Commit 25176c5

Browse files
committed
Merge branch '1784-json-serializer-decode-hashref'
2 parents cebd677 + ba1d90a commit 25176c5

3 files changed

Lines changed: 16 additions & 10 deletions

File tree

Changes

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
{{$NEXT}}
22

33
[ BUG FIXES ]
4+
* GH #1784: Fix UTF-8 handling in Serializer::JSON for readonly
5+
values (Russell @veryrusty Jenkins)
6+
* GH #1790: Fix infinite recursion into blessed objects in JSON
7+
Serializer (Russell @veryrusty Jenkins)
48
* PR #1791: Send correct error codes in send_file (Anton Lundin)
59

610
[ ENHANCEMENTS ]

lib/Dancer2/Serializer/JSON.pm

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package Dancer2::Serializer::JSON;
22
# ABSTRACT: Serializer for handling JSON data
33

44
use Moo;
5-
use Ref::Util qw< is_arrayref is_hashref >;
5+
use Ref::Util qw< is_plain_arrayref is_plain_hashref >;
66
use JSON::MaybeXS ();
77
use Encode qw(decode FB_CROAK);
88
use Scalar::Util 'blessed';
@@ -76,14 +76,15 @@ sub _ensure_characters {
7676
return $entity if !defined $entity;
7777
return _ensure_scalar( $entity, $strict_utf8, $self ) if !ref $entity;
7878

79-
if ( is_arrayref($entity) ) {
79+
if ( is_plain_arrayref($entity) ) {
8080
for my $i ( 0 .. $#{$entity} ) {
8181
$entity->[$i] = _ensure_characters( $entity->[$i], $strict_utf8, $self );
8282
}
8383
return $entity;
8484
}
8585

86-
if ( is_hashref($entity) ) {
86+
if ( is_plain_hashref($entity) ) {
87+
my %ret;
8788
for my $key ( keys %{$entity} ) {
8889
my $value = $entity->{$key};
8990
my $decoded_key = _ensure_scalar( $key, $strict_utf8, $self );
@@ -92,12 +93,12 @@ sub _ensure_characters {
9293

9394
if ( $decoded_key ne $key ) {
9495
delete $entity->{$key};
95-
$entity->{$decoded_key} = $decoded_value;
96+
$ret{$decoded_key} = $decoded_value;
9697
} else {
97-
$entity->{$key} = $decoded_value;
98+
$ret{$key} = $decoded_value;
9899
}
99100
}
100-
return $entity;
101+
return \%ret;
101102
}
102103

103104
return $entity;

t/serializer_json.t

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ use Dancer2::Serializer::JSON;
1818
set engines => {
1919
serializer => {
2020
JSON => {
21-
pretty => 1,
21+
canonical => 1,
22+
pretty => 1,
2223
}
2324
}
2425
};
@@ -31,16 +32,16 @@ use Dancer2::Serializer::JSON;
3132

3233
my @tests = (
3334
{ entity => { a => 1, b => 2, },
34-
options => { pretty => 1 },
35+
options => { canonical => 1, pretty => 1 },
3536
name => "basic hash",
3637
},
3738
{ entity =>
3839
{ c => [ { d => 3, e => { f => 4, g => 'word', } } ], h => 6 },
39-
options => { pretty => 1 },
40+
options => { canonical => 1, pretty => 1 },
4041
name => "nested",
4142
},
4243
{ entity => { data => "\x{2620}" x 10 },
43-
options => { pretty => 1, utf8 => 1 },
44+
options => { canonical => 1, pretty => 1, utf8 => 1 },
4445
name => "utf8",
4546
}
4647
);

0 commit comments

Comments
 (0)