Sha256: 8697cf2678353729966e7b6338e5b6a29166dc829fb027b394643cc3e35a3169

Contents?: true

Size: 1.98 KB

Versions: 1

Compression:

Stored size: 1.98 KB

Contents

# frozen_string_literal: true

class Pathname

  # Parses the contents of the file indicated by the Pathname as JSON.
  # The returned result will composed of only basic data types, e.g.
  # +nil+, +true+, +false+, +Numeric+, +String+, +Array+, and +Hash+.
  #
  # For information about +options+, see
  # {https://docs.ruby-lang.org/en/trunk/JSON.html#method-i-parse
  # +JSON.parse+}.  By default, this method uses
  # {https://docs.ruby-lang.org/en/trunk/JSON.html#attribute-c-load_default_options
  # +JSON.load_default_options+} plus +create_additions: false+.
  #
  # @example
  #   File.write("in.json", '{"key": "value"}')
  #
  #   Pathname.new("in.json").read_json  # == { "key" => "value" }
  #
  # @param options [Hash<Symbol, Object>]
  # @return [nil, true, false, Numeric, String, Symbol, Array, Hash]
  def read_json(options = {})
    JSON.load(self, nil, { create_additions: false, **options })
  end

  # Parses the contents of the file indicated by the Pathname 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 {Pathname#read_json} instead.
  #
  # For information about +options+, see
  # {https://docs.ruby-lang.org/en/trunk/JSON.html#method-i-parse
  # +JSON.parse+}.  By default, this method uses
  # {https://docs.ruby-lang.org/en/trunk/JSON.html#attribute-c-load_default_options
  # +JSON.load_default_options+}.
  #
  # For information about serializing custom types to JSON, see the
  # {https://github.com/flori/json/blob/master/README.md#more-examples
  # JSON readme}.
  #
  # @example
  #   require "json/add/core" # provides Struct#to_json
  #   Point = Struct.new(:x, :y)
  #   point = Point.new(10, 20)
  #   File.write("in.json", point.to_json)
  #
  #   Pathname.new("in.json").load_json  # == Point.new(10, 20)
  #
  # @param options [Hash<Symbol, Object>]
  # @return [Object]
  def load_json(options = {})
    JSON.load(self, nil, options)
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pleasant_path-1.3.0 lib/pleasant_path/json/pathname.rb