json 2.6.2 → 2.10.1

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.
Files changed (44) hide show
  1. checksums.yaml +4 -4
  2. data/BSDL +22 -0
  3. data/CHANGES.md +144 -17
  4. data/LEGAL +8 -0
  5. data/README.md +67 -224
  6. data/ext/json/ext/fbuffer/fbuffer.h +110 -92
  7. data/ext/json/ext/generator/extconf.rb +8 -2
  8. data/ext/json/ext/generator/generator.c +1020 -806
  9. data/ext/json/ext/parser/extconf.rb +7 -27
  10. data/ext/json/ext/parser/parser.c +1343 -3212
  11. data/json.gemspec +48 -52
  12. data/lib/json/add/bigdecimal.rb +39 -10
  13. data/lib/json/add/complex.rb +29 -6
  14. data/lib/json/add/core.rb +1 -1
  15. data/lib/json/add/date.rb +27 -7
  16. data/lib/json/add/date_time.rb +26 -9
  17. data/lib/json/add/exception.rb +25 -7
  18. data/lib/json/add/ostruct.rb +32 -9
  19. data/lib/json/add/range.rb +33 -8
  20. data/lib/json/add/rational.rb +28 -6
  21. data/lib/json/add/regexp.rb +26 -8
  22. data/lib/json/add/set.rb +25 -6
  23. data/lib/json/add/struct.rb +29 -7
  24. data/lib/json/add/symbol.rb +34 -7
  25. data/lib/json/add/time.rb +29 -15
  26. data/lib/json/common.rb +418 -128
  27. data/lib/json/ext/generator/state.rb +106 -0
  28. data/lib/json/ext.rb +34 -4
  29. data/lib/json/generic_object.rb +7 -3
  30. data/lib/json/truffle_ruby/generator.rb +690 -0
  31. data/lib/json/version.rb +3 -7
  32. data/lib/json.rb +25 -21
  33. metadata +15 -26
  34. data/VERSION +0 -1
  35. data/ext/json/ext/generator/depend +0 -1
  36. data/ext/json/ext/generator/generator.h +0 -174
  37. data/ext/json/ext/parser/depend +0 -1
  38. data/ext/json/ext/parser/parser.h +0 -96
  39. data/ext/json/ext/parser/parser.rl +0 -986
  40. data/ext/json/extconf.rb +0 -3
  41. data/lib/json/pure/generator.rb +0 -479
  42. data/lib/json/pure/parser.rb +0 -337
  43. data/lib/json/pure.rb +0 -15
  44. /data/{LICENSE → COPYING} +0 -0
data/json.gemspec CHANGED
@@ -1,67 +1,63 @@
1
- # -*- encoding: utf-8 -*-
1
+ # frozen_string_literal: true
2
+
3
+ version = File.foreach(File.join(__dir__, "lib/json/version.rb")) do |line|
4
+ /^\s*VERSION\s*=\s*'(.*)'/ =~ line and break $1
5
+ end rescue nil
6
+
7
+ spec = Gem::Specification.new do |s|
8
+ java_ext = Gem::Platform === s.platform && s.platform =~ 'java' || RUBY_ENGINE == 'jruby'
2
9
 
3
- Gem::Specification.new do |s|
4
10
  s.name = "json"
5
- s.version = File.read(File.expand_path('../VERSION', __FILE__)).chomp
11
+ s.version = version
6
12
 
7
13
  s.summary = "JSON Implementation for Ruby"
8
- s.description = "This is a JSON implementation as a Ruby extension in C."
14
+ s.homepage = "https://github.com/ruby/json"
15
+ s.metadata = {
16
+ 'bug_tracker_uri' => 'https://github.com/ruby/json/issues',
17
+ 'changelog_uri' => 'https://github.com/ruby/json/blob/master/CHANGES.md',
18
+ 'documentation_uri' => 'https://docs.ruby-lang.org/en/master/JSON.html',
19
+ 'homepage_uri' => s.homepage,
20
+ 'source_code_uri' => 'https://github.com/ruby/json',
21
+ }
22
+
23
+ s.required_ruby_version = Gem::Requirement.new(">= 2.7")
24
+
25
+ if java_ext
26
+ s.description = "A JSON implementation as a JRuby extension."
27
+ s.author = "Daniel Luz"
28
+ s.email = "dev+ruby@mernen.com"
29
+ else
30
+ s.description = "This is a JSON implementation as a Ruby extension in C."
31
+ s.authors = ["Florian Frank"]
32
+ s.email = "flori@ping.de"
33
+ end
34
+
9
35
  s.licenses = ["Ruby"]
10
- s.authors = ["Florian Frank"]
11
- s.email = "flori@ping.de"
12
36
 
13
- s.extensions = ["ext/json/ext/generator/extconf.rb", "ext/json/ext/parser/extconf.rb", "ext/json/extconf.rb"]
14
37
  s.extra_rdoc_files = ["README.md"]
15
38
  s.rdoc_options = ["--title", "JSON implementation for Ruby", "--main", "README.md"]
39
+
16
40
  s.files = [
17
41
  "CHANGES.md",
18
- "LICENSE",
42
+ "COPYING",
43
+ "BSDL",
44
+ "LEGAL",
19
45
  "README.md",
20
- "VERSION",
21
- "ext/json/ext/fbuffer/fbuffer.h",
22
- "ext/json/ext/generator/depend",
23
- "ext/json/ext/generator/extconf.rb",
24
- "ext/json/ext/generator/generator.c",
25
- "ext/json/ext/generator/generator.h",
26
- "ext/json/ext/parser/depend",
27
- "ext/json/ext/parser/extconf.rb",
28
- "ext/json/ext/parser/parser.c",
29
- "ext/json/ext/parser/parser.h",
30
- "ext/json/ext/parser/parser.rl",
31
- "ext/json/extconf.rb",
32
46
  "json.gemspec",
33
- "lib/json.rb",
34
- "lib/json/add/bigdecimal.rb",
35
- "lib/json/add/complex.rb",
36
- "lib/json/add/core.rb",
37
- "lib/json/add/date.rb",
38
- "lib/json/add/date_time.rb",
39
- "lib/json/add/exception.rb",
40
- "lib/json/add/ostruct.rb",
41
- "lib/json/add/range.rb",
42
- "lib/json/add/rational.rb",
43
- "lib/json/add/regexp.rb",
44
- "lib/json/add/set.rb",
45
- "lib/json/add/struct.rb",
46
- "lib/json/add/symbol.rb",
47
- "lib/json/add/time.rb",
48
- "lib/json/common.rb",
49
- "lib/json/ext.rb",
50
- "lib/json/generic_object.rb",
51
- "lib/json/pure.rb",
52
- "lib/json/pure/generator.rb",
53
- "lib/json/pure/parser.rb",
54
- "lib/json/version.rb",
47
+ *Dir["lib/**/*.rb"],
55
48
  ]
56
- s.homepage = "http://flori.github.com/json"
57
- s.metadata = {
58
- 'bug_tracker_uri' => 'https://github.com/flori/json/issues',
59
- 'changelog_uri' => 'https://github.com/flori/json/blob/master/CHANGES.md',
60
- 'documentation_uri' => 'http://flori.github.io/json/doc/index.html',
61
- 'homepage_uri' => 'http://flori.github.io/json/',
62
- 'source_code_uri' => 'https://github.com/flori/json',
63
- 'wiki_uri' => 'https://github.com/flori/json/wiki'
64
- }
65
49
 
66
- s.required_ruby_version = Gem::Requirement.new(">= 2.3")
50
+ if java_ext
51
+ s.platform = 'java'
52
+ s.files += Dir["lib/json/ext/**/*.jar"]
53
+ else
54
+ s.extensions = Dir["ext/json/**/extconf.rb"]
55
+ s.files += Dir["ext/json/**/*.{c,h}"]
56
+ end
57
+ end
58
+
59
+ if RUBY_ENGINE == 'jruby' && $0 == __FILE__
60
+ Gem::Builder.new(spec).build
61
+ else
62
+ spec
67
63
  end
@@ -1,29 +1,58 @@
1
- #frozen_string_literal: false
1
+ # frozen_string_literal: true
2
2
  unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
3
3
  require 'json'
4
4
  end
5
- defined?(::BigDecimal) or require 'bigdecimal'
5
+ begin
6
+ require 'bigdecimal'
7
+ rescue LoadError
8
+ end
6
9
 
7
10
  class BigDecimal
8
- # Import a JSON Marshalled object.
9
- #
10
- # method used for JSON marshalling support.
11
+
12
+ # See #as_json.
11
13
  def self.json_create(object)
12
14
  BigDecimal._load object['b']
13
15
  end
14
16
 
15
- # Marshal the object to JSON.
17
+ # Methods <tt>BigDecimal#as_json</tt> and +BigDecimal.json_create+ may be used
18
+ # to serialize and deserialize a \BigDecimal object;
19
+ # see Marshal[https://docs.ruby-lang.org/en/master/Marshal.html].
20
+ #
21
+ # \Method <tt>BigDecimal#as_json</tt> serializes +self+,
22
+ # returning a 2-element hash representing +self+:
23
+ #
24
+ # require 'json/add/bigdecimal'
25
+ # x = BigDecimal(2).as_json # => {"json_class"=>"BigDecimal", "b"=>"27:0.2e1"}
26
+ # y = BigDecimal(2.0, 4).as_json # => {"json_class"=>"BigDecimal", "b"=>"36:0.2e1"}
27
+ # z = BigDecimal(Complex(2, 0)).as_json # => {"json_class"=>"BigDecimal", "b"=>"27:0.2e1"}
28
+ #
29
+ # \Method +JSON.create+ deserializes such a hash, returning a \BigDecimal object:
30
+ #
31
+ # BigDecimal.json_create(x) # => 0.2e1
32
+ # BigDecimal.json_create(y) # => 0.2e1
33
+ # BigDecimal.json_create(z) # => 0.2e1
16
34
  #
17
- # method used for JSON marshalling support.
18
35
  def as_json(*)
19
36
  {
20
37
  JSON.create_id => self.class.name,
21
- 'b' => _dump,
38
+ 'b' => _dump.force_encoding(Encoding::UTF_8),
22
39
  }
23
40
  end
24
41
 
25
- # return the JSON value
42
+ # Returns a JSON string representing +self+:
43
+ #
44
+ # require 'json/add/bigdecimal'
45
+ # puts BigDecimal(2).to_json
46
+ # puts BigDecimal(2.0, 4).to_json
47
+ # puts BigDecimal(Complex(2, 0)).to_json
48
+ #
49
+ # Output:
50
+ #
51
+ # {"json_class":"BigDecimal","b":"27:0.2e1"}
52
+ # {"json_class":"BigDecimal","b":"36:0.2e1"}
53
+ # {"json_class":"BigDecimal","b":"27:0.2e1"}
54
+ #
26
55
  def to_json(*args)
27
56
  as_json.to_json(*args)
28
57
  end
29
- end
58
+ end if defined?(::BigDecimal)
@@ -1,18 +1,31 @@
1
- #frozen_string_literal: false
1
+ # frozen_string_literal: true
2
2
  unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
3
3
  require 'json'
4
4
  end
5
5
 
6
6
  class Complex
7
7
 
8
- # Deserializes JSON string by converting Real value <tt>r</tt>, imaginary
9
- # value <tt>i</tt>, to a Complex object.
8
+ # See #as_json.
10
9
  def self.json_create(object)
11
10
  Complex(object['r'], object['i'])
12
11
  end
13
12
 
14
- # Returns a hash, that will be turned into a JSON object and represent this
15
- # object.
13
+ # Methods <tt>Complex#as_json</tt> and +Complex.json_create+ may be used
14
+ # to serialize and deserialize a \Complex object;
15
+ # see Marshal[https://docs.ruby-lang.org/en/master/Marshal.html].
16
+ #
17
+ # \Method <tt>Complex#as_json</tt> serializes +self+,
18
+ # returning a 2-element hash representing +self+:
19
+ #
20
+ # require 'json/add/complex'
21
+ # x = Complex(2).as_json # => {"json_class"=>"Complex", "r"=>2, "i"=>0}
22
+ # y = Complex(2.0, 4).as_json # => {"json_class"=>"Complex", "r"=>2.0, "i"=>4}
23
+ #
24
+ # \Method +JSON.create+ deserializes such a hash, returning a \Complex object:
25
+ #
26
+ # Complex.json_create(x) # => (2+0i)
27
+ # Complex.json_create(y) # => (2.0+4i)
28
+ #
16
29
  def as_json(*)
17
30
  {
18
31
  JSON.create_id => self.class.name,
@@ -21,7 +34,17 @@ class Complex
21
34
  }
22
35
  end
23
36
 
24
- # Stores class name (Complex) along with real value <tt>r</tt> and imaginary value <tt>i</tt> as JSON string
37
+ # Returns a JSON string representing +self+:
38
+ #
39
+ # require 'json/add/complex'
40
+ # puts Complex(2).to_json
41
+ # puts Complex(2.0, 4).to_json
42
+ #
43
+ # Output:
44
+ #
45
+ # {"json_class":"Complex","r":2,"i":0}
46
+ # {"json_class":"Complex","r":2.0,"i":4}
47
+ #
25
48
  def to_json(*args)
26
49
  as_json.to_json(*args)
27
50
  end
data/lib/json/add/core.rb CHANGED
@@ -1,4 +1,4 @@
1
- #frozen_string_literal: false
1
+ # frozen_string_literal: true
2
2
  # This file requires the implementations of ruby core's custom objects for
3
3
  # serialisation/deserialisation.
4
4
 
data/lib/json/add/date.rb CHANGED
@@ -1,4 +1,4 @@
1
- #frozen_string_literal: false
1
+ # frozen_string_literal: true
2
2
  unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
3
3
  require 'json'
4
4
  end
@@ -6,16 +6,29 @@ require 'date'
6
6
 
7
7
  class Date
8
8
 
9
- # Deserializes JSON string by converting Julian year <tt>y</tt>, month
10
- # <tt>m</tt>, day <tt>d</tt> and Day of Calendar Reform <tt>sg</tt> to Date.
9
+ # See #as_json.
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
- # Returns a hash, that will be turned into a JSON object and represent this
18
- # object.
16
+ # Methods <tt>Date#as_json</tt> and +Date.json_create+ may be used
17
+ # to serialize and deserialize a \Date object;
18
+ # see Marshal[https://docs.ruby-lang.org/en/master/Marshal.html].
19
+ #
20
+ # \Method <tt>Date#as_json</tt> serializes +self+,
21
+ # returning a 2-element hash representing +self+:
22
+ #
23
+ # require 'json/add/date'
24
+ # x = Date.today.as_json
25
+ # # => {"json_class"=>"Date", "y"=>2023, "m"=>11, "d"=>21, "sg"=>2299161.0}
26
+ #
27
+ # \Method +JSON.create+ deserializes such a hash, returning a \Date object:
28
+ #
29
+ # Date.json_create(x)
30
+ # # => #<Date: 2023-11-21 ((2460270j,0s,0n),+0s,2299161j)>
31
+ #
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
- # Stores class name (Date) with Julian year <tt>y</tt>, month <tt>m</tt>, day
30
- # <tt>d</tt> and Day of Calendar Reform <tt>sg</tt> as JSON string
42
+ # Returns a JSON string representing +self+:
43
+ #
44
+ # require 'json/add/date'
45
+ # puts Date.today.to_json
46
+ #
47
+ # Output:
48
+ #
49
+ # {"json_class":"Date","y":2023,"m":11,"d":21,"sg":2299161.0}
50
+ #
31
51
  def to_json(*args)
32
52
  as_json.to_json(*args)
33
53
  end
@@ -1,4 +1,4 @@
1
- #frozen_string_literal: false
1
+ # frozen_string_literal: true
2
2
  unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
3
3
  require 'json'
4
4
  end
@@ -6,9 +6,7 @@ require 'date'
6
6
 
7
7
  class DateTime
8
8
 
9
- # Deserializes JSON string by converting year <tt>y</tt>, month <tt>m</tt>,
10
- # day <tt>d</tt>, hour <tt>H</tt>, minute <tt>M</tt>, second <tt>S</tt>,
11
- # offset <tt>of</tt> and Day of Calendar Reform <tt>sg</tt> to DateTime.
9
+ # See #as_json.
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
- # Returns a hash, that will be turned into a JSON object and represent this
27
- # object.
24
+ # Methods <tt>DateTime#as_json</tt> and +DateTime.json_create+ may be used
25
+ # to serialize and deserialize a \DateTime object;
26
+ # see Marshal[https://docs.ruby-lang.org/en/master/Marshal.html].
27
+ #
28
+ # \Method <tt>DateTime#as_json</tt> serializes +self+,
29
+ # returning a 2-element hash representing +self+:
30
+ #
31
+ # require 'json/add/datetime'
32
+ # x = DateTime.now.as_json
33
+ # # => {"json_class"=>"DateTime", "y"=>2023, "m"=>11, "d"=>21, "sg"=>2299161.0}
34
+ #
35
+ # \Method +JSON.create+ deserializes such a hash, returning a \DateTime object:
36
+ #
37
+ # DateTime.json_create(x) # BUG? Raises Date::Error "invalid date"
38
+ #
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
- # Stores class name (DateTime) with Julian year <tt>y</tt>, month <tt>m</tt>,
43
- # day <tt>d</tt>, hour <tt>H</tt>, minute <tt>M</tt>, second <tt>S</tt>,
44
- # offset <tt>of</tt> and Day of Calendar Reform <tt>sg</tt> as JSON string
53
+ # Returns a JSON string representing +self+:
54
+ #
55
+ # require 'json/add/datetime'
56
+ # puts DateTime.now.to_json
57
+ #
58
+ # Output:
59
+ #
60
+ # {"json_class":"DateTime","y":2023,"m":11,"d":21,"sg":2299161.0}
61
+ #
45
62
  def to_json(*args)
46
63
  as_json.to_json(*args)
47
64
  end
@@ -1,20 +1,31 @@
1
- #frozen_string_literal: false
1
+ # frozen_string_literal: true
2
2
  unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
3
3
  require 'json'
4
4
  end
5
5
 
6
6
  class Exception
7
7
 
8
- # Deserializes JSON string by constructing new Exception object with message
9
- # <tt>m</tt> and backtrace <tt>b</tt> serialized with <tt>to_json</tt>
8
+ # See #as_json.
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
- # Returns a hash, that will be turned into a JSON object and represent this
17
- # object.
15
+ # Methods <tt>Exception#as_json</tt> and +Exception.json_create+ may be used
16
+ # to serialize and deserialize a \Exception object;
17
+ # see Marshal[https://docs.ruby-lang.org/en/master/Marshal.html].
18
+ #
19
+ # \Method <tt>Exception#as_json</tt> serializes +self+,
20
+ # returning a 2-element hash representing +self+:
21
+ #
22
+ # require 'json/add/exception'
23
+ # x = Exception.new('Foo').as_json # => {"json_class"=>"Exception", "m"=>"Foo", "b"=>nil}
24
+ #
25
+ # \Method +JSON.create+ deserializes such a hash, returning a \Exception object:
26
+ #
27
+ # Exception.json_create(x) # => #<Exception: Foo>
28
+ #
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
- # Stores class name (Exception) with message <tt>m</tt> and backtrace array
27
- # <tt>b</tt> as JSON string
37
+ # Returns a JSON string representing +self+:
38
+ #
39
+ # require 'json/add/exception'
40
+ # puts Exception.new('Foo').to_json
41
+ #
42
+ # Output:
43
+ #
44
+ # {"json_class":"Exception","m":"Foo","b":null}
45
+ #
28
46
  def to_json(*args)
29
47
  as_json.to_json(*args)
30
48
  end
@@ -1,19 +1,35 @@
1
- #frozen_string_literal: false
1
+ # frozen_string_literal: true
2
2
  unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
3
3
  require 'json'
4
4
  end
5
- require 'ostruct'
5
+ begin
6
+ require 'ostruct'
7
+ rescue LoadError
8
+ end
6
9
 
7
10
  class OpenStruct
8
11
 
9
- # Deserializes JSON string by constructing new Struct object with values
10
- # <tt>t</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
- # Returns a hash, that will be turned into a JSON object and represent this
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
- # Stores class name (OpenStruct) with this struct's values <tt>t</tt> as a
27
- # JSON string.
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)
@@ -1,18 +1,33 @@
1
- #frozen_string_literal: false
1
+ # frozen_string_literal: true
2
2
  unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
3
3
  require 'json'
4
4
  end
5
5
 
6
6
  class Range
7
7
 
8
- # Deserializes JSON string by constructing new Range object with arguments
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
- # Returns a hash, that will be turned into a JSON object and represent this
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
- # Stores class name (Range) with JSON array of arguments <tt>a</tt> which
24
- # include <tt>first</tt> (integer), <tt>last</tt> (integer), and
25
- # <tt>exclude_end?</tt> (boolean) as JSON string.
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
@@ -1,17 +1,31 @@
1
- #frozen_string_literal: false
1
+ # frozen_string_literal: true
2
2
  unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
3
3
  require 'json'
4
4
  end
5
5
 
6
6
  class Rational
7
- # Deserializes JSON string by converting numerator value <tt>n</tt>,
8
- # denominator value <tt>d</tt>, to a Rational object.
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
- # Returns a hash, that will be turned into a JSON object and represent this
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,7 +34,15 @@ class Rational
20
34
  }
21
35
  end
22
36
 
23
- # Stores class name (Rational) along with numerator value <tt>n</tt> and denominator value <tt>d</tt> as JSON string
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
+ #
24
46
  def to_json(*args)
25
47
  as_json.to_json(*args)
26
48
  end
@@ -1,19 +1,30 @@
1
- #frozen_string_literal: false
1
+ # frozen_string_literal: true
2
2
  unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
3
3
  require 'json'
4
4
  end
5
5
 
6
6
  class Regexp
7
7
 
8
- # Deserializes JSON string by constructing new Regexp object with source
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
- # Returns a hash, that will be turned into a JSON object and represent this
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,8 +33,15 @@ class Regexp
22
33
  }
23
34
  end
24
35
 
25
- # Stores class name (Regexp) with options <tt>o</tt> and source <tt>s</tt>
26
- # (Regexp or String) as JSON string
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
+ #
27
45
  def to_json(*args)
28
46
  as_json.to_json(*args)
29
47
  end