Module: Familia::Refinements::DearJsonArrayMethods

Defined in:
lib/familia/refinements/dear_json.rb

Instance Method Summary collapse

Instance Method Details

#as_json(options = nil) ⇒ Array

Convert array to JSON-serializable representation. This method recursively calls as_json on nested elements to ensure Familia objects are properly serialized in nested structures.

Parameters:

  • options (Hash) (defaults to: nil)

    Optional parameters (currently unused)

Returns:

  • (Array)

    A new array with all elements converted via as_json



103
104
105
106
107
108
109
110
111
112
# File 'lib/familia/refinements/dear_json.rb', line 103

def as_json(options = nil)
  # Create a new array, calling as_json on each element.
  map do |item|
    if item.respond_to?(:as_json)
      item.as_json(options)
    else
      item
    end
  end
end

#to_json(options = nil) ⇒ String

Convert array to JSON string using Familia's secure JsonSerializer. This method preprocesses the array to handle Familia objects properly by calling as_json on any objects that support it.

Parameters:

  • options (Hash) (defaults to: nil)

    Optional parameters (currently unused, for compatibility)

Returns:

  • (String)

    JSON string representation



83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/familia/refinements/dear_json.rb', line 83

def to_json(options = nil)
  # Preprocess the array to handle Familia objects
  processed_array = map do |item|
    if item.respond_to?(:as_json)
      item.as_json(options)
    else
      item
    end
  end

  Familia::JsonSerializer.dump(processed_array)
end