aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib
diff options
context:
space:
mode:
authorJosé Valim <jose.valim@gmail.com>2010-06-26 11:57:43 +0200
committerJosé Valim <jose.valim@gmail.com>2010-06-26 12:01:13 +0200
commit7bd85a8fc2d216a5e2b1d0380df572f782a54d1c (patch)
tree0e9a1b63353b01244bce81bb7262bfb87e92b997 /activesupport/lib
parentcfaaed3f40e820d2b4d60c2d8bc1f9a005cee086 (diff)
downloadrails-7bd85a8fc2d216a5e2b1d0380df572f782a54d1c.tar.gz
rails-7bd85a8fc2d216a5e2b1d0380df572f782a54d1c.tar.bz2
rails-7bd85a8fc2d216a5e2b1d0380df572f782a54d1c.zip
Work around the fact the JSON gem was overwriting to_json implementation for all Ruby core classes.
This is required because the JSON gem is incompatible with Rails behavior and was not allowing ActiveModel::Errors to be serialized. So we need to ensure Rails implementation is the one triggered. [#4890 state:resolved]
Diffstat (limited to 'activesupport/lib')
-rw-r--r--activesupport/lib/active_support/json/encoding.rb19
1 files changed, 14 insertions, 5 deletions
diff --git a/activesupport/lib/active_support/json/encoding.rb b/activesupport/lib/active_support/json/encoding.rb
index 02c233595d..dd94315111 100644
--- a/activesupport/lib/active_support/json/encoding.rb
+++ b/activesupport/lib/active_support/json/encoding.rb
@@ -128,12 +128,21 @@ module ActiveSupport
end
end
-class Object
- # 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
+# 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
+class Object
def as_json(options = nil) #:nodoc:
if respond_to?(:to_hash)
to_hash