Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) 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:

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


In reply to Re^3: Json::PP data error by davido
in thread Json::PP data error by IB2017

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found