Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance RDoc for JSON.parse #433

Merged
merged 1 commit into from Jul 4, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
49 changes: 40 additions & 9 deletions lib/json/common.rb
Expand Up @@ -4,13 +4,16 @@

module JSON
class << self
# :call-seq:
# JSON[object] -> new_array or new_string
#
# If +object+ is a
# {String-convertible object}[doc/implicit_conversion_rdoc.html#label-String-Convertible+Objects]
# (implementing +to_str+), calls JSON.parse with +object+ and +opts+:
# {String-convertible object}[doc/implicit_conversion_rdoc.html#label-String-Convertible+Objects],
# calls JSON.parse with +object+ and +opts+ (see method #parse):
# json = '[0, 1, null]'
# JSON[json]# => [0, 1, nil]
#
# Otherwise, calls JSON.generate with +object+ and +opts+:
# Otherwise, calls JSON.generate with +object+ and +opts+ (see method #generate):
# ruby = [0, 1, nil]
# JSON[ruby] # => '[0,1,null]'
def [](object, opts = {})
Expand Down Expand Up @@ -171,10 +174,24 @@ class MissingUnicodeSupport < JSONError; end
# For examples of parsing for all \JSON data types, see
# {Parsing \JSON}[#module-JSON-label-Parsing+JSON].
#
# ====== Exceptions
# Parses nested JSON objects:
# source = <<-EOT
# {
# "name": "Dave",
# "age" :40,
# "hats": [
# "Cattleman's",
# "Panama",
# "Tophat"
# ]
# }
# EOT
# ruby = JSON.parse(source)
# ruby # => {"name"=>"Dave", "age"=>40, "hats"=>["Cattleman's", "Panama", "Tophat"]}
#
# Raises an exception if +source+ is not valid JSON:
# ---
#
# Raises an exception if +source+ is not valid JSON:
# # Raises JSON::ParserError (783: unexpected token at ''):
# JSON.parse('')
#
Expand All @@ -201,12 +218,24 @@ def parse!(source, opts = {})
Parser.new(source, **(opts||{})).parse
end

# Parses the content of a file (see parse method documentation for more information).
# :call-seq:
# CSV.load_file(path, opts={}) -> object
#
# Calls:
# parse(File.read(path), opts)
#
# See method #parse.
def load_file(filespec, opts = {})
parse(File.read(filespec), opts)
end

# Parses the content of a file (see parse! method documentation for more information).
# :call-seq:
# CSV.load_file!(path, opts = {})
#
# Calls:
# CSV.parse!(File.read(path, opts))
#
# See method #parse!
def load_file!(filespec, opts = {})
parse!(File.read(filespec), opts)
end
Expand Down Expand Up @@ -247,8 +276,6 @@ def load_file!(filespec, opts = {})
#
# Raises an exception if any formatting option is not a \String.
#
# ====== Exceptions
#
# Raises an exception if +obj+ contains circular references:
# a = []; b = []; a.push(b); b.push(a)
# # Raises JSON::NestingError (nesting of 100 is too deep):
Expand Down Expand Up @@ -280,6 +307,9 @@ def generate(obj, opts = nil)
module_function :unparse
# :startdoc:

# :call-seq:
# JSON.fast_generate(obj, opts) -> new_string
#
# Arguments +obj+ and +opts+ here are the same as
# arguments +obj+ and +opts+ in JSON.generate.
#
Expand Down Expand Up @@ -398,6 +428,7 @@ class << self
#
# This method is part of the implementation of the load/dump interface of
# Marshal and YAML.
#
def load(source, proc = nil, options = {})
opts = load_default_options.merge options
if source.respond_to? :to_str
Expand Down