Class: MultiJSON::Adapters::JsonGem Private

Inherits:
MultiJSON::Adapter show all
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

Options::EMPTY_OPTIONS

Instance Method Summary collapse

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

Examples:

Serialize object to JSON

adapter.dump({key: "value"}) #=> '{"key":"value"}'

Parameters:

  • object (Object)

    object to serialize

  • options (Hash) (defaults to: {})

    serialization options

Returns:

  • (String)

    JSON string



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/multi_json/adapters/json_gem.rb', line 59

def dump(object, options = {})
  json_object = object.respond_to?(:as_json) ? object.as_json : object
  return ::JSON.dump(json_object) if options.empty?
  return ::JSON.generate(json_object, options) unless options.key?(: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 options.size == 1

  ::JSON.pretty_generate(json_object, PRETTY_STATE_PROTOTYPE.merge(options.except(:pretty)))
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.

Examples:

Parse JSON string

adapter.load('{"key":"value"}') #=> {"key" => "value"}

Parameters:

  • string (String)

    JSON string to parse

  • options (Hash) (defaults to: {})

    parsing options

Returns:

  • (Object)

    parsed Ruby object

Raises:

  • (::JSON::ParserError)

    when the input is not valid UTF-8



41
42
43
44
45
46
47
48
# File 'lib/multi_json/adapters/json_gem.rb', line 41

def load(string, options = {})
  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, options)
end