Method: Object#write_to_json

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

#write_to_json(file, options = {}) ⇒ self

Writes the Object serialized as JSON to the specified file, overwriting the file if it exists. Creates the file if it does not exist, including any necessary parent directories. Returns the Object, unmodified.

For information about options see JSON.generate. By default, this method uses JSON.dump_default_options.

Examples:

{ "key" => "value" }.write_to_json("file.json")  # == { "key" => "value" }
File.read("file.json")                           # == '{"key":"value"}'

Parameters:

Returns:

  • (self)


21
22
23
24
25
# File 'lib/pleasant_path/json/object.rb', line 21

def write_to_json(file, options = {})
  options = JSON.dump_default_options.merge(options)
  file.to_pathname.write_text(self.to_json(options))
  self
end