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

Latest JSON::Parse (mine is 0.40) has a warn_only() function which does supposedly not let program die but return undef. How do I use this feature?
use JSON::Parse; my $JP = JSON::Parse->new(); $JP->warn_only(); $JP->warn_only(1);
or even
JSON::Parse->warn_only($JP, 'on');
it gives me:
Usage: JSON::Parse::warn_only(parser, onoff)
or:
JSON::Parse::warn_only: parser is not of type JSON::Parse
EDIT: note: the following does not complain but has no effect i can notice of.
$j->warn_only(1);
Below is a perl snippet to demonstrate the issue:
use strict; use warnings; use JSON::Parse 0.38; use Data::Dumper; my $j = JSON::Parse->new(); # no complain, no effect: $j->warn_only(1); # legal json: my $pl = $j->run('{"k":"v"}'); print Dumper($pl); print "I am not dead 1\n"; # illegal json, the following statement dies: $pl = $j->run('illegal json'); print Dumper($pl); print "I am not dead 2\n";
Results:
$VAR1 = { 'k' => 'v' }; I am not dead 1 JSON error at line 1, byte 1/12: blah blah blah at which point it has +died.
many thanks monks.

Replies are listed 'Best First'.
Re: JSON::Parse how to warn_only()?
by stevieb (Canon) on Jun 11, 2016 at 15:44 UTC

    I've tried it as well, with the same results. This may be a bug. Perhaps if more experienced XS Monks have a look and confirm, you could open a ticket.

    I thought if I looked through the test files for an example, that'd help, but there don't seem to be any tests at all for this feature:

    $ cd JSON-Parse-0.40 $ grep -r "warn_only" * Json3.xs:warn_only (parser, onoff) Json3.xs: parser->warn_only = SvTRUE (onoff) ? 1 : 0; json-common.c: unsigned int warn_only : 1; json-entry-points.c: parser_o.warn_only = 1; lib/JSON/Parse.pod:=head2 warn_only

    Relevant code in Json3.xs:

    void warn_only (parser, onoff) JSON::Parse parser; SV * onoff; CODE: parser->warn_only = SvTRUE (onoff) ? 1 : 0;
Re: JSON::Parse how to warn_only()?
by stevieb (Canon) on Jun 12, 2016 at 15:07 UTC

    Decided not to wait, and went ahead and opened a bug ticket.

    If I've missed something, I've missed something.

Re: JSON::Parse how to warn_only()?
by Anonymous Monk on Jun 11, 2016 at 17:42 UTC

    This works for me:

    use JSON::Parse; my $j = JSON::Parse->new(); $j->warn_only(1);

      I concur that this works, unfortunately, it doesn't affect the parsing functions. If you treat the functions as object methods, things break, because the functions aren't expecting the object as its first parameter.

      It seems that there's a discrepancy between the docs and the functionality (appears to be both, but I'm not positive). If nobody else can confirm by EOD today, I'll open a bug report (if it's not done by then), and simply link to this thread as it has all of the relevant information.

      I don't use JSON::Parse (I use JSON), but a possible alternative would be to use eval:

      my $perl; my $ok = eval { $perl = parse_json('[x]'); 1; }; if (! $ok){ warn "json load failed...\n"; ...; # do something else }
Re: JSON::Parse how to warn_only()?
by ikegami (Patriarch) on Jun 14, 2016 at 16:55 UTC

    You are getting the following error message:

    Usage: JSON::Parse::warn_only(parser, onoff)

    It indicates that you need to use the following syntax:

    my $jp = JSON::Parse->new(); JSON::Parse::warn_only($jp, 1);

    The above simplifies to the following:

    my $jp = JSON::Parse->new(); $jp->warn_only(1);

    To parse the JSON, you need to use that $jp object, so you can't parse_json. You need to use run instead.

    my $data = $jp->run($json);

    That's why warn_only is documented as a method that can be used to modify run.

      I have done all these you describe. thanks, bliako
        You are using 0.40, but warn_only was only implemented in 0.41.
Re: JSON::Parse how to warn_only()?
by stevieb (Canon) on Jun 14, 2016 at 13:41 UTC

    Apparently, the author has fixed this in the ticket. I have not tested it.

      ok thanks,