in reply to Re: Outputting JSON with function () {...} values
in thread Outputting JSON with function () {...} values

I've been looking at JSON "things" if only to be able to figure out a taxonomy for them. I get the same output as haukex does from the preceding script, but I would think they this should be decodable as JSON. Instead, I get an error:

$ $ ./1.js.pl {"baz":function () { /* some JS here? */ },"foo":["bar","quz"]} $ ./1.json.pl 'false' expected, at character offset 7 (before "function () { /* som. +..") at ./1.json.pl line 7, <DATA> line 1. $ cat 1.json.pl #!/usr/bin/perl -w use 5.011; use Data::Dumper; use JSON; my $json = <DATA>; my $perl = decode_json $json; print Dumper $perl; __DATA__ {"baz":function () { /* some JS here? */ },"foo":["bar","quz"]} $

Replies are listed 'Best First'.
Re^3: Outputting JSON with function () {...} values
by haukex (Archbishop) on Oct 25, 2018 at 07:51 UTC
    this should be decodable as JSON

    Note that JSON is only a subset of JavaScript, you can see the grammar e.g. here - it doesn't even allow comments. That's why Perl's most common JSON modules, or browsers, won't handle it. Although the example code can be parsed with JavaScript's eval, of course that's a huge security hole because it allows for arbitrary code execution, which is one of the reasons why JSON was invented in the first place.

Re^3: Outputting JSON with function () {...} values
by LanX (Saint) on Oct 25, 2018 at 06:56 UTC
    > should be decodable as JSON

    The OP didn't ask for real JSON but a nested JS data structure, which is an upper set.

    This can't be decoded, Perl doesn't have a builtin JS parser (yet).

    The only way I see is to serialize objects like this function as string - hence lethal JSON - and to have a conversion filter on the JS side before using it (here with eval)

    Though I'm not sure about the best way to mark such objects to be distinguished from normal strings.

    See also point 3

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice