Module: Gitlab::Json

Defined in:
lib/gitlab/json.rb,
lib/gitlab/json/stream_validator.rb

Defined Under Namespace

Classes: GrapeFormatter, LimitedEncoder, PrecompiledJson, RailsEncoder, StreamValidator

Constant Summary collapse

INVALID_LEGACY_TYPES =
[String, TrueClass, FalseClass].freeze
PARSE_LIMITS =
{
  max_depth: 32,
  max_array_size: 50000,
  max_hash_size: 50000,
  max_total_elements: 100000,
  max_json_size_bytes: 20.megabytes
}.freeze

Class Method Summary collapse

Class Method Details

.dump(object) ⇒ String

Restricted method for converting a Ruby object to JSON. If you need to pass options to this, you should use .generate instead, as the underlying implementation of this varies wildly based on the adapter in use.

This method does, in some situations, differ in the data it returns compared to .generate. Counter-intuitively, this is closest in terms of response to JSON.generate and to the default ActiveSupport .to_json method.

Parameters:

  • object (Object)

    the object to convert to JSON

Returns:

  • (String)


78
79
80
# File 'lib/gitlab/json.rb', line 78

def dump(object)
  adapter_dump(object)
end

.generate(object, opts = {}) ⇒ String Also known as: encode

Generates JSON for an object. In Oj this takes fewer options than .dump, in the JSON gem this is the only method which takes an options argument.

Parameters:

  • object (Hash, Array, Object)

    must be hash, array, or an object that responds to .to_h or .to_json

  • opts (Hash) (defaults to: {})

    an options hash with fewer supported settings than .dump

Returns:

  • (String)


88
89
90
# File 'lib/gitlab/json.rb', line 88

def generate(object, opts = {})
  adapter_generate(object, opts)
end

.parse(string, opts = {}) ⇒ Boolean, ... Also known as: parse!, load, decode

Parse a string and convert it to a Ruby object

Parameters:

  • string (String)

    the JSON string to convert to Ruby objects

  • opts (Hash) (defaults to: {})

    an options hash in the standard JSON gem format

Returns:

  • (Boolean, String, Array, Hash)

Raises:

  • (JSON::ParserError)

    raised if parsing fails



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/gitlab/json.rb', line 26

def parse(string, opts = {})
  # Parse nil as nil
  return if string.nil?

  # First we should ensure this really is a string, not some other
  # type which purports to be a string. This handles some legacy
  # usage of the JSON class.
  string = string.to_s unless string.is_a?(String)

  legacy_mode = legacy_mode_enabled?(opts.delete(:legacy_mode))

  log_oversize_object(string)

  data = adapter_load(string, **opts)

  handle_legacy_mode!(data) if legacy_mode

  data
end

.parser_errorJSON::ParserError

The standard parser error we should be returning. Defined in a method so we can potentially override it later.

Returns:

  • (JSON::ParserError)


113
114
115
# File 'lib/gitlab/json.rb', line 113

def parser_error
  ::JSON::ParserError
end

.pretty_generate(object, opts = {}) ⇒ String

Generates JSON for an object and makes it look purdy

The Oj variant in this looks seriously weird but these are the settings needed to emulate the style generated by the JSON gem.

NOTE: This currently ignores Oj, because Oj doesn’t generate identical

formatting, issue: https://github.com/ohler55/oj/issues/608

Parameters:

  • object (Hash, Array, Object)

    must be hash, array, or an object that responds to .to_h or .to_json

  • opts (Hash) (defaults to: {})

    an options hash with fewer supported settings than .dump

Returns:

  • (String)


105
106
107
# File 'lib/gitlab/json.rb', line 105

def pretty_generate(object, opts = {})
  ::JSON.pretty_generate(object, opts)
end

.safe_parse(string, opts = {}) ⇒ Boolean, ...

Parse a string and convert it to a Ruby object, but with limits

Parameters:

  • string (String)

    the JSON string to convert to Ruby objects

  • opts (Hash) (defaults to: {})

    an options hash in the standard JSON gem format

Returns:

  • (Boolean, String, Array, Hash)

Raises:

  • (JSON::ParserError)

    raised if parsing fails



56
57
58
59
60
61
62
63
64
# File 'lib/gitlab/json.rb', line 56

def safe_parse(string, opts = {})
  return if string.nil?

  parse_limits = PARSE_LIMITS.merge(opts.delete(:parse_limits) || {})

  validate!(string, parse_limits)

  parse(string, opts)
end