aboutsummaryrefslogtreecommitdiffstats
path: root/activejob/lib/active_job/serializers/hash_with_indifferent_access_serializer.rb
blob: af3576dd573420eae874e7f11ca9303e1ee22526 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# frozen_string_literal: true

module ActiveJob
  module Serializers
    # Provides methods to serialize and deserialize `ActiveSupport::HashWithIndifferentAccess`
    # Values will be serialized by known serializers
    class HashWithIndifferentAccessSerializer < HashSerializer
      class << self
        def serialize(hash)
          result = serialize_hash(hash)
          result[key] = Serializers.serialize(true)
          result
        end

        def deserialize(hash)
          result = hash.transform_values { |v| Serializers.deserialize(v) }
          result.delete(key)
          result.with_indifferent_access
        end

        private

        def key
          WITH_INDIFFERENT_ACCESS_KEY
        end

        def klass
          ActiveSupport::HashWithIndifferentAccess
        end
      end
    end
  end
end