Both JSON servers (JSON :: PP and JSON :: XS) base the output type on the internal memory of the value. The solution is to reinforce the incorrect scalars in your data structure.
sub recursive_inplace_stringification {
my $reftype = ref($_[0]);
if (!length($reftype)) {
$_[0] = "$_[0]" if defined($_[0]);
}
elsif ($reftype eq 'ARRAY') {
recursive_inplace_stringification($_) for @{ $_[0] };
}
elsif ($reftype eq 'HASH') {
recursive_inplace_stringification($_) for values %{ $_[0] };
}
else {
die("Unsupported reference to $reftype\n");
}
}
recursive_inplace_stringification($hash);
my $json = JSON->new->allow_nonref->utf8->encode($hash);
, allow_unknown allow_blessed, recursive_inplace_stringification (, JSON:: PP, ), recursive_inplace_stringification:
$hash = JSON->new->allow_nonref->decode(
JSON->new->allow_nonref->allow_unknown->allow_blessed->encode(
$hash));