Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improper handling of NULL and number values #6

Open
Laz80UK opened this issue Mar 21, 2019 · 0 comments
Open

Improper handling of NULL and number values #6

Laz80UK opened this issue Mar 21, 2019 · 0 comments

Comments

@Laz80UK
Copy link

Laz80UK commented Mar 21, 2019

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant