Module: Gitlab::Json

Defined in:
lib/gitlab/json.rb

Defined Under Namespace

Classes: GrapeFormatter, LimitedEncoder

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)

41
42
43
# File 'lib/gitlab/json.rb', line 41

def dump(object)
  adapter_dump(object)
end

.enable_oj?Boolean

Feature detection for using Oj instead of the `json` gem.

Returns:

  • (Boolean)

73
74
75
76
77
# File 'lib/gitlab/json.rb', line 73

def enable_oj?
  return false unless feature_table_exists?

  Feature.enabled?(:oj_json, default_enabled: true)
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)

51
52
53
# File 'lib/gitlab/json.rb', line 51

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

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

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
# File 'lib/gitlab/json.rb', line 18

def parse(string, opts = {})
  # 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

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

66
67
68
# File 'lib/gitlab/json.rb', line 66

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