json 1.8.6-java → 2.11.1-java
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- data/BSDL +22 -0
- data/CHANGES.md +619 -0
- data/COPYING +56 -0
- data/LEGAL +8 -0
- data/README.md +268 -0
- data/json.gemspec +63 -0
- data/lib/json/add/bigdecimal.rb +41 -11
- 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 +34 -7
- data/lib/json/add/time.rb +29 -15
- data/lib/json/common.rb +916 -316
- data/lib/json/ext/generator/state.rb +106 -0
- data/lib/json/ext/generator.jar +0 -0
- data/lib/json/ext/parser.jar +0 -0
- data/lib/json/ext.rb +34 -10
- data/lib/json/generic_object.rb +11 -6
- data/lib/json/truffle_ruby/generator.rb +690 -0
- data/lib/json/version.rb +3 -6
- data/lib/json.rb +560 -35
- metadata +29 -82
- data/lib/json/pure/generator.rb +0 -530
- data/lib/json/pure/parser.rb +0 -359
- data/lib/json/pure.rb +0 -21
- 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/lib/json/add/exception.rb
CHANGED
@@ -1,20 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
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: true
|
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: true
|
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: true
|
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: true
|
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: true
|
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
|
data/lib/json/add/symbol.rb
CHANGED
@@ -1,11 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
|
2
3
|
require 'json'
|
3
4
|
end
|
4
5
|
|
5
|
-
# Symbol serialization/deserialization
|
6
6
|
class Symbol
|
7
|
-
|
8
|
-
#
|
7
|
+
|
8
|
+
# Methods <tt>Symbol#as_json</tt> and +Symbol.json_create+ may be used
|
9
|
+
# to serialize and deserialize a \Symbol object;
|
10
|
+
# see Marshal[https://docs.ruby-lang.org/en/master/Marshal.html].
|
11
|
+
#
|
12
|
+
# \Method <tt>Symbol#as_json</tt> serializes +self+,
|
13
|
+
# returning a 2-element hash representing +self+:
|
14
|
+
#
|
15
|
+
# require 'json/add/symbol'
|
16
|
+
# x = :foo.as_json
|
17
|
+
# # => {"json_class"=>"Symbol", "s"=>"foo"}
|
18
|
+
#
|
19
|
+
# \Method +JSON.create+ deserializes such a hash, returning a \Symbol object:
|
20
|
+
#
|
21
|
+
# Symbol.json_create(x) # => :foo
|
22
|
+
#
|
9
23
|
def as_json(*)
|
10
24
|
{
|
11
25
|
JSON.create_id => self.class.name,
|
@@ -13,12 +27,25 @@ class Symbol
|
|
13
27
|
}
|
14
28
|
end
|
15
29
|
|
16
|
-
#
|
17
|
-
|
18
|
-
|
30
|
+
# Returns a JSON string representing +self+:
|
31
|
+
#
|
32
|
+
# require 'json/add/symbol'
|
33
|
+
# puts :foo.to_json
|
34
|
+
#
|
35
|
+
# Output:
|
36
|
+
#
|
37
|
+
# # {"json_class":"Symbol","s":"foo"}
|
38
|
+
#
|
39
|
+
def to_json(state = nil, *a)
|
40
|
+
state = ::JSON::State.from_state(state)
|
41
|
+
if state.strict?
|
42
|
+
super
|
43
|
+
else
|
44
|
+
as_json.to_json(state, *a)
|
45
|
+
end
|
19
46
|
end
|
20
47
|
|
21
|
-
#
|
48
|
+
# See #as_json.
|
22
49
|
def self.json_create(o)
|
23
50
|
o['s'].to_sym
|
24
51
|
end
|
data/lib/json/add/time.rb
CHANGED
@@ -1,37 +1,51 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
|
2
3
|
require 'json'
|
3
4
|
end
|
4
5
|
|
5
|
-
# Time serialization/deserialization
|
6
6
|
class Time
|
7
7
|
|
8
|
-
#
|
8
|
+
# See #as_json.
|
9
9
|
def self.json_create(object)
|
10
10
|
if usec = object.delete('u') # used to be tv_usec -> tv_nsec
|
11
11
|
object['n'] = usec * 1000
|
12
12
|
end
|
13
|
-
|
14
|
-
at(object['s'], Rational(object['n'], 1000))
|
15
|
-
else
|
16
|
-
at(object['s'], object['n'] / 1000)
|
17
|
-
end
|
13
|
+
at(object['s'], Rational(object['n'], 1000))
|
18
14
|
end
|
19
15
|
|
20
|
-
#
|
21
|
-
# object
|
16
|
+
# Methods <tt>Time#as_json</tt> and +Time.json_create+ may be used
|
17
|
+
# to serialize and deserialize a \Time object;
|
18
|
+
# see Marshal[https://docs.ruby-lang.org/en/master/Marshal.html].
|
19
|
+
#
|
20
|
+
# \Method <tt>Time#as_json</tt> serializes +self+,
|
21
|
+
# returning a 2-element hash representing +self+:
|
22
|
+
#
|
23
|
+
# require 'json/add/time'
|
24
|
+
# x = Time.now.as_json
|
25
|
+
# # => {"json_class"=>"Time", "s"=>1700931656, "n"=>472846644}
|
26
|
+
#
|
27
|
+
# \Method +JSON.create+ deserializes such a hash, returning a \Time object:
|
28
|
+
#
|
29
|
+
# Time.json_create(x)
|
30
|
+
# # => 2023-11-25 11:00:56.472846644 -0600
|
31
|
+
#
|
22
32
|
def as_json(*)
|
23
|
-
nanoseconds = [ tv_usec * 1000 ]
|
24
|
-
respond_to?(:tv_nsec) and nanoseconds << tv_nsec
|
25
|
-
nanoseconds = nanoseconds.max
|
26
33
|
{
|
27
34
|
JSON.create_id => self.class.name,
|
28
35
|
's' => tv_sec,
|
29
|
-
'n' =>
|
36
|
+
'n' => tv_nsec,
|
30
37
|
}
|
31
38
|
end
|
32
39
|
|
33
|
-
#
|
34
|
-
#
|
40
|
+
# Returns a JSON string representing +self+:
|
41
|
+
#
|
42
|
+
# require 'json/add/time'
|
43
|
+
# puts Time.now.to_json
|
44
|
+
#
|
45
|
+
# Output:
|
46
|
+
#
|
47
|
+
# {"json_class":"Time","s":1700931678,"n":980650786}
|
48
|
+
#
|
35
49
|
def to_json(*args)
|
36
50
|
as_json.to_json(*args)
|
37
51
|
end
|