summaryrefslogtreecommitdiff
path: root/lib/serializers/json.rb
blob: 93cb192087af8891f1561b134769f05c8ef859aa (plain)
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
34
# frozen_string_literal: true

module Serializers
  # This serializer exports data as JSON,
  # it is designed to be used with interwork compatibility between MySQL and PostgreSQL
  # implementations, as used version of MySQL does not support native json type
  #
  # Secondly, the loader makes the resulting hash to have deep indifferent access
  class JSON
    class << self
      def dump(obj)
        # MySQL stores data as text
        # look at ./config/initializers/ar_mysql_jsonb_support.rb
        if Gitlab::Database.mysql?
          obj = ActiveSupport::JSON.encode(obj)
        end

        obj
      end

      def load(data)
        return if data.nil?

        # On MySQL we store data as text
        # look at ./config/initializers/ar_mysql_jsonb_support.rb
        if Gitlab::Database.mysql?
          data = ActiveSupport::JSON.decode(data)
        end

        Gitlab::Utils.deep_indifferent_access(data)
      end
    end
  end
end