2020-04-22 19:07:51 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Json
|
2020-05-24 23:13:21 +05:30
|
|
|
INVALID_LEGACY_TYPES = [String, TrueClass, FalseClass].freeze
|
|
|
|
|
2020-04-22 19:07:51 +05:30
|
|
|
class << self
|
2020-05-24 23:13:21 +05:30
|
|
|
def parse(string, *args, **named_args)
|
|
|
|
legacy_mode = legacy_mode_enabled?(named_args.delete(:legacy_mode))
|
|
|
|
data = adapter.parse(string, *args, **named_args)
|
|
|
|
|
|
|
|
handle_legacy_mode!(data) if legacy_mode
|
|
|
|
|
|
|
|
data
|
2020-04-22 19:07:51 +05:30
|
|
|
end
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
def parse!(string, *args, **named_args)
|
|
|
|
legacy_mode = legacy_mode_enabled?(named_args.delete(:legacy_mode))
|
|
|
|
data = adapter.parse!(string, *args, **named_args)
|
|
|
|
|
|
|
|
handle_legacy_mode!(data) if legacy_mode
|
|
|
|
|
|
|
|
data
|
2020-04-22 19:07:51 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def dump(*args)
|
|
|
|
adapter.dump(*args)
|
|
|
|
end
|
|
|
|
|
|
|
|
def generate(*args)
|
|
|
|
adapter.generate(*args)
|
|
|
|
end
|
|
|
|
|
|
|
|
def pretty_generate(*args)
|
|
|
|
adapter.pretty_generate(*args)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def adapter
|
|
|
|
::JSON
|
|
|
|
end
|
2020-05-24 23:13:21 +05:30
|
|
|
|
|
|
|
def parser_error
|
|
|
|
::JSON::ParserError
|
|
|
|
end
|
|
|
|
|
|
|
|
def legacy_mode_enabled?(arg_value)
|
|
|
|
arg_value.nil? ? false : arg_value
|
|
|
|
end
|
|
|
|
|
|
|
|
def handle_legacy_mode!(data)
|
|
|
|
return data unless Feature.enabled?(:json_wrapper_legacy_mode, default_enabled: true)
|
|
|
|
|
|
|
|
raise parser_error if INVALID_LEGACY_TYPES.any? { |type| data.is_a?(type) }
|
|
|
|
end
|
2020-04-22 19:07:51 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|