我在PostgreSQL系统上查询返回一个布尔值:
my $sth = $dbh->prepare("select 'f'::boolean");
$sth->execute;
my @vals = $sth->fetchrow_array;根据the DBD::Pg docs,
The current implementation of
PostgreSQL returns 't' for true and
'f' for false. From the Perl point of
view, this is a rather unfortunate
choice. DBD::Pg therefore translates
the result for the BOOL data type in a
Perlish manner: 'f' becomes the number
0 and 't' becomes the number 1. This
way the application does not have to
check the database-specific returned
values for the data-type BOOL because
Perl treats 0 as false and 1 as true.
You may set the pg_bool_tf attribute
to a true value to change the values
back to 't' and 'f' if you wish.
因此,只要pg_bool_tf返回0,那么该语句应该返回一个0,它会这样做。然而,在JSON :: XS(和纯JSON)方式的某处,将返回的0解释为一个字符串:
use JSON::XS qw(encode_json);
my $options =
{
layout => 0,
show_widget_help => $vals[0] // 1,
};
die encode_json($options);......与......一起死去
{ “布局”:0 “show_widget_help”: “0”}
...这将是好的,除了我的JavaScript期望在那里布尔值,并且非空字符串“0”被评估为真。为什么后者被引用而前者不是?
根据the JSON::XS docs,这是一个主要特点:
round-trip integrity
When you serialise a perl data
structure using only data types
supported by JSON, the deserialised
data structure is identical on the
Perl level. (e.g. the string "2.0"
doesn't suddenly become "2" just
because it looks like a number). There
minor are exceptions to this, read the
MAPPING section below to learn about
those.
说:
Simple Perl scalars (any scalar that
is not a reference) are the most
difficult objects to encode: JSON::XS
will encode undefined scalars as JSON
null values, scalars that have last
been used in a string context before
encoding as JSON strings, and anything
else as number value.
但是我从来没有在字符串上下文中使用@vals [0]。也许DBD :: Pg在返回之前将其布尔0作为字符串使用?