use JSON::PP qw(encode_json);
my $json = JSON::PP->new->canonical->allow_nonref;
my $encoded = $json->encode($somehash);
# I would like to use the one-liner code below.
my $json = encode_json($somehash);
# Can I set properties like these? Which one is correct?
$JSON::PP::P_CANONICAL = 1;
$JSON::PP::P_ALLOW_NONREF = 1;
# or
$JSON::PP::canonical = 1;
$JSON::PP::allow_nonref = 1;我想使用简单的encode_json()函数。我们可以设置规范属性和allow_nonref属性吗?哪一个是对的?
发布于 2022-07-29 13:05:08
不是的。但没有什么能阻止你创造自己的潜艇。
sub my_encode_json {
return JSON::PP->new->canonical->allow_nonref->encode( $_[0] );
}或
sub my_encode_json {
state $encoder = JSON::PP->new->canonical->allow_nonref;
return $encoder->encode( $_[0] );
}https://stackoverflow.com/questions/73164763
复制相似问题