Class: Cuprum::Rails::Serializers::Json::ArraySerializer

Inherits:
BaseSerializer
  • Object
show all
Defined in:
lib/cuprum/rails/serializers/json/array_serializer.rb

Overview

Converts Array data structures to JSON based on configured serializers.

Instance Method Summary collapse

Methods inherited from BaseSerializer

instance

Instance Method Details

#call(array, context:) ⇒ Array

Converts the array to JSON using the given serializers.

First, #call finds the best serializer from the :serializers Hash for each item in the Array. This is done by walking up the object class’s ancestors to find the closest ancestor which is a key in the :serializers Hash. The corresponding value is then called with the object, and the results are combined into a new Array and returned.

Parameters:

  • array (Array)

    The array to convert to JSON.

  • context (Cuprum::Rails::Serializers::Context)

    The serialization context, which includes the configured serializers for attributes or collection items.

Returns:

  • (Array)

    a JSON-compatible representation of the array.

Raises:

  • UndefinedSerializerError if there is no matching serializer for any of the items in the array.



26
27
28
29
30
# File 'lib/cuprum/rails/serializers/json/array_serializer.rb', line 26

def call(array, context:)
  raise ArgumentError, 'object must be an Array' unless array.is_a?(Array)

  array.map { |item| super(item, context: context) }
end