Skip to content

Commit 3f2efd6

Browse files
BurdetteLamarhsbt
andauthored
[DOC] RDoc for additions (#557)
* RDoc for additions * Update lib/json/add/time.rb Co-authored-by: Hiroshi SHIBATA <hsbt@ruby-lang.org> --------- Co-authored-by: Hiroshi SHIBATA <hsbt@ruby-lang.org>
1 parent f11c570 commit 3f2efd6

13 files changed

Lines changed: 344 additions & 85 deletions

File tree

lib/json/add/bigdecimal.rb

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,50 @@
88
end
99

1010
class BigDecimal
11-
# Import a JSON Marshalled object.
12-
#
13-
# method used for JSON marshalling support.
11+
12+
# See #as_json.
1413
def self.json_create(object)
1514
BigDecimal._load object['b']
1615
end
1716

18-
# Marshal the object to JSON.
17+
# Methods <tt>BigDecimal#as_json</tt> and +BigDecimal.json_create+ may be used
18+
# to serialize and deserialize a \BigDecimal object;
19+
# see Marshal[https://docs.ruby-lang.org/en/master/Marshal.html].
20+
#
21+
# \Method <tt>BigDecimal#as_json</tt> serializes +self+,
22+
# returning a 2-element hash representing +self+:
23+
#
24+
# require 'json/add/bigdecimal'
25+
# x = BigDecimal(2).as_json # => {"json_class"=>"BigDecimal", "b"=>"27:0.2e1"}
26+
# y = BigDecimal(2.0, 4).as_json # => {"json_class"=>"BigDecimal", "b"=>"36:0.2e1"}
27+
# z = BigDecimal(Complex(2, 0)).as_json # => {"json_class"=>"BigDecimal", "b"=>"27:0.2e1"}
28+
#
29+
# \Method +JSON.create+ deserializes such a hash, returning a \BigDecimal object:
30+
#
31+
# BigDecimal.json_create(x) # => 0.2e1
32+
# BigDecimal.json_create(y) # => 0.2e1
33+
# BigDecimal.json_create(z) # => 0.2e1
1934
#
20-
# method used for JSON marshalling support.
2135
def as_json(*)
2236
{
2337
JSON.create_id => self.class.name,
2438
'b' => _dump,
2539
}
2640
end
2741

28-
# return the JSON value
42+
# Returns a JSON string representing +self+:
43+
#
44+
# require 'json/add/bigdecimal'
45+
# puts BigDecimal(2).to_json
46+
# puts BigDecimal(2.0, 4).to_json
47+
# puts BigDecimal(Complex(2, 0)).to_json
48+
#
49+
# Output:
50+
#
51+
# {"json_class":"BigDecimal","b":"27:0.2e1"}
52+
# {"json_class":"BigDecimal","b":"36:0.2e1"}
53+
# {"json_class":"BigDecimal","b":"27:0.2e1"}
54+
#
2955
def to_json(*args)
3056
as_json.to_json(*args)
3157
end

lib/json/add/complex.rb

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,27 @@
55

66
class Complex
77

8-
# Deserializes JSON string by converting Real value <tt>r</tt>, imaginary
9-
# value <tt>i</tt>, to a Complex object.
8+
# See #as_json.
109
def self.json_create(object)
1110
Complex(object['r'], object['i'])
1211
end
1312

14-
# Returns a hash, that will be turned into a JSON object and represent this
15-
# object.
13+
# Methods <tt>Complex#as_json</tt> and +Complex.json_create+ may be used
14+
# to serialize and deserialize a \Complex object;
15+
# see Marshal[https://docs.ruby-lang.org/en/master/Marshal.html].
16+
#
17+
# \Method <tt>Complex#as_json</tt> serializes +self+,
18+
# returning a 2-element hash representing +self+:
19+
#
20+
# require 'json/add/complex'
21+
# x = Complex(2).as_json # => {"json_class"=>"Complex", "r"=>2, "i"=>0}
22+
# y = Complex(2.0, 4).as_json # => {"json_class"=>"Complex", "r"=>2.0, "i"=>4}
23+
#
24+
# \Method +JSON.create+ deserializes such a hash, returning a \Complex object:
25+
#
26+
# Complex.json_create(x) # => (2+0i)
27+
# Complex.json_create(y) # => (2.0+4i)
28+
#
1629
def as_json(*)
1730
{
1831
JSON.create_id => self.class.name,
@@ -21,7 +34,17 @@ def as_json(*)
2134
}
2235
end
2336

24-
# Stores class name (Complex) along with real value <tt>r</tt> and imaginary value <tt>i</tt> as JSON string
37+
# Returns a JSON string representing +self+:
38+
#
39+
# require 'json/add/complex'
40+
# puts Complex(2).to_json
41+
# puts Complex(2.0, 4).to_json
42+
#
43+
# Output:
44+
#
45+
# {"json_class":"Complex","r":2,"i":0}
46+
# {"json_class":"Complex","r":2.0,"i":4}
47+
#
2548
def to_json(*args)
2649
as_json.to_json(*args)
2750
end

lib/json/add/date.rb

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,29 @@
66

77
class Date
88

9-
# Deserializes JSON string by converting Julian year <tt>y</tt>, month
10-
# <tt>m</tt>, day <tt>d</tt> and Day of Calendar Reform <tt>sg</tt> to Date.
9+
# See #as_json.
1110
def self.json_create(object)
1211
civil(*object.values_at('y', 'm', 'd', 'sg'))
1312
end
1413

1514
alias start sg unless method_defined?(:start)
1615

17-
# Returns a hash, that will be turned into a JSON object and represent this
18-
# object.
16+
# Methods <tt>Date#as_json</tt> and +Date.json_create+ may be used
17+
# to serialize and deserialize a \Date object;
18+
# see Marshal[https://docs.ruby-lang.org/en/master/Marshal.html].
19+
#
20+
# \Method <tt>Date#as_json</tt> serializes +self+,
21+
# returning a 2-element hash representing +self+:
22+
#
23+
# require 'json/add/date'
24+
# x = Date.today.as_json
25+
# # => {"json_class"=>"Date", "y"=>2023, "m"=>11, "d"=>21, "sg"=>2299161.0}
26+
#
27+
# \Method +JSON.create+ deserializes such a hash, returning a \Date object:
28+
#
29+
# Date.json_create(x)
30+
# # => #<Date: 2023-11-21 ((2460270j,0s,0n),+0s,2299161j)>
31+
#
1932
def as_json(*)
2033
{
2134
JSON.create_id => self.class.name,
@@ -26,8 +39,15 @@ def as_json(*)
2639
}
2740
end
2841

29-
# Stores class name (Date) with Julian year <tt>y</tt>, month <tt>m</tt>, day
30-
# <tt>d</tt> and Day of Calendar Reform <tt>sg</tt> as JSON string
42+
# Returns a JSON string representing +self+:
43+
#
44+
# require 'json/add/date'
45+
# puts Date.today.to_json
46+
#
47+
# Output:
48+
#
49+
# {"json_class":"Date","y":2023,"m":11,"d":21,"sg":2299161.0}
50+
#
3151
def to_json(*args)
3252
as_json.to_json(*args)
3353
end

lib/json/add/date_time.rb

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@
66

77
class DateTime
88

9-
# Deserializes JSON string by converting year <tt>y</tt>, month <tt>m</tt>,
10-
# day <tt>d</tt>, hour <tt>H</tt>, minute <tt>M</tt>, second <tt>S</tt>,
11-
# offset <tt>of</tt> and Day of Calendar Reform <tt>sg</tt> to DateTime.
9+
# See #as_json.
1210
def self.json_create(object)
1311
args = object.values_at('y', 'm', 'd', 'H', 'M', 'S')
1412
of_a, of_b = object['of'].split('/')
@@ -23,8 +21,21 @@ def self.json_create(object)
2321

2422
alias start sg unless method_defined?(:start)
2523

26-
# Returns a hash, that will be turned into a JSON object and represent this
27-
# object.
24+
# Methods <tt>DateTime#as_json</tt> and +DateTime.json_create+ may be used
25+
# to serialize and deserialize a \DateTime object;
26+
# see Marshal[https://docs.ruby-lang.org/en/master/Marshal.html].
27+
#
28+
# \Method <tt>DateTime#as_json</tt> serializes +self+,
29+
# returning a 2-element hash representing +self+:
30+
#
31+
# require 'json/add/datetime'
32+
# x = DateTime.now.as_json
33+
# # => {"json_class"=>"DateTime", "y"=>2023, "m"=>11, "d"=>21, "sg"=>2299161.0}
34+
#
35+
# \Method +JSON.create+ deserializes such a hash, returning a \DateTime object:
36+
#
37+
# DateTime.json_create(x) # BUG? Raises Date::Error "invalid date"
38+
#
2839
def as_json(*)
2940
{
3041
JSON.create_id => self.class.name,
@@ -39,9 +50,15 @@ def as_json(*)
3950
}
4051
end
4152

42-
# Stores class name (DateTime) with Julian year <tt>y</tt>, month <tt>m</tt>,
43-
# day <tt>d</tt>, hour <tt>H</tt>, minute <tt>M</tt>, second <tt>S</tt>,
44-
# offset <tt>of</tt> and Day of Calendar Reform <tt>sg</tt> as JSON string
53+
# Returns a JSON string representing +self+:
54+
#
55+
# require 'json/add/datetime'
56+
# puts DateTime.now.to_json
57+
#
58+
# Output:
59+
#
60+
# {"json_class":"DateTime","y":2023,"m":11,"d":21,"sg":2299161.0}
61+
#
4562
def to_json(*args)
4663
as_json.to_json(*args)
4764
end

lib/json/add/exception.rb

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,27 @@
55

66
class Exception
77

8-
# Deserializes JSON string by constructing new Exception object with message
9-
# <tt>m</tt> and backtrace <tt>b</tt> serialized with <tt>to_json</tt>
8+
# See #as_json.
109
def self.json_create(object)
1110
result = new(object['m'])
1211
result.set_backtrace object['b']
1312
result
1413
end
1514

16-
# Returns a hash, that will be turned into a JSON object and represent this
17-
# object.
15+
# Methods <tt>Exception#as_json</tt> and +Exception.json_create+ may be used
16+
# to serialize and deserialize a \Exception object;
17+
# see Marshal[https://docs.ruby-lang.org/en/master/Marshal.html].
18+
#
19+
# \Method <tt>Exception#as_json</tt> serializes +self+,
20+
# returning a 2-element hash representing +self+:
21+
#
22+
# require 'json/add/exception'
23+
# x = Exception.new('Foo').as_json # => {"json_class"=>"Exception", "m"=>"Foo", "b"=>nil}
24+
#
25+
# \Method +JSON.create+ deserializes such a hash, returning a \Exception object:
26+
#
27+
# Exception.json_create(x) # => #<Exception: Foo>
28+
#
1829
def as_json(*)
1930
{
2031
JSON.create_id => self.class.name,
@@ -23,8 +34,15 @@ def as_json(*)
2334
}
2435
end
2536

26-
# Stores class name (Exception) with message <tt>m</tt> and backtrace array
27-
# <tt>b</tt> as JSON string
37+
# Returns a JSON string representing +self+:
38+
#
39+
# require 'json/add/exception'
40+
# puts Exception.new('Foo').to_json
41+
#
42+
# Output:
43+
#
44+
# {"json_class":"Exception","m":"Foo","b":null}
45+
#
2846
def to_json(*args)
2947
as_json.to_json(*args)
3048
end

lib/json/add/ostruct.rb

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,27 @@
66

77
class OpenStruct
88

9-
# Deserializes JSON string by constructing new Struct object with values
10-
# <tt>t</tt> serialized by <tt>to_json</tt>.
9+
# See #as_json.
1110
def self.json_create(object)
1211
new(object['t'] || object[:t])
1312
end
1413

15-
# Returns a hash, that will be turned into a JSON object and represent this
16-
# object.
14+
# Methods <tt>OpenStruct#as_json</tt> and +OpenStruct.json_create+ may be used
15+
# to serialize and deserialize a \OpenStruct object;
16+
# see Marshal[https://docs.ruby-lang.org/en/master/Marshal.html].
17+
#
18+
# \Method <tt>OpenStruct#as_json</tt> serializes +self+,
19+
# returning a 2-element hash representing +self+:
20+
#
21+
# require 'json/add/ostruct'
22+
# x = OpenStruct.new('name' => 'Rowdy', :age => nil).as_json
23+
# # => {"json_class"=>"OpenStruct", "t"=>{:name=>'Rowdy', :age=>nil}}
24+
#
25+
# \Method +JSON.create+ deserializes such a hash, returning a \OpenStruct object:
26+
#
27+
# OpenStruct.json_create(x)
28+
# # => #<OpenStruct name='Rowdy', age=nil>
29+
#
1730
def as_json(*)
1831
klass = self.class.name
1932
klass.to_s.empty? and raise JSON::JSONError, "Only named structs are supported!"
@@ -23,8 +36,15 @@ def as_json(*)
2336
}
2437
end
2538

26-
# Stores class name (OpenStruct) with this struct's values <tt>t</tt> as a
27-
# JSON string.
39+
# Returns a JSON string representing +self+:
40+
#
41+
# require 'json/add/ostruct'
42+
# puts OpenStruct.new('name' => 'Rowdy', :age => nil).to_json
43+
#
44+
# Output:
45+
#
46+
# {"json_class":"OpenStruct","t":{'name':'Rowdy',"age":null}}
47+
#
2848
def to_json(*args)
2949
as_json.to_json(*args)
3050
end

lib/json/add/range.rb

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,28 @@
55

66
class Range
77

8-
# Returns a new \Range object constructed from <tt>object['a']</tt>,
9-
# which must be an array of values suitable for a call to Range.new:
10-
#
11-
# require 'json/add/range'
12-
# Range.json_create({"a"=>[1, 4]}) # => 1..4
13-
# Range.json_create({"a"=>[1, 4, true]}) # => 1...4
14-
# Range.json_create({"a" => ['a', 'd']}) # => "a".."d"
15-
#
8+
# See #as_json.
169
def self.json_create(object)
1710
new(*object['a'])
1811
end
1912

20-
# Returns a 2-element hash representing +self+:
13+
# Methods <tt>Range#as_json</tt> and +Range.json_create+ may be used
14+
# to serialize and deserialize a \Range object;
15+
# see Marshal[https://docs.ruby-lang.org/en/master/Marshal.html].
16+
#
17+
# \Method <tt>Range#as_json</tt> serializes +self+,
18+
# returning a 2-element hash representing +self+:
2119
#
2220
# require 'json/add/range'
23-
# (1..4).as_json # => {"json_class"=>"Range", "a"=>[1, 4, false]}
24-
# (1...4).as_json # => {"json_class"=>"Range", "a"=>[1, 4, true]}
25-
# ('a'..'d').as_json # => {"json_class"=>"Range", "a"=>["a", "d", false]}
21+
# x = (1..4).as_json # => {"json_class"=>"Range", "a"=>[1, 4, false]}
22+
# y = (1...4).as_json # => {"json_class"=>"Range", "a"=>[1, 4, true]}
23+
# z = ('a'..'d').as_json # => {"json_class"=>"Range", "a"=>["a", "d", false]}
24+
#
25+
# \Method +JSON.create+ deserializes such a hash, returning a \Range object:
26+
#
27+
# Range.json_create(x) # => 1..4
28+
# Range.json_create(y) # => 1...4
29+
# Range.json_create(z) # => "a".."d"
2630
#
2731
def as_json(*)
2832
{
@@ -34,9 +38,15 @@ def as_json(*)
3438
# Returns a JSON string representing +self+:
3539
#
3640
# require 'json/add/range'
37-
# (1..4).to_json # => "{\"json_class\":\"Range\",\"a\":[1,4,false]}"
38-
# (1...4).to_json # => "{\"json_class\":\"Range\",\"a\":[1,4,true]}"
39-
# ('a'..'d').to_json # => "{\"json_class\":\"Range\",\"a\":[\"a\",\"d\",false]}"
41+
# puts (1..4).to_json
42+
# puts (1...4).to_json
43+
# puts ('a'..'d').to_json
44+
#
45+
# Output:
46+
#
47+
# {"json_class":"Range","a":[1,4,false]}
48+
# {"json_class":"Range","a":[1,4,true]}
49+
# {"json_class":"Range","a":["a","d",false]}
4050
#
4151
def to_json(*args)
4252
as_json.to_json(*args)

0 commit comments

Comments
 (0)