Skip to content

Improper handling of NULL and number values #6

@Laz80UK

Description

@Laz80UK

Hi,

The module fails to produce JSON compliant output when it encounters NULL and number (integer/float) values.
One of the problems seems to be that the Log::Log4perl::Layout::PatternLayout module automatically turns undef to a string ("[undef]").
The other is probably a Perl autovivification issue; numbers are represented as strings.
Impacted version is 0.56.

Output generated:

{"level":"DEBUG","location_id":"[undef]","timestamp":"2019-03-20 12:31:45.544","pid":"25388","program":"test","message":"Database connection object created"}
{"level":"INFO","location_id":"[undef]","timestamp":"2019-03-20 16:51:36.152","pid":"25388","program":"test","message":"Signal received, quitting..."}
{"level":"INFO","location_id":"[undef]","timestamp":"2019-03-20 16:51:36.153","pid":"25388","program":"test","message":"Main program finished"}

I could manage to patch this by adding:

my $fields_to_encode = { @fields };
%$fields_to_encode = map { (defined($fields_to_encode->{$_}) and ($fields_to_encode->{$_} =~ m/^\d+$/s or $fields_to_encode->{$_} =~ m/^\d+\.\d+$/s)) ? ($_ => $fields_to_encode->{$_}+0) : ($_ => $fields_to_encode->{$_}) } (keys %$fields_to_encode);
%$fields_to_encode = map { (defined($fields_to_encode->{$_}) and $fields_to_encode->{$_} =~ m/^\[undef\]$/s) ? ($_ => undef) : ($_ => $fields_to_encode->{$_}) } (keys %$fields_to_encode);
$json = $self->codec->encode($fields_to_encode);
# $json = $self->codec->encode(+{ @fields });

which produces the expected output:

{"level":"INFO","location_id":null,"timestamp":"2019-03-20 16:51:37.137","pid":26290,"program":"test","message":"Main program started"}
{"level":"DEBUG","location_id":null,"timestamp":"2019-03-20 16:51:37.158","pid":26290,"program":"test","message":"Database connection object created"}

It only seems to be working however using Cpanel::JSON::XS, but not with JSON::XS.

Could this be looked into please?
Many thanks.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions