Telephone +44(0)1524 64544

mstpan 7 - JSON

Tue Dec 9 22:00:00 2014

mstpan 7 - JSON

In Crockford We Trust.

JSON is a wrapper around JSON::PP and JSON::XS - it also does a bunch of moderately crazy things for compatibility reasons, including extra shim code for very old perls (just because I remember 5.005_03 doesn't mean I have to remember it fondly ... now get off my lawn).

If you're currently using it, you're far from alone, and not doing anything actively wrong, but you may wish to switch your 'use' statement to one of the alternatives mentioned below.


This is the standard pure perl implementation, and if you're not performance dependent, there's nothing wrong with using it directly. It fatpacks fine, and as of 5.14 is supplied by core so a dependency on it will likely be a no-op.


Ridiculously fast JSON implementation in C. Absolutely wonderful, except that the maintainer makes Ulrich Drepper look like Mother Theresa. Which brings us to ...


This is a fork/patchset-atop JSON::XS that adds 5.6.2 compat, thread safety, a bunch of other bugfixes and performance tweaks, and a public bugtracker where the maintainer actually talks to people.

If you want speed uber alles, I'd recommend simply requiring this module directly.


Since (a) contains a bunch of compat code most users don't care about (b) doesn't know about Cpanel::JSON::XS, I wrote an alternative implementation switcher.

It's much shorter, simpler code, exports a JSON constant instead of DWIMming behind a class name, and prefers Cpanel::JSON::XS over JSON::XS over JSON::PP, but keeps to JSON::XS if that's already loaded to try and avoid loading both XS modules in mixed codebases.

If you're writing new code, I'd default to this, but given I wrote it, I would say that. It's lower overhead and fatpacks much more nicely than though, and will continue to be a curated source of which implementations I think are worth using.


Written by one of my team for a customer, JSON::Diffable writes JSON out with trailing commas on arrays and objects, and sets the relaxed option on its JSON implementation for reading so that the data it writes can be easily read.

This makes life significantly more pleasant if you're committing JSON to a git repository.


Mojo, as ever, has its own. It's actually really quite nice, and has been forked onto CPAN as JSON::Tiny. If code size is crucial to you, it's well worth a look.

Plus, Mojo::JSON handles the "string or number" question in a different way to the other modules - a way that, in fact, I consider to be more DWIM than the standard approach, albeit not quite enough more DWIM to want to put everybody through the pain of changing the standard.

There's also a Mojo::JSON::MaybeXS to replace Mojo::JSON with JSON::MaybeXS, and its documentation provides an excellent run-down of the precise differences between the various modules.


That was shorter than a decaptitated halfling. Oh, well, JSON isn't supposed to be complicated ... or at least, that's my excuse and I'm sticking to it.

Tomorrow I shall mostly be waffling about async.

-- mst, out.