Class: MultiJSON::Adapters::JsonGem Private
- Inherits:
-
MultiJSON::Adapter
- Object
- MultiJSON::Adapter
- MultiJSON::Adapters::JsonGem
- Defined in:
- lib/multi_json/adapters/json_gem.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
Use the JSON gem to dump/load.
Constant Summary collapse
- ParseError =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Exception raised when JSON parsing fails
::JSON::ParserError
Constants included from Options
Instance Method Summary collapse
-
#dump(object, options = {}) ⇒ String
private
Serialize a Ruby object to JSON.
-
#load(string, options = {}) ⇒ Object
private
Parse a JSON string into a Ruby object.
Methods inherited from MultiJSON::Adapter
default_dump_options, default_generate_options, default_load_options, default_parse_options, defaults, dump, load
Methods included from Options
#default_dump_options, #default_generate_options, #default_load_options, #default_parse_options, #dump_options, #dump_options=, #generate_options, #generate_options=, #load_options, #load_options=, #parse_options, #parse_options=
Instance Method Details
#dump(object, options = {}) ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Serialize a Ruby object to JSON
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/multi_json/adapters/json_gem.rb', line 59 def dump(object, = {}) json_object = object.respond_to?(:as_json) ? object.as_json : object return ::JSON.dump(json_object) if .empty? return ::JSON.generate(json_object, ) unless .key?(:pretty) # ``:pretty`` is MultiJSON's own option, not one the JSON gem's # generator understands, so it's stripped rather than passed # through. A falsy value asks for compact output, which is what # the generator produces with no pretty state at all. rest = .except(:pretty) return generate_compact(json_object, rest) unless [:pretty] # Common case: ``pretty: true`` is the only option, so the merge # would just produce a copy of PRETTY_STATE_PROTOTYPE. return ::JSON.pretty_generate(json_object, PRETTY_STATE_PROTOTYPE) if rest.empty? ::JSON.pretty_generate(json_object, PRETTY_STATE_PROTOTYPE.merge(rest)) end |
#load(string, options = {}) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Parse a JSON string into a Ruby object
Non-UTF-8 strings are re-labeled via force_encoding (not
transcoded) and then validated. This handles the dominant
real-world case: Ruby HTTP libraries return response bodies
tagged as ASCII-8BIT even when the bytes are valid UTF-8.
encode(Encoding::UTF_8) would raise on any multi-byte
sequence in that scenario because it tries to transcode each
byte individually from ASCII-8BIT to UTF-8.
41 42 43 44 45 46 47 48 |
# File 'lib/multi_json/adapters/json_gem.rb', line 41 def load(string, = {}) if string.encoding != Encoding::UTF_8 string = string.dup.force_encoding(Encoding::UTF_8) raise ::JSON::ParserError, "Invalid UTF-8 byte sequence in JSON input" unless string.valid_encoding? end ::JSON.parse(string, **) end |