Perl Folks,

>> I need to call a JSON web service with dates.  ...  
>> '/Date(1392233750000-0600)/'

> As far as I am aware, JSON supports auto-serialization of a blessed object 
> only if allow_blessed => 1 and convert_blessed => 1.
 
Just to follow up on JSON Dates.  So, after much research I've found out that 
this JSON date format is called the MicrosoftDateFormat.  It was only used in 
JSON.NET framework 4.5 and is now a defunct standard in favor of ISO standard 
for dates.
 
So, I went ahead and bit the bullet and wrote a DateTime formatter.  I've 
called it DateTime::Format::JSON::MicrosoftDateFormat.
 
But, the API is just like any other date time formatter object except that it 
optionally loads the DateTime::TO_JSON subroutine in the import section of the 
package.
 
    *DateTime::TO_JSON=sub {shift->_stringify};

So, JSON serialization of DateTime objects will be automatic.  De-serialization 
is still manual at this time as there appears to be no way to identify the date 
data without scanning all values.  I'll most likely implement some kind of 
object key de-serialization mechanism.
Thanks,
Mike
 
Example Script
 
perl -e '
use strict;
use warnings;
use JSON;
use DateTime;
use DateTime::Format::JSON::MicrosoftDateFormat (to_json=>1);
 
my $formatter=DateTime::Format::JSON::MicrosoftDateFormat->new;
 
print $formatter->parse_datetime("/Date(1392606509000)/"),      "\n";
print $formatter->parse_datetime("/Date(1392089278000-0600)/"), "\n";
print $formatter->parse_datetime("/Date(1392067678000)/"),      "\n";
 
my $dt=DateTime->now;
$dt->set_formatter($formatter); #we still need to set the formatter
print "$dt\n";                  #"" overloaded
print JSON->new->convert_blessed->encode($dt), "\n"; #JSON calls 
DateTime->TO_JSON
'
2014-02-17T03:08:29
2014-02-10T21:27:58
2014-02-10T21:27:58
/Date(1392666175000)/
"/Date(1392666175000)/"
_______________________________________________
Houston mailing list
[email protected]
http://mail.pm.org/mailman/listinfo/houston
Website: http://houston.pm.org/

Reply via email to