[flori/json] Enhance RDoc for JSON.parse
https://github.com/flori/json/commit/33e64ef255
This commit is contained in:
parent
71b1bbad02
commit
36b2177ea8
Notes:
git
2020-09-25 17:29:14 +09:00
@ -4,13 +4,16 @@ require 'json/generic_object'
|
|||||||
|
|
||||||
module JSON
|
module JSON
|
||||||
class << self
|
class << self
|
||||||
|
# :call-seq:
|
||||||
|
# JSON[object] -> new_array or new_string
|
||||||
|
#
|
||||||
# If +object+ is a
|
# If +object+ is a
|
||||||
# {String-convertible object}[doc/implicit_conversion_rdoc.html#label-String-Convertible+Objects]
|
# {String-convertible object}[doc/implicit_conversion_rdoc.html#label-String-Convertible+Objects],
|
||||||
# (implementing +to_str+), calls JSON.parse with +object+ and +opts+:
|
# calls JSON.parse with +object+ and +opts+ (see method #parse):
|
||||||
# json = '[0, 1, null]'
|
# json = '[0, 1, null]'
|
||||||
# JSON[json]# => [0, 1, nil]
|
# 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]
|
# ruby = [0, 1, nil]
|
||||||
# JSON[ruby] # => '[0,1,null]'
|
# JSON[ruby] # => '[0,1,null]'
|
||||||
def [](object, opts = {})
|
def [](object, opts = {})
|
||||||
@ -171,10 +174,24 @@ module JSON
|
|||||||
# For examples of parsing for all \JSON data types, see
|
# For examples of parsing for all \JSON data types, see
|
||||||
# {Parsing \JSON}[#module-JSON-label-Parsing+JSON].
|
# {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 ''):
|
# # Raises JSON::ParserError (783: unexpected token at ''):
|
||||||
# JSON.parse('')
|
# JSON.parse('')
|
||||||
#
|
#
|
||||||
@ -201,12 +218,24 @@ module JSON
|
|||||||
Parser.new(source, **(opts||{})).parse
|
Parser.new(source, **(opts||{})).parse
|
||||||
end
|
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 = {})
|
def load_file(filespec, opts = {})
|
||||||
parse(File.read(filespec), opts)
|
parse(File.read(filespec), opts)
|
||||||
end
|
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 = {})
|
def load_file!(filespec, opts = {})
|
||||||
parse!(File.read(filespec), opts)
|
parse!(File.read(filespec), opts)
|
||||||
end
|
end
|
||||||
@ -247,8 +276,6 @@ module JSON
|
|||||||
#
|
#
|
||||||
# Raises an exception if any formatting option is not a \String.
|
# Raises an exception if any formatting option is not a \String.
|
||||||
#
|
#
|
||||||
# ====== Exceptions
|
|
||||||
#
|
|
||||||
# Raises an exception if +obj+ contains circular references:
|
# Raises an exception if +obj+ contains circular references:
|
||||||
# a = []; b = []; a.push(b); b.push(a)
|
# a = []; b = []; a.push(b); b.push(a)
|
||||||
# # Raises JSON::NestingError (nesting of 100 is too deep):
|
# # Raises JSON::NestingError (nesting of 100 is too deep):
|
||||||
@ -280,6 +307,9 @@ module JSON
|
|||||||
module_function :unparse
|
module_function :unparse
|
||||||
# :startdoc:
|
# :startdoc:
|
||||||
|
|
||||||
|
# :call-seq:
|
||||||
|
# JSON.fast_generate(obj, opts) -> new_string
|
||||||
|
#
|
||||||
# Arguments +obj+ and +opts+ here are the same as
|
# Arguments +obj+ and +opts+ here are the same as
|
||||||
# arguments +obj+ and +opts+ in JSON.generate.
|
# arguments +obj+ and +opts+ in JSON.generate.
|
||||||
#
|
#
|
||||||
@ -398,6 +428,7 @@ module JSON
|
|||||||
#
|
#
|
||||||
# This method is part of the implementation of the load/dump interface of
|
# This method is part of the implementation of the load/dump interface of
|
||||||
# Marshal and YAML.
|
# Marshal and YAML.
|
||||||
|
#
|
||||||
def load(source, proc = nil, options = {})
|
def load(source, proc = nil, options = {})
|
||||||
opts = load_default_options.merge options
|
opts = load_default_options.merge options
|
||||||
if source.respond_to? :to_str
|
if source.respond_to? :to_str
|
||||||
|
Loading…
x
Reference in New Issue
Block a user