Skip to content

Instantly share code, notes, and snippets.

@jasonm
Created August 28, 2011 05:00
Show Gist options
  • Save jasonm/1176262 to your computer and use it in GitHub Desktop.
Save jasonm/1176262 to your computer and use it in GitHub Desktop.
# Original to_json.rb
$ cat ~/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.1.0.rc6/lib/active_support/core_ext/object/to_json.rb
# Hack to load json gem first so we can overwrite its to_json.
begin
require 'json'
rescue LoadError
end
# The JSON gem adds a few modules to Ruby core classes containing :to_json definition, overwriting
# their default behavior. That said, we need to define the basic to_json method in all of them,
# otherwise they will always use to_json gem implementation, which is backwards incompatible in
# several cases (for instance, the JSON implementation for Hash does not work) with inheritance
# and consequently classes as ActiveSupport::OrderedHash cannot be serialized to json.
[Object, Array, FalseClass, Float, Hash, Integer, NilClass, String, TrueClass].each do |klass|
klass.class_eval <<-RUBY, __FILE__, __LINE__
# Dumps object in JSON (JavaScript Object Notation). See www.json.org for more info.
def to_json(options = nil)
ActiveSupport::JSON.encode(self, options)
end
RUBY
end
$ be rails c
Loading development environment (Rails 3.1.0.rc6)
ruby-1.9.2-p180 :001 > JSON.dump Time.now
ArgumentError: wrong number of arguments (2 for 1)
from /Users/jason/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.1.0.rc6/lib/active_support/core_ext/object/to_json.rb:19:in `to_json'
from /Users/jason/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/json/common.rb:212:in `generate'
from /Users/jason/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/json/common.rb:212:in `generate'
from /Users/jason/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/json/common.rb:336:in `dump'
from (irb):1
from /Users/jason/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.1.0.rc6/lib/rails/commands/console.rb:45:in `start'
from /Users/jason/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.1.0.rc6/lib/rails/commands/console.rb:8:in `start'
from /Users/jason/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.1.0.rc6/lib/rails/commands.rb:40:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
ruby-1.9.2-p180 :002 >
# I changed the signature to see what is getting passed in.
$ cat ~/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.1.0.rc6/lib/active_support/core_ext/object/to_json.rb
# Hack to load json gem first so we can overwrite its to_json.
begin
require 'json'
rescue LoadError
end
# The JSON gem adds a few modules to Ruby core classes containing :to_json definition, overwriting
# their default behavior. That said, we need to define the basic to_json method in all of them,
# otherwise they will always use to_json gem implementation, which is backwards incompatible in
# several cases (for instance, the JSON implementation for Hash does not work) with inheritance
# and consequently classes as ActiveSupport::OrderedHash cannot be serialized to json.
[Object, Array, FalseClass, Float, Hash, Integer, NilClass, String, TrueClass].each do |klass|
klass.class_eval <<-RUBY, __FILE__, __LINE__
# Dumps object in JSON (JavaScript Object Notation). See www.json.org for more info.
def to_json(options = nil, new_arg=nil)
puts 'Object#to_json received. self: ' + self.inspect + ' options: ' + options.inspect + ' mystery arg: ' + new_arg.inspect
ActiveSupport::JSON.encode(self, options)
end
RUBY
end
$ be rails c
Loading development environment (Rails 3.1.0.rc6)
ruby-1.9.2-p180 :001 > JSON.dump Time.now
Object#to_json received. self: 2011-08-28 00:58:23 -0400 options: #<JSON::Ext::Generator::State:0x00000101626298> mystery arg: 1
JSON::GeneratorError: only generation of JSON objects or arrays allowed
from /Users/jason/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/json/common.rb:212:in `generate'
from /Users/jason/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/json/common.rb:212:in `generate'
from /Users/jason/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/json/common.rb:336:in `dump'
from (irb):1
from /Users/jason/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.1.0.rc6/lib/rails/commands/console.rb:45:in `start'
from /Users/jason/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.1.0.rc6/lib/rails/commands/console.rb:8:in `start'
from /Users/jason/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.1.0.rc6/lib/rails/commands.rb:40:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
ruby-1.9.2-p180 :002 > exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment