Method: Dry::Transformer::HashTransformations.deep_stringify_keys

Defined in:
lib/dry/transformer/hash_transformations.rb

.deep_stringify_keys(hash) ⇒ Hash

Stringify keys in a hash recursively

Examples:

input = { :foo => "bar", :baz => [{ :one => 1 }] }

t(:deep_stringify_keys)[input]
# => { "foo" => "bar", "baz" => [{ "one" => 1 }] }

Parameters:

  • (Hash)

Returns:

  • (Hash)


116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/dry/transformer/hash_transformations.rb', line 116

def self.deep_stringify_keys(hash)
  hash.each_with_object({}) do |(key, value), output|
    output[key.to_s] =
      case value
      when ::Hash
        deep_stringify_keys(value)
      when ::Array
        value.map { |item|
          item.is_a?(Hash) ? deep_stringify_keys(item) : item
        }
      else
        value
      end
  end
end