How do you deal with the conflict between ActiveSupport::JSON and the JSON gem?
- by Luke Francl
I am stumped with this problem.
ActiveSupport::JSON defines to_json on various core objects and so does the JSON gem. However, the implementation is not the same -- the ActiveSupport version takes arguments and the JSON gem version doesn't.
I installed a gem that required the JSON gem and my app broke. The issue is that I'm using to_json in a controller that returns a list of objects, but I want to control which attributes are returned.
When code anywhere in my system does require 'json' I get this error message:
TypeError: wrong argument type Hash (expected Data)
I tried a couple of things that I read online to fix it, but nothing worked. I ended up re-writing the gem to use ActiveSupport::JSON.decode instead of JSON.parse.
This works but it's not sustainable...I can't be forking gems every time I want to use a gem that requires the JSON gem.
Update: The best solution of this problem is to upgrade to Rails 2.3 or higher, which fixed it.