Method: Pathname#load_json

Defined in:
lib/pleasant_path/json/pathname.rb

#load_json(options = {}) ⇒ Object

Reads the file indicated by the Pathname, and parses the contents as JSON, deserializing arbitrary data types via type information embedded in the JSON. This is UNSAFE for JSON from an untrusted source. To consume untrusted JSON, use #read_json instead.

For information about options, see JSON.parse. By default, this method uses JSON.load_default_options.

For information about serializing custom types to JSON, see the JSON readme.

Examples:

require "json/add/core" # provides Struct#to_json
Point = Struct.new(:x, :y)
point = Point.new(10, 20)
File.write("file.json", point.to_json)

Pathname.new("file.json").load_json  # == Point.new(10, 20)

Parameters:

  • options (Hash{Symbol => Object}) (defaults to: {})

Returns:



52
53
54
# File 'lib/pleasant_path/json/pathname.rb', line 52

def load_json(options = {})
  JSON.load(self, nil, options)
end