You cannot do this without verification. The simplest approach is to simply decode and handle the exception.
use JSON::Any;
use Try::Tiny;
my $perl_data;
for my $perhaps_json (
q(this won't decode), q({"how":"ever", "this":"will"}),
) {
try {
$perl_data = JSON::Any->jsonToObj($perhaps_json);
} catch {
warn "decoding failed: $_\n";
}
}
say "Even with invalid input, I did not crash!";
__END__
decoding failed: 'true' expected, at character offset 0 (before "this won't decode") at .../lib/perl5/site_perl/5.10.1/JSON/Any.pm line 529.
Even with invalid input, I did not crash!
source
share