JSON reviews

cpanratings
 

RSS | Module Info | Add a review of JSON

JSON (2.51)

Because I saw Ben Bullock's terrible review, I comment a bit.

The how-to of (de|en)code_json, (from|to)_json and object methods
are described in the document (each sections and SYNOPSIS).
decode_json expects an UTF-8 (binary) string and identical to:

JSON->new->utf8->decode( $utf8_encoded_text )

So when you correctly use Encode::encode, the below code works.

# $string is a string (or UNICODE)

decode_json( encode('utf8', $string ) );

If decode_json croaks, it's your wrong use of Encode method.
That is not because of the JSON interface difficulty.

Additionally,
from_json is identical to:

JSON->new->decode( $scalar );

# from_json( $scalar, { allow_nonref => 1 } );

# => JSON->new->allow_nonref->decode( $scalar );

So from_json() can take a non utf8 encoded string.

Please see to
Encode, perluniintro,
search.cpan.org/~mlehmann/JSON-XS-2.3...
search.cpan.org/~makamaka/JSON-2.51/l...

JSON (2.90)

This module parses the JSON format and puts it into Perl structures. I haven't used it the other way around so I don't know if it turns Perl structures into JSON or not, although it claims to.

Although it definitely works correctly, this module is difficult to use and the interface is over-complex and badly designed. It's also rather difficult to navigate the documentation. Before the maintainer changed the behaviour recently, the default behaviour was that it blew up with weird error messages like this:

Wide character in subroutine entry at ../lib/Blah.pm line 54

if it was sent "utf8" encoded Unicode text. Even after trying all kinds of different options, in the end it was easier just to re-encode the text using "encode_utf8" from the Encode module and send it. Even then I was still getting problems with "decode_json", for example

my $text = encode ('utf8', $contents->{response});

my $json = decode_json ($text);

caused an error

Cannot decode string with wide characters at /home/ben/software/install/lib/perl5/5.12.3/i386-freebsd/Encode.pm line 174.

but

my $text = encode ('utf8', $contents->{response});

my $json = from_json ($text);

did not produce this error, so I had to use "from_json" instead.

Why? Eventually I found out that one of these routines defaults to turning on the UTF-8 flag, and the other one defaults to turning it off. Then the error was bubbling up from another routine which sent the text to DBI. Why the module is written in this confusing way, I don't know, is it something to do with JSON::XS compatibility? Then the maintainer wrote a response review which demonstrates my point: to understand what this module is doing you need to spend a long time scrabbling through the documentation. Why on earth is it necessary to spend such a long time doing these things when JSON is such an absurdly simple format?

In the end, after seeing that maintainer's review, to save time:

search.cpan.org/dist/JSON-Parse/

It has two routines, one validator and one parser. Put UTF-8 flagged text in, get UTF-8 flagged text out. Put non-UTF-8 flagged text in, get non-UTF-8 flagged text out.

JSON (2.53) *****

Fast, reliable, and versatile without trying to be everything and please everyone. A fine JSON parser, reasonably documented, regularly updated, with a response and polite author - a rarer thing than it ought to be. Highly recommended.

JSON (2.12) ***

I like the module, but I wish there could be more stability in its interface from one version to the next. Lots of functions were renamed between 1.x and 2.x. It seems like the author could have done a better job of continuing support for code that depended on the old API. The focus seems to be on speed, which is fine for applications that need speed. However, it would also be good to have more sophisticated error handling.

JSON (1.05) ****

I first stumbled on this module from a CPAN search for json and it fit my needs at the moment and it may fit yours, but with more experience, I've moved to JSON::Syck. My usage doesn't need anything other than parsing or exporting JSON data, so a lot of what JSON offers is irrelevant. More importantly and objectively, JSON::Syck is faster (over 370% on my box!!).

JSON (1.07) ***

On the pro side, it's quite easy to use and straightforward.

On the con side:

* There's no documentation about what happens if a parsing error occurs.

* In fact, if an error occurs, an unhelpful message is returned - "syntax error (word) at 199". It would be nice if the module could count lines, and if its error messages were a bit more descriptive.

* Along those lines, it would be nice if the module had a routine for parsing a file containing JSON.

* The bare keywords feature is nice (for data that's only supposed to be used by this module), but turning it on is awkward - I'd rather it be an instance variable than a module-level variable.
2 hidden unhelpful reviews