Class: MultiJSON::Adapters::FastJsonparser Private

Inherits:
MultiJSON::Adapter show all
Defined in:
lib/multi_json/adapters/fast_jsonparser.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 FastJsonparser library to load, and the fastest other available adapter to dump.

FastJsonparser only implements parsing, so the “dump“ side of the adapter is delegated to whichever adapter MultiJSON would pick if FastJsonparser weren’t installed (oj → yajl → jr_jackson → json_gem → gson). The delegate is resolved lazily at the first “dump“ call, not at file load time, so load order doesn’t lock in the wrong delegate. Require any preferred dump backend before the first “dump“ call (typical applications already have “oj“ loaded by then).

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

::FastJsonparser::ParseError

Constants included from Options

Options::EMPTY_OPTIONS

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from MultiJSON::Adapter

default_dump_options, default_generate_options, default_load_options, default_parse_options, defaults, 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=

Class 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 via the lazy delegate

Examples:

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

Parameters:

  • object (Object)

    object to serialize

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

    serialization options

Returns:

  • (String)

    JSON string



35
36
37
# File 'lib/multi_json/adapters/fast_jsonparser.rb', line 35

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

Instance Method Details

#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

FastJsonparser.parse only accepts “symbolize_keys“ and raises on unknown keyword arguments, so the adapter explicitly forwards MultiJSON’s canonical “:symbolize_names“ option as FastJsonparser’s native “symbolize_keys:“ kwarg and silently drops the rest. Pass other options through “MultiJSON.parse_options=“ and they’ll apply to whichever adapter MultiJSON selects when fast_jsonparser isn’t installed.

Examples:

Parse JSON string

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

Parameters:

  • string (String)

    JSON string to parse

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

    parsing options (only :symbolize_names is honored)

Returns:

  • (Object)

    parsed Ruby object



69
70
71
# File 'lib/multi_json/adapters/fast_jsonparser.rb', line 69

def load(string, options = {})
  ::FastJsonparser.parse(string, symbolize_keys: options[:symbolize_names])
end