Fork me on GitHub

Project Notes

JSON Parsing

Generating pretty JSON with Ruby and dealing with quirks.

:arrow_forward: return to the Catalog

Notes

Ruby’s support for JSON is implemented as a core but separate json gem. This is a implementation of the JSON specification according to RFC 4627.

There are perhaps four main methods most commonly used form the JSON library:

  • For serialializing an object to JSON:
    • to_json instance method available for all supported classes
    • JSON.pretty_generate class 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

  • the pretty_generate method 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.

The test_pretty_generate.rb script tests some of this version-dependent behaviour. The test.sh shell script runs the tests with different versions of the json gem:

$ ./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

Credits and References

About LCK#58 ruby
Project Source on GitHub Return to the Project Catalog

This page is a web-friendly rendering of my project notes shared in the LittleCodingKata GitHub repository.

LittleCodingKata is my collection of programming exercises, research and code toys broadly spanning things that relate to programming and software development (languages, frameworks and tools).

These range from the trivial to the complex and serious. Many are inspired by existing work and I'll note credits and references where applicable. The focus is quite scattered, as I variously work on things new and important in the moment, or go back to revisit things from the past.

This is primarily a personal collection for my own edification and learning, but anyone who stumbles by is welcome to borrow, steal or reference the work here. And if you spot errors or issues I'd really appreciate some feedback - create an issue, send me an email or even send a pull-request.