[pm-h] JSON Dates and DateTime

Mark Allen mrallen1 at yahoo.com
Thu Feb 13 13:40:45 PST 2014


As far as I am aware, JSON supports auto-serialization of a blessed object only if allow_blessed => 1 and convert_blessed => 1.

See https://metacpan.org/pod/JSON::PP#allow_blessed

Then in your class you need a method called TO_JSON which JSON will (attempt to) call when it encounters it during serialization.  There's a semi-lame example here:

https://github.com/makamaka/JSON-PP/blob/master/lib/JSON/PP.pm#L2176-L2187


I have an example here, which might be more illustrative:

https://github.com/mrallen1/WebService-Geocodio/blob/master/lib/WebService/Geocodio/Location.pm#L88-L111


For deserialization you may use filter_json_object
     => https://metacpan.org/pod/JSON::PP#filter_json_object

but that may not be performant enough for your needs.




On Thursday, February 13, 2014 2:57 PM, Michael R. Davis <mrdvt92 at yahoo.com> wrote:
 
Perl Folks,
I need to call a JSON web service with dates.  The web service uses this format which I hear is a fairly well used JSON date format.  (Note: ISO 8601 is now the preferred format).

 '/Date(1392233750000-0600)/'

What I want to do is to pass DateTime objects to and from the Perl API and have it seamlessly translate to and from this milliseconds from epoch format.

Issues: 
  - I don't see a DateTime formatter that supports this format.  Maybe, I just don't know the name of the format.  But, I was expecting to find DateTime::Format::JSON::Epoch or something similar.
  - I did not see an easy way to use JSON::PP mapping to map objects to strings and back.
I'd hate to have to go through every value searching for strings that matched /Date()/ and convert them to DateTime format.  It's would be easy on the send side as I already have hooks in my code.
Has anyone does this kind of thing before?  I've searched but found nothing on the 'Net related to Perl.

Thanks,
Mike

_______________________________________________
Houston mailing list
Houston at pm.org
http://mail.pm.org/mailman/listinfo/houston
Website: http://houston.pm.org/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/mailman/private/houston/attachments/20140213/ac9d63a8/attachment-0003.html>


More information about the Houston mailing list