Hey 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.
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. 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.
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.
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. 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.
If you're using Rails 3, you can add this to an initializer:
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.
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 :)