mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 15:23:06 +00:00
support different serializations in ArraySerializer
ArraySerializer can serialize arrays of Hash, ActiveModel::Serializer, or any PORO that responds to :as_json Each item in the array should not include its type as a root element, since normal arrays do not do this. If you want "typed" collections, don't use an Array.
This commit is contained in:
@@ -52,7 +52,15 @@ module ActiveModel
|
||||
@options[:hash] = hash = {}
|
||||
@options[:unique_values] = {}
|
||||
|
||||
array = serializable_array.map(&:serializable_hash)
|
||||
array = serializable_array.map do |item|
|
||||
if item.is_a?(Hash)
|
||||
item
|
||||
elsif item.respond_to?(:serializable_hash)
|
||||
item.serializable_hash
|
||||
else
|
||||
item.as_json
|
||||
end
|
||||
end
|
||||
|
||||
if root = @options[:root]
|
||||
hash.merge!(root => array)
|
||||
|
||||
Reference in New Issue
Block a user