How to find the difference between plain text and JSON encoded text in Perl?

I used JSON :: Any in my program to transfer the hash between the client and server.

I had one problem: I want to find if the text (sent by the client) is plain text or JSON-encoded text.

Can someone tell me how to find

without checking, i got some server side error and closed.

+3
source share
2 answers

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!
+4
source

, JSON HTTP, JSON MIME. , MIME. , MIME.

- text/plain , daxim ' ' .

+1

Source: https://habr.com/ru/post/1739956/


All Articles