Skip Menu |

This queue is for tickets about the Test-JSON CPAN distribution.

Report information
The Basics
Id: 85113
Status: open
Priority: 0/
Queue: Test-JSON

People
Owner: Nobody in particular
Requestors: bniessen [...] 3taps.com
Cc:
AdminCc:

Bug Information
Severity: (no value)
Broken in: (no value)
Fixed in: (no value)



Subject: Response Code.
Date: Tue, 07 May 2013 09:49:20 -0400
To: bug-Test-JSON [...] rt.cpan.org
From: Brian Niessen <bniessen [...] 3taps.com>
Good Day, Is there a way to stop is_valid_json() from printing "ok" or the error message each time it tests? Thanks, Brian
Show quoted text
> Is there a way to stop is_valid_json() from printing "ok" or the > error message each time it tests?
Hi Brian, Could you explain your use case for me? Outputting the "ok" or "error message" in TAP is the reason for this module. Cheers, Ovid
Subject: Re: [rt.cpan.org #85113] Response Code.
Date: Tue, 07 May 2013 10:07:47 -0400
To: bug-Test-JSON [...] rt.cpan.org
From: Brian Niessen <bniessen [...] 3taps.com>
Hi Ovid.

I am using it in a program to test the validity of the JSON object prior to using json->decode (which dies).

eg:

if (is_valid_json($data[$mythreadnum]))
                                   {
                                    $js=$json->decode($data[$mythreadnum]) or die()."\n\n\n\n";;
                                   }
                          else
                                   {
                                   print "\007PUKED ON JSON\n\n".$data[$mythreadnum]."\n\n";
                                    $hide_record->execute($id);
                                   exit;
                                   }


The reason for this is that some time, the server I am talking to (Google API) returns bad JSON  (quotes in strings, etc.)

Thanks,
Bri


At 2013/05/07 09:54 AM, you wrote:
Show quoted text
<URL: https://rt.cpan.org/Ticket/Display.html?id=85113 >

> Is there a way to stop is_valid_json() from printing "ok" or the
> error message each time it tests?

Hi Brian,

Could you explain your use case for me? Outputting the "ok" or "error message" in TAP is the reason for this module.

Cheers,
Ovid
On Tue May 07 10:06:58 2013, bniessen@3taps.com wrote: Show quoted text
> Hi Ovid. > > I am using it in a program to test the validity of the JSON object > prior to > using json->decode (which dies). > > eg: > > if (is_valid_json($data[$mythreadnum])) > { > $js=$json->decode($data[$mythreadnum]) or die()."\n\n\n\n";; > } > else > { > print "\007PUKED ON JSON\n\n".$data[$mythreadnum]."\n\n"; > $hide_record->execute($id); > exit; > }
OK, it's a common issue with people trying to push test code into their production code. You generally want to avoid that because test code is generally not designed to function that way. In this case, if you look at is_valid_json(), you'll see that it merely wraps json->decode in an eval and tests $@. That's the solution you can consider, or perhaps: use Try::Tiny; my $js; try { $js = $json->decode($data[$mythreadnum]); } catch { my $error = $_; print "\007PUKED ON JSON with error ($error)\n\n".$data[$mythreadnum]."\n\n"; $hide_record->execute($id); exit; }; Cheers, Ovid
Subject: Re: [rt.cpan.org #85113] Response Code.
Date: Tue, 07 May 2013 10:22:17 -0400
To: bug-Test-JSON [...] rt.cpan.org
From: Brian Niessen <bniessen [...] 3taps.com>
Thanks Ovid... Yes, I agree with the test/production... but this is still 'dev' code... and one expects google to provide good json :) Thanks again, Cheers, Brian At 2013/05/07 10:18 AM, you wrote: Show quoted text
><URL: https://rt.cpan.org/Ticket/Display.html?id=85113 > > >On Tue May 07 10:06:58 2013, bniessen@3taps.com wrote:
> > Hi Ovid. > > > > I am using it in a program to test the validity of the JSON object > > prior to > > using json->decode (which dies). > > > > eg: > > > > if (is_valid_json($data[$mythreadnum])) > > { > > $js=$json->decode($data[$mythreadnum]) or die()."\n\n\n\n";; > > } > > else > > { > > print "\007PUKED ON JSON\n\n".$data[$mythreadnum]."\n\n"; > > $hide_record->execute($id); > > exit; > > }
> >OK, it's a common issue with people trying to push test code into >their production code. You generally want to avoid that because test >code is generally not designed to function that way. > >In this case, if you look at is_valid_json(), you'll see that it >merely wraps json->decode in an eval and tests $@. That's the >solution you can consider, or perhaps: > > use Try::Tiny; > my $js; > try { > $js = $json->decode($data[$mythreadnum]); > } > catch { > my $error = $_; > print "\007PUKED ON JSON with error > ($error)\n\n".$data[$mythreadnum]."\n\n"; > $hide_record->execute($id); > exit; > }; > >Cheers, >Ovid