json 2.18.1 → 3.0.0

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.
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
@@ -1,67 +0,0 @@
1
- # frozen_string_literal: true
2
- begin
3
- require 'ostruct'
4
- rescue LoadError
5
- warn "JSON::GenericObject requires 'ostruct'. Please install it with `gem install ostruct`."
6
- end
7
-
8
- module JSON
9
- class GenericObject < OpenStruct
10
- class << self
11
- alias [] new
12
-
13
- def json_creatable?
14
- @json_creatable
15
- end
16
-
17
- attr_writer :json_creatable
18
-
19
- def json_create(data)
20
- data = data.dup
21
- data.delete JSON.create_id
22
- self[data]
23
- end
24
-
25
- def from_hash(object)
26
- case
27
- when object.respond_to?(:to_hash)
28
- result = new
29
- object.to_hash.each do |key, value|
30
- result[key] = from_hash(value)
31
- end
32
- result
33
- when object.respond_to?(:to_ary)
34
- object.to_ary.map { |a| from_hash(a) }
35
- else
36
- object
37
- end
38
- end
39
-
40
- def load(source, proc = nil, opts = {})
41
- result = ::JSON.load(source, proc, opts.merge(:object_class => self))
42
- result.nil? ? new : result
43
- end
44
-
45
- def dump(obj, *args)
46
- ::JSON.dump(obj, *args)
47
- end
48
- end
49
- self.json_creatable = false
50
-
51
- def to_hash
52
- table
53
- end
54
-
55
- def |(other)
56
- self.class[other.to_hash.merge(to_hash)]
57
- end
58
-
59
- def as_json(*)
60
- { JSON.create_id => self.class.name }.merge to_hash
61
- end
62
-
63
- def to_json(*a)
64
- as_json.to_json(*a)
65
- end
66
- end if defined?(::OpenStruct)
67
- end