Module: Ryo::JSON

Extended by:
JSON
Included in:
JSON
Defined in:
lib/ryo/json.rb

Overview

The Ryo::JSON module provides a number of options for coercing JSON data into a Ryo object. The methods of this module are available on the Ryo module as singleton methods

Instance Method Summary collapse

Instance Method Details

#from_json(path: nil, string: nil, object: Ryo::Object) ⇒ Ryo::Object, Ryo::BasicObject

Returns a Ryo object

Examples:

Ryo.from_json(path: "/foo/bar/baz.json")
Ryo.from_json(string: "[]")

Parameters:

  • path (String) (defaults to: nil)

    The path to a JSON file

  • string (String) (defaults to: nil)

    A blob of JSON

  • object (Ryo) (defaults to: Ryo::Object)

Returns:

Raises:

  • (SystemCallError)

    Might raise a number of Errno exceptions



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ryo/json.rb', line 26

def from_json(path: nil, string: nil, object: Ryo::Object)
  if path && string
    raise ArgumentError, "Provide a path or string but not both"
  elsif path
    require "json" unless defined?(JSON)
    object.from JSON.parse(File.binread(path))
  elsif string
    require "json" unless defined?(JSON)
    object.from JSON.parse(string)
  else
    raise ArgumentError, "No path or string provided"
  end
end