'Nesting too deep' error while retrieving JSON using HTTParty

后端 未结 3 1502
遇见更好的自我
遇见更好的自我 2021-01-13 20:33

I am writing a very simple proxy wrapper for the reddit api so I can make cross domain JSONP requests (reddit does not allow JSONP calls to my knowledge).

I am using

相关标签:
3条回答
  • 2021-01-13 20:52

    It looks like this can be worked around by calling to_json with the max_nesting option set.

    json = obj.to_json(max_nesting: 50)
    
    0 讨论(0)
  • 2021-01-13 20:57

    It's not that the JSON is too large. It's nested too deeply. HTTParty tries to decode the results that it gets automatically. Following your stack trace, and the HTTParty dependencies, it relies on multi_json which is using the json gem.

    Inside of json there is lib/json/pure/parser.rb. The default max depth set is set in there, specifically on line 79. Something in your returned JSON is 20+ levels deep, triggering the exception.

     if !opts.key?(:max_nesting) # defaults to 19
       @max_nesting = 19
    
    0 讨论(0)
  • Switched to the OJ JSON parser by adding the following to my Gemfile:

    gem 'oj'
    

    and this issue resolved itself.

    0 讨论(0)
提交回复
热议问题