Module: Gitlab::Json
- Defined in:
- lib/gitlab/json.rb
Defined Under Namespace
Classes: GrapeFormatter, LimitedEncoder, PrecompiledJson, RailsEncoder
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
(also: encode)
Generates JSON for an object.
-
.parse(string, opts = {}) ⇒ Boolean, ...
(also: parse!, load, decode)
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.
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.
51 52 53 |
# File 'lib/gitlab/json.rb', line 51 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.
61 62 63 |
# File 'lib/gitlab/json.rb', line 61 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
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.
86 87 88 |
# File 'lib/gitlab/json.rb', line 86 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
78 79 80 |
# File 'lib/gitlab/json.rb', line 78 def pretty_generate(object, opts = {}) ::JSON.pretty_generate(object, opts) end |