dim*_*hus 4 perl json boolean decoding
包 JSON::XS 使用 JSON::XS::Boolean 对象来表示真/假。是否可以将真/假 json 值强制解码为 1/0 Perl 数字?
#!/usr/bin/env perl
use JSON::XS;
use Data::Dumper;
my $json = decode_json(join('', <DATA>));
print Dumper $json;
__DATA__
{
"test_true": true,
"test_false": false
}
Run Code Online (Sandbox Code Playgroud)
输出:
$VAR1 = {
'test_true' => bless( do{\(my $o = 1)}, 'JSON::XS::Boolean' ),
'test_false' => bless( do{\(my $o = 0)}, 'JSON::XS::Boolean' )
};
Run Code Online (Sandbox Code Playgroud)
在 decode_json 之后我想要这样的东西:
$VAR1 = {
'test_true' => 1,
'test_false' => 0
};
Run Code Online (Sandbox Code Playgroud)
原因:在某些情况下,很难预测 JSON::XS::Boolean 将如何使用 SOAP 序列化程序或其他序列化程序进行序列化。