简体   繁体   English

在我的Rails项目中使用yajl-ruby的最佳方法是什么?

[英]What's the best way to use yajl-ruby with my Rails project?

Rails 2.3.6 started using the fast new json library, yajl-ruby , "if available" . Rails 2.3.6开始使用快速新的json库yajl-ruby“如果可用”

In the "JSON gem Compatibility API" section of the yajl-ruby readme it outlines a method to just drop in yajl-ruby inclusion and have the rest of the app seamlessly pick it up. 在yajl-ruby自述文件的“JSON gem Compatibility API”部分中,它概述了一种方法,可以放入yajl-ruby包含并让应用程序的其余部分无缝地获取它。

So, ideally, I'd like 所以,理想情况下,我想

  1. Rails to use it Rails使用它
  2. My gems to use it 我的宝石使用它
  3. My application code to use it 我的应用程序代码使用它

What's the easiest way to achieve this? 实现这一目标的最简单方法是什么? My guess: 我猜:

config.gem 'yajl-ruby', :lib => 'yajl/json_gem'

As the very first gem in environment.rb. 作为environment.rb中的第一个宝石。 Doing this doesn't result in any errors, but I'm not sure how to know if rails is picking it up for its own use. 这样做不会导致任何错误,但我不知道如何知道rails是否正在为自己使用它。

Thanks! 谢谢! John 约翰

I'd recommend using yajl-ruby 's API directly instead of the JSON gem compatibility API mainly for the reason that the JSON gem's to_json method conflict with ActiveSupport and has had long-standing issues making them work together. 我建议直接使用yajl-ruby的API而不是JSON gem兼容API,主要是因为JSON gem的to_json方法与ActiveSupport冲突并且长期存在使它们协同工作的问题。

If you just do config.gem 'yajl-ruby', :lib => 'yajl' instead, you'll need to use Yajl::Parser and Yajl::Encoder directly to parse/encode objects. 如果您只是执行config.gem 'yajl-ruby', :lib => 'yajl' ,则需要直接使用Yajl::ParserYajl::Encoder来解析/编码对象。 The advantage of this is you'll be certain there won't be any conflicts with method overrides and as such, be guaranteed your JSON encoding/parsing code will work as expected. 这样做的好处是你可以肯定不会与方法覆盖有任何冲突,因此,保证你的JSON编码/解析代码将按预期工作。 The disadvantage is if you're using any gems that use the JSON gem, they'll continue to do so but you're own code will use yajl-ruby. 缺点是如果你使用任何使用JSON gem的gem,他们会继续这样做,但你自己的代码将使用yajl-ruby。

If you wanted to, you could use your config.gem line, then in an initializer require 'yajl' so you'd have both API's loaded. 如果你愿意,可以使用你的config.gem行,然后在初始化程序中require 'yajl'这样你就可以加载两个API。 The yajl/json_gem include will override anything that's using the JSON gem with yajl - to ensure this overrides those methods try to make sure require 'yajl/json_gem' happens last. yajl/json_gem include将覆盖使用带有yajl的JSON gem的yajl - 为了确保覆盖这些方法,尝试确保最后require 'yajl/json_gem'

If you're using Rails 3, you can add this to an initializer: 如果您使用的是Rails 3,则可以将其添加到初始化程序中:

ActionController::Renderers.add :json do |json, options|
  json = Yajl.dump(json) unless json.respond_to?(:to_str)
  json = "#{options[:callback]}(#{json})" unless options[:callback].blank?
  self.content_type ||= Mime::JSON
  self.response_body  = json
end

To make sure render :json => ... calls use yajl-ruby as well. 为了确保render :json => ...调用也使用yajl-ruby

Sorry if this isn't really answering your question but I wanted to at least give the suggestion of using yajl-ruby 's API directly :) 对不起,如果这不是真的回答你的问题,但我想至少给出建议直接使用yajl-ruby的API :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM