json 1.0.0 → 2.7.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (109) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGES.md +503 -0
  3. data/LICENSE +56 -0
  4. data/README.md +416 -0
  5. data/ext/json/ext/fbuffer/fbuffer.h +187 -0
  6. data/ext/json/ext/generator/depend +1 -0
  7. data/ext/json/ext/generator/extconf.rb +2 -7
  8. data/ext/json/ext/generator/generator.c +1312 -338
  9. data/ext/json/ext/generator/generator.h +177 -0
  10. data/ext/json/ext/parser/depend +1 -0
  11. data/ext/json/ext/parser/extconf.rb +28 -5
  12. data/ext/json/ext/parser/parser.c +1349 -689
  13. data/ext/json/ext/parser/parser.h +96 -0
  14. data/ext/json/ext/parser/parser.rl +644 -188
  15. data/ext/json/extconf.rb +3 -0
  16. data/json.gemspec +68 -0
  17. data/lib/json/add/bigdecimal.rb +58 -0
  18. data/lib/json/add/complex.rb +51 -0
  19. data/lib/json/add/core.rb +12 -0
  20. data/lib/json/add/date.rb +54 -0
  21. data/lib/json/add/date_time.rb +67 -0
  22. data/lib/json/add/exception.rb +49 -0
  23. data/lib/json/add/ostruct.rb +54 -0
  24. data/lib/json/add/range.rb +54 -0
  25. data/lib/json/add/rational.rb +49 -0
  26. data/lib/json/add/regexp.rb +48 -0
  27. data/lib/json/add/set.rb +48 -0
  28. data/lib/json/add/struct.rb +52 -0
  29. data/lib/json/add/symbol.rb +48 -0
  30. data/lib/json/add/time.rb +59 -0
  31. data/lib/json/common.rb +588 -74
  32. data/lib/json/ext.rb +3 -1
  33. data/lib/json/generic_object.rb +75 -0
  34. data/lib/json/pure/generator.rb +311 -119
  35. data/lib/json/pure/parser.rb +182 -55
  36. data/lib/json/pure.rb +5 -65
  37. data/lib/json/version.rb +2 -1
  38. data/lib/json.rb +583 -196
  39. metadata +78 -137
  40. data/CHANGES +0 -25
  41. data/GPL +0 -340
  42. data/README +0 -77
  43. data/Rakefile +0 -250
  44. data/TODO +0 -1
  45. data/VERSION +0 -1
  46. data/benchmarks/benchmark.txt +0 -133
  47. data/benchmarks/benchmark_generator.rb +0 -44
  48. data/benchmarks/benchmark_parser.rb +0 -22
  49. data/benchmarks/benchmark_rails.rb +0 -26
  50. data/bin/edit_json.rb +0 -11
  51. data/data/example.json +0 -1
  52. data/data/index.html +0 -37
  53. data/data/prototype.js +0 -2515
  54. data/ext/json/ext/generator/Makefile +0 -149
  55. data/ext/json/ext/generator/unicode.c +0 -184
  56. data/ext/json/ext/generator/unicode.h +0 -40
  57. data/ext/json/ext/parser/Makefile +0 -149
  58. data/ext/json/ext/parser/unicode.c +0 -156
  59. data/ext/json/ext/parser/unicode.h +0 -44
  60. data/install.rb +0 -26
  61. data/lib/json/Array.xpm +0 -21
  62. data/lib/json/FalseClass.xpm +0 -21
  63. data/lib/json/Hash.xpm +0 -21
  64. data/lib/json/Key.xpm +0 -73
  65. data/lib/json/NilClass.xpm +0 -21
  66. data/lib/json/Numeric.xpm +0 -28
  67. data/lib/json/String.xpm +0 -96
  68. data/lib/json/TrueClass.xpm +0 -21
  69. data/lib/json/editor.rb +0 -1207
  70. data/lib/json/json.xpm +0 -1499
  71. data/tests/fixtures/fail1.json +0 -1
  72. data/tests/fixtures/fail10.json +0 -1
  73. data/tests/fixtures/fail11.json +0 -1
  74. data/tests/fixtures/fail12.json +0 -1
  75. data/tests/fixtures/fail13.json +0 -1
  76. data/tests/fixtures/fail14.json +0 -1
  77. data/tests/fixtures/fail15.json +0 -1
  78. data/tests/fixtures/fail16.json +0 -1
  79. data/tests/fixtures/fail17.json +0 -1
  80. data/tests/fixtures/fail19.json +0 -1
  81. data/tests/fixtures/fail2.json +0 -1
  82. data/tests/fixtures/fail20.json +0 -1
  83. data/tests/fixtures/fail21.json +0 -1
  84. data/tests/fixtures/fail22.json +0 -1
  85. data/tests/fixtures/fail23.json +0 -1
  86. data/tests/fixtures/fail24.json +0 -1
  87. data/tests/fixtures/fail25.json +0 -1
  88. data/tests/fixtures/fail26.json +0 -1
  89. data/tests/fixtures/fail27.json +0 -2
  90. data/tests/fixtures/fail28.json +0 -2
  91. data/tests/fixtures/fail3.json +0 -1
  92. data/tests/fixtures/fail4.json +0 -1
  93. data/tests/fixtures/fail5.json +0 -1
  94. data/tests/fixtures/fail6.json +0 -1
  95. data/tests/fixtures/fail7.json +0 -1
  96. data/tests/fixtures/fail8.json +0 -1
  97. data/tests/fixtures/fail9.json +0 -1
  98. data/tests/fixtures/pass1.json +0 -56
  99. data/tests/fixtures/pass18.json +0 -1
  100. data/tests/fixtures/pass2.json +0 -1
  101. data/tests/fixtures/pass3.json +0 -6
  102. data/tests/runner.rb +0 -24
  103. data/tests/test_json.rb +0 -235
  104. data/tests/test_json_addition.rb +0 -94
  105. data/tests/test_json_fixtures.rb +0 -30
  106. data/tests/test_json_generate.rb +0 -81
  107. data/tests/test_json_unicode.rb +0 -55
  108. data/tools/fuzz.rb +0 -133
  109. data/tools/server.rb +0 -62
@@ -0,0 +1,3 @@
1
+ require 'mkmf'
2
+
3
+ create_makefile('json')
data/json.gemspec ADDED
@@ -0,0 +1,68 @@
1
+ version = File.foreach(File.join(__dir__, "lib/json/version.rb")) do |line|
2
+ /^\s*VERSION\s*=\s*'(.*)'/ =~ line and break $1
3
+ end rescue nil
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "json"
7
+ s.version = version
8
+
9
+ s.summary = "JSON Implementation for Ruby"
10
+ s.description = "This is a JSON implementation as a Ruby extension in C."
11
+ s.licenses = ["Ruby"]
12
+ s.authors = ["Florian Frank"]
13
+ s.email = "flori@ping.de"
14
+
15
+ s.extensions = ["ext/json/ext/generator/extconf.rb", "ext/json/ext/parser/extconf.rb", "ext/json/extconf.rb"]
16
+ s.extra_rdoc_files = ["README.md"]
17
+ s.rdoc_options = ["--title", "JSON implementation for Ruby", "--main", "README.md"]
18
+ s.files = [
19
+ "CHANGES.md",
20
+ "LICENSE",
21
+ "README.md",
22
+ "ext/json/ext/fbuffer/fbuffer.h",
23
+ "ext/json/ext/generator/depend",
24
+ "ext/json/ext/generator/extconf.rb",
25
+ "ext/json/ext/generator/generator.c",
26
+ "ext/json/ext/generator/generator.h",
27
+ "ext/json/ext/parser/depend",
28
+ "ext/json/ext/parser/extconf.rb",
29
+ "ext/json/ext/parser/parser.c",
30
+ "ext/json/ext/parser/parser.h",
31
+ "ext/json/ext/parser/parser.rl",
32
+ "ext/json/extconf.rb",
33
+ "json.gemspec",
34
+ "lib/json.rb",
35
+ "lib/json/add/bigdecimal.rb",
36
+ "lib/json/add/complex.rb",
37
+ "lib/json/add/core.rb",
38
+ "lib/json/add/date.rb",
39
+ "lib/json/add/date_time.rb",
40
+ "lib/json/add/exception.rb",
41
+ "lib/json/add/ostruct.rb",
42
+ "lib/json/add/range.rb",
43
+ "lib/json/add/rational.rb",
44
+ "lib/json/add/regexp.rb",
45
+ "lib/json/add/set.rb",
46
+ "lib/json/add/struct.rb",
47
+ "lib/json/add/symbol.rb",
48
+ "lib/json/add/time.rb",
49
+ "lib/json/common.rb",
50
+ "lib/json/ext.rb",
51
+ "lib/json/generic_object.rb",
52
+ "lib/json/pure.rb",
53
+ "lib/json/pure/generator.rb",
54
+ "lib/json/pure/parser.rb",
55
+ "lib/json/version.rb",
56
+ ]
57
+ s.homepage = "https://flori.github.io/json"
58
+ s.metadata = {
59
+ 'bug_tracker_uri' => 'https://github.com/flori/json/issues',
60
+ 'changelog_uri' => 'https://github.com/flori/json/blob/master/CHANGES.md',
61
+ 'documentation_uri' => 'https://flori.github.io/json/doc/index.html',
62
+ 'homepage_uri' => s.homepage,
63
+ 'source_code_uri' => 'https://github.com/flori/json',
64
+ 'wiki_uri' => 'https://github.com/flori/json/wiki'
65
+ }
66
+
67
+ s.required_ruby_version = Gem::Requirement.new(">= 2.3")
68
+ end
@@ -0,0 +1,58 @@
1
+ #frozen_string_literal: false
2
+ unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
3
+ require 'json'
4
+ end
5
+ begin
6
+ require 'bigdecimal'
7
+ rescue LoadError
8
+ end
9
+
10
+ class BigDecimal
11
+
12
+ # See #as_json.
13
+ def self.json_create(object)
14
+ BigDecimal._load object['b']
15
+ end
16
+
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
34
+ #
35
+ def as_json(*)
36
+ {
37
+ JSON.create_id => self.class.name,
38
+ 'b' => _dump,
39
+ }
40
+ end
41
+
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)
57
+ end
58
+ end if defined?(::BigDecimal)
@@ -0,0 +1,51 @@
1
+ #frozen_string_literal: false
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
@@ -0,0 +1,12 @@
1
+ #frozen_string_literal: false
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/struct'
11
+ require 'json/add/symbol'
12
+ require 'json/add/time'
@@ -0,0 +1,54 @@
1
+ #frozen_string_literal: false
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
@@ -0,0 +1,67 @@
1
+ #frozen_string_literal: false
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
+
@@ -0,0 +1,49 @@
1
+ #frozen_string_literal: false
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
@@ -0,0 +1,54 @@
1
+ #frozen_string_literal: false
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)
@@ -0,0 +1,54 @@
1
+ #frozen_string_literal: false
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
@@ -0,0 +1,49 @@
1
+ #frozen_string_literal: false
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
@@ -0,0 +1,48 @@
1
+ #frozen_string_literal: false
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
@@ -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
+
@@ -0,0 +1,52 @@
1
+ #frozen_string_literal: false
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