Rails 2.3.6 started using the fast new json library, yajl-ruby, “if available”.
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.
So, ideally, I’d like
- Rails to use it
- My gems to use it
- 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. 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.
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’sto_jsonmethod conflict withActiveSupportand has had long-standing issues making them work together.If you just do
config.gem 'yajl-ruby', :lib => 'yajl'instead, you’ll need to useYajl::ParserandYajl::Encoderdirectly 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. Theyajl/json_geminclude will override anything that’s using the JSON gem withyajl– to ensure this overrides those methods try to make surerequire 'yajl/json_gem'happens last.If you’re using Rails 3, you can add this to an initializer:
To make sure
render :json => ...calls useyajl-rubyas 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 🙂