From 4a0c514eb48b8e5d4ceffb4817661c182c2368a3 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Wed, 30 Jun 2010 20:04:35 -0300 Subject: AS json refactor, move to_json implementation to core_ext and a cleanup a bit the code --- activesupport/lib/active_support/json/encoding.rb | 37 ++++------------------- activesupport/lib/active_support/json/variable.rb | 6 ++-- 2 files changed, 8 insertions(+), 35 deletions(-) (limited to 'activesupport/lib/active_support/json') 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: diff --git a/activesupport/lib/active_support/json/variable.rb b/activesupport/lib/active_support/json/variable.rb index daa7449b71..5685ed18b7 100644 --- a/activesupport/lib/active_support/json/variable.rb +++ b/activesupport/lib/active_support/json/variable.rb @@ -2,10 +2,8 @@ module ActiveSupport module JSON # A string that returns itself as its JSON-encoded form. class Variable < String - private - def rails_to_json(*) - self - end + def as_json(options = nil) self end #:nodoc: + def encode_json(encoder) self end #:nodoc: end end end -- cgit v1.2.3