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

Class Method Summary collapse

Methods included from Options

default_dump_options, default_load_options, dump_options, dump_options=, load_options, load_options=

Class Method Details

.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

Parameters:

  • action (Symbol)

    :load or :dump

  • value (Hash)

    default options for the action

Returns:

  • (Hash)

    the frozen options hash



42
43
44
# File 'lib/multi_json/adapter.rb', line 42

def defaults(action, value)
  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



65
66
67
# File 'lib/multi_json/adapter.rb', line 65

def dump(object, options = {})
  instance.dump(object, merged_dump_options(options))
end

.inherited(subclass) ⇒ void

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.

This method returns an undefined value.

Hook called when a subclass is created

Parameters:

  • subclass (Class)

    the new subclass



29
30
31
32
33
34
# File 'lib/multi_json/adapter.rb', line 29

def inherited(subclass)
  super
  # Propagate default options to subclasses
  subclass.instance_variable_set(:@default_load_options, @default_load_options) if defined?(@default_load_options)
  subclass.instance_variable_set(:@default_dump_options, @default_dump_options) if defined?(@default_dump_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



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

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