Subject: | json encoding error for DateTime objects with timezone at 4.01 |
Date: | Tue, 26 Feb 2019 13:36:31 +0100 |
To: | bug-JSON-XS [...] rt.cpan.org |
From: | Nicolás de los Santos <nicolas.delossantos [...] meteologica.com> |
Hi,
I've noticed that since the upgrade to JSON::XS 4.01 a piece of code that
transforms DateTime objects to JSON format (while applying a change in the
timezone) is broken. I realize doing this is unorthodox but it used to work.
This code snippet reproduces the error:
#!/usr/bin/env perl
use strict;
use warnings;
use utf8;
use JSON;
use JSON::XS;
use DateTime;
sub DateTime::TO_JSON {
my $self = shift;
$self->set_time_zone('Europe/Madrid');
return $self->strftime('%FT%T%z');
}
my $serializer = JSON->new->convert_blessed();
my $dt = DateTime->new( year => 2000, month => 1, day => 1, time_zone =>
'UTC' );
my $json = $serializer->encode({ dt => $dt });
print $json, "\n";
You can run it using docker with version 4.00 and 4.01 and see the
difference. The 4.01 version produces an uninitialized error, but the old
one produces a string. You can reproduce it running the following commands
(assuming you named the file json_xs_error.pl and are on the folder where
you created the script):
*4.01 version*
$> docker run --rm -v "$PWD":/usr/src/app:ro -w /usr/src/app perl:5.24
/bin/bash -c 'cpanm JSON::XS@4.01 JSON DateTime && perl json_xs_error.pl'
.....
Use of uninitialized value $json in print at error_json_xs.pl line 19
*4.00 version*
$> docker run --rm -v "$PWD":/usr/src/app:ro -w /usr/src/app perl:5.24
/bin/bash -c 'cpanm JSON::XS@4.00 JSON DateTime && perl json_xs_error.pl'
.....
{"dt":"2000-01-01T01:00:00+0100"}
Thanks in advance for your efforts.
Regards
--
==========================================
Nicolás De los Santos Cicutto
Meteologica S.A.
C. Costa Brava, 10
<https://maps.google.com/?q=C.+Costa+Brava,+10+28034+Madrid&entry=gmail&source=g>
28034 Madrid
<https://maps.google.com/?q=C.+Costa+Brava,+10+28034+Madrid&entry=gmail&source=g>
Tel. +34 914 561 001 ext. 302
Fax. +34 914 561 002
nicolas.delossantos@meteologica.com <pablo.rodriguez@meteologica.com>
www.meteologica.com
==========================================