ed-precompiled_json 2.15.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.
@@ -0,0 +1,267 @@
1
+ /*
2
+
3
+ This file is released under the terms of the MIT License. It is based on the
4
+ work of James Edward Anhalt III, with the original license listed below.
5
+
6
+ MIT License
7
+
8
+ Copyright (c) 2024,2025 Enrico Thierbach - https://github.com/radiospiel
9
+ Copyright (c) 2022 James Edward Anhalt III - https://github.com/jeaiii/itoa
10
+
11
+ Permission is hereby granted, free of charge, to any person obtaining a copy
12
+ of this software and associated documentation files (the "Software"), to deal
13
+ in the Software without restriction, including without limitation the rights
14
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15
+ copies of the Software, and to permit persons to whom the Software is
16
+ furnished to do so, subject to the following conditions:
17
+
18
+ The above copyright notice and this permission notice shall be included in all
19
+ copies or substantial portions of the Software.
20
+
21
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27
+ SOFTWARE.
28
+ */
29
+
30
+ #ifndef JEAIII_TO_TEXT_H_
31
+ #define JEAIII_TO_TEXT_H_
32
+
33
+ #include <stdint.h>
34
+
35
+ typedef uint_fast32_t u32_t;
36
+ typedef uint_fast64_t u64_t;
37
+
38
+ #define u32(x) ((u32_t)(x))
39
+ #define u64(x) ((u64_t)(x))
40
+
41
+ struct digit_pair
42
+ {
43
+ char dd[2];
44
+ };
45
+
46
+ static const struct digit_pair *digits_dd = (struct digit_pair *)(
47
+ "00" "01" "02" "03" "04" "05" "06" "07" "08" "09"
48
+ "10" "11" "12" "13" "14" "15" "16" "17" "18" "19"
49
+ "20" "21" "22" "23" "24" "25" "26" "27" "28" "29"
50
+ "30" "31" "32" "33" "34" "35" "36" "37" "38" "39"
51
+ "40" "41" "42" "43" "44" "45" "46" "47" "48" "49"
52
+ "50" "51" "52" "53" "54" "55" "56" "57" "58" "59"
53
+ "60" "61" "62" "63" "64" "65" "66" "67" "68" "69"
54
+ "70" "71" "72" "73" "74" "75" "76" "77" "78" "79"
55
+ "80" "81" "82" "83" "84" "85" "86" "87" "88" "89"
56
+ "90" "91" "92" "93" "94" "95" "96" "97" "98" "99"
57
+ );
58
+
59
+ static const struct digit_pair *digits_fd = (struct digit_pair *)(
60
+ "0_" "1_" "2_" "3_" "4_" "5_" "6_" "7_" "8_" "9_"
61
+ "10" "11" "12" "13" "14" "15" "16" "17" "18" "19"
62
+ "20" "21" "22" "23" "24" "25" "26" "27" "28" "29"
63
+ "30" "31" "32" "33" "34" "35" "36" "37" "38" "39"
64
+ "40" "41" "42" "43" "44" "45" "46" "47" "48" "49"
65
+ "50" "51" "52" "53" "54" "55" "56" "57" "58" "59"
66
+ "60" "61" "62" "63" "64" "65" "66" "67" "68" "69"
67
+ "70" "71" "72" "73" "74" "75" "76" "77" "78" "79"
68
+ "80" "81" "82" "83" "84" "85" "86" "87" "88" "89"
69
+ "90" "91" "92" "93" "94" "95" "96" "97" "98" "99"
70
+ );
71
+
72
+ static const u64_t mask24 = (u64(1) << 24) - 1;
73
+ static const u64_t mask32 = (u64(1) << 32) - 1;
74
+ static const u64_t mask57 = (u64(1) << 57) - 1;
75
+
76
+ #define COPY(buffer, digits) memcpy(buffer, &(digits), sizeof(struct digit_pair))
77
+
78
+ static char *
79
+ jeaiii_ultoa(char *b, u64_t n)
80
+ {
81
+ if (n < u32(1e2)) {
82
+ COPY(b, digits_fd[n]);
83
+ return n < 10 ? b + 1 : b + 2;
84
+ }
85
+
86
+ if (n < u32(1e6)) {
87
+ if (n < u32(1e4)) {
88
+ u32_t f0 = u32((10 * (1 << 24) / 1e3 + 1) * n);
89
+ COPY(b, digits_fd[f0 >> 24]);
90
+
91
+ b -= n < u32(1e3);
92
+ u32_t f2 = (f0 & mask24) * 100;
93
+ COPY(b + 2, digits_dd[f2 >> 24]);
94
+
95
+ return b + 4;
96
+ }
97
+
98
+ u64_t f0 = u64(10 * (1ull << 32ull)/ 1e5 + 1) * n;
99
+ COPY(b, digits_fd[f0 >> 32]);
100
+
101
+ b -= n < u32(1e5);
102
+ u64_t f2 = (f0 & mask32) * 100;
103
+ COPY(b + 2, digits_dd[f2 >> 32]);
104
+
105
+ u64_t f4 = (f2 & mask32) * 100;
106
+ COPY(b + 4, digits_dd[f4 >> 32]);
107
+ return b + 6;
108
+ }
109
+
110
+ if (n < u64(1ull << 32ull)) {
111
+ if (n < u32(1e8)) {
112
+ u64_t f0 = u64(10 * (1ull << 48ull) / 1e7 + 1) * n >> 16;
113
+ COPY(b, digits_fd[f0 >> 32]);
114
+
115
+ b -= n < u32(1e7);
116
+ u64_t f2 = (f0 & mask32) * 100;
117
+ COPY(b + 2, digits_dd[f2 >> 32]);
118
+
119
+ u64_t f4 = (f2 & mask32) * 100;
120
+ COPY(b + 4, digits_dd[f4 >> 32]);
121
+
122
+ u64_t f6 = (f4 & mask32) * 100;
123
+ COPY(b + 6, digits_dd[f6 >> 32]);
124
+
125
+ return b + 8;
126
+ }
127
+
128
+ u64_t f0 = u64(10 * (1ull << 57ull) / 1e9 + 1) * n;
129
+ COPY(b, digits_fd[f0 >> 57]);
130
+
131
+ b -= n < u32(1e9);
132
+ u64_t f2 = (f0 & mask57) * 100;
133
+ COPY(b + 2, digits_dd[f2 >> 57]);
134
+
135
+ u64_t f4 = (f2 & mask57) * 100;
136
+ COPY(b + 4, digits_dd[f4 >> 57]);
137
+
138
+ u64_t f6 = (f4 & mask57) * 100;
139
+ COPY(b + 6, digits_dd[f6 >> 57]);
140
+
141
+ u64_t f8 = (f6 & mask57) * 100;
142
+ COPY(b + 8, digits_dd[f8 >> 57]);
143
+
144
+ return b + 10;
145
+ }
146
+
147
+ // if we get here U must be u64 but some compilers don't know that, so reassign n to a u64 to avoid warnings
148
+ u32_t z = n % u32(1e8);
149
+ u64_t u = n / u32(1e8);
150
+
151
+ if (u < u32(1e2)) {
152
+ // u can't be 1 digit (if u < 10 it would have been handled above as a 9 digit 32bit number)
153
+ COPY(b, digits_dd[u]);
154
+ b += 2;
155
+ }
156
+ else if (u < u32(1e6)) {
157
+ if (u < u32(1e4)) {
158
+ u32_t f0 = u32((10 * (1 << 24) / 1e3 + 1) * u);
159
+ COPY(b, digits_fd[f0 >> 24]);
160
+
161
+ b -= u < u32(1e3);
162
+ u32_t f2 = (f0 & mask24) * 100;
163
+ COPY(b + 2, digits_dd[f2 >> 24]);
164
+ b += 4;
165
+ }
166
+ else {
167
+ u64_t f0 = u64(10 * (1ull << 32ull) / 1e5 + 1) * u;
168
+ COPY(b, digits_fd[f0 >> 32]);
169
+
170
+ b -= u < u32(1e5);
171
+ u64_t f2 = (f0 & mask32) * 100;
172
+ COPY(b + 2, digits_dd[f2 >> 32]);
173
+
174
+ u64_t f4 = (f2 & mask32) * 100;
175
+ COPY(b + 4, digits_dd[f4 >> 32]);
176
+ b += 6;
177
+ }
178
+ }
179
+ else if (u < u32(1e8)) {
180
+ u64_t f0 = u64(10 * (1ull << 48ull) / 1e7 + 1) * u >> 16;
181
+ COPY(b, digits_fd[f0 >> 32]);
182
+
183
+ b -= u < u32(1e7);
184
+ u64_t f2 = (f0 & mask32) * 100;
185
+ COPY(b + 2, digits_dd[f2 >> 32]);
186
+
187
+ u64_t f4 = (f2 & mask32) * 100;
188
+ COPY(b + 4, digits_dd[f4 >> 32]);
189
+
190
+ u64_t f6 = (f4 & mask32) * 100;
191
+ COPY(b + 6, digits_dd[f6 >> 32]);
192
+
193
+ b += 8;
194
+ }
195
+ else if (u < u64(1ull << 32ull)) {
196
+ u64_t f0 = u64(10 * (1ull << 57ull) / 1e9 + 1) * u;
197
+ COPY(b, digits_fd[f0 >> 57]);
198
+
199
+ b -= u < u32(1e9);
200
+ u64_t f2 = (f0 & mask57) * 100;
201
+ COPY(b + 2, digits_dd[f2 >> 57]);
202
+
203
+ u64_t f4 = (f2 & mask57) * 100;
204
+ COPY(b + 4, digits_dd[f4 >> 57]);
205
+
206
+ u64_t f6 = (f4 & mask57) * 100;
207
+ COPY(b + 6, digits_dd[f6 >> 57]);
208
+
209
+ u64_t f8 = (f6 & mask57) * 100;
210
+ COPY(b + 8, digits_dd[f8 >> 57]);
211
+ b += 10;
212
+ }
213
+ else {
214
+ u32_t y = u % u32(1e8);
215
+ u /= u32(1e8);
216
+
217
+ // u is 2, 3, or 4 digits (if u < 10 it would have been handled above)
218
+ if (u < u32(1e2)) {
219
+ COPY(b, digits_dd[u]);
220
+ b += 2;
221
+ }
222
+ else {
223
+ u32_t f0 = u32((10 * (1 << 24) / 1e3 + 1) * u);
224
+ COPY(b, digits_fd[f0 >> 24]);
225
+
226
+ b -= u < u32(1e3);
227
+ u32_t f2 = (f0 & mask24) * 100;
228
+ COPY(b + 2, digits_dd[f2 >> 24]);
229
+
230
+ b += 4;
231
+ }
232
+ // do 8 digits
233
+ u64_t f0 = (u64((1ull << 48ull) / 1e6 + 1) * y >> 16) + 1;
234
+ COPY(b, digits_dd[f0 >> 32]);
235
+
236
+ u64_t f2 = (f0 & mask32) * 100;
237
+ COPY(b + 2, digits_dd[f2 >> 32]);
238
+
239
+ u64_t f4 = (f2 & mask32) * 100;
240
+ COPY(b + 4, digits_dd[f4 >> 32]);
241
+
242
+ u64_t f6 = (f4 & mask32) * 100;
243
+ COPY(b + 6, digits_dd[f6 >> 32]);
244
+ b += 8;
245
+ }
246
+
247
+ // do 8 digits
248
+ u64_t f0 = (u64((1ull << 48ull) / 1e6 + 1) * z >> 16) + 1;
249
+ COPY(b, digits_dd[f0 >> 32]);
250
+
251
+ u64_t f2 = (f0 & mask32) * 100;
252
+ COPY(b + 2, digits_dd[f2 >> 32]);
253
+
254
+ u64_t f4 = (f2 & mask32) * 100;
255
+ COPY(b + 4, digits_dd[f4 >> 32]);
256
+
257
+ u64_t f6 = (f4 & mask32) * 100;
258
+ COPY(b + 6, digits_dd[f6 >> 32]);
259
+
260
+ return b + 8;
261
+ }
262
+
263
+ #undef u32
264
+ #undef u64
265
+ #undef COPY
266
+
267
+ #endif // JEAIII_TO_TEXT_H_
data/json.gemspec ADDED
@@ -0,0 +1,62 @@
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'
9
+
10
+ s.name = "ed-precompiled_json"
11
+ s.version = version
12
+
13
+ s.summary = "JSON Implementation for Ruby"
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
+
35
+ s.licenses = ["Ruby"]
36
+
37
+ s.extra_rdoc_files = ["README.md"]
38
+ s.rdoc_options = ["--title", "JSON implementation for Ruby", "--main", "README.md"]
39
+
40
+ s.files = [
41
+ "CHANGES.md",
42
+ "COPYING",
43
+ "BSDL",
44
+ "LEGAL",
45
+ "README.md",
46
+ "json.gemspec",
47
+ ] + Dir.glob("lib/**/*.rb", base: File.expand_path("..", __FILE__))
48
+
49
+ if java_ext
50
+ s.platform = 'java'
51
+ s.files += Dir["lib/json/ext/**/*.jar"]
52
+ else
53
+ s.extensions = Dir["ext/json/**/extconf.rb"]
54
+ s.files += Dir["ext/json/**/*.{c,h,rb}"]
55
+ end
56
+ end
57
+
58
+ if RUBY_ENGINE == 'jruby' && $0 == __FILE__
59
+ Gem::Builder.new(spec).build
60
+ else
61
+ spec
62
+ end
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
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.force_encoding(Encoding::UTF_8),
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: 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
@@ -0,0 +1,13 @@
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'
@@ -0,0 +1,54 @@
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
@@ -0,0 +1,67 @@
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
+
@@ -0,0 +1,49 @@
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
@@ -0,0 +1,54 @@
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)