Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Json::PP data error

by stevieb (Canon)
on Dec 23, 2019 at 22:47 UTC ( [id://11110566]=note: print w/replies, xml ) Need Help??


in reply to Json::PP data error

What's in $today? It seems to be an object of some sort, not a string, which may explain why putting the double-quotes around it allows it to be processed.

This works:

use warnings; use strict; use Data::Dumper; use JSON; my $today = '2019-12-23'; my @array = ($today); my $j = encode_json \@array; my $p = decode_json $j; print Dumper $p;

But changing the $today assignment to:

my $today = DateTime->now;

...results in:

encountered object '2019-12-23T22:45:49', but neither allow_blessed, c +onvert_blessed nor allow_tags settings are enabled (or TO_JSON/FREEZE + method missing) at date.pl line 12.

Do a print ref $today; within the "working" version of the script to understand what type of object $today is, and we'll help you figure out if there's a more proper way to stringify the object.

Replies are listed 'Best First'.
Re^2: Json::PP data error
by IB2017 (Pilgrim) on Dec 23, 2019 at 22:58 UTC

    Perfect! I thougth it was a string, but at closer examination it is a Date::Simple ref. Everything clear now!

      Date::Simple uses overload to cause the Date::Simple object to act appropriately according to the operators it's used with. So where the object, if not using overload, would stringify as something like Date::Simple=HASH(0x556b93cb2780), because stringification has been overloaded, the handler converts it to a date stamp.

      However, JSON::PP uses ref to determine how to handle the structure being JSONified. If ref returns a hash, then a JSON hash is created. If ref returns an array, then a JSON array is created. If ref returns an object of some type, then JSON::PP looks for a TO_JSON method in that object's class for the object to know how to JSON encode it. If there isn't one, you get the error you've seen. It's just an impedance mismatch, and can be dealt with by forcing the object to the format you need. In this case wrapping it in quotes will use the overloaded stringification, which does what you want.

      Alternatively you could subclass Date::Simple and provide your own TO_JSON method that munges the object as you wish. Something like this:

      package Date::Simple::JSONable; use parent 'Date::Simple'; sub TO_JSON { my $self = shift; return "$self"; } 1;

      The problem with this, though, is if the Date::Simple object is created by another library. That won't give you an easy opportunity to prevent that library from just creating a Date::Simple object rather than your Date::Simple::JSONable. If that's the case you now find yourself needing to subclass that library. And that's probably too much work.

      Another alternative is to provide your own TO_JSON method to Date::Simple:

      package main; use strict; use warnings; use Module::That::Instantiates::A::Date::Simple; sub Date::Simple::TO_JSON { return "$_[0]"; } # The rest of your code here...

      Now you're just injecting a TO_JSON method into Date::Simple so that when JSON::PP needs one, it's there.


      Dave

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11110566]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-04-25 17:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found