What is the best way to pretty print JSON in Ruby with smart line wrapping, the way the "json" output formatter in underscore-cli handles it?
For instance, a regular JSON pretty printer would output:
{
  "a": [
    "b",
    "c"
  ],
  "d": {
    "e": "f"
  },
  "g": [
    "some longer string"
  ]
}
But I'm looking for a configurable pretty printer in Ruby that, like underscore-cli, notices that small structures make for pointlessly short lines, and so outputs something like this:
{
  "a": [ "b", "c" ],
  "d": { "e": "f" },
  "g": [ "some longer string" ]
}
I've played around with the options available in JSON.generate() (space, space_before, etc.), to no avail.
I'm writing a script which generates multiple JSON files intended to be reasonably comprehensible by humans (should the need arise) --- I can't expect underscore to be available everywhere so I can't (and ugh, wouldn't want to) just pipe the output through underscore, but the default JSON.pretty_generate() outputs files which are far less readable with around three times as many lines as underscore (2,200 lines vs 750).
Ruby 2.0.0p481.
 
     
     
    