mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
[flori/json] RDoc enhancements
https://github.com/flori/json/commit/470d909c0d
This commit is contained in:
parent
99980b3034
commit
be6447381c
2 changed files with 22 additions and 22 deletions
|
@ -82,7 +82,7 @@ require 'json/common'
|
||||||
#
|
#
|
||||||
# \String:
|
# \String:
|
||||||
# ruby = JSON.parse('"foo"')
|
# ruby = JSON.parse('"foo"')
|
||||||
# ruby # => "foo"
|
# ruby # => 'foo'
|
||||||
# ruby.class # => String
|
# ruby.class # => String
|
||||||
# \Integer:
|
# \Integer:
|
||||||
# ruby = JSON.parse('1')
|
# ruby = JSON.parse('1')
|
||||||
|
@ -121,13 +121,13 @@ require 'json/common'
|
||||||
# a \String containing a \JSON array:
|
# a \String containing a \JSON array:
|
||||||
# ruby = [0, 's', :foo]
|
# ruby = [0, 's', :foo]
|
||||||
# json = JSON.generate(ruby)
|
# json = JSON.generate(ruby)
|
||||||
# json # => "[0,\"s\",\"foo\"]"
|
# json # => '[0,"s","foo"]'
|
||||||
#
|
#
|
||||||
# The Ruby \Array array may contain nested arrays, hashes, and scalars
|
# The Ruby \Array array may contain nested arrays, hashes, and scalars
|
||||||
# to any depth:
|
# to any depth:
|
||||||
# ruby = [0, [1, 2], {foo: 3, bar: 4}]
|
# ruby = [0, [1, 2], {foo: 3, bar: 4}]
|
||||||
# json = JSON.generate(ruby)
|
# json = JSON.generate(ruby)
|
||||||
# json # => "[0,[1,2],{\"foo\":3,\"bar\":4}]"
|
# json # => '[0,[1,2],{"foo":3,"bar":4}]'
|
||||||
#
|
#
|
||||||
# ==== Generating \JSON from Hashes
|
# ==== Generating \JSON from Hashes
|
||||||
#
|
#
|
||||||
|
@ -135,13 +135,13 @@ require 'json/common'
|
||||||
# a \String containing a \JSON object:
|
# a \String containing a \JSON object:
|
||||||
# ruby = {foo: 0, bar: 's', baz: :bat}
|
# ruby = {foo: 0, bar: 's', baz: :bat}
|
||||||
# json = JSON.generate(ruby)
|
# json = JSON.generate(ruby)
|
||||||
# json # => "{\"foo\":0,\"bar\":\"s\",\"baz\":\"bat\"}"
|
# json # => '{"foo":0,"bar":"s","baz":"bat"}'
|
||||||
#
|
#
|
||||||
# The Ruby \Hash array may contain nested arrays, hashes, and scalars
|
# The Ruby \Hash array may contain nested arrays, hashes, and scalars
|
||||||
# to any depth:
|
# to any depth:
|
||||||
# ruby = {foo: [0, 1], bar: {baz: 2, bat: 3}, bam: :bad}
|
# ruby = {foo: [0, 1], bar: {baz: 2, bat: 3}, bam: :bad}
|
||||||
# json = JSON.generate(ruby)
|
# json = JSON.generate(ruby)
|
||||||
# json # => "{\"foo\":[0,1],\"bar\":{\"baz\":2,\"bat\":3},\"bam\":\"bad\"}"
|
# json # => '{"foo":[0,1],"bar":{"baz":2,"bat":3},"bam":"bad"}'
|
||||||
#
|
#
|
||||||
# ==== Generating \JSON from Other Objects
|
# ==== Generating \JSON from Other Objects
|
||||||
#
|
#
|
||||||
|
@ -150,24 +150,24 @@ require 'json/common'
|
||||||
#
|
#
|
||||||
# When the source is a Ruby \Integer or \Float, JSON.generate returns
|
# When the source is a Ruby \Integer or \Float, JSON.generate returns
|
||||||
# a \String containing a \JSON number:
|
# a \String containing a \JSON number:
|
||||||
# JSON.generate(42) # => "42"
|
# JSON.generate(42) # => '42'
|
||||||
# JSON.generate(0.42) # => "0.42"
|
# JSON.generate(0.42) # => '0.42'
|
||||||
#
|
#
|
||||||
# When the source is a Ruby \String, JSON.generate returns
|
# When the source is a Ruby \String, JSON.generate returns
|
||||||
# a \String containing a \JSON string (with double-quotes):
|
# a \String containing a \JSON string (with double-quotes):
|
||||||
# JSON.generate('A string') # => "\"A string\""
|
# JSON.generate('A string') # => '"A string"'
|
||||||
#
|
#
|
||||||
# When the source is +true+, +false+ or +nil+, JSON.generate returns
|
# When the source is +true+, +false+ or +nil+, JSON.generate returns
|
||||||
# a \String containing the corresponding \JSON token:
|
# a \String containing the corresponding \JSON token:
|
||||||
# JSON.generate(true) # => "true"
|
# JSON.generate(true) # => 'true'
|
||||||
# JSON.generate(false) # => "false"
|
# JSON.generate(false) # => 'false'
|
||||||
# JSON.generate(nil) # => "null"
|
# JSON.generate(nil) # => 'null'
|
||||||
#
|
#
|
||||||
# When the source is none of the above, JSON.generate returns
|
# When the source is none of the above, JSON.generate returns
|
||||||
# a \String containing a \JSON string representation of the source:
|
# a \String containing a \JSON string representation of the source:
|
||||||
# JSON.generate(:foo) # => "\"foo\""
|
# JSON.generate(:foo) # => '"foo"'
|
||||||
# JSON.generate(Complex(0, 0)) # => "\"0+0i\""
|
# JSON.generate(Complex(0, 0)) # => '"0+0i"'
|
||||||
# JSON.generate(Dir.new('.')) # => "\"#<Dir:0x0000000006bb30b8>\""
|
# JSON.generate(Dir.new('.')) # => '"#<Dir>"'
|
||||||
#
|
#
|
||||||
# == \JSON Additions
|
# == \JSON Additions
|
||||||
#
|
#
|
||||||
|
@ -175,9 +175,9 @@ require 'json/common'
|
||||||
# you have a new \String, instead of the object you began with:
|
# you have a new \String, instead of the object you began with:
|
||||||
# ruby0 = Range.new(0, 2)
|
# ruby0 = Range.new(0, 2)
|
||||||
# json = JSON.generate(ruby0)
|
# json = JSON.generate(ruby0)
|
||||||
# json # => "\"0..2\""
|
# json # => '0..2"'
|
||||||
# ruby1 = JSON.parse(json)
|
# ruby1 = JSON.parse(json)
|
||||||
# ruby1 # => "0..2"
|
# ruby1 # => '0..2'
|
||||||
# ruby1.class # => String
|
# ruby1.class # => String
|
||||||
#
|
#
|
||||||
# You can use \JSON _additions_ to preserve the original object.
|
# You can use \JSON _additions_ to preserve the original object.
|
||||||
|
|
|
@ -12,7 +12,7 @@ module JSON
|
||||||
#
|
#
|
||||||
# Otherwise, calls JSON.generate with +object+ and +opts+:
|
# Otherwise, calls JSON.generate with +object+ and +opts+:
|
||||||
# ruby = [0, 1, nil]
|
# ruby = [0, 1, nil]
|
||||||
# JSON[ruby] # => "[0,1,null]"
|
# JSON[ruby] # => '[0,1,null]'
|
||||||
def [](object, opts = {})
|
def [](object, opts = {})
|
||||||
if object.respond_to? :to_str
|
if object.respond_to? :to_str
|
||||||
JSON.parse(object.to_str, opts)
|
JSON.parse(object.to_str, opts)
|
||||||
|
@ -99,7 +99,7 @@ module JSON
|
||||||
|
|
||||||
# Sets or returns create identifier, which is used to decide if the _json_create_
|
# Sets or returns create identifier, which is used to decide if the _json_create_
|
||||||
# hook of a class should be called; initial value is +json_class+:
|
# hook of a class should be called; initial value is +json_class+:
|
||||||
# JSON.create_id # => "json_class"
|
# JSON.create_id # => 'json_class'
|
||||||
attr_accessor :create_id
|
attr_accessor :create_id
|
||||||
end
|
end
|
||||||
self.create_id = 'json_class'
|
self.create_id = 'json_class'
|
||||||
|
@ -309,14 +309,14 @@ module JSON
|
||||||
# (implementing +to_ary+), returns a \String containing a \JSON array:
|
# (implementing +to_ary+), returns a \String containing a \JSON array:
|
||||||
# obj = ["foo", 1.0, true, false, nil]
|
# obj = ["foo", 1.0, true, false, nil]
|
||||||
# json = JSON.generate(obj)
|
# json = JSON.generate(obj)
|
||||||
# json # => "[\"foo\",1.0,true,false,null]"
|
# json # => '["foo",1.0,true,false,null]'
|
||||||
#
|
#
|
||||||
# When +obj+ is a
|
# When +obj+ is a
|
||||||
# {Hash-convertible object}[doc/implicit_conversion_rdoc.html#label-Hash-Convertible+Objects],
|
# {Hash-convertible object}[doc/implicit_conversion_rdoc.html#label-Hash-Convertible+Objects],
|
||||||
# return a \String containing a \JSON object:
|
# return a \String containing a \JSON object:
|
||||||
# obj = {foo: 0, bar: 's', baz: :bat}
|
# obj = {foo: 0, bar: 's', baz: :bat}
|
||||||
# json = JSON.generate(obj)
|
# json = JSON.generate(obj)
|
||||||
# json # => "{\"foo\":0,\"bar\":\"s\",\"baz\":\"bat\"}"
|
# json # => '{"foo":0,"bar":"s","baz":"bat"}'
|
||||||
#
|
#
|
||||||
# For examples of generating from other Ruby objects, see
|
# For examples of generating from other Ruby objects, see
|
||||||
# {Generating \JSON from Other Objects}[#module-JSON-label-Generating+JSON+from+Other+Objects].
|
# {Generating \JSON from Other Objects}[#module-JSON-label-Generating+JSON+from+Other+Objects].
|
||||||
|
@ -337,7 +337,7 @@ module JSON
|
||||||
#
|
#
|
||||||
# Allow:
|
# Allow:
|
||||||
# ruby = [Float::NaN, Float::Infinity, Float::MinusInfinity]
|
# ruby = [Float::NaN, Float::Infinity, Float::MinusInfinity]
|
||||||
# JSON.generate(ruby, allow_nan: true) # => "[NaN,Infinity,-Infinity]"
|
# JSON.generate(ruby, allow_nan: true) # => '[NaN,Infinity,-Infinity]'
|
||||||
#
|
#
|
||||||
# ---
|
# ---
|
||||||
#
|
#
|
||||||
|
@ -346,7 +346,7 @@ module JSON
|
||||||
#
|
#
|
||||||
# With the default, +100+:
|
# With the default, +100+:
|
||||||
# obj = [[[[[[0]]]]]]
|
# obj = [[[[[[0]]]]]]
|
||||||
# JSON.generate(obj) # => "[[[[[[0]]]]]]"
|
# JSON.generate(obj) # => '[[[[[[0]]]]]]'
|
||||||
#
|
#
|
||||||
# Too deep:
|
# Too deep:
|
||||||
# # Raises JSON::NestingError (nesting of 2 is too deep):
|
# # Raises JSON::NestingError (nesting of 2 is too deep):
|
||||||
|
|
Loading…
Reference in a new issue