2

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?

Community
  • 1
  • 1
bmesuere
  • 502
  • 3
  • 12

2 Answers2

3

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.

Javache
  • 3,398
  • 3
  • 21
  • 25
0

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
dimuch
  • 12,728
  • 2
  • 39
  • 23