set json max_nesting option from within Ruby on Rails application

I'm migrating a Ruby on Rails application from 3.0 to 3.1 and ran into some problems with the change of the JSON backend.

The line ActiveSupport::JSON.decode(some_variable) gives me the MultiJson::DecodeError nesting of 20 is too deep. The variable I'm trying to decode is indeed heavily nested (max 29 levels).

The maximum nesting level should be changeable with the :max_nesting option ('Nesting too deep' error while retrieving JSON using HTTParty) but this doesn't seem to work.

Both ActiveSupport::JSON.decode(some_variable, :max_nesting => false) and ActiveSupport::JSON.decode(some_variable, :max_nesting => 100) result in the same error.

Is there a way to pass the max_nesting option or set it application wide?

Answers


ActiveSupport::JSON uses the multi-json gem for encoding and decoding operations. The multi-json gem supports a variety of engines, and supported options will differ between them.

You can check which engine you are using by running

require 'multi_json'
puts MultiJson.engine

Mine was MultiJson::Adapters::Yajl but other options are possible as well. Multi-json doesn't seem to pass options to each engine in the same way, so I'd recommend using the JSON-gem directly.

If you're using the json-gem, you could just skip the ActiveSupport-chain and parse your data using JSON.parse, to which you can pass the :max_nesting option directly.


Try this monkey-patch

module JSON
  class << self
    def parse(source, opts = {})
      opts = ({:max_nesting => 100}).merge(opts)
      Parser.new(source, opts).parse
    end
  end
end

Need Your Help

Accessing kernel memory from within the kernel given a pointer

c linux memory kernel semaphore

I am trying to learn about the kernel and have been trying, unsuccessfully, for some time to print some of the basic data structures that make up the kernel landscape. My issue is that given a mem...

Run JS after rendering a meteor template

meteor

I have a template that looks something like this: