Re: [Boston.pm] perl data to/from JSON?
David Cantrell <[email protected]> Mon, 28 Jan 2019 10:51:04 +0000
| Newsgroups | gmane.comp.lang.perl.perl-mongers.boston |
|---|---|
| Message-ID | <[email protected]> |
On Fri, Jan 25, 2019 at 03:47:50PM -0500, Greg London wrote: > We have a number of perl and python scripts and we are looking to share > data between them. It was decided that we use a JSON file format to store > data and then have all the scripts read/write this format so it wouldn't > matter if the script was python or perl. > > I looked for JSON on CPAN and found... hundreds of matches??? > > It looks like JSON::PP is pure perl and does what I want. > https://metacpan.org/pod/JSON::PP > however, it says: > "JSON::PP is a pure perl JSON decoder/encoder, and (almost) compatible to > much faster JSON::XS" > > How compatible is "almost" compatible? They have been indistinguishable whenever I've used them. > I'm not actually worried about being compatible with JSON::XS. I'm worried > about being compatible with whatever JSON file some python script ends up > generating, or generating a JSON file that a python script may have to > read. You'll just have to try it and see. I'm sure that the perl and python modules will both have exciting, but different bugs. Define the data you want to pass back and forth, write acceptance tests on both ends for both reading and writing data. > JSON::XS https://metacpan.org/pod/JSON::XS says: > "This module converts Perl data structures to JSON and vice versa. Its > primary goal is to be correct" > > How "incorrect" might JSON::PP be? > > JSON::PP says it is not "java script friendly" and that "If you need > JavaScript-friendly RFC7159-compliant pure perl module, try JSON::Tiny," The difference appears to be that JSON::PP is bug-compatible with JSON::XS and so doesn't escape U+2028 and U+2029. This <https://stackoverflow.com/questions/2965293/javascript-parse-error-on-u2028-unicode-character> seems to indicate that that's only a problem if you're trying to pass your JSON directly to *Javascript*, which you're not. > JSON::Tiny says "it is among the fastest pure-Perl implementations of RFC > 7159." But it doesn't say anything about "correct"ness. > > So, what's the point of JSON::PP if JSON::Tiny does everything better and > is also pure perl? Compatibility with JSON::XS, which is (presumably) faster than JSON::Tiny. And maybe some NIH too. > Has anyone used any of these modules? > > If I wanted to take a complex perl data structure, save it to JSON format, > and then have a python script read that JSON file, possibly modify the > structure, and save it back as JSON, and then my perl script open that > JSON back up, and read it back in again, which module is most likely to be > cross-language compatible? I can't answer the Python bit, but I work with a large perl application that reads and writes JSON all day, using JSON.pm (with JSON::PP back-end, *not* JSON::XS). Our data is consumed by and comes from PHP and Javascript (the Javascript app uses JSON.parse to cope with the above Unicode problem) and AFAIK no-one has ever complained about anyone's inability to read anyone else's JSON. -- David Cantrell | even more awesome than a panda-fur coat What plaything can you offer me today?