您如何处理ActiveSupport :: JSON和JSON gem之间的冲突? [英] How do you deal with the conflict between ActiveSupport::JSON and the JSON gem?
问题描述
我为这个问题感到困惑.
I am stumped with this problem.
ActiveSupport::JSON
在各种核心对象上定义to_json
,JSON gem也是如此.但是,实现方式并不相同-ActiveSupport版本带有参数,而JSON gem版本没有参数.
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.
我安装了需要JSON gem的gem,但我的应用程序坏了.问题是我在返回对象列表的控制器中使用to_json
,但是我想控制要返回的属性.
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.
当系统中任何地方的代码执行require 'json'
时,我都会显示以下错误消息:
When code anywhere in my system does require 'json'
I get this error message:
TypeError: wrong argument type Hash (expected Data)
我尝试了一些在线阅读的方法来修复它,但是没有任何效果.我最终重新编写了gem以使用ActiveSupport::JSON.decode
而不是JSON.parse
.
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
.
这行得通,但这是不可持续的...我不能每次都想使用需要JSON宝石的宝石时分叉宝石.
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.
更新:此问题的最佳解决方案是升级到Rails 2.3或更高版本,从而对其进行了修复.
Update: The best solution of this problem is to upgrade to Rails 2.3 or higher, which fixed it.
推荐答案
更新 该修补程序仅适用于Rails< 2.3.就像Giles在下面提到的那样,他们使用几乎相同的技术在内部将其固定在2.3中.但是要当心json gem在Rails兼容性上的早期尝试(json/add/rails
),,如果明确要求,则会再次破坏一切.
您是说require 'json'
语句本身引发该异常吗?还是说当您致电@something.to_json(:something => value)
时收到错误消息?后者是我所期望的,如果您在使用JSON gem时遇到问题,那么我不确定会发生什么.
Do you mean the require 'json'
statement itself raises that Exception? Or do you mean when you call @something.to_json(:something => value)
you get the error? The latter is what I would expect, if you have a problem requiring the JSON gem then I'm not sure what's going on.
我刚遇到了oauth gem的问题.在我的情况下,没有真正的冲突,因为oauth gem不依赖于to_json
实现.因此,问题在于JSON破坏了ActiveSupport声明.我通过在加载ActiveSupport之前只需要json来解决了这个问题.放置
I just ran into this problem with the oauth gem. In my case, there is not a true conflict, because the oauth gem doesn't depend on to_json
implementation. Therefore the problem is that JSON is clobbering the ActiveSupport declarations. I solved this by simply requiring json before ActiveSupport is loaded. Putting
require 'json'
在Rails::Initializer
内部的
做到了这一点(尽管没有把它放在块后面).
inside the Rails::Initializer
did the trick (though putting it after the block did NOT).
这允许ActiveSupport代替默认的JSON实现.
That allows ActiveSupport to clobber the default JSON implementation instead.
现在,如果您使用的宝石实际上依赖于to_json
的JSON实现,那么您将大步前进.这绝对是元编程中最糟糕的情况,我会提倡让Rails和JSON gem开发人员解决冲突,尽管这会很痛苦,因为彼此之间都必须打破向后兼容性.
Now if you are using a gem that actually depends on the JSON implementation of to_json
then you are up a creek. This is definitely the worst of meta-programming, and I would advocate for the Rails and JSON gem developers to resolve the conflict, though it will be painful because one or the other will have to break backwards compatibility.
从短期来看,gem作者可能能够通过支持这两种实现来弥合差距.根据宝石如何使用该方法,这或多或少是可行的.最坏的情况是使用官方分叉(即gem
和gem-rails
).
In the short term, gem authors may be able to bridge the gap by supporting both implementations. This is more or less feasible depending on how the gem uses the method. A worst case scenario is an official fork (ie. gem
and gem-rails
).
这篇关于您如何处理ActiveSupport :: JSON和JSON gem之间的冲突?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!