From 134c1156dd5713da41c62ff798fe3979723e64cc Mon Sep 17 00:00:00 2001 From: Godfrey Chan Date: Tue, 5 Nov 2013 19:22:03 -0800 Subject: Fixed Object#as_json and Struct#as_json with options These methods now takes the same options as Hash#as_json, for example: struct = Struct.new(:foo, :bar).new struct.foo = "hello" struct.bar = "world" json = struct.as_json(only: [:foo]) # => {foo: "hello"} This is extracted from PR #11728 from @sergiocampama, see also the discussion in #11460. --- activesupport/lib/active_support/core_ext/object/json.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'activesupport/lib/active_support') diff --git a/activesupport/lib/active_support/core_ext/object/json.rb b/activesupport/lib/active_support/core_ext/object/json.rb index 554da1a2aa..c50cd57c2c 100644 --- a/activesupport/lib/active_support/core_ext/object/json.rb +++ b/activesupport/lib/active_support/core_ext/object/json.rb @@ -20,16 +20,16 @@ end class Object def as_json(options = nil) #:nodoc: if respond_to?(:to_hash) - to_hash + to_hash.as_json(options) else - instance_values + instance_values.as_json(options) end end end class Struct #:nodoc: def as_json(options = nil) - Hash[members.zip(values)] + Hash[members.zip(values)].as_json(options) end end -- cgit v1.2.3