diff options
author | Santiago Pastorino <santiago@wyeworks.com> | 2010-06-30 20:04:35 -0300 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2010-07-01 12:04:11 -0700 |
commit | 4a0c514eb48b8e5d4ceffb4817661c182c2368a3 (patch) | |
tree | 4b52cc7c03889950c6b08b47bf468c758129ac6b /activesupport/lib/active_support/core_ext | |
parent | d7c1057652cfc971bb35ef09b0b1560fcd28ed70 (diff) | |
download | rails-4a0c514eb48b8e5d4ceffb4817661c182c2368a3.tar.gz rails-4a0c514eb48b8e5d4ceffb4817661c182c2368a3.tar.bz2 rails-4a0c514eb48b8e5d4ceffb4817661c182c2368a3.zip |
AS json refactor, move to_json implementation to core_ext and a cleanup a bit the code
Diffstat (limited to 'activesupport/lib/active_support/core_ext')
-rw-r--r-- | activesupport/lib/active_support/core_ext/object.rb | 1 | ||||
-rw-r--r-- | activesupport/lib/active_support/core_ext/object/to_json.rb | 19 |
2 files changed, 20 insertions, 0 deletions
diff --git a/activesupport/lib/active_support/core_ext/object.rb b/activesupport/lib/active_support/core_ext/object.rb index 3a6100f776..8922016cd7 100644 --- a/activesupport/lib/active_support/core_ext/object.rb +++ b/activesupport/lib/active_support/core_ext/object.rb @@ -9,6 +9,7 @@ require 'active_support/core_ext/object/misc' require 'active_support/core_ext/object/extending' require 'active_support/core_ext/object/returning' +require 'active_support/core_ext/object/to_json' require 'active_support/core_ext/object/to_param' require 'active_support/core_ext/object/to_query' require 'active_support/core_ext/object/with_options' diff --git a/activesupport/lib/active_support/core_ext/object/to_json.rb b/activesupport/lib/active_support/core_ext/object/to_json.rb new file mode 100644 index 0000000000..14ef27340e --- /dev/null +++ b/activesupport/lib/active_support/core_ext/object/to_json.rb @@ -0,0 +1,19 @@ +# 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 |