Safe Object As JSON
This gem provides an enhancement to the implementation for as_json
on the core Object
class in the ActiveSupport library. The implementation provided by ActiveSupport dumps the instance variables as name value pairs in a Hash. However, this is susceptible to infinite recursion when dumping an object that maintains references to other objects that then maintain back references to the original object.
The fix provided by this gem maintains a state of the current stack of objects being used to construct the as_json
hash. If an object has already been referenced in the current call to Object#as_json
, it is left out of the hash in lieu of having it raise a stack level too deep error.
It also omits any Proc
or IO
references since these are inherently not serializable.
Usage
No changes are needed to use this gem. It will just replace the method definition of Object#as_json
. It will not impact any class that defines its own as_json
or to_hash
method which includes all the core Ruby classes (String, Numeric, Array, Hash) as well as ActiveModel classes.
The Object#as_json
method is really just a fallback method that exists so that all objects can be sent to a JSON serializer. If you do have classes that rely on this method, you should really just implement the as_json
method yourself. The main reason this gem exists is to handle cases where you don't control the class definition in your application code.
Installation
Add this line to your application's Gemfile:
gem 'safe_object_as_json'
And then execute:
$ bundle
Or install it yourself as:
$ gem install safe_object_as_json
Contributing
Open a pull request on GitHub.
Please use the standardrb syntax and lint your code with standardrb --fix
before submitting.
License
The gem is available as open source under the terms of the MIT License.