kraih/mojo — pull request #666 by daoswald (2014-08-06 17:45:09+00:00)
Title: Canon branch: Mojo::JSON support for canonical encoding via OO interface

This patch adds canonical encoding support to Mojo::JSON via the OO interface in a way that creates no significant performance impact in the general case, and a 2% impact in the case of using canonical (sorted keys) encoding.

Please refer to the following gist, which demonstrates the sample JSON, and the code used to benchmark the original against the modified code, for both general and sorted encoding.

https://gist.github.com/daoswald/5ba4d04fcfe54ce9cd66

The effect was achieved by localizing *MOJO::JSON::_encode_object and injecting a modified version that is only visible if the Mojo::JSON object is constructed with the 'canonical=>1' attribute. This adds one "exists" test to Mojo::JSON::encode. The encode_json() sub is entirely unaffected. ->encode() is affected only in that it now tests whether $_[0]->{canonical} exists, and if so, if it's true.

This patch removes performance as an issue blocking canonical JSON encoding support. But it does add complexity to Mojo::JSON, and probably makes it messy to inherit from Mojo::JSON (is anyone actually subclassing it though?). It's up to those with a vote to decide whether that's a worthwhile tradeoff. I don't intend to pursue it further after a decision is made, unless requested to make a modification of some sort.