json 2.21.1-java → 3.0.0.rc1-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 +4 -4
- data/CHANGES.md +45 -0
- data/README.md +4 -84
- data/lib/json/common.rb +86 -402
- data/lib/json/ext/generator/state.rb +0 -27
- data/lib/json/ext/generator.jar +0 -0
- data/lib/json/ext/parser.jar +0 -0
- data/lib/json/ext.rb +0 -2
- data/lib/json/truffle_ruby/generator.rb +38 -80
- data/lib/json/version.rb +1 -1
- data/lib/json.rb +24 -265
- metadata +2 -18
- data/lib/json/add/bigdecimal.rb +0 -58
- data/lib/json/add/complex.rb +0 -51
- data/lib/json/add/core.rb +0 -13
- data/lib/json/add/date.rb +0 -54
- data/lib/json/add/date_time.rb +0 -67
- data/lib/json/add/exception.rb +0 -49
- data/lib/json/add/ostruct.rb +0 -54
- data/lib/json/add/range.rb +0 -54
- data/lib/json/add/rational.rb +0 -49
- data/lib/json/add/regexp.rb +0 -48
- data/lib/json/add/set.rb +0 -48
- data/lib/json/add/string.rb +0 -35
- data/lib/json/add/struct.rb +0 -52
- data/lib/json/add/symbol.rb +0 -52
- data/lib/json/add/time.rb +0 -52
- data/lib/json/generic_object.rb +0 -67
data/lib/json/add/complex.rb
DELETED
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
|
|
3
|
-
require 'json'
|
|
4
|
-
end
|
|
5
|
-
|
|
6
|
-
class Complex
|
|
7
|
-
|
|
8
|
-
# See #as_json.
|
|
9
|
-
def self.json_create(object)
|
|
10
|
-
Complex(object['r'], object['i'])
|
|
11
|
-
end
|
|
12
|
-
|
|
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
|
-
#
|
|
29
|
-
def as_json(*)
|
|
30
|
-
{
|
|
31
|
-
JSON.create_id => self.class.name,
|
|
32
|
-
'r' => real,
|
|
33
|
-
'i' => imag,
|
|
34
|
-
}
|
|
35
|
-
end
|
|
36
|
-
|
|
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)
|
|
50
|
-
end
|
|
51
|
-
end
|
data/lib/json/add/core.rb
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
# This file requires the implementations of ruby core's custom objects for
|
|
3
|
-
# serialisation/deserialisation.
|
|
4
|
-
|
|
5
|
-
require 'json/add/date'
|
|
6
|
-
require 'json/add/date_time'
|
|
7
|
-
require 'json/add/exception'
|
|
8
|
-
require 'json/add/range'
|
|
9
|
-
require 'json/add/regexp'
|
|
10
|
-
require 'json/add/string'
|
|
11
|
-
require 'json/add/struct'
|
|
12
|
-
require 'json/add/symbol'
|
|
13
|
-
require 'json/add/time'
|
data/lib/json/add/date.rb
DELETED
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
|
|
3
|
-
require 'json'
|
|
4
|
-
end
|
|
5
|
-
require 'date'
|
|
6
|
-
|
|
7
|
-
class Date
|
|
8
|
-
|
|
9
|
-
# See #as_json.
|
|
10
|
-
def self.json_create(object)
|
|
11
|
-
civil(*object.values_at('y', 'm', 'd', 'sg'))
|
|
12
|
-
end
|
|
13
|
-
|
|
14
|
-
alias start sg unless method_defined?(:start)
|
|
15
|
-
|
|
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
|
-
#
|
|
32
|
-
def as_json(*)
|
|
33
|
-
{
|
|
34
|
-
JSON.create_id => self.class.name,
|
|
35
|
-
'y' => year,
|
|
36
|
-
'm' => month,
|
|
37
|
-
'd' => day,
|
|
38
|
-
'sg' => start,
|
|
39
|
-
}
|
|
40
|
-
end
|
|
41
|
-
|
|
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
|
-
#
|
|
51
|
-
def to_json(*args)
|
|
52
|
-
as_json.to_json(*args)
|
|
53
|
-
end
|
|
54
|
-
end
|
data/lib/json/add/date_time.rb
DELETED
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
|
|
3
|
-
require 'json'
|
|
4
|
-
end
|
|
5
|
-
require 'date'
|
|
6
|
-
|
|
7
|
-
class DateTime
|
|
8
|
-
|
|
9
|
-
# See #as_json.
|
|
10
|
-
def self.json_create(object)
|
|
11
|
-
args = object.values_at('y', 'm', 'd', 'H', 'M', 'S')
|
|
12
|
-
of_a, of_b = object['of'].split('/')
|
|
13
|
-
if of_b and of_b != '0'
|
|
14
|
-
args << Rational(of_a.to_i, of_b.to_i)
|
|
15
|
-
else
|
|
16
|
-
args << of_a
|
|
17
|
-
end
|
|
18
|
-
args << object['sg']
|
|
19
|
-
civil(*args)
|
|
20
|
-
end
|
|
21
|
-
|
|
22
|
-
alias start sg unless method_defined?(:start)
|
|
23
|
-
|
|
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
|
-
#
|
|
39
|
-
def as_json(*)
|
|
40
|
-
{
|
|
41
|
-
JSON.create_id => self.class.name,
|
|
42
|
-
'y' => year,
|
|
43
|
-
'm' => month,
|
|
44
|
-
'd' => day,
|
|
45
|
-
'H' => hour,
|
|
46
|
-
'M' => min,
|
|
47
|
-
'S' => sec,
|
|
48
|
-
'of' => offset.to_s,
|
|
49
|
-
'sg' => start,
|
|
50
|
-
}
|
|
51
|
-
end
|
|
52
|
-
|
|
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
|
-
#
|
|
62
|
-
def to_json(*args)
|
|
63
|
-
as_json.to_json(*args)
|
|
64
|
-
end
|
|
65
|
-
end
|
|
66
|
-
|
|
67
|
-
|
data/lib/json/add/exception.rb
DELETED
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
|
|
3
|
-
require 'json'
|
|
4
|
-
end
|
|
5
|
-
|
|
6
|
-
class Exception
|
|
7
|
-
|
|
8
|
-
# See #as_json.
|
|
9
|
-
def self.json_create(object)
|
|
10
|
-
result = new(object['m'])
|
|
11
|
-
result.set_backtrace object['b']
|
|
12
|
-
result
|
|
13
|
-
end
|
|
14
|
-
|
|
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
|
-
#
|
|
29
|
-
def as_json(*)
|
|
30
|
-
{
|
|
31
|
-
JSON.create_id => self.class.name,
|
|
32
|
-
'm' => message,
|
|
33
|
-
'b' => backtrace,
|
|
34
|
-
}
|
|
35
|
-
end
|
|
36
|
-
|
|
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
|
-
#
|
|
46
|
-
def to_json(*args)
|
|
47
|
-
as_json.to_json(*args)
|
|
48
|
-
end
|
|
49
|
-
end
|
data/lib/json/add/ostruct.rb
DELETED
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
|
|
3
|
-
require 'json'
|
|
4
|
-
end
|
|
5
|
-
begin
|
|
6
|
-
require 'ostruct'
|
|
7
|
-
rescue LoadError
|
|
8
|
-
end
|
|
9
|
-
|
|
10
|
-
class OpenStruct
|
|
11
|
-
|
|
12
|
-
# See #as_json.
|
|
13
|
-
def self.json_create(object)
|
|
14
|
-
new(object['t'] || object[:t])
|
|
15
|
-
end
|
|
16
|
-
|
|
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
|
-
#
|
|
33
|
-
def as_json(*)
|
|
34
|
-
klass = self.class.name
|
|
35
|
-
klass.to_s.empty? and raise JSON::JSONError, "Only named structs are supported!"
|
|
36
|
-
{
|
|
37
|
-
JSON.create_id => klass,
|
|
38
|
-
't' => table,
|
|
39
|
-
}
|
|
40
|
-
end
|
|
41
|
-
|
|
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
|
-
#
|
|
51
|
-
def to_json(*args)
|
|
52
|
-
as_json.to_json(*args)
|
|
53
|
-
end
|
|
54
|
-
end if defined?(::OpenStruct)
|
data/lib/json/add/range.rb
DELETED
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
|
|
3
|
-
require 'json'
|
|
4
|
-
end
|
|
5
|
-
|
|
6
|
-
class Range
|
|
7
|
-
|
|
8
|
-
# See #as_json.
|
|
9
|
-
def self.json_create(object)
|
|
10
|
-
new(*object['a'])
|
|
11
|
-
end
|
|
12
|
-
|
|
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
|
-
#
|
|
31
|
-
def as_json(*)
|
|
32
|
-
{
|
|
33
|
-
JSON.create_id => self.class.name,
|
|
34
|
-
'a' => [ first, last, exclude_end? ]
|
|
35
|
-
}
|
|
36
|
-
end
|
|
37
|
-
|
|
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
|
-
#
|
|
51
|
-
def to_json(*args)
|
|
52
|
-
as_json.to_json(*args)
|
|
53
|
-
end
|
|
54
|
-
end
|
data/lib/json/add/rational.rb
DELETED
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
|
|
3
|
-
require 'json'
|
|
4
|
-
end
|
|
5
|
-
|
|
6
|
-
class Rational
|
|
7
|
-
|
|
8
|
-
# See #as_json.
|
|
9
|
-
def self.json_create(object)
|
|
10
|
-
Rational(object['n'], object['d'])
|
|
11
|
-
end
|
|
12
|
-
|
|
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
|
-
#
|
|
29
|
-
def as_json(*)
|
|
30
|
-
{
|
|
31
|
-
JSON.create_id => self.class.name,
|
|
32
|
-
'n' => numerator,
|
|
33
|
-
'd' => denominator,
|
|
34
|
-
}
|
|
35
|
-
end
|
|
36
|
-
|
|
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)
|
|
48
|
-
end
|
|
49
|
-
end
|
data/lib/json/add/regexp.rb
DELETED
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
|
|
3
|
-
require 'json'
|
|
4
|
-
end
|
|
5
|
-
|
|
6
|
-
class Regexp
|
|
7
|
-
|
|
8
|
-
# See #as_json.
|
|
9
|
-
def self.json_create(object)
|
|
10
|
-
new(object['s'], object['o'])
|
|
11
|
-
end
|
|
12
|
-
|
|
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
|
-
#
|
|
28
|
-
def as_json(*)
|
|
29
|
-
{
|
|
30
|
-
JSON.create_id => self.class.name,
|
|
31
|
-
'o' => options,
|
|
32
|
-
's' => source,
|
|
33
|
-
}
|
|
34
|
-
end
|
|
35
|
-
|
|
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)
|
|
47
|
-
end
|
|
48
|
-
end
|
data/lib/json/add/set.rb
DELETED
|
@@ -1,48 +0,0 @@
|
|
|
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/string.rb
DELETED
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
|
|
3
|
-
require 'json'
|
|
4
|
-
end
|
|
5
|
-
|
|
6
|
-
class String
|
|
7
|
-
# call-seq: json_create(o)
|
|
8
|
-
#
|
|
9
|
-
# Raw Strings are JSON Objects (the raw bytes are stored in an array for the
|
|
10
|
-
# key "raw"). The Ruby String can be created by this class method.
|
|
11
|
-
def self.json_create(object)
|
|
12
|
-
object["raw"].pack("C*")
|
|
13
|
-
end
|
|
14
|
-
|
|
15
|
-
# call-seq: to_json_raw_object()
|
|
16
|
-
#
|
|
17
|
-
# This method creates a raw object hash, that can be nested into
|
|
18
|
-
# other data structures and will be generated as a raw string. This
|
|
19
|
-
# method should be used, if you want to convert raw strings to JSON
|
|
20
|
-
# instead of UTF-8 strings, e. g. binary data.
|
|
21
|
-
def to_json_raw_object
|
|
22
|
-
{
|
|
23
|
-
JSON.create_id => self.class.name,
|
|
24
|
-
"raw" => unpack("C*"),
|
|
25
|
-
}
|
|
26
|
-
end
|
|
27
|
-
|
|
28
|
-
# call-seq: to_json_raw(*args)
|
|
29
|
-
#
|
|
30
|
-
# This method creates a JSON text from the result of a call to
|
|
31
|
-
# to_json_raw_object of this String.
|
|
32
|
-
def to_json_raw(...)
|
|
33
|
-
to_json_raw_object.to_json(...)
|
|
34
|
-
end
|
|
35
|
-
end
|
data/lib/json/add/struct.rb
DELETED
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
|
|
3
|
-
require 'json'
|
|
4
|
-
end
|
|
5
|
-
|
|
6
|
-
class Struct
|
|
7
|
-
|
|
8
|
-
# See #as_json.
|
|
9
|
-
def self.json_create(object)
|
|
10
|
-
new(*object['v'])
|
|
11
|
-
end
|
|
12
|
-
|
|
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
|
-
#
|
|
30
|
-
def as_json(*)
|
|
31
|
-
klass = self.class.name
|
|
32
|
-
klass.to_s.empty? and raise JSON::JSONError, "Only named structs are supported!"
|
|
33
|
-
{
|
|
34
|
-
JSON.create_id => klass,
|
|
35
|
-
'v' => values,
|
|
36
|
-
}
|
|
37
|
-
end
|
|
38
|
-
|
|
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
|
-
#
|
|
49
|
-
def to_json(*args)
|
|
50
|
-
as_json.to_json(*args)
|
|
51
|
-
end
|
|
52
|
-
end
|
data/lib/json/add/symbol.rb
DELETED
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
|
|
3
|
-
require 'json'
|
|
4
|
-
end
|
|
5
|
-
|
|
6
|
-
class Symbol
|
|
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
|
-
#
|
|
23
|
-
def as_json(*)
|
|
24
|
-
{
|
|
25
|
-
JSON.create_id => self.class.name,
|
|
26
|
-
's' => to_s,
|
|
27
|
-
}
|
|
28
|
-
end
|
|
29
|
-
|
|
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
|
|
46
|
-
end
|
|
47
|
-
|
|
48
|
-
# See #as_json.
|
|
49
|
-
def self.json_create(o)
|
|
50
|
-
o['s'].to_sym
|
|
51
|
-
end
|
|
52
|
-
end
|
data/lib/json/add/time.rb
DELETED
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
|
|
3
|
-
require 'json'
|
|
4
|
-
end
|
|
5
|
-
|
|
6
|
-
class Time
|
|
7
|
-
|
|
8
|
-
# See #as_json.
|
|
9
|
-
def self.json_create(object)
|
|
10
|
-
if usec = object.delete('u') # used to be tv_usec -> tv_nsec
|
|
11
|
-
object['n'] = usec * 1000
|
|
12
|
-
end
|
|
13
|
-
at(object['s'], Rational(object['n'], 1000))
|
|
14
|
-
end
|
|
15
|
-
|
|
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
|
-
#
|
|
32
|
-
def as_json(*)
|
|
33
|
-
{
|
|
34
|
-
JSON.create_id => self.class.name,
|
|
35
|
-
's' => tv_sec,
|
|
36
|
-
'n' => tv_nsec,
|
|
37
|
-
}
|
|
38
|
-
end
|
|
39
|
-
|
|
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
|
-
#
|
|
49
|
-
def to_json(*args)
|
|
50
|
-
as_json.to_json(*args)
|
|
51
|
-
end
|
|
52
|
-
end
|