aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/json/encoding.rb
diff options
context:
space:
mode:
authorSantiago Pastorino <santiago@wyeworks.com>2010-06-30 20:04:35 -0300
committerJeremy Kemper <jeremy@bitsweat.net>2010-07-01 12:04:11 -0700
commit4a0c514eb48b8e5d4ceffb4817661c182c2368a3 (patch)
tree4b52cc7c03889950c6b08b47bf468c758129ac6b /activesupport/lib/active_support/json/encoding.rb
parentd7c1057652cfc971bb35ef09b0b1560fcd28ed70 (diff)
downloadrails-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/json/encoding.rb')
-rw-r--r--activesupport/lib/active_support/json/encoding.rb37
1 files changed, 6 insertions, 31 deletions
diff --git a/activesupport/lib/active_support/json/encoding.rb b/activesupport/lib/active_support/json/encoding.rb
index dd94315111..3f266d1e96 100644
--- a/activesupport/lib/active_support/json/encoding.rb
+++ b/activesupport/lib/active_support/json/encoding.rb
@@ -1,21 +1,16 @@
-# encoding: utf-8
+require 'active_support/core_ext/object/to_json'
+require 'active_support/core_ext/module/delegation'
+require 'active_support/deprecation'
+require 'active_support/json/variable'
+
require 'bigdecimal'
-require 'active_support/core_ext/array/wrap'
require 'active_support/core_ext/big_decimal/conversions' # for #to_s
+require 'active_support/core_ext/array/wrap'
require 'active_support/core_ext/hash/except'
require 'active_support/core_ext/hash/slice'
-require 'active_support/core_ext/module/delegation'
require 'active_support/core_ext/object/instance_variables'
-require 'active_support/deprecation'
-
require 'active_support/time'
-# Hack to load json gem first so we can overwrite its to_json.
-begin
- require 'json'
-rescue LoadError
-end
-
module ActiveSupport
class << self
delegate :use_standard_json_time_format, :use_standard_json_time_format=,
@@ -128,20 +123,6 @@ module ActiveSupport
end
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)
@@ -152,12 +133,6 @@ class Object
end
end
-# A string that returns itself as its JSON-encoded form.
-class ActiveSupport::JSON::Variable < String
- def as_json(options = nil) self end #:nodoc:
- def encode_json(encoder) self end #:nodoc:
-end
-
class TrueClass
AS_JSON = ActiveSupport::JSON::Variable.new('true').freeze
def as_json(options = nil) AS_JSON end #:nodoc: