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/lib/json/add/set.rb CHANGED
@@ -4,16 +4,27 @@ end
4
4
  defined?(::Set) or require 'set'
5
5
 
6
6
  class Set
7
- # Import a JSON Marshalled object.
8
- #
9
- # method used for JSON marshalling support.
7
+
8
+ # See #as_json.
10
9
  def self.json_create(object)
11
10
  new object['a']
12
11
  end
13
12
 
14
- # Marshal the object to JSON.
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"}>
15
27
  #
16
- # method used for JSON marshalling support.
17
28
  def as_json(*)
18
29
  {
19
30
  JSON.create_id => self.class.name,
@@ -21,7 +32,15 @@ class Set
21
32
  }
22
33
  end
23
34
 
24
- # return the JSON value
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
+ #
25
44
  def to_json(*args)
26
45
  as_json.to_json(*args)
27
46
  end
@@ -1,18 +1,32 @@
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 Struct
7
7
 
8
- # Deserializes JSON string by constructing new Struct object with values
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
- # Returns a hash, that will be turned into a JSON object and represent this
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
- # Stores class name (Struct) with Struct values <tt>v</tt> as a JSON string.
26
- # Only named structs are supported.
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
@@ -1,11 +1,25 @@
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 Symbol
7
- # Returns a hash, that will be turned into a JSON object and represent this
8
- # object.
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
- # Stores class name (Symbol) with String representation of Symbol as a JSON string.
17
- def to_json(*a)
18
- as_json.to_json(*a)
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
- # Deserializes JSON string by converting the <tt>string</tt> value stored in the object to a Symbol
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: 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 Time
7
7
 
8
- # Deserializes JSON string by converting time since epoch to Time
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
- if method_defined?(:tv_nsec)
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
- # Returns a hash, that will be turned into a JSON object and represent this
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' => nanoseconds,
36
+ 'n' => tv_nsec,
30
37
  }
31
38
  end
32
39
 
33
- # Stores class name (Time) with number of seconds since epoch and number of
34
- # microseconds for Time as JSON string
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