Fork me on GitHub

Project Notes

JSON Parsing

Generating pretty JSON with Ruby and dealing with quirks.

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

Note: this is only an issue with ruby <= 2.3 (I think) as later rubies require a later json gem version anyway.

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
ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-darwin17]
Gem 'json' is not installed

*** LOCAL GEMS ***

json (default: 1.8.3)


*** RUNNING TESTS WITH JSON v1.8.3 ***

Run options: --seed 7402

# Running:

....

Finished in 0.002317s, 1726.3703 runs/s, 1726.3703 assertions/s.

4 runs, 4 assertions, 0 failures, 0 errors, 0 skips
Fetching json-2.1.0.gem
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)


*** RUNNING TESTS WITH JSON v2.1.0 ***

Run options: --seed 24841

# Running:

....

Finished in 0.001192s, 3355.7047 runs/s, 3355.7047 assertions/s.

4 runs, 4 assertions, 0 failures, 0 errors, 0 skips

Credits and References

About LCK#44 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.