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
-
.dump(object) ⇒ String
Restricted method for converting a Ruby object to JSON.
-
.generate(object, opts = {}) ⇒ String
Generates JSON for an object.
-
.parse(string, opts = {}) ⇒ Boolean, ...
(also: parse!, load)
Parse a string and convert it to a Ruby object.
-
.parser_error ⇒ JSON::ParserError
The standard parser error we should be returning.
-
.pretty_generate(object, opts = {}) ⇒ String
Generates JSON for an object and makes it look purdy.
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.
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.
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
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_error ⇒ JSON::ParserError
The standard parser error we should be returning. Defined in a method so we can potentially override it later.
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
70 71 72 |
# File 'lib/gitlab/json.rb', line 70 def pretty_generate(object, opts = {}) ::JSON.pretty_generate(object, opts) end |