Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Non solution for JSON

by matija (Priest)
on Feb 28, 2012 at 15:56 UTC ( #956694=perlquestion: print w/replies, xml ) Need Help??
matija has asked for the wisdom of the Perl Monks concerning the following question:

Does anybody have code handy for parsing and generating JSON without all the complications accounted for by, and

Explanation: I have to write a script for a client machine which has Perl 5.8.8, and was deliberately set up without gcc and other development tools "to make it safe from hackers" (sheesh).

Since the installation does not contain B, and I can't install it, none of the above (standard, expected) JSON modules can be installed: they all seem to need B.

Does anybody know of a very simple module that would do basic JSON parsing and generating without needing B? Or at least a way to find which version is old enough that it would run without B on the server?

Replies are listed 'Best First'.
Re: Non solution for JSON
by ikegami (Pope) on Feb 28, 2012 at 16:33 UTC

    JSON encodes numbers differently than strings. JSON::PP uses B to peek at the internals of scalars to figure out how the value of the scalar is stored to guess if it's more appropriate to store a scalar as a string or as a number.

    You could create a copy of JSON::PP that encodes everything as strings, removing the need for B. (Scalar::Util's reftype would do for the other uses of B, and ref could be used if even that's not available.)

    An alternative would be to install JSON::XS on one machine and copy the installed files over. That would require a machine with the same architecture and Perl version as the target. The perl on the source would have to be built with the same options as the perl on the target.

Re: Non solution for JSON
by salva (Abbot) on Feb 28, 2012 at 17:13 UTC
    Fork the JSON module and remove the usage of B yourself. It seems something easy to do.

    JSON uses B to find out when some Perl scalar is a number looking at its internal representation, but this heuristic is very broken because Perl doesn't really differentiate between numbers and strings.

    So, you could use a regular expression to check when something is a number of just handle everything as it were a string (no heuristic is going to give you the desired result in all the possible cases anyway).

      Downloading JSON-1.15 seems easier :)
Re: Non solution for JSON
by Anonymous Monk on Feb 28, 2012 at 16:13 UTC

      I can assure you that the 5.8.8 installation I have to deal with does not have it. I looked for it, both with Perl and with locate.

      perl -MB -e print Can't locate in @INC (@INC contains: /usr/lib/perl5/site_perl/5.8 +.8/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.3/i386-linux- +thread-multi /usr/lib/perl5/site_perl/5.8.8 /usr/lib/perl5/site_perl/ +5.8.3 /usr/lib/perl5/site_perl /usr/lib/perl5/vendor_perl/5.8.8/i386- +linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.3/i386-linux-thread +-multi /usr/lib/perl5/vendor_perl/5.8.8 /usr/lib/perl5/vendor_perl/5. +8.3 /usr/lib/perl5/vendor_perl /usr/lib/perl5/5.8.8/i386-linux-thread +-multi /usr/lib/perl5/5.8.8 .). BEGIN failed--compilation aborted.

        I can assure you that the 5.8.8 installation I have to deal with does not have it. I looked for it, both with Perl and with locate.

        Aha, extra crippled, dare I say its not even perl :) miniperl maybe, but damn, that crippled

Re: Non solution for JSON
by tobyink (Abbot) on Feb 28, 2012 at 16:33 UTC

    Try Mojo::JSON.

    Update: dammit - that uses B. You could quite easily strip out the B parts though - it's only used to differentiate between numbers and strings. If you're happy with a sloppy conversion, you could just detect which scalars "look like numbers" using a regular expression instead of B.

    To see what I mean, on a Perl box with installed, try:

    perl -MJSON -e'print to_json {number=>1, string=>"1"}, {pretty=>1, can +onical=>1}'
Re: Non solution for JSON (jfdi)
by tye (Sage) on Feb 28, 2012 at 23:43 UTC

    If you ignore the edge cases, JSON is drop-dead simple. I bet it'd take me maybe an hour to whip out a Tiny::JSON and it'd probably run significantly faster than the character-at-a-time state machine of JSON::PP. So do that. Then you can upload to CPAN.

    - tye        

      Assuming JSON::Tiny is sound and works in rakudo, I bet it'd take you 10min to translate it to perl5 :)

        And I bet you didn't read the source code. On my way over to see it, I found that there are at least 3 files of it. Maybe there's a core of useful stuff that you can find in 10 minutes and throw the rest away (I found plenty to be thrown away before I stopped reading). Then you can upload to CPAN. :)

        - tye        

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://956694]
Approved by Eliya
[gnosti]: I'm reading from ffmpeg lengths of video clips in H:M:S and needing to add them and format them back to H:M:S for feeding back to ffmpeg
[marioroy]: kcott Thanks
[Corion]: gnosti: I would cheat and use my approach instead of dealing with durations as objects, or a module
[Corion]: You could even do the calculation directly - my $duration = $H * 3600+ $M*60 + $S; strftime '%H:%M:%S', timelocal($duratio n)
[Corion]: (and that strftime call is largely superfluous too, converting back to H:M:S isn't hard either)

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (9)
As of 2017-08-18 09:02 GMT
Find Nodes?
    Voting Booth?
    Who is your favorite scientist and why?

    Results (297 votes). Check out past polls.