json 1.8.6 → 2.7.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/{CHANGES → CHANGES.md} +292 -96
- data/LICENSE +56 -0
- data/README.md +185 -114
- data/ext/json/ext/fbuffer/fbuffer.h +0 -3
- data/ext/json/ext/generator/generator.c +328 -117
- data/ext/json/ext/generator/generator.h +8 -8
- data/ext/json/ext/parser/extconf.rb +29 -0
- data/ext/json/ext/parser/parser.c +540 -569
- data/ext/json/ext/parser/parser.h +10 -6
- data/ext/json/ext/parser/parser.rl +269 -261
- data/ext/json/extconf.rb +1 -1
- data/json.gemspec +0 -0
- data/lib/json/add/bigdecimal.rb +40 -10
- data/lib/json/add/complex.rb +32 -9
- data/lib/json/add/core.rb +1 -0
- data/lib/json/add/date.rb +27 -7
- data/lib/json/add/date_time.rb +26 -9
- data/lib/json/add/exception.rb +25 -7
- data/lib/json/add/ostruct.rb +32 -9
- data/lib/json/add/range.rb +33 -8
- data/lib/json/add/rational.rb +30 -8
- data/lib/json/add/regexp.rb +28 -10
- data/lib/json/add/set.rb +48 -0
- data/lib/json/add/struct.rb +29 -7
- data/lib/json/add/symbol.rb +28 -5
- data/lib/json/add/time.rb +27 -6
- data/lib/json/common.rb +402 -188
- data/lib/json/ext.rb +0 -6
- data/lib/json/generic_object.rb +11 -6
- data/lib/json/pure/generator.rb +120 -137
- data/lib/json/pure/parser.rb +64 -86
- data/lib/json/pure.rb +2 -8
- data/lib/json/version.rb +2 -1
- data/lib/json.rb +559 -29
- metadata +18 -129
- data/.gitignore +0 -17
- data/.travis.yml +0 -18
- data/Gemfile +0 -7
- data/README-json-jruby.markdown +0 -33
- data/Rakefile +0 -402
- data/TODO +0 -1
- data/VERSION +0 -1
- data/data/example.json +0 -1
- data/data/index.html +0 -38
- data/data/prototype.js +0 -4184
- data/diagrams/.keep +0 -0
- data/install.rb +0 -23
- data/java/src/json/ext/ByteListTranscoder.java +0 -166
- data/java/src/json/ext/Generator.java +0 -446
- data/java/src/json/ext/GeneratorMethods.java +0 -231
- data/java/src/json/ext/GeneratorService.java +0 -42
- data/java/src/json/ext/GeneratorState.java +0 -542
- data/java/src/json/ext/OptionsReader.java +0 -113
- data/java/src/json/ext/Parser.java +0 -2644
- data/java/src/json/ext/Parser.rl +0 -968
- data/java/src/json/ext/ParserService.java +0 -34
- data/java/src/json/ext/RuntimeInfo.java +0 -120
- data/java/src/json/ext/StringDecoder.java +0 -166
- data/java/src/json/ext/StringEncoder.java +0 -111
- data/java/src/json/ext/Utils.java +0 -88
- data/json-java.gemspec +0 -38
- data/json_pure.gemspec +0 -37
- data/lib/json/ext/.keep +0 -0
- data/tests/fixtures/fail1.json +0 -1
- data/tests/fixtures/fail10.json +0 -1
- data/tests/fixtures/fail11.json +0 -1
- data/tests/fixtures/fail12.json +0 -1
- data/tests/fixtures/fail13.json +0 -1
- data/tests/fixtures/fail14.json +0 -1
- data/tests/fixtures/fail18.json +0 -1
- data/tests/fixtures/fail19.json +0 -1
- data/tests/fixtures/fail2.json +0 -1
- data/tests/fixtures/fail20.json +0 -1
- data/tests/fixtures/fail21.json +0 -1
- data/tests/fixtures/fail22.json +0 -1
- data/tests/fixtures/fail23.json +0 -1
- data/tests/fixtures/fail24.json +0 -1
- data/tests/fixtures/fail25.json +0 -1
- data/tests/fixtures/fail27.json +0 -2
- data/tests/fixtures/fail28.json +0 -2
- data/tests/fixtures/fail3.json +0 -1
- data/tests/fixtures/fail4.json +0 -1
- data/tests/fixtures/fail5.json +0 -1
- data/tests/fixtures/fail6.json +0 -1
- data/tests/fixtures/fail7.json +0 -1
- data/tests/fixtures/fail8.json +0 -1
- data/tests/fixtures/fail9.json +0 -1
- data/tests/fixtures/pass1.json +0 -56
- data/tests/fixtures/pass15.json +0 -1
- data/tests/fixtures/pass16.json +0 -1
- data/tests/fixtures/pass17.json +0 -1
- data/tests/fixtures/pass2.json +0 -1
- data/tests/fixtures/pass26.json +0 -1
- data/tests/fixtures/pass3.json +0 -6
- data/tests/setup_variant.rb +0 -11
- data/tests/test_json.rb +0 -519
- data/tests/test_json_addition.rb +0 -196
- data/tests/test_json_encoding.rb +0 -65
- data/tests/test_json_fixtures.rb +0 -35
- data/tests/test_json_generate.rb +0 -348
- data/tests/test_json_generic_object.rb +0 -75
- data/tests/test_json_string_matching.rb +0 -39
- data/tests/test_json_unicode.rb +0 -72
- data/tools/diff.sh +0 -18
- data/tools/fuzz.rb +0 -139
- data/tools/server.rb +0 -62
data/ext/json/extconf.rb
CHANGED
data/json.gemspec
CHANGED
Binary file
|
data/lib/json/add/bigdecimal.rb
CHANGED
@@ -1,19 +1,37 @@
|
|
1
|
+
#frozen_string_literal: false
|
1
2
|
unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
|
2
3
|
require 'json'
|
3
4
|
end
|
4
|
-
|
5
|
+
begin
|
6
|
+
require 'bigdecimal'
|
7
|
+
rescue LoadError
|
8
|
+
end
|
5
9
|
|
6
10
|
class BigDecimal
|
7
|
-
|
8
|
-
#
|
9
|
-
# method used for JSON marshalling support.
|
11
|
+
|
12
|
+
# See #as_json.
|
10
13
|
def self.json_create(object)
|
11
14
|
BigDecimal._load object['b']
|
12
15
|
end
|
13
16
|
|
14
|
-
#
|
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
|
15
34
|
#
|
16
|
-
# method used for JSON marshalling support.
|
17
35
|
def as_json(*)
|
18
36
|
{
|
19
37
|
JSON.create_id => self.class.name,
|
@@ -21,8 +39,20 @@ class BigDecimal
|
|
21
39
|
}
|
22
40
|
end
|
23
41
|
|
24
|
-
#
|
25
|
-
|
26
|
-
|
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
|
+
#
|
55
|
+
def to_json(*args)
|
56
|
+
as_json.to_json(*args)
|
27
57
|
end
|
28
|
-
end
|
58
|
+
end if defined?(::BigDecimal)
|
data/lib/json/add/complex.rb
CHANGED
@@ -1,18 +1,31 @@
|
|
1
|
+
#frozen_string_literal: false
|
1
2
|
unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
|
2
3
|
require 'json'
|
3
4
|
end
|
4
|
-
defined?(::Complex) or require 'complex'
|
5
5
|
|
6
6
|
class Complex
|
7
7
|
|
8
|
-
#
|
9
|
-
# value <tt>i</tt>, to a Complex object.
|
8
|
+
# See #as_json.
|
10
9
|
def self.json_create(object)
|
11
10
|
Complex(object['r'], object['i'])
|
12
11
|
end
|
13
12
|
|
14
|
-
#
|
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
|
+
#
|
16
29
|
def as_json(*)
|
17
30
|
{
|
18
31
|
JSON.create_id => self.class.name,
|
@@ -21,8 +34,18 @@ class Complex
|
|
21
34
|
}
|
22
35
|
end
|
23
36
|
|
24
|
-
#
|
25
|
-
|
26
|
-
|
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
|
+
#
|
48
|
+
def to_json(*args)
|
49
|
+
as_json.to_json(*args)
|
27
50
|
end
|
28
|
-
end
|
51
|
+
end
|
data/lib/json/add/core.rb
CHANGED
data/lib/json/add/date.rb
CHANGED
@@ -1,21 +1,34 @@
|
|
1
|
+
#frozen_string_literal: false
|
1
2
|
unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
|
2
3
|
require 'json'
|
3
4
|
end
|
4
5
|
require 'date'
|
5
6
|
|
6
|
-
# Date serialization/deserialization
|
7
7
|
class Date
|
8
8
|
|
9
|
-
#
|
10
|
-
# <tt>m</tt>, day <tt>d</tt> and Day of Calendar Reform <tt>sg</tt> to Date.
|
9
|
+
# See #as_json.
|
11
10
|
def self.json_create(object)
|
12
11
|
civil(*object.values_at('y', 'm', 'd', 'sg'))
|
13
12
|
end
|
14
13
|
|
15
14
|
alias start sg unless method_defined?(:start)
|
16
15
|
|
17
|
-
#
|
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
|
+
#
|
19
32
|
def as_json(*)
|
20
33
|
{
|
21
34
|
JSON.create_id => self.class.name,
|
@@ -26,8 +39,15 @@ class Date
|
|
26
39
|
}
|
27
40
|
end
|
28
41
|
|
29
|
-
#
|
30
|
-
#
|
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
|
+
#
|
31
51
|
def to_json(*args)
|
32
52
|
as_json.to_json(*args)
|
33
53
|
end
|
data/lib/json/add/date_time.rb
CHANGED
@@ -1,14 +1,12 @@
|
|
1
|
+
#frozen_string_literal: false
|
1
2
|
unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
|
2
3
|
require 'json'
|
3
4
|
end
|
4
5
|
require 'date'
|
5
6
|
|
6
|
-
# DateTime serialization/deserialization
|
7
7
|
class DateTime
|
8
8
|
|
9
|
-
#
|
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.
|
12
10
|
def self.json_create(object)
|
13
11
|
args = object.values_at('y', 'm', 'd', 'H', 'M', 'S')
|
14
12
|
of_a, of_b = object['of'].split('/')
|
@@ -23,8 +21,21 @@ class DateTime
|
|
23
21
|
|
24
22
|
alias start sg unless method_defined?(:start)
|
25
23
|
|
26
|
-
#
|
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
|
+
#
|
28
39
|
def as_json(*)
|
29
40
|
{
|
30
41
|
JSON.create_id => self.class.name,
|
@@ -39,9 +50,15 @@ class DateTime
|
|
39
50
|
}
|
40
51
|
end
|
41
52
|
|
42
|
-
#
|
43
|
-
#
|
44
|
-
#
|
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
|
+
#
|
45
62
|
def to_json(*args)
|
46
63
|
as_json.to_json(*args)
|
47
64
|
end
|
data/lib/json/add/exception.rb
CHANGED
@@ -1,20 +1,31 @@
|
|
1
|
+
#frozen_string_literal: false
|
1
2
|
unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
|
2
3
|
require 'json'
|
3
4
|
end
|
4
5
|
|
5
|
-
# Exception serialization/deserialization
|
6
6
|
class Exception
|
7
7
|
|
8
|
-
#
|
9
|
-
# <tt>m</tt> and backtrace <tt>b</tt> serialized with <tt>to_json</tt>
|
8
|
+
# See #as_json.
|
10
9
|
def self.json_create(object)
|
11
10
|
result = new(object['m'])
|
12
11
|
result.set_backtrace object['b']
|
13
12
|
result
|
14
13
|
end
|
15
14
|
|
16
|
-
#
|
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
|
+
#
|
18
29
|
def as_json(*)
|
19
30
|
{
|
20
31
|
JSON.create_id => self.class.name,
|
@@ -23,8 +34,15 @@ class Exception
|
|
23
34
|
}
|
24
35
|
end
|
25
36
|
|
26
|
-
#
|
27
|
-
#
|
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
|
+
#
|
28
46
|
def to_json(*args)
|
29
47
|
as_json.to_json(*args)
|
30
48
|
end
|
data/lib/json/add/ostruct.rb
CHANGED
@@ -1,19 +1,35 @@
|
|
1
|
+
#frozen_string_literal: false
|
1
2
|
unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
|
2
3
|
require 'json'
|
3
4
|
end
|
4
|
-
|
5
|
+
begin
|
6
|
+
require 'ostruct'
|
7
|
+
rescue LoadError
|
8
|
+
end
|
5
9
|
|
6
|
-
# OpenStruct serialization/deserialization
|
7
10
|
class OpenStruct
|
8
11
|
|
9
|
-
#
|
10
|
-
# <tt>v</tt> serialized by <tt>to_json</tt>.
|
12
|
+
# See #as_json.
|
11
13
|
def self.json_create(object)
|
12
14
|
new(object['t'] || object[:t])
|
13
15
|
end
|
14
16
|
|
15
|
-
#
|
16
|
-
# object
|
17
|
+
# Methods <tt>OpenStruct#as_json</tt> and +OpenStruct.json_create+ may be used
|
18
|
+
# to serialize and deserialize a \OpenStruct object;
|
19
|
+
# see Marshal[https://docs.ruby-lang.org/en/master/Marshal.html].
|
20
|
+
#
|
21
|
+
# \Method <tt>OpenStruct#as_json</tt> serializes +self+,
|
22
|
+
# returning a 2-element hash representing +self+:
|
23
|
+
#
|
24
|
+
# require 'json/add/ostruct'
|
25
|
+
# x = OpenStruct.new('name' => 'Rowdy', :age => nil).as_json
|
26
|
+
# # => {"json_class"=>"OpenStruct", "t"=>{:name=>'Rowdy', :age=>nil}}
|
27
|
+
#
|
28
|
+
# \Method +JSON.create+ deserializes such a hash, returning a \OpenStruct object:
|
29
|
+
#
|
30
|
+
# OpenStruct.json_create(x)
|
31
|
+
# # => #<OpenStruct name='Rowdy', age=nil>
|
32
|
+
#
|
17
33
|
def as_json(*)
|
18
34
|
klass = self.class.name
|
19
35
|
klass.to_s.empty? and raise JSON::JSONError, "Only named structs are supported!"
|
@@ -23,9 +39,16 @@ class OpenStruct
|
|
23
39
|
}
|
24
40
|
end
|
25
41
|
|
26
|
-
#
|
27
|
-
#
|
42
|
+
# Returns a JSON string representing +self+:
|
43
|
+
#
|
44
|
+
# require 'json/add/ostruct'
|
45
|
+
# puts OpenStruct.new('name' => 'Rowdy', :age => nil).to_json
|
46
|
+
#
|
47
|
+
# Output:
|
48
|
+
#
|
49
|
+
# {"json_class":"OpenStruct","t":{'name':'Rowdy',"age":null}}
|
50
|
+
#
|
28
51
|
def to_json(*args)
|
29
52
|
as_json.to_json(*args)
|
30
53
|
end
|
31
|
-
end
|
54
|
+
end if defined?(::OpenStruct)
|
data/lib/json/add/range.rb
CHANGED
@@ -1,18 +1,33 @@
|
|
1
|
+
#frozen_string_literal: false
|
1
2
|
unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
|
2
3
|
require 'json'
|
3
4
|
end
|
4
5
|
|
5
|
-
# Range serialization/deserialization
|
6
6
|
class Range
|
7
7
|
|
8
|
-
#
|
9
|
-
# <tt>a</tt> serialized by <tt>to_json</tt>.
|
8
|
+
# See #as_json.
|
10
9
|
def self.json_create(object)
|
11
10
|
new(*object['a'])
|
12
11
|
end
|
13
12
|
|
14
|
-
#
|
15
|
-
# object
|
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+:
|
19
|
+
#
|
20
|
+
# require 'json/add/range'
|
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"
|
30
|
+
#
|
16
31
|
def as_json(*)
|
17
32
|
{
|
18
33
|
JSON.create_id => self.class.name,
|
@@ -20,9 +35,19 @@ class Range
|
|
20
35
|
}
|
21
36
|
end
|
22
37
|
|
23
|
-
#
|
24
|
-
#
|
25
|
-
#
|
38
|
+
# Returns a JSON string representing +self+:
|
39
|
+
#
|
40
|
+
# require 'json/add/range'
|
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]}
|
50
|
+
#
|
26
51
|
def to_json(*args)
|
27
52
|
as_json.to_json(*args)
|
28
53
|
end
|
data/lib/json/add/rational.rb
CHANGED
@@ -1,17 +1,31 @@
|
|
1
|
+
#frozen_string_literal: false
|
1
2
|
unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
|
2
3
|
require 'json'
|
3
4
|
end
|
4
|
-
defined?(::Rational) or require 'rational'
|
5
5
|
|
6
6
|
class Rational
|
7
|
-
|
8
|
-
#
|
7
|
+
|
8
|
+
# See #as_json.
|
9
9
|
def self.json_create(object)
|
10
10
|
Rational(object['n'], object['d'])
|
11
11
|
end
|
12
12
|
|
13
|
-
#
|
14
|
-
# object
|
13
|
+
# Methods <tt>Rational#as_json</tt> and +Rational.json_create+ may be used
|
14
|
+
# to serialize and deserialize a \Rational object;
|
15
|
+
# see Marshal[https://docs.ruby-lang.org/en/master/Marshal.html].
|
16
|
+
#
|
17
|
+
# \Method <tt>Rational#as_json</tt> serializes +self+,
|
18
|
+
# returning a 2-element hash representing +self+:
|
19
|
+
#
|
20
|
+
# require 'json/add/rational'
|
21
|
+
# x = Rational(2, 3).as_json
|
22
|
+
# # => {"json_class"=>"Rational", "n"=>2, "d"=>3}
|
23
|
+
#
|
24
|
+
# \Method +JSON.create+ deserializes such a hash, returning a \Rational object:
|
25
|
+
#
|
26
|
+
# Rational.json_create(x)
|
27
|
+
# # => (2/3)
|
28
|
+
#
|
15
29
|
def as_json(*)
|
16
30
|
{
|
17
31
|
JSON.create_id => self.class.name,
|
@@ -20,8 +34,16 @@ class Rational
|
|
20
34
|
}
|
21
35
|
end
|
22
36
|
|
23
|
-
#
|
24
|
-
|
25
|
-
|
37
|
+
# Returns a JSON string representing +self+:
|
38
|
+
#
|
39
|
+
# require 'json/add/rational'
|
40
|
+
# puts Rational(2, 3).to_json
|
41
|
+
#
|
42
|
+
# Output:
|
43
|
+
#
|
44
|
+
# {"json_class":"Rational","n":2,"d":3}
|
45
|
+
#
|
46
|
+
def to_json(*args)
|
47
|
+
as_json.to_json(*args)
|
26
48
|
end
|
27
49
|
end
|
data/lib/json/add/regexp.rb
CHANGED
@@ -1,19 +1,30 @@
|
|
1
|
+
#frozen_string_literal: false
|
1
2
|
unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
|
2
3
|
require 'json'
|
3
4
|
end
|
4
5
|
|
5
|
-
# Regexp serialization/deserialization
|
6
6
|
class Regexp
|
7
7
|
|
8
|
-
#
|
9
|
-
# <tt>s</tt> (Regexp or String) and options <tt>o</tt> serialized by
|
10
|
-
# <tt>to_json</tt>
|
8
|
+
# See #as_json.
|
11
9
|
def self.json_create(object)
|
12
10
|
new(object['s'], object['o'])
|
13
11
|
end
|
14
12
|
|
15
|
-
#
|
16
|
-
# object
|
13
|
+
# Methods <tt>Regexp#as_json</tt> and +Regexp.json_create+ may be used
|
14
|
+
# to serialize and deserialize a \Regexp object;
|
15
|
+
# see Marshal[https://docs.ruby-lang.org/en/master/Marshal.html].
|
16
|
+
#
|
17
|
+
# \Method <tt>Regexp#as_json</tt> serializes +self+,
|
18
|
+
# returning a 2-element hash representing +self+:
|
19
|
+
#
|
20
|
+
# require 'json/add/regexp'
|
21
|
+
# x = /foo/.as_json
|
22
|
+
# # => {"json_class"=>"Regexp", "o"=>0, "s"=>"foo"}
|
23
|
+
#
|
24
|
+
# \Method +JSON.create+ deserializes such a hash, returning a \Regexp object:
|
25
|
+
#
|
26
|
+
# Regexp.json_create(x) # => /foo/
|
27
|
+
#
|
17
28
|
def as_json(*)
|
18
29
|
{
|
19
30
|
JSON.create_id => self.class.name,
|
@@ -22,9 +33,16 @@ class Regexp
|
|
22
33
|
}
|
23
34
|
end
|
24
35
|
|
25
|
-
#
|
26
|
-
#
|
27
|
-
|
28
|
-
|
36
|
+
# Returns a JSON string representing +self+:
|
37
|
+
#
|
38
|
+
# require 'json/add/regexp'
|
39
|
+
# puts /foo/.to_json
|
40
|
+
#
|
41
|
+
# Output:
|
42
|
+
#
|
43
|
+
# {"json_class":"Regexp","o":0,"s":"foo"}
|
44
|
+
#
|
45
|
+
def to_json(*args)
|
46
|
+
as_json.to_json(*args)
|
29
47
|
end
|
30
48
|
end
|
data/lib/json/add/set.rb
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
|
2
|
+
require 'json'
|
3
|
+
end
|
4
|
+
defined?(::Set) or require 'set'
|
5
|
+
|
6
|
+
class Set
|
7
|
+
|
8
|
+
# See #as_json.
|
9
|
+
def self.json_create(object)
|
10
|
+
new object['a']
|
11
|
+
end
|
12
|
+
|
13
|
+
# Methods <tt>Set#as_json</tt> and +Set.json_create+ may be used
|
14
|
+
# to serialize and deserialize a \Set object;
|
15
|
+
# see Marshal[https://docs.ruby-lang.org/en/master/Marshal.html].
|
16
|
+
#
|
17
|
+
# \Method <tt>Set#as_json</tt> serializes +self+,
|
18
|
+
# returning a 2-element hash representing +self+:
|
19
|
+
#
|
20
|
+
# require 'json/add/set'
|
21
|
+
# x = Set.new(%w/foo bar baz/).as_json
|
22
|
+
# # => {"json_class"=>"Set", "a"=>["foo", "bar", "baz"]}
|
23
|
+
#
|
24
|
+
# \Method +JSON.create+ deserializes such a hash, returning a \Set object:
|
25
|
+
#
|
26
|
+
# Set.json_create(x) # => #<Set: {"foo", "bar", "baz"}>
|
27
|
+
#
|
28
|
+
def as_json(*)
|
29
|
+
{
|
30
|
+
JSON.create_id => self.class.name,
|
31
|
+
'a' => to_a,
|
32
|
+
}
|
33
|
+
end
|
34
|
+
|
35
|
+
# Returns a JSON string representing +self+:
|
36
|
+
#
|
37
|
+
# require 'json/add/set'
|
38
|
+
# puts Set.new(%w/foo bar baz/).to_json
|
39
|
+
#
|
40
|
+
# Output:
|
41
|
+
#
|
42
|
+
# {"json_class":"Set","a":["foo","bar","baz"]}
|
43
|
+
#
|
44
|
+
def to_json(*args)
|
45
|
+
as_json.to_json(*args)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
data/lib/json/add/struct.rb
CHANGED
@@ -1,18 +1,32 @@
|
|
1
|
+
#frozen_string_literal: false
|
1
2
|
unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
|
2
3
|
require 'json'
|
3
4
|
end
|
4
5
|
|
5
|
-
# Struct serialization/deserialization
|
6
6
|
class Struct
|
7
7
|
|
8
|
-
#
|
9
|
-
# <tt>v</tt> serialized by <tt>to_json</tt>.
|
8
|
+
# See #as_json.
|
10
9
|
def self.json_create(object)
|
11
10
|
new(*object['v'])
|
12
11
|
end
|
13
12
|
|
14
|
-
#
|
15
|
-
# object
|
13
|
+
# Methods <tt>Struct#as_json</tt> and +Struct.json_create+ may be used
|
14
|
+
# to serialize and deserialize a \Struct object;
|
15
|
+
# see Marshal[https://docs.ruby-lang.org/en/master/Marshal.html].
|
16
|
+
#
|
17
|
+
# \Method <tt>Struct#as_json</tt> serializes +self+,
|
18
|
+
# returning a 2-element hash representing +self+:
|
19
|
+
#
|
20
|
+
# require 'json/add/struct'
|
21
|
+
# Customer = Struct.new('Customer', :name, :address, :zip)
|
22
|
+
# x = Struct::Customer.new.as_json
|
23
|
+
# # => {"json_class"=>"Struct::Customer", "v"=>[nil, nil, nil]}
|
24
|
+
#
|
25
|
+
# \Method +JSON.create+ deserializes such a hash, returning a \Struct object:
|
26
|
+
#
|
27
|
+
# Struct::Customer.json_create(x)
|
28
|
+
# # => #<struct Struct::Customer name=nil, address=nil, zip=nil>
|
29
|
+
#
|
16
30
|
def as_json(*)
|
17
31
|
klass = self.class.name
|
18
32
|
klass.to_s.empty? and raise JSON::JSONError, "Only named structs are supported!"
|
@@ -22,8 +36,16 @@ class Struct
|
|
22
36
|
}
|
23
37
|
end
|
24
38
|
|
25
|
-
#
|
26
|
-
#
|
39
|
+
# Returns a JSON string representing +self+:
|
40
|
+
#
|
41
|
+
# require 'json/add/struct'
|
42
|
+
# Customer = Struct.new('Customer', :name, :address, :zip)
|
43
|
+
# puts Struct::Customer.new.to_json
|
44
|
+
#
|
45
|
+
# Output:
|
46
|
+
#
|
47
|
+
# {"json_class":"Struct","t":{'name':'Rowdy',"age":null}}
|
48
|
+
#
|
27
49
|
def to_json(*args)
|
28
50
|
as_json.to_json(*args)
|
29
51
|
end
|