Monday, August 9, 2010

To defeat Rails' conspiracy to have you not use the JSON gem.
See activesupport-2.3.8/lib/active_support/json/encoding.rb line 92 gRRRRRRRRRRRRRRRR!!!
Call UseJsonGemInRails.save_json_gem_to_json in your RAILS_ROOT/config/preinitializer.rb file.
Then set up an initializer and call UseJsonGemInRails.reload_json_gem_to_json in it.

class UseJsonGemInRails
  class << self
    def save_json_gem_to_json
      require 'json'
      classes.each do |klass|
        klass.class_eval do
          alias_method :to_json_from_gem, :to_json
        end
      end
    end
    def reload_json_gem_to_json
      (classes + [ActiveSupport::JSON::Variable]).each do |klass|
        klass.class_eval do
          alias_method :to_json, :to_json_from_gem
        end
      end
    end
    def classes
      [
        Object,
        Hash,
        Array,
        String,
        Numeric,
        Float,
        Integer,
        Regexp,
      ]
    end
  end
end

http://gist.github.com/461938

No comments: