class MultiJson::Adapter

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:

@api private

Constants

BLANK_PATTERN

Public Class Methods

defaults(action, value) click to toggle source

DSL for setting adapter-specific default options

@api private @param action [Symbol] :load or :dump @param value [Hash] default options for the action @return [Hash] the frozen options hash

# File lib/multi_json/adapter.rb, line 42
def defaults(action, value)
  instance_variable_set(:"@default_#{action}_options", value.freeze)
end
dump(object, options = {}) click to toggle source

Serialize a Ruby object to JSON

@api private @param object [Object] object to serialize @param options [Hash] serialization options @return [String] JSON string

# File lib/multi_json/adapter.rb, line 65
def dump(object, options = {})
  instance.dump(object, merged_dump_options(options))
end
inherited(subclass) click to toggle source

Hook called when a subclass is created

@api private @param subclass [Class] the new subclass @return [void]

Calls superclass method
# 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 = {}) click to toggle source

Parse a JSON string into a Ruby object

@api private @param string [String, read] JSON string or IO-like object @param options [Hash] parsing options @return [Object, nil] parsed object or nil for blank input

# 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

Private Class Methods

blank?(input) click to toggle source

Checks if the input is blank (nil or whitespace-only)

@api private @param input [String, nil] input to check @return [Boolean] true if input is blank

# File lib/multi_json/adapter.rb, line 76
def blank?(input)
  input.nil? || BLANK_PATTERN.match?(input)
rescue ArgumentError
  # Invalid byte sequence in UTF-8 - treat as non-blank
  false
end
merged_dump_options(options) click to toggle source

Merges dump options from adapter, global, and call-site

@api private @param options [Hash] call-site options @return [Hash] merged options hash

# File lib/multi_json/adapter.rb, line 88
def merged_dump_options(options)
  cache_key = strip_adapter_key(options)
  OptionsCache.dump.fetch(cache_key) do
    dump_options(cache_key).merge(MultiJson.dump_options(cache_key)).merge!(cache_key)
  end
end
merged_load_options(options) click to toggle source

Merges load options from adapter, global, and call-site

@api private @param options [Hash] call-site options @return [Hash] merged options hash

# File lib/multi_json/adapter.rb, line 100
def merged_load_options(options)
  cache_key = strip_adapter_key(options)
  OptionsCache.load.fetch(cache_key) do
    load_options(cache_key).merge(MultiJson.load_options(cache_key)).merge!(cache_key)
  end
end
strip_adapter_key(options) click to toggle source

Removes the :adapter key from options for cache key

@api private @param options [Hash] original options @return [Hash] frozen options without :adapter key

# File lib/multi_json/adapter.rb, line 112
def strip_adapter_key(options)
  options.except(:adapter).freeze
end