Class: MultiJson::Adapters::OkJson

Inherits:
MultiJson::Adapter show all
Includes:
ConvertibleHashKeys
Defined in:
lib/multi_json/adapters/ok_json.rb

Overview

Use the vendored OkJson library to dump/load.

Constant Summary collapse

ParseError =
::MultiJson::OkJson::Error

Instance Method Summary collapse

Methods inherited from MultiJson::Adapter

defaults, dump, inherited, load

Methods included from Options

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

Instance Method Details

#dump(object, _ = {}) ⇒ 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

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

    serialization options (unused)

Returns:

  • (String)

    JSON string



38
39
40
# File 'lib/multi_json/adapters/ok_json.rb', line 38

def dump(object, _ = {})
  ::MultiJson::OkJson.valenc(stringify_keys(object))
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

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



22
23
24
25
26
27
# File 'lib/multi_json/adapters/ok_json.rb', line 22

def load(string, options = {})
  result = ::MultiJson::OkJson.decode("[#{string}]").first
  options[:symbolize_keys] ? symbolize_keys(result) : result
rescue ArgumentError # invalid byte sequence in UTF-8
  raise ParseError
end