Class: MultiJSON::Adapter Private

Inherits:
Object
  • Object
show all
Extended by:
Options
Includes:
Singleton
Defined in:
lib/multi_json/adapter.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.

Base class for JSON adapter implementations

Each adapter wraps a specific JSON library (Oj, JSON gem, etc.) and provides a consistent interface. Uses Singleton pattern so each adapter class has exactly one instance.

Subclasses must implement:

  • #load(string, options) -> parsed object

  • #dump(object, options) -> JSON string

Constant Summary

Constants included from Options

Options::EMPTY_OPTIONS

Class Method Summary collapse

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=

Class Method Details

.default_dump_optionsHash

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.

Deprecated.

Use default_generate_options instead. Will be removed in v2.0.

Get default generate options, walking the superclass chain

Returns:

  • (Hash)

    frozen options hash



62
63
64
# File 'lib/multi_json/adapter.rb', line 62

def default_dump_options
  default_generate_options
end

.default_generate_optionsHash

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.

Get default generate options, walking the superclass chain

Returns:

  • (Hash)

    frozen options hash



44
45
46
# File 'lib/multi_json/adapter.rb', line 44

def default_generate_options
  walk_default_options(:@default_dump_options)
end

.default_load_optionsHash

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.

Deprecated.

Use default_parse_options instead. Will be removed in v2.0.

Get default parse options, walking the superclass chain

Returns:

  • (Hash)

    frozen options hash



53
54
55
# File 'lib/multi_json/adapter.rb', line 53

def default_load_options
  default_parse_options
end

.default_parse_optionsHash

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.

Get default parse options, walking the superclass chain

Returns the closest ancestor’s ‘@default_load_options` ivar so a parent class calling defaults after a subclass has been defined still propagates to the subclass. Falls back to the shared frozen empty hash when no ancestor has defaults set.

Returns:

  • (Hash)

    frozen options hash



36
37
38
# File 'lib/multi_json/adapter.rb', line 36

def default_parse_options
  walk_default_options(:@default_load_options)
end

.defaults(action, value) ⇒ Hash

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.

DSL for setting adapter-specific default options

“action“ must be “:load“ or “:dump“; “value“ must be a Hash. Both arguments are validated up front so a typo at the adapter’s class definition fails fast instead of producing a silent no-op default that crashes later in the merge path.

Examples:

Set load defaults for an adapter

class MyAdapter < MultiJSON::Adapter
  defaults :load, symbolize_keys: false
end

Parameters:

  • action (Symbol)

    :load or :dump

  • value (Hash)

    default options for the action

Returns:

  • (Hash)

    the frozen options hash

Raises:

  • (ArgumentError)

    when action is anything other than :load or :dump, or when value isn’t a Hash



83
84
85
86
87
88
# File 'lib/multi_json/adapter.rb', line 83

def defaults(action, value)
  raise ArgumentError, "expected action to be :load or :dump, got #{action.inspect}" unless VALID_DEFAULTS_ACTIONS.include?(action)
  raise ArgumentError, "expected value to be a Hash, got #{value.class}" unless value.is_a?(Hash)

  instance_variable_set(:"@default_#{action}_options", value.freeze)
end

.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

Parameters:

  • object (Object)

    object to serialize

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

    serialization options

Returns:

  • (String)

    JSON string



109
110
111
# File 'lib/multi_json/adapter.rb', line 109

def dump(object, options = {})
  instance.dump(object, merged_dump_options(options))
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

Parameters:

  • string (String, #read)

    JSON string or IO-like object

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

    parsing options

Returns:

  • (Object, nil)

    parsed object or nil for blank input



96
97
98
99
100
101
# File 'lib/multi_json/adapter.rb', line 96

def load(string, options = {})
  string = string.read if string.respond_to?(:read)
  return nil if blank?(string)

  instance.load(string, merged_load_options(options))
end