Module: Gitlab::Json

Defined in:
lib/gitlab/json.rb

Defined Under Namespace

Classes: GrapeFormatter, LimitedEncoder, PrecompiledJson

Constant Summary collapse

INVALID_LEGACY_TYPES =
[String, TrueClass, FalseClass].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.

Parameters:

  • object (Object)

    the object to convert to JSON

Returns:

  • (String)

45
46
47
# File 'lib/gitlab/json.rb', line 45

def dump(object)
  adapter_dump(object)
end

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

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)

55
56
57
# File 'lib/gitlab/json.rb', line 55

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

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

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


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/gitlab/json.rb', line 18

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))
  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)

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

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)

70
71
72
# File 'lib/gitlab/json.rb', line 70

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