IB2017 has asked for the wisdom of the Perl Monks concerning the following question:

Hello

I send data to a cgi-bin script in JSON format using JSON::PP

my $url= $Server . "/cgi-bin/DB_NewTerm.pl"; my $data = { record => \@record}; my $request = POST($url, Content_Type=>'application/json', Content => encode_json($data) ); my $response = $ua->request($request); print $response->content, "\n";

One of the element of my @record is a date. And here seems to be the problem, since I get the following error:

Tk::Error: encountered object '2019-12-23', but neither allow_blessed, convert_blessed nor allow_tags settings are enabled (or TO_JSON/FREEZE method missing)

Why is this data considered an issue?

Edit

This produces the error:

my @record=( $Tag1, $Tag2, $CommentAll, $today );

However, the following does not:

my @record=( $Tag1, $Tag2, $CommentAll, "$today" );

(Being $date the variable containing the date)

Replies are listed 'Best First'.
Re: Json::PP data error
by stevieb (Canon) on Dec 23, 2019 at 22:47 UTC

    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.

      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