|go ahead... be a heretic|
Re^3: Json::PP data errorby davido (Cardinal)
|on Dec 24, 2019 at 04:48 UTC||Need Help??|
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:
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:
Now you're just injecting a TO_JSON method into Date::Simple so that when JSON::PP needs one, it's there.