aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/lib
diff options
context:
space:
mode:
authorJakub Suder <jakub.suder@gmail.com>2010-08-29 16:10:31 +0200
committerJeremy Kemper <jeremy@bitsweat.net>2010-09-07 11:33:10 -0700
commit2524cf404ce943eca8a5f2d173188fd0cf2ac8b9 (patch)
tree0d589e9e06623d0bda440b78ccd93a426c2e4807 /activemodel/lib
parent2e8a3d0f43d7b394873ce21396ae49feacb99a74 (diff)
downloadrails-2524cf404ce943eca8a5f2d173188fd0cf2ac8b9.tar.gz
rails-2524cf404ce943eca8a5f2d173188fd0cf2ac8b9.tar.bz2
rails-2524cf404ce943eca8a5f2d173188fd0cf2ac8b9.zip
fixed some issues with JSON encoding
- as_json in ActiveModel should return a hash and handle :only/:except/:methods options - Array and Hash should call as_json on their elements - json methods should not modify options argument [#5374 state:committed] Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
Diffstat (limited to 'activemodel/lib')
-rw-r--r--activemodel/lib/active_model/serialization.rb18
-rw-r--r--activemodel/lib/active_model/serializers/json.rb14
2 files changed, 16 insertions, 16 deletions
diff --git a/activemodel/lib/active_model/serialization.rb b/activemodel/lib/active_model/serialization.rb
index 542cff3aaa..8f90ef64ba 100644
--- a/activemodel/lib/active_model/serialization.rb
+++ b/activemodel/lib/active_model/serialization.rb
@@ -54,12 +54,14 @@ module ActiveModel
#
# person = Person.new
# person.serializable_hash # => {"name"=>nil}
- # person.as_json # => "{\"name\":null}"
+ # person.as_json # => {"name"=>nil}
+ # person.to_json # => "{\"name\":null}"
# person.to_xml # => "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<serial-person...
#
# person.name = "Bob"
# person.serializable_hash # => {"name"=>"Bob"}
- # person.as_json # => "{\"name\":\"Bob\"}"
+ # person.as_json # => {"name"=>"Bob"}
+ # person.to_json # => "{\"name\":\"Bob\"}"
# person.to_xml # => "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<serial-person...
#
# Valid options are <tt>:only</tt>, <tt>:except</tt> and <tt>:methods</tt> .
@@ -67,14 +69,14 @@ module ActiveModel
def serializable_hash(options = nil)
options ||= {}
- options[:only] = Array.wrap(options[:only]).map { |n| n.to_s }
- options[:except] = Array.wrap(options[:except]).map { |n| n.to_s }
+ only = Array.wrap(options[:only]).map(&:to_s)
+ except = Array.wrap(options[:except]).map(&:to_s)
attribute_names = attributes.keys.sort
- if options[:only].any?
- attribute_names &= options[:only]
- elsif options[:except].any?
- attribute_names -= options[:except]
+ if only.any?
+ attribute_names &= only
+ elsif except.any?
+ attribute_names -= except
end
method_names = Array.wrap(options[:methods]).inject([]) do |methods, name|
diff --git a/activemodel/lib/active_model/serializers/json.rb b/activemodel/lib/active_model/serializers/json.rb
index c9271ed9b4..0bfbf2aa06 100644
--- a/activemodel/lib/active_model/serializers/json.rb
+++ b/activemodel/lib/active_model/serializers/json.rb
@@ -79,18 +79,16 @@ module ActiveModel
# "title": "Welcome to the weblog"},
# {"comments": [{"body": "Don't think too hard"}],
# "title": "So I was thinking"}]}
- def encode_json(encoder)
- hash = serializable_hash(encoder.options)
+
+ def as_json(options = nil)
+ hash = serializable_hash(options)
+
if include_root_in_json
- custom_root = encoder.options && encoder.options[:root]
+ custom_root = options && options[:root]
hash = { custom_root || self.class.model_name.element => hash }
end
- ActiveSupport::JSON.encode(hash)
- end
-
- def as_json(options = nil)
- self
+ hash
end
def from_json(json)