2020-04-21 11:21:10 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Json
|
2020-05-06 17:10:00 -04:00
|
|
|
INVALID_LEGACY_TYPES = [String, TrueClass, FalseClass].freeze
|
|
|
|
|
2020-04-21 11:21:10 -04:00
|
|
|
class << self
|
2020-07-09 20:09:13 -04:00
|
|
|
def parse(string, *args, **named_args)
|
|
|
|
legacy_mode = legacy_mode_enabled?(named_args.delete(:legacy_mode))
|
|
|
|
data = adapter.parse(string, *args, **named_args)
|
2020-05-06 17:10:00 -04:00
|
|
|
|
|
|
|
handle_legacy_mode!(data) if legacy_mode
|
|
|
|
|
|
|
|
data
|
2020-04-21 11:21:10 -04:00
|
|
|
end
|
|
|
|
|
2020-07-09 20:09:13 -04:00
|
|
|
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
|
|
|
|
end
|
2020-04-21 11:21:10 -04:00
|
|
|
|
2020-07-09 20:09:13 -04:00
|
|
|
def dump(*args)
|
|
|
|
adapter.dump(*args)
|
2020-04-21 11:21:10 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def generate(*args)
|
2020-07-09 20:09:13 -04:00
|
|
|
adapter.generate(*args)
|
2020-04-21 11:21:10 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def pretty_generate(*args)
|
2020-07-09 20:09:13 -04:00
|
|
|
adapter.pretty_generate(*args)
|
2020-04-21 11:21:10 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2020-07-09 20:09:13 -04:00
|
|
|
def adapter
|
|
|
|
::JSON
|
2020-04-21 11:21:10 -04:00
|
|
|
end
|
2020-05-06 17:10:00 -04:00
|
|
|
|
|
|
|
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-21 11:21:10 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|