Generating pretty JSON with Ruby and dealing with quirks.
There are perhaps four main methods most commonly used form the JSON library:
- For serialializing an object to JSON:
to_jsoninstance method available for all supported classes
JSON.pretty_generateclass method generates a more human-readable JSON representation
- For deserializing JSON:
JSON.load- given a string or stream
JSON.parse- given a string
pretty_generate Verson-dependent Behaviour
Prior to v2.0.0 - actually before this commit
pretty_generatemethod only worked with arrays and hashes. Attempting to prettify any other type of object would result in an exception:
JSON::GeneratorError: only generation of JSON objects or arrays allowed.
This can be an upgrade gotcha, as early code may for example have been written to expect nil to raise an exception. After json gem upgrade, code paths will change.
$ ./test.sh Successfully uninstalled json-2.1.0 *** LOCAL GEMS *** json (default: 1.8.3) multi_json (1.12.1) *** RUNNING TESTS WITH JSON v1.8.3 *** Run options: --seed 12498 # Running: .... Finished in 0.001092s, 3661.5551 runs/s, 3661.5551 assertions/s. 4 runs, 4 assertions, 0 failures, 0 errors, 0 skips Fetching: json-2.1.0.gem (100%) Building native extensions. This could take a while... Successfully installed json-2.1.0 Parsing documentation for json-2.1.0 Installing ri documentation for json-2.1.0 Done installing documentation for json after 1 seconds 1 gem installed *** LOCAL GEMS *** json (2.1.0, default: 1.8.3) multi_json (1.12.1) *** RUNNING TESTS WITH JSON v2.1.0 *** Run options: --seed 29051 # Running: .... Finished in 0.001093s, 3658.6649 runs/s, 3658.6649 assertions/s. 4 runs, 4 assertions, 0 failures, 0 errors, 0 skips