Re: [Boston.pm] perl data to/from JSON?

"Morse, Richard E.,MGH via Boston-pm" <[email protected]> Mon, 28 Jan 2019 14:25:48 +0000
Newsgroups gmane.comp.lang.perl.perl-mongers.boston
Message-ID <[email protected]>
(I sent this on Friday, but apparently something went wrong?)

Hi! I have an opinion on this…

First, my recommendation, if you want to TL;DR, is to use JSON::Tiny.

That being said, there are issues that have been highlighted recently  
regarding serializing to JSON and how it changes with respect to different  
data types.

Some things you may want to look at:

	http://blogs.perl.org/users/e_choroba/2018/03/numbers-and-strings-in-json.html

	http://perladvent.org/2018/2018-12-03.html
	https://www.perlmonks.org/?node_id=1210167

	(Also, MSTPAN on JSON: https://shadow.cat/blog/matt-s-trout/mstpan-7)

So, if you want to be certain that an integer is always serialized as ‘1’,  
and not as ‘“1”’ (depending on the most recent thing to look at it), you  
probably will want to work with Cpanel::JSON::XS and Cpanel::JSON::XS::Type.

That being said, if you are thinking of using JSON::PP, I think now the  
best bet is to use JSON::MaybeXS, which first tries for Cpanel::JSON::XS,  
then JSON::XS, then JSON::PP.

However, unless performance is a major concern, and provided you’re dealing  
with reasonably sized data structures (ie, not something with thousands and  
thousands of keys at hundreds of levels deep), you could probably try  
JSON::Tiny first.

(BTW: JSON::Tiny is effectively a hoist of Mojo::JSON, so if you’re in any  
way using the Mojo libraries, you should probably just use that…)

Hope this helps,
Ricky

On Jan 25, 2019, at 3:47 PM, Greg London <[email protected]> wrote:

> This is a work-related issue, so certain decisions are out of my control.
> Also, I know only rudimentary python, and I know absolutely nothing about
> JSON, so, this whole thing may be complete garbage for reasons I'm unaware
> of. That said:
>
> 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.
>
> basically I want to do this:
>
> my $my_complex_data = [ {key1=>data1,key2=>data2}, [ {}, {key=>[] } ]];
> use Storable qw(nstore dclone retrieve);
> nstore ($my_complex_data, 'filename');
> my $revived = retrieve('filename');
>
> But have the intermediate file format be JSON instead of Storable's format.
>
> We also have to go through a process to get executable modules installed,
> so if it can be pure perl, I can just copy the file to a local directory,
> check it into the project, and use it without delay.
> I can get executables installed, but they have to be approved through a
> process, and then someone has to make sure they're installed on every
> machine everywhere, including all teh machines on LSF. so executables are
> possible, but likely a bit more painful.
>
> 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?
>
> 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.
>
>
> 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,"
>
> https://metacpan.org/pod/JSON::Tiny
>
> 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?
>
> 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?
>
> Any help woudl be appreciated.
> Greg
>
> _______________________________________________
> Boston-pm mailing list
> [email protected]
> https://mail.pm.org/mailman/listinfo/boston-pm


The information in this e-mail is intended only for the person to whom it is
addressed. If you believe this e-mail was sent to you in error and the e-mail
contains patient information, please contact the Partners Compliance HelpLine at
http://www.partners.org/complianceline . If the e-mail was sent to you in error
but does not contain patient information, please contact the sender and properly
dispose of the e-mail.

_______________________________________________
Boston-pm mailing list
[email protected]
https://mail.pm.org/mailman/listinfo/boston-pm