Class: NOSJ::MultiJsonAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/nosj/multi_json.rb,
sig/nosj.rbs

Overview

Defined by require "nosj/multi_json". The runtime superclass is multi_json's Adapter (whose namespace differs across multi_json versions), so it is not declared here.

Constant Summary collapse

ParseError =

multi_json wraps whatever the adapter's ParseError names.

Returns:

  • (singleton(::JSON::ParserError))
::JSON::ParserError

Instance Method Summary collapse

Instance Method Details

#dump(object, options = {}) ⇒ String

Returns the JSON document.

Parameters:

  • object (Object)

    the value tree to serialize

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

    multi_json dump options; pretty selects pretty-printing

Returns:

  • (String)

    the JSON document



47
48
49
# File 'lib/nosj/multi_json.rb', line 47

def dump(object, options = {})
  options[:pretty] ? ::NOSJ.pretty_generate(object) : ::NOSJ.generate(object)
end

#load(string, options = {}) ⇒ Object

Returns the parsed value tree.

Parameters:

  • string (String)

    the JSON document

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

    multi_json load options; symbolize_keys arrives normalized as symbolize_names

Returns:

  • (Object)

    the parsed value tree

Raises:

  • (JSON::ParserError)

    when the document is malformed



37
38
39
40
41
# File 'lib/nosj/multi_json.rb', line 37

def load(string, options = {})
  ::NOSJ.parse(string, options[:symbolize_names] ? SYMBOLIZE : nil)
rescue RuntimeError => e
  raise ParseError, e.message
end