rbs 2.0.0 → 2.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/ruby.yml +5 -0
- data/CHANGELOG.md +23 -0
- data/README.md +6 -1
- data/core/array.rbs +2866 -1086
- data/core/basic_object.rbs +150 -30
- data/core/binding.rbs +33 -0
- data/core/builtin.rbs +4 -4
- data/core/class.rbs +43 -5
- data/core/comparable.rbs +57 -0
- data/core/complex.rbs +170 -4
- data/core/constants.rbs +51 -0
- data/core/deprecated.rbs +7 -0
- data/core/dir.rbs +305 -20
- data/core/encoding.rbs +472 -77
- data/core/enumerable.rbs +2173 -234
- data/core/enumerator.rbs +448 -182
- data/core/env.rbs +448 -1
- data/core/errno.rbs +1 -10
- data/core/errors.rbs +152 -2
- data/core/exception.rbs +201 -127
- data/core/false_class.rbs +27 -0
- data/core/fiber.rbs +118 -37
- data/core/fiber_error.rbs +8 -9
- data/core/file.rbs +1059 -139
- data/core/file_test.rbs +287 -32
- data/core/float.rbs +776 -300
- data/core/gc.rbs +185 -34
- data/core/global_variables.rbs +5 -1
- data/core/hash.rbs +1582 -649
- data/core/integer.rbs +974 -204
- data/core/io/buffer.rbs +710 -0
- data/core/io/wait.rbs +29 -8
- data/core/io.rbs +2438 -417
- data/core/kernel.rbs +2315 -316
- data/core/marshal.rbs +37 -2
- data/core/match_data.rbs +123 -6
- data/core/math.rbs +126 -6
- data/core/method.rbs +226 -102
- data/core/module.rbs +421 -45
- data/core/nil_class.rbs +64 -0
- data/core/numeric.rbs +620 -142
- data/core/object.rbs +453 -81
- data/core/object_space.rbs +92 -2
- data/core/proc.rbs +482 -285
- data/core/process.rbs +443 -34
- data/core/ractor.rbs +232 -9
- data/core/random.rbs +151 -52
- data/core/range.rbs +885 -160
- data/core/rational.rbs +122 -6
- data/core/rb_config.rbs +14 -4
- data/core/refinement.rbs +44 -0
- data/core/regexp.rbs +156 -14
- data/core/ruby_vm.rbs +42 -3
- data/core/signal.rbs +78 -39
- data/core/string.rbs +2123 -567
- data/core/string_io.rbs +204 -0
- data/core/struct.rbs +283 -28
- data/core/symbol.rbs +304 -30
- data/core/thread.rbs +1288 -688
- data/core/thread_group.rbs +66 -10
- data/core/time.rbs +643 -217
- data/core/trace_point.rbs +100 -12
- data/core/true_class.rbs +24 -0
- data/core/unbound_method.rbs +73 -7
- data/core/warning.rbs +37 -12
- data/docs/CONTRIBUTING.md +40 -34
- data/docs/stdlib.md +3 -102
- data/lib/rbs/annotate/annotations.rb +197 -0
- data/lib/rbs/annotate/formatter.rb +80 -0
- data/lib/rbs/annotate/rdoc_annotator.rb +398 -0
- data/lib/rbs/annotate/rdoc_source.rb +120 -0
- data/lib/rbs/annotate.rb +6 -0
- data/lib/rbs/cli.rb +45 -1
- data/lib/rbs/definition_builder.rb +5 -1
- data/lib/rbs/location_aux.rb +12 -0
- data/lib/rbs/prototype/rb.rb +12 -0
- data/lib/rbs/version.rb +1 -1
- data/sig/annotate/annotations.rbs +102 -0
- data/sig/annotate/formatter.rbs +24 -0
- data/sig/annotate/rdoc_annotater.rbs +82 -0
- data/sig/annotate/rdoc_source.rbs +30 -0
- data/sig/cli.rbs +2 -0
- data/sig/collection/{collections.rbs → sources.rbs} +0 -0
- data/sig/location.rbs +6 -0
- data/sig/method_types.rbs +5 -1
- data/sig/polyfill.rbs +78 -0
- data/stdlib/abbrev/0/abbrev.rbs +6 -0
- data/stdlib/abbrev/0/array.rbs +26 -0
- data/stdlib/base64/0/base64.rbs +31 -0
- data/stdlib/benchmark/0/benchmark.rbs +74 -3
- data/stdlib/bigdecimal/0/big_decimal.rbs +614 -165
- data/stdlib/bigdecimal-math/0/big_math.rbs +41 -64
- data/stdlib/cgi/0/core.rbs +59 -0
- data/stdlib/coverage/0/coverage.rbs +164 -2
- data/stdlib/csv/0/csv.rbs +2862 -398
- data/stdlib/date/0/date.rbs +483 -25
- data/stdlib/date/0/date_time.rbs +187 -12
- data/stdlib/dbm/0/dbm.rbs +152 -17
- data/stdlib/digest/0/digest.rbs +146 -0
- data/stdlib/erb/0/erb.rbs +65 -245
- data/stdlib/fiber/0/fiber.rbs +73 -91
- data/stdlib/fileutils/0/fileutils.rbs +301 -1
- data/stdlib/find/0/find.rbs +9 -0
- data/stdlib/forwardable/0/forwardable.rbs +65 -1
- data/stdlib/io-console/0/io-console.rbs +227 -15
- data/stdlib/ipaddr/0/ipaddr.rbs +161 -0
- data/stdlib/json/0/json.rbs +1146 -144
- data/stdlib/logger/0/formatter.rbs +24 -0
- data/stdlib/logger/0/log_device.rbs +64 -0
- data/stdlib/logger/0/logger.rbs +165 -13
- data/stdlib/logger/0/period.rbs +10 -0
- data/stdlib/logger/0/severity.rbs +26 -0
- data/stdlib/monitor/0/monitor.rbs +163 -0
- data/stdlib/mutex_m/0/mutex_m.rbs +35 -6
- data/stdlib/net-http/0/net-http.rbs +1492 -683
- data/stdlib/nkf/0/nkf.rbs +372 -0
- data/stdlib/objspace/0/objspace.rbs +149 -90
- data/stdlib/openssl/0/openssl.rbs +8108 -71
- data/stdlib/optparse/0/optparse.rbs +487 -19
- data/stdlib/pathname/0/pathname.rbs +425 -124
- data/stdlib/prettyprint/0/prettyprint.rbs +120 -99
- data/stdlib/prime/0/integer-extension.rbs +20 -2
- data/stdlib/prime/0/prime.rbs +88 -21
- data/stdlib/pstore/0/pstore.rbs +102 -0
- data/stdlib/pty/0/pty.rbs +64 -14
- data/stdlib/resolv/0/resolv.rbs +420 -31
- data/stdlib/rubygems/0/basic_specification.rbs +4 -1
- data/stdlib/rubygems/0/config_file.rbs +33 -1
- data/stdlib/rubygems/0/dependency_installer.rbs +4 -3
- data/stdlib/rubygems/0/installer.rbs +13 -1
- data/stdlib/rubygems/0/path_support.rbs +4 -1
- data/stdlib/rubygems/0/platform.rbs +5 -1
- data/stdlib/rubygems/0/request_set.rbs +44 -2
- data/stdlib/rubygems/0/requirement.rbs +65 -2
- data/stdlib/rubygems/0/rubygems.rbs +407 -0
- data/stdlib/rubygems/0/source_list.rbs +13 -0
- data/stdlib/rubygems/0/specification.rbs +21 -1
- data/stdlib/rubygems/0/stream_ui.rbs +3 -1
- data/stdlib/rubygems/0/uninstaller.rbs +8 -1
- data/stdlib/rubygems/0/version.rbs +60 -157
- data/stdlib/securerandom/0/securerandom.rbs +44 -0
- data/stdlib/set/0/set.rbs +420 -106
- data/stdlib/shellwords/0/shellwords.rbs +55 -77
- data/stdlib/singleton/0/singleton.rbs +20 -0
- data/stdlib/socket/0/addrinfo.rbs +210 -9
- data/stdlib/socket/0/basic_socket.rbs +103 -11
- data/stdlib/socket/0/ip_socket.rbs +31 -9
- data/stdlib/socket/0/socket.rbs +586 -38
- data/stdlib/socket/0/tcp_server.rbs +22 -2
- data/stdlib/socket/0/tcp_socket.rbs +12 -1
- data/stdlib/socket/0/udp_socket.rbs +25 -2
- data/stdlib/socket/0/unix_server.rbs +22 -2
- data/stdlib/socket/0/unix_socket.rbs +45 -5
- data/stdlib/strscan/0/string_scanner.rbs +210 -9
- data/stdlib/tempfile/0/tempfile.rbs +58 -10
- data/stdlib/time/0/time.rbs +208 -116
- data/stdlib/timeout/0/timeout.rbs +10 -0
- data/stdlib/tmpdir/0/tmpdir.rbs +13 -4
- data/stdlib/tsort/0/cyclic.rbs +1 -0
- data/stdlib/tsort/0/interfaces.rbs +1 -0
- data/stdlib/tsort/0/tsort.rbs +42 -0
- data/stdlib/uri/0/common.rbs +57 -8
- data/stdlib/uri/0/file.rbs +55 -109
- data/stdlib/uri/0/ftp.rbs +6 -3
- data/stdlib/uri/0/generic.rbs +556 -327
- data/stdlib/uri/0/http.rbs +26 -115
- data/stdlib/uri/0/https.rbs +8 -102
- data/stdlib/uri/0/ldap.rbs +143 -137
- data/stdlib/uri/0/ldaps.rbs +8 -102
- data/stdlib/uri/0/mailto.rbs +3 -0
- data/stdlib/uri/0/rfc2396_parser.rbs +66 -26
- data/stdlib/uri/0/ws.rbs +6 -3
- data/stdlib/uri/0/wss.rbs +5 -3
- data/stdlib/yaml/0/dbm.rbs +151 -87
- data/stdlib/yaml/0/store.rbs +6 -0
- data/stdlib/zlib/0/zlib.rbs +90 -31
- metadata +17 -5
- data/lib/rbs/location.rb +0 -221
data/stdlib/json/0/json.rbs
CHANGED
@@ -20,15 +20,44 @@ end
|
|
20
20
|
|
21
21
|
type json_options = Hash[Symbol, untyped]
|
22
22
|
|
23
|
+
# <!-- rdoc-file=ext/json/lib/json/common.rb -->
|
24
|
+
# The base exception for JSON errors.
|
25
|
+
#
|
26
|
+
class JSON::JSONError < StandardError
|
27
|
+
end
|
28
|
+
|
29
|
+
# <!-- rdoc-file=ext/json/lib/json/common.rb -->
|
30
|
+
# This exception is raised if a generator or unparser error occurs.
|
31
|
+
#
|
32
|
+
class JSON::GeneratorError < JSON::JSONError
|
33
|
+
end
|
34
|
+
|
35
|
+
class JSON::UnparserError < JSON::GeneratorError
|
36
|
+
end
|
37
|
+
|
38
|
+
# <!-- rdoc-file=ext/json/lib/json/common.rb -->
|
39
|
+
# This exception is raised if a parser error occurs.
|
40
|
+
#
|
41
|
+
class JSON::ParserError < JSON::JSONError
|
42
|
+
end
|
43
|
+
|
44
|
+
# <!-- rdoc-file=ext/json/lib/json/common.rb -->
|
45
|
+
# This exception is raised if the nesting of parsed data structures is too deep.
|
46
|
+
#
|
47
|
+
class JSON::NestingError < JSON::ParserError
|
48
|
+
end
|
49
|
+
|
23
50
|
class JSON::State
|
24
51
|
end
|
25
52
|
|
53
|
+
# <!-- rdoc-file=ext/json/lib/json/ext.rb -->
|
26
54
|
# This module holds all the modules/classes that implement JSON's functionality
|
27
55
|
# as C extensions.
|
28
56
|
#
|
29
57
|
module JSON::Ext
|
30
58
|
end
|
31
59
|
|
60
|
+
# <!-- rdoc-file=ext/json/generator/generator.c -->
|
32
61
|
# This is the JSON generator implemented as a C extension. It can be configured
|
33
62
|
# to be used by setting
|
34
63
|
#
|
@@ -42,6 +71,14 @@ end
|
|
42
71
|
class JSON::Ext::Generator::State
|
43
72
|
end
|
44
73
|
|
74
|
+
# <!-- rdoc-file=ext/json/parser/parser.c -->
|
75
|
+
# This is the JSON parser implemented as a C extension. It can be configured to
|
76
|
+
# be used by setting
|
77
|
+
#
|
78
|
+
# JSON.parser = JSON::Ext::Parser
|
79
|
+
#
|
80
|
+
# with the method parser= in JSON.
|
81
|
+
#
|
45
82
|
class JSON::Ext::Parser
|
46
83
|
end
|
47
84
|
|
@@ -61,238 +98,1005 @@ type json_generator = singleton(::JSON::Ext::Generator) | singleton(::JSON::Pure
|
|
61
98
|
type json_parser = singleton(::JSON::Ext::Parser) | singleton(::JSON::Pure::Parser)
|
62
99
|
type json_state = singleton(JSON::Ext::Generator::State) | singleton(JSON::Pure::Generator::State)
|
63
100
|
|
101
|
+
# <!-- rdoc-file=ext/json/lib/json.rb -->
|
64
102
|
# # JavaScript Object Notation (JSON)
|
65
103
|
#
|
66
|
-
# JSON is a lightweight data-interchange format.
|
67
|
-
# read and write. Plus, equally simple for machines to generate or parse. JSON
|
68
|
-
# is completely language agnostic, making it the ideal interchange format.
|
104
|
+
# JSON is a lightweight data-interchange format.
|
69
105
|
#
|
70
|
-
#
|
71
|
-
#
|
72
|
-
#
|
106
|
+
# A JSON value is one of the following:
|
107
|
+
# * Double-quoted text: `"foo"`.
|
108
|
+
# * Number: `1`, `1.0`, `2.0e2`.
|
109
|
+
# * Boolean: `true`, `false`.
|
110
|
+
# * Null: `null`.
|
111
|
+
# * Array: an ordered list of values, enclosed by square brackets:
|
112
|
+
# ["foo", 1, 1.0, 2.0e2, true, false, null]
|
73
113
|
#
|
74
|
-
#
|
114
|
+
# * Object: a collection of name/value pairs, enclosed by curly braces; each
|
115
|
+
# name is double-quoted text; the values may be any JSON values:
|
116
|
+
# {"a": "foo", "b": 1, "c": 1.0, "d": 2.0e2, "e": true, "f": false, "g": null}
|
75
117
|
#
|
76
|
-
# ## Parsing JSON
|
77
118
|
#
|
78
|
-
#
|
79
|
-
#
|
119
|
+
# A JSON array or object may contain nested arrays, objects, and scalars to any
|
120
|
+
# depth:
|
121
|
+
# {"foo": {"bar": 1, "baz": 2}, "bat": [0, 1, 2]}
|
122
|
+
# [{"foo": 0, "bar": 1}, ["baz", 2]]
|
80
123
|
#
|
124
|
+
# ## Using Module JSON
|
125
|
+
#
|
126
|
+
# To make module JSON available in your code, begin with:
|
81
127
|
# require 'json'
|
82
128
|
#
|
83
|
-
#
|
84
|
-
# puts my_hash["hello"] => "goodbye"
|
129
|
+
# All examples here assume that this has been done.
|
85
130
|
#
|
86
|
-
#
|
87
|
-
# argument to be a string and can't convert objects like a hash or array.
|
131
|
+
# ### Parsing JSON
|
88
132
|
#
|
89
|
-
#
|
133
|
+
# You can parse a String containing JSON data using either of two methods:
|
134
|
+
# * `JSON.parse(source, opts)`
|
135
|
+
# * `JSON.parse!(source, opts)`
|
90
136
|
#
|
91
|
-
# ## Generating JSON
|
92
137
|
#
|
93
|
-
#
|
138
|
+
# where
|
139
|
+
# * `source` is a Ruby object.
|
140
|
+
# * `opts` is a Hash object containing options that control both input allowed
|
141
|
+
# and output formatting.
|
94
142
|
#
|
95
|
-
# require 'json'
|
96
143
|
#
|
97
|
-
#
|
98
|
-
#
|
144
|
+
# The difference between the two methods is that JSON.parse! omits some checks
|
145
|
+
# and may not be safe for some `source` data; use it only for data from trusted
|
146
|
+
# sources. Use the safer method JSON.parse for less trusted sources.
|
99
147
|
#
|
100
|
-
#
|
148
|
+
# #### Parsing JSON Arrays
|
101
149
|
#
|
102
|
-
#
|
103
|
-
#
|
150
|
+
# When `source` is a JSON array, JSON.parse by default returns a Ruby Array:
|
151
|
+
# json = '["foo", 1, 1.0, 2.0e2, true, false, null]'
|
152
|
+
# ruby = JSON.parse(json)
|
153
|
+
# ruby # => ["foo", 1, 1.0, 200.0, true, false, nil]
|
154
|
+
# ruby.class # => Array
|
155
|
+
#
|
156
|
+
# The JSON array may contain nested arrays, objects, and scalars to any depth:
|
157
|
+
# json = '[{"foo": 0, "bar": 1}, ["baz", 2]]'
|
158
|
+
# JSON.parse(json) # => [{"foo"=>0, "bar"=>1}, ["baz", 2]]
|
159
|
+
#
|
160
|
+
# #### Parsing JSON Objects
|
161
|
+
#
|
162
|
+
# When the source is a JSON object, JSON.parse by default returns a Ruby Hash:
|
163
|
+
# json = '{"a": "foo", "b": 1, "c": 1.0, "d": 2.0e2, "e": true, "f": false, "g": null}'
|
164
|
+
# ruby = JSON.parse(json)
|
165
|
+
# ruby # => {"a"=>"foo", "b"=>1, "c"=>1.0, "d"=>200.0, "e"=>true, "f"=>false, "g"=>nil}
|
166
|
+
# ruby.class # => Hash
|
167
|
+
#
|
168
|
+
# The JSON object may contain nested arrays, objects, and scalars to any depth:
|
169
|
+
# json = '{"foo": {"bar": 1, "baz": 2}, "bat": [0, 1, 2]}'
|
170
|
+
# JSON.parse(json) # => {"foo"=>{"bar"=>1, "baz"=>2}, "bat"=>[0, 1, 2]}
|
171
|
+
#
|
172
|
+
# #### Parsing JSON Scalars
|
173
|
+
#
|
174
|
+
# When the source is a JSON scalar (not an array or object), JSON.parse returns
|
175
|
+
# a Ruby scalar.
|
176
|
+
#
|
177
|
+
# String:
|
178
|
+
# ruby = JSON.parse('"foo"')
|
179
|
+
# ruby # => 'foo'
|
180
|
+
# ruby.class # => String
|
181
|
+
#
|
182
|
+
# Integer:
|
183
|
+
# ruby = JSON.parse('1')
|
184
|
+
# ruby # => 1
|
185
|
+
# ruby.class # => Integer
|
186
|
+
#
|
187
|
+
# Float:
|
188
|
+
# ruby = JSON.parse('1.0')
|
189
|
+
# ruby # => 1.0
|
190
|
+
# ruby.class # => Float
|
191
|
+
# ruby = JSON.parse('2.0e2')
|
192
|
+
# ruby # => 200
|
193
|
+
# ruby.class # => Float
|
194
|
+
#
|
195
|
+
# Boolean:
|
196
|
+
# ruby = JSON.parse('true')
|
197
|
+
# ruby # => true
|
198
|
+
# ruby.class # => TrueClass
|
199
|
+
# ruby = JSON.parse('false')
|
200
|
+
# ruby # => false
|
201
|
+
# ruby.class # => FalseClass
|
202
|
+
#
|
203
|
+
# Null:
|
204
|
+
# ruby = JSON.parse('null')
|
205
|
+
# ruby # => nil
|
206
|
+
# ruby.class # => NilClass
|
207
|
+
#
|
208
|
+
# #### Parsing Options
|
209
|
+
#
|
210
|
+
# ###### Input Options
|
211
|
+
#
|
212
|
+
# Option `max_nesting` (Integer) specifies the maximum nesting depth allowed;
|
213
|
+
# defaults to `100`; specify `false` to disable depth checking.
|
214
|
+
#
|
215
|
+
# With the default, `false`:
|
216
|
+
# source = '[0, [1, [2, [3]]]]'
|
217
|
+
# ruby = JSON.parse(source)
|
218
|
+
# ruby # => [0, [1, [2, [3]]]]
|
219
|
+
#
|
220
|
+
# Too deep:
|
221
|
+
# # Raises JSON::NestingError (nesting of 2 is too deep):
|
222
|
+
# JSON.parse(source, {max_nesting: 1})
|
223
|
+
#
|
224
|
+
# Bad value:
|
225
|
+
# # Raises TypeError (wrong argument type Symbol (expected Fixnum)):
|
226
|
+
# JSON.parse(source, {max_nesting: :foo})
|
227
|
+
#
|
228
|
+
# ---
|
229
|
+
#
|
230
|
+
# Option `allow_nan` (boolean) specifies whether to allow NaN, Infinity, and
|
231
|
+
# MinusInfinity in `source`; defaults to `false`.
|
232
|
+
#
|
233
|
+
# With the default, `false`:
|
234
|
+
# # Raises JSON::ParserError (225: unexpected token at '[NaN]'):
|
235
|
+
# JSON.parse('[NaN]')
|
236
|
+
# # Raises JSON::ParserError (232: unexpected token at '[Infinity]'):
|
237
|
+
# JSON.parse('[Infinity]')
|
238
|
+
# # Raises JSON::ParserError (248: unexpected token at '[-Infinity]'):
|
239
|
+
# JSON.parse('[-Infinity]')
|
240
|
+
#
|
241
|
+
# Allow:
|
242
|
+
# source = '[NaN, Infinity, -Infinity]'
|
243
|
+
# ruby = JSON.parse(source, {allow_nan: true})
|
244
|
+
# ruby # => [NaN, Infinity, -Infinity]
|
245
|
+
#
|
246
|
+
# ###### Output Options
|
247
|
+
#
|
248
|
+
# Option `symbolize_names` (boolean) specifies whether returned Hash keys should
|
249
|
+
# be Symbols; defaults to `false` (use Strings).
|
250
|
+
#
|
251
|
+
# With the default, `false`:
|
252
|
+
# source = '{"a": "foo", "b": 1.0, "c": true, "d": false, "e": null}'
|
253
|
+
# ruby = JSON.parse(source)
|
254
|
+
# ruby # => {"a"=>"foo", "b"=>1.0, "c"=>true, "d"=>false, "e"=>nil}
|
255
|
+
#
|
256
|
+
# Use Symbols:
|
257
|
+
# ruby = JSON.parse(source, {symbolize_names: true})
|
258
|
+
# ruby # => {:a=>"foo", :b=>1.0, :c=>true, :d=>false, :e=>nil}
|
259
|
+
#
|
260
|
+
# ---
|
261
|
+
#
|
262
|
+
# Option `object_class` (Class) specifies the Ruby class to be used for each
|
263
|
+
# JSON object; defaults to Hash.
|
264
|
+
#
|
265
|
+
# With the default, Hash:
|
266
|
+
# source = '{"a": "foo", "b": 1.0, "c": true, "d": false, "e": null}'
|
267
|
+
# ruby = JSON.parse(source)
|
268
|
+
# ruby.class # => Hash
|
269
|
+
#
|
270
|
+
# Use class OpenStruct:
|
271
|
+
# ruby = JSON.parse(source, {object_class: OpenStruct})
|
272
|
+
# ruby # => #<OpenStruct a="foo", b=1.0, c=true, d=false, e=nil>
|
273
|
+
#
|
274
|
+
# ---
|
275
|
+
#
|
276
|
+
# Option `array_class` (Class) specifies the Ruby class to be used for each JSON
|
277
|
+
# array; defaults to Array.
|
278
|
+
#
|
279
|
+
# With the default, Array:
|
280
|
+
# source = '["foo", 1.0, true, false, null]'
|
281
|
+
# ruby = JSON.parse(source)
|
282
|
+
# ruby.class # => Array
|
283
|
+
#
|
284
|
+
# Use class Set:
|
285
|
+
# ruby = JSON.parse(source, {array_class: Set})
|
286
|
+
# ruby # => #<Set: {"foo", 1.0, true, false, nil}>
|
287
|
+
#
|
288
|
+
# ---
|
289
|
+
#
|
290
|
+
# Option `create_additions` (boolean) specifies whether to use JSON additions in
|
291
|
+
# parsing. See [\JSON Additions](#module-JSON-label-JSON+Additions).
|
292
|
+
#
|
293
|
+
# ### Generating JSON
|
294
|
+
#
|
295
|
+
# To generate a Ruby String containing JSON data, use method
|
296
|
+
# `JSON.generate(source, opts)`, where
|
297
|
+
# * `source` is a Ruby object.
|
298
|
+
# * `opts` is a Hash object containing options that control both input allowed
|
299
|
+
# and output formatting.
|
300
|
+
#
|
301
|
+
#
|
302
|
+
# #### Generating JSON from Arrays
|
303
|
+
#
|
304
|
+
# When the source is a Ruby Array, JSON.generate returns a String containing a
|
305
|
+
# JSON array:
|
306
|
+
# ruby = [0, 's', :foo]
|
307
|
+
# json = JSON.generate(ruby)
|
308
|
+
# json # => '[0,"s","foo"]'
|
309
|
+
#
|
310
|
+
# The Ruby Array array may contain nested arrays, hashes, and scalars to any
|
311
|
+
# depth:
|
312
|
+
# ruby = [0, [1, 2], {foo: 3, bar: 4}]
|
313
|
+
# json = JSON.generate(ruby)
|
314
|
+
# json # => '[0,[1,2],{"foo":3,"bar":4}]'
|
315
|
+
#
|
316
|
+
# #### Generating JSON from Hashes
|
317
|
+
#
|
318
|
+
# When the source is a Ruby Hash, JSON.generate returns a String containing a
|
319
|
+
# JSON object:
|
320
|
+
# ruby = {foo: 0, bar: 's', baz: :bat}
|
321
|
+
# json = JSON.generate(ruby)
|
322
|
+
# json # => '{"foo":0,"bar":"s","baz":"bat"}'
|
323
|
+
#
|
324
|
+
# The Ruby Hash array may contain nested arrays, hashes, and scalars to any
|
325
|
+
# depth:
|
326
|
+
# ruby = {foo: [0, 1], bar: {baz: 2, bat: 3}, bam: :bad}
|
327
|
+
# json = JSON.generate(ruby)
|
328
|
+
# json # => '{"foo":[0,1],"bar":{"baz":2,"bat":3},"bam":"bad"}'
|
329
|
+
#
|
330
|
+
# #### Generating JSON from Other Objects
|
331
|
+
#
|
332
|
+
# When the source is neither an Array nor a Hash, the generated JSON data
|
333
|
+
# depends on the class of the source.
|
334
|
+
#
|
335
|
+
# When the source is a Ruby Integer or Float, JSON.generate returns a String
|
336
|
+
# containing a JSON number:
|
337
|
+
# JSON.generate(42) # => '42'
|
338
|
+
# JSON.generate(0.42) # => '0.42'
|
339
|
+
#
|
340
|
+
# When the source is a Ruby String, JSON.generate returns a String containing a
|
341
|
+
# JSON string (with double-quotes):
|
342
|
+
# JSON.generate('A string') # => '"A string"'
|
343
|
+
#
|
344
|
+
# When the source is `true`, `false` or `nil`, JSON.generate returns a String
|
345
|
+
# containing the corresponding JSON token:
|
346
|
+
# JSON.generate(true) # => 'true'
|
347
|
+
# JSON.generate(false) # => 'false'
|
348
|
+
# JSON.generate(nil) # => 'null'
|
349
|
+
#
|
350
|
+
# When the source is none of the above, JSON.generate returns a String
|
351
|
+
# containing a JSON string representation of the source:
|
352
|
+
# JSON.generate(:foo) # => '"foo"'
|
353
|
+
# JSON.generate(Complex(0, 0)) # => '"0+0i"'
|
354
|
+
# JSON.generate(Dir.new('.')) # => '"#<Dir>"'
|
355
|
+
#
|
356
|
+
# #### Generating Options
|
357
|
+
#
|
358
|
+
# ###### Input Options
|
359
|
+
#
|
360
|
+
# Option `allow_nan` (boolean) specifies whether `NaN`, `Infinity`, and
|
361
|
+
# `-Infinity` may be generated; defaults to `false`.
|
104
362
|
#
|
105
|
-
#
|
106
|
-
#
|
107
|
-
#
|
363
|
+
# With the default, `false`:
|
364
|
+
# # Raises JSON::GeneratorError (920: NaN not allowed in JSON):
|
365
|
+
# JSON.generate(JSON::NaN)
|
366
|
+
# # Raises JSON::GeneratorError (917: Infinity not allowed in JSON):
|
367
|
+
# JSON.generate(JSON::Infinity)
|
368
|
+
# # Raises JSON::GeneratorError (917: -Infinity not allowed in JSON):
|
369
|
+
# JSON.generate(JSON::MinusInfinity)
|
108
370
|
#
|
371
|
+
# Allow:
|
372
|
+
# ruby = [Float::NaN, Float::Infinity, Float::MinusInfinity]
|
373
|
+
# JSON.generate(ruby, allow_nan: true) # => '[NaN,Infinity,-Infinity]'
|
374
|
+
#
|
375
|
+
# ---
|
376
|
+
#
|
377
|
+
# Option `max_nesting` (Integer) specifies the maximum nesting depth in `obj`;
|
378
|
+
# defaults to `100`.
|
379
|
+
#
|
380
|
+
# With the default, `100`:
|
381
|
+
# obj = [[[[[[0]]]]]]
|
382
|
+
# JSON.generate(obj) # => '[[[[[[0]]]]]]'
|
383
|
+
#
|
384
|
+
# Too deep:
|
385
|
+
# # Raises JSON::NestingError (nesting of 2 is too deep):
|
386
|
+
# JSON.generate(obj, max_nesting: 2)
|
387
|
+
#
|
388
|
+
# ###### Output Options
|
389
|
+
#
|
390
|
+
# The default formatting options generate the most compact JSON data, all on one
|
391
|
+
# line and with no whitespace.
|
392
|
+
#
|
393
|
+
# You can use these formatting options to generate JSON data in a more open
|
394
|
+
# format, using whitespace. See also JSON.pretty_generate.
|
395
|
+
#
|
396
|
+
# * Option `array_nl` (String) specifies a string (usually a newline) to be
|
397
|
+
# inserted after each JSON array; defaults to the empty String, `''`.
|
398
|
+
# * Option `object_nl` (String) specifies a string (usually a newline) to be
|
399
|
+
# inserted after each JSON object; defaults to the empty String, `''`.
|
400
|
+
# * Option `indent` (String) specifies the string (usually spaces) to be used
|
401
|
+
# for indentation; defaults to the empty String, `''`; defaults to the empty
|
402
|
+
# String, `''`; has no effect unless options `array_nl` or `object_nl`
|
403
|
+
# specify newlines.
|
404
|
+
# * Option `space` (String) specifies a string (usually a space) to be
|
405
|
+
# inserted after the colon in each JSON object's pair; defaults to the empty
|
406
|
+
# String, `''`.
|
407
|
+
# * Option `space_before` (String) specifies a string (usually a space) to be
|
408
|
+
# inserted before the colon in each JSON object's pair; defaults to the
|
409
|
+
# empty String, `''`.
|
410
|
+
#
|
411
|
+
#
|
412
|
+
# In this example, `obj` is used first to generate the shortest JSON data (no
|
413
|
+
# whitespace), then again with all formatting options specified:
|
414
|
+
#
|
415
|
+
# obj = {foo: [:bar, :baz], bat: {bam: 0, bad: 1}}
|
416
|
+
# json = JSON.generate(obj)
|
417
|
+
# puts 'Compact:', json
|
418
|
+
# opts = {
|
419
|
+
# array_nl: "\n",
|
420
|
+
# object_nl: "\n",
|
421
|
+
# indent: ' ',
|
422
|
+
# space_before: ' ',
|
423
|
+
# space: ' '
|
424
|
+
# }
|
425
|
+
# puts 'Open:', JSON.generate(obj, opts)
|
426
|
+
#
|
427
|
+
# Output:
|
428
|
+
# Compact:
|
429
|
+
# {"foo":["bar","baz"],"bat":{"bam":0,"bad":1}}
|
430
|
+
# Open:
|
431
|
+
# {
|
432
|
+
# "foo" : [
|
433
|
+
# "bar",
|
434
|
+
# "baz"
|
435
|
+
# ],
|
436
|
+
# "bat" : {
|
437
|
+
# "bam" : 0,
|
438
|
+
# "bad" : 1
|
439
|
+
# }
|
440
|
+
# }
|
441
|
+
#
|
442
|
+
# ## JSON Additions
|
443
|
+
#
|
444
|
+
# When you "round trip" a non-String object from Ruby to JSON and back, you have
|
445
|
+
# a new String, instead of the object you began with:
|
446
|
+
# ruby0 = Range.new(0, 2)
|
447
|
+
# json = JSON.generate(ruby0)
|
448
|
+
# json # => '0..2"'
|
449
|
+
# ruby1 = JSON.parse(json)
|
450
|
+
# ruby1 # => '0..2'
|
451
|
+
# ruby1.class # => String
|
452
|
+
#
|
453
|
+
# You can use JSON *additions* to preserve the original object. The addition is
|
454
|
+
# an extension of a ruby class, so that:
|
455
|
+
# * JSON.generate stores more information in the JSON string.
|
456
|
+
# * JSON.parse, called with option `create_additions`, uses that information
|
457
|
+
# to create a proper Ruby object.
|
458
|
+
#
|
459
|
+
#
|
460
|
+
# This example shows a Range being generated into JSON and parsed back into
|
461
|
+
# Ruby, both without and with the addition for Range:
|
462
|
+
# ruby = Range.new(0, 2)
|
463
|
+
# # This passage does not use the addition for Range.
|
464
|
+
# json0 = JSON.generate(ruby)
|
465
|
+
# ruby0 = JSON.parse(json0)
|
466
|
+
# # This passage uses the addition for Range.
|
467
|
+
# require 'json/add/range'
|
468
|
+
# json1 = JSON.generate(ruby)
|
469
|
+
# ruby1 = JSON.parse(json1, create_additions: true)
|
470
|
+
# # Make a nice display.
|
471
|
+
# display = <<EOT
|
472
|
+
# Generated JSON:
|
473
|
+
# Without addition: #{json0} (#{json0.class})
|
474
|
+
# With addition: #{json1} (#{json1.class})
|
475
|
+
# Parsed JSON:
|
476
|
+
# Without addition: #{ruby0.inspect} (#{ruby0.class})
|
477
|
+
# With addition: #{ruby1.inspect} (#{ruby1.class})
|
478
|
+
# EOT
|
479
|
+
# puts display
|
480
|
+
#
|
481
|
+
# This output shows the different results:
|
482
|
+
# Generated JSON:
|
483
|
+
# Without addition: "0..2" (String)
|
484
|
+
# With addition: {"json_class":"Range","a":[0,2,false]} (String)
|
485
|
+
# Parsed JSON:
|
486
|
+
# Without addition: "0..2" (String)
|
487
|
+
# With addition: 0..2 (Range)
|
488
|
+
#
|
489
|
+
# The JSON module includes additions for certain classes. You can also craft
|
490
|
+
# custom additions. See [Custom \JSON
|
491
|
+
# Additions](#module-JSON-label-Custom+JSON+Additions).
|
492
|
+
#
|
493
|
+
# ### Built-in Additions
|
494
|
+
#
|
495
|
+
# The JSON module includes additions for certain classes. To use an addition,
|
496
|
+
# `require` its source:
|
497
|
+
# * BigDecimal: `require 'json/add/bigdecimal'`
|
498
|
+
# * Complex: `require 'json/add/complex'`
|
499
|
+
# * Date: `require 'json/add/date'`
|
500
|
+
# * DateTime: `require 'json/add/date_time'`
|
501
|
+
# * Exception: `require 'json/add/exception'`
|
502
|
+
# * OpenStruct: `require 'json/add/ostruct'`
|
503
|
+
# * Range: `require 'json/add/range'`
|
504
|
+
# * Rational: `require 'json/add/rational'`
|
505
|
+
# * Regexp: `require 'json/add/regexp'`
|
506
|
+
# * Set: `require 'json/add/set'`
|
507
|
+
# * Struct: `require 'json/add/struct'`
|
508
|
+
# * Symbol: `require 'json/add/symbol'`
|
509
|
+
# * Time: `require 'json/add/time'`
|
510
|
+
#
|
511
|
+
#
|
512
|
+
# To reduce punctuation clutter, the examples below show the generated JSON via
|
513
|
+
# `puts`, rather than the usual `inspect`,
|
514
|
+
#
|
515
|
+
# BigDecimal:
|
516
|
+
# require 'json/add/bigdecimal'
|
517
|
+
# ruby0 = BigDecimal(0) # 0.0
|
518
|
+
# json = JSON.generate(ruby0) # {"json_class":"BigDecimal","b":"27:0.0"}
|
519
|
+
# ruby1 = JSON.parse(json, create_additions: true) # 0.0
|
520
|
+
# ruby1.class # => BigDecimal
|
521
|
+
#
|
522
|
+
# Complex:
|
523
|
+
# require 'json/add/complex'
|
524
|
+
# ruby0 = Complex(1+0i) # 1+0i
|
525
|
+
# json = JSON.generate(ruby0) # {"json_class":"Complex","r":1,"i":0}
|
526
|
+
# ruby1 = JSON.parse(json, create_additions: true) # 1+0i
|
527
|
+
# ruby1.class # Complex
|
528
|
+
#
|
529
|
+
# Date:
|
530
|
+
# require 'json/add/date'
|
531
|
+
# ruby0 = Date.today # 2020-05-02
|
532
|
+
# json = JSON.generate(ruby0) # {"json_class":"Date","y":2020,"m":5,"d":2,"sg":2299161.0}
|
533
|
+
# ruby1 = JSON.parse(json, create_additions: true) # 2020-05-02
|
534
|
+
# ruby1.class # Date
|
535
|
+
#
|
536
|
+
# DateTime:
|
537
|
+
# require 'json/add/date_time'
|
538
|
+
# ruby0 = DateTime.now # 2020-05-02T10:38:13-05:00
|
539
|
+
# json = JSON.generate(ruby0) # {"json_class":"DateTime","y":2020,"m":5,"d":2,"H":10,"M":38,"S":13,"of":"-5/24","sg":2299161.0}
|
540
|
+
# ruby1 = JSON.parse(json, create_additions: true) # 2020-05-02T10:38:13-05:00
|
541
|
+
# ruby1.class # DateTime
|
542
|
+
#
|
543
|
+
# Exception (and its subclasses including RuntimeError):
|
544
|
+
# require 'json/add/exception'
|
545
|
+
# ruby0 = Exception.new('A message') # A message
|
546
|
+
# json = JSON.generate(ruby0) # {"json_class":"Exception","m":"A message","b":null}
|
547
|
+
# ruby1 = JSON.parse(json, create_additions: true) # A message
|
548
|
+
# ruby1.class # Exception
|
549
|
+
# ruby0 = RuntimeError.new('Another message') # Another message
|
550
|
+
# json = JSON.generate(ruby0) # {"json_class":"RuntimeError","m":"Another message","b":null}
|
551
|
+
# ruby1 = JSON.parse(json, create_additions: true) # Another message
|
552
|
+
# ruby1.class # RuntimeError
|
553
|
+
#
|
554
|
+
# OpenStruct:
|
555
|
+
# require 'json/add/ostruct'
|
556
|
+
# ruby0 = OpenStruct.new(name: 'Matz', language: 'Ruby') # #<OpenStruct name="Matz", language="Ruby">
|
557
|
+
# json = JSON.generate(ruby0) # {"json_class":"OpenStruct","t":{"name":"Matz","language":"Ruby"}}
|
558
|
+
# ruby1 = JSON.parse(json, create_additions: true) # #<OpenStruct name="Matz", language="Ruby">
|
559
|
+
# ruby1.class # OpenStruct
|
560
|
+
#
|
561
|
+
# Range:
|
562
|
+
# require 'json/add/range'
|
563
|
+
# ruby0 = Range.new(0, 2) # 0..2
|
564
|
+
# json = JSON.generate(ruby0) # {"json_class":"Range","a":[0,2,false]}
|
565
|
+
# ruby1 = JSON.parse(json, create_additions: true) # 0..2
|
566
|
+
# ruby1.class # Range
|
567
|
+
#
|
568
|
+
# Rational:
|
569
|
+
# require 'json/add/rational'
|
570
|
+
# ruby0 = Rational(1, 3) # 1/3
|
571
|
+
# json = JSON.generate(ruby0) # {"json_class":"Rational","n":1,"d":3}
|
572
|
+
# ruby1 = JSON.parse(json, create_additions: true) # 1/3
|
573
|
+
# ruby1.class # Rational
|
574
|
+
#
|
575
|
+
# Regexp:
|
576
|
+
# require 'json/add/regexp'
|
577
|
+
# ruby0 = Regexp.new('foo') # (?-mix:foo)
|
578
|
+
# json = JSON.generate(ruby0) # {"json_class":"Regexp","o":0,"s":"foo"}
|
579
|
+
# ruby1 = JSON.parse(json, create_additions: true) # (?-mix:foo)
|
580
|
+
# ruby1.class # Regexp
|
581
|
+
#
|
582
|
+
# Set:
|
583
|
+
# require 'json/add/set'
|
584
|
+
# ruby0 = Set.new([0, 1, 2]) # #<Set: {0, 1, 2}>
|
585
|
+
# json = JSON.generate(ruby0) # {"json_class":"Set","a":[0,1,2]}
|
586
|
+
# ruby1 = JSON.parse(json, create_additions: true) # #<Set: {0, 1, 2}>
|
587
|
+
# ruby1.class # Set
|
588
|
+
#
|
589
|
+
# Struct:
|
590
|
+
# require 'json/add/struct'
|
591
|
+
# Customer = Struct.new(:name, :address) # Customer
|
592
|
+
# ruby0 = Customer.new("Dave", "123 Main") # #<struct Customer name="Dave", address="123 Main">
|
593
|
+
# json = JSON.generate(ruby0) # {"json_class":"Customer","v":["Dave","123 Main"]}
|
594
|
+
# ruby1 = JSON.parse(json, create_additions: true) # #<struct Customer name="Dave", address="123 Main">
|
595
|
+
# ruby1.class # Customer
|
596
|
+
#
|
597
|
+
# Symbol:
|
598
|
+
# require 'json/add/symbol'
|
599
|
+
# ruby0 = :foo # foo
|
600
|
+
# json = JSON.generate(ruby0) # {"json_class":"Symbol","s":"foo"}
|
601
|
+
# ruby1 = JSON.parse(json, create_additions: true) # foo
|
602
|
+
# ruby1.class # Symbol
|
603
|
+
#
|
604
|
+
# Time:
|
605
|
+
# require 'json/add/time'
|
606
|
+
# ruby0 = Time.now # 2020-05-02 11:28:26 -0500
|
607
|
+
# json = JSON.generate(ruby0) # {"json_class":"Time","s":1588436906,"n":840560000}
|
608
|
+
# ruby1 = JSON.parse(json, create_additions: true) # 2020-05-02 11:28:26 -0500
|
609
|
+
# ruby1.class # Time
|
610
|
+
#
|
611
|
+
# ### Custom JSON Additions
|
612
|
+
#
|
613
|
+
# In addition to the JSON additions provided, you can craft JSON additions of
|
614
|
+
# your own, either for Ruby built-in classes or for user-defined classes.
|
615
|
+
#
|
616
|
+
# Here's a user-defined class `Foo`:
|
617
|
+
# class Foo
|
618
|
+
# attr_accessor :bar, :baz
|
619
|
+
# def initialize(bar, baz)
|
620
|
+
# self.bar = bar
|
621
|
+
# self.baz = baz
|
622
|
+
# end
|
623
|
+
# end
|
624
|
+
#
|
625
|
+
# Here's the JSON addition for it:
|
626
|
+
# # Extend class Foo with JSON addition.
|
627
|
+
# class Foo
|
628
|
+
# # Serialize Foo object with its class name and arguments
|
629
|
+
# def to_json(*args)
|
630
|
+
# {
|
631
|
+
# JSON.create_id => self.class.name,
|
632
|
+
# 'a' => [ bar, baz ]
|
633
|
+
# }.to_json(*args)
|
634
|
+
# end
|
635
|
+
# # Deserialize JSON string by constructing new Foo object with arguments.
|
636
|
+
# def self.json_create(object)
|
637
|
+
# new(*object['a'])
|
638
|
+
# end
|
639
|
+
# end
|
640
|
+
#
|
641
|
+
# Demonstration:
|
109
642
|
# require 'json'
|
643
|
+
# # This Foo object has no custom addition.
|
644
|
+
# foo0 = Foo.new(0, 1)
|
645
|
+
# json0 = JSON.generate(foo0)
|
646
|
+
# obj0 = JSON.parse(json0)
|
647
|
+
# # Lood the custom addition.
|
648
|
+
# require_relative 'foo_addition'
|
649
|
+
# # This foo has the custom addition.
|
650
|
+
# foo1 = Foo.new(0, 1)
|
651
|
+
# json1 = JSON.generate(foo1)
|
652
|
+
# obj1 = JSON.parse(json1, create_additions: true)
|
653
|
+
# # Make a nice display.
|
654
|
+
# display = <<EOT
|
655
|
+
# Generated JSON:
|
656
|
+
# Without custom addition: #{json0} (#{json0.class})
|
657
|
+
# With custom addition: #{json1} (#{json1.class})
|
658
|
+
# Parsed JSON:
|
659
|
+
# Without custom addition: #{obj0.inspect} (#{obj0.class})
|
660
|
+
# With custom addition: #{obj1.inspect} (#{obj1.class})
|
661
|
+
# EOT
|
662
|
+
# puts display
|
663
|
+
#
|
664
|
+
# Output:
|
110
665
|
#
|
111
|
-
#
|
666
|
+
# Generated JSON:
|
667
|
+
# Without custom addition: "#<Foo:0x0000000006534e80>" (String)
|
668
|
+
# With custom addition: {"json_class":"Foo","a":[0,1]} (String)
|
669
|
+
# Parsed JSON:
|
670
|
+
# Without custom addition: "#<Foo:0x0000000006534e80>" (String)
|
671
|
+
# With custom addition: #<Foo:0x0000000006473bb8 @bar=0, @baz=1> (Foo)
|
112
672
|
#
|
113
673
|
module JSON
|
114
|
-
#
|
115
|
-
#
|
116
|
-
#
|
117
|
-
#
|
118
|
-
#
|
119
|
-
#
|
674
|
+
# <!--
|
675
|
+
# rdoc-file=ext/json/lib/json/common.rb
|
676
|
+
# - JSON[object] -> new_array or new_string
|
677
|
+
# -->
|
678
|
+
# If `object` is a String, calls JSON.parse with `object` and `opts` (see method
|
679
|
+
# #parse):
|
680
|
+
# json = '[0, 1, null]'
|
681
|
+
# JSON[json]# => [0, 1, nil]
|
682
|
+
#
|
683
|
+
# Otherwise, calls JSON.generate with `object` and `opts` (see method
|
684
|
+
# #generate):
|
685
|
+
# ruby = [0, 1, nil]
|
686
|
+
# JSON[ruby] # => '[0,1,null]'
|
120
687
|
#
|
121
688
|
def self.[]: (untyped object, ?json_options opts) -> untyped
|
122
689
|
|
123
|
-
#
|
124
|
-
#
|
690
|
+
# <!--
|
691
|
+
# rdoc-file=ext/json/lib/json/common.rb
|
692
|
+
# - create_id()
|
693
|
+
# -->
|
694
|
+
# Returns the current create identifier. See also JSON.create_id=.
|
125
695
|
#
|
126
696
|
def self.create_id: () -> _ToS
|
127
697
|
|
698
|
+
# <!--
|
699
|
+
# rdoc-file=ext/json/lib/json/common.rb
|
700
|
+
# - create_id=(new_value)
|
701
|
+
# -->
|
702
|
+
# Sets create identifier, which is used to decide if the *json_create* hook of a
|
703
|
+
# class should be called; initial value is `json_class`:
|
704
|
+
# JSON.create_id # => 'json_class'
|
705
|
+
#
|
128
706
|
def self.create_id=: (_ToS create_id) -> _ToS
|
129
707
|
|
130
708
|
def self.deep_const_get: (_ToS path) -> untyped
|
131
|
-
|
709
|
+
|
710
|
+
# <!--
|
711
|
+
# rdoc-file=ext/json/lib/json/common.rb
|
712
|
+
# - JSON.dump(obj, io = nil, limit = nil)
|
713
|
+
# -->
|
714
|
+
# Dumps `obj` as a JSON string, i.e. calls generate on the object and returns
|
132
715
|
# the result.
|
133
716
|
#
|
134
|
-
#
|
135
|
-
#
|
717
|
+
# The default options can be changed via method JSON.dump_default_options.
|
718
|
+
#
|
719
|
+
# * Argument `io`, if given, should respond to method `write`; the JSON String
|
720
|
+
# is written to `io`, and `io` is returned. If `io` is not given, the JSON
|
721
|
+
# String is returned.
|
722
|
+
# * Argument `limit`, if given, is passed to JSON.generate as option
|
723
|
+
# `max_nesting`.
|
136
724
|
#
|
137
|
-
# If the number of nested arrays or objects exceeds *limit*, an ArgumentError
|
138
|
-
# exception is raised. This argument is similar (but not exactly the same!) to
|
139
|
-
# the *limit* argument in Marshal.dump.
|
140
725
|
#
|
141
|
-
#
|
142
|
-
# dump_default_options method.
|
726
|
+
# ---
|
143
727
|
#
|
144
|
-
#
|
145
|
-
#
|
728
|
+
# When argument `io` is not given, returns the JSON String generated from `obj`:
|
729
|
+
# obj = {foo: [0, 1], bar: {baz: 2, bat: 3}, bam: :bad}
|
730
|
+
# json = JSON.dump(obj)
|
731
|
+
# json # => "{\"foo\":[0,1],\"bar\":{\"baz\":2,\"bat\":3},\"bam\":\"bad\"}"
|
732
|
+
#
|
733
|
+
# When argument `io` is given, writes the JSON String to `io` and returns `io`:
|
734
|
+
# path = 't.json'
|
735
|
+
# File.open(path, 'w') do |file|
|
736
|
+
# JSON.dump(obj, file)
|
737
|
+
# end # => #<File:t.json (closed)>
|
738
|
+
# puts File.read(path)
|
739
|
+
#
|
740
|
+
# Output:
|
741
|
+
# {"foo":[0,1],"bar":{"baz":2,"bat":3},"bam":"bad"}
|
146
742
|
#
|
147
743
|
def self?.dump: (_ToJson obj, ?Integer limit) -> String
|
148
744
|
| (_ToJson obj, _JsonToWritableIO anIO) -> _JsonWrite
|
149
745
|
| (_ToJson obj, _JsonWrite anIO, ?Integer limit) -> _JsonWrite
|
150
746
|
|
151
|
-
#
|
152
|
-
#
|
153
|
-
#
|
154
|
-
# :
|
747
|
+
# <!-- rdoc-file=ext/json/lib/json/common.rb -->
|
748
|
+
# Sets or returns the default options for the JSON.dump method. Initially:
|
749
|
+
# opts = JSON.dump_default_options
|
750
|
+
# opts # => {:max_nesting=>false, :allow_nan=>true, :escape_slash=>false}
|
155
751
|
#
|
156
752
|
def self.dump_default_options: () -> json_options
|
157
753
|
|
754
|
+
# <!-- rdoc-file=ext/json/lib/json/common.rb -->
|
755
|
+
# Sets or returns the default options for the JSON.dump method. Initially:
|
756
|
+
# opts = JSON.dump_default_options
|
757
|
+
# opts # => {:max_nesting=>false, :allow_nan=>true, :escape_slash=>false}
|
758
|
+
#
|
158
759
|
def self.dump_default_options=: (json_options) -> json_options
|
159
760
|
|
160
|
-
#
|
161
|
-
#
|
761
|
+
# <!--
|
762
|
+
# rdoc-file=ext/json/lib/json/common.rb
|
763
|
+
# - JSON.fast_generate(obj, opts) -> new_string
|
764
|
+
# -->
|
765
|
+
# Arguments `obj` and `opts` here are the same as arguments `obj` and `opts` in
|
766
|
+
# JSON.generate.
|
162
767
|
#
|
163
|
-
#
|
164
|
-
#
|
768
|
+
# By default, generates JSON data without checking for circular references in
|
769
|
+
# `obj` (option `max_nesting` set to `false`, disabled).
|
770
|
+
#
|
771
|
+
# Raises an exception if `obj` contains circular references:
|
772
|
+
# a = []; b = []; a.push(b); b.push(a)
|
773
|
+
# # Raises SystemStackError (stack level too deep):
|
774
|
+
# JSON.fast_generate(a)
|
165
775
|
#
|
166
776
|
def self?.fast_generate: (_ToJson obj, ?json_options opts) -> String
|
167
777
|
|
168
778
|
alias self.fast_unparse self.fast_generate
|
779
|
+
|
169
780
|
alias fast_unparse fast_generate
|
170
781
|
|
171
|
-
#
|
172
|
-
#
|
173
|
-
#
|
174
|
-
#
|
175
|
-
#
|
176
|
-
#
|
177
|
-
#
|
178
|
-
#
|
179
|
-
#
|
180
|
-
#
|
181
|
-
#
|
182
|
-
#
|
183
|
-
#
|
184
|
-
#
|
185
|
-
#
|
186
|
-
#
|
187
|
-
#
|
188
|
-
#
|
189
|
-
#
|
190
|
-
#
|
191
|
-
#
|
192
|
-
#
|
193
|
-
#
|
194
|
-
#
|
195
|
-
#
|
196
|
-
#
|
197
|
-
#
|
198
|
-
#
|
199
|
-
#
|
200
|
-
#
|
201
|
-
#
|
782
|
+
# <!--
|
783
|
+
# rdoc-file=ext/json/lib/json/common.rb
|
784
|
+
# - JSON.generate(obj, opts = nil) -> new_string
|
785
|
+
# -->
|
786
|
+
# Returns a String containing the generated JSON data.
|
787
|
+
#
|
788
|
+
# See also JSON.fast_generate, JSON.pretty_generate.
|
789
|
+
#
|
790
|
+
# Argument `obj` is the Ruby object to be converted to JSON.
|
791
|
+
#
|
792
|
+
# Argument `opts`, if given, contains a Hash of options for the generation. See
|
793
|
+
# [Generating Options](#module-JSON-label-Generating+Options).
|
794
|
+
#
|
795
|
+
# ---
|
796
|
+
#
|
797
|
+
# When `obj` is an Array, returns a String containing a JSON array:
|
798
|
+
# obj = ["foo", 1.0, true, false, nil]
|
799
|
+
# json = JSON.generate(obj)
|
800
|
+
# json # => '["foo",1.0,true,false,null]'
|
801
|
+
#
|
802
|
+
# When `obj` is a Hash, returns a String containing a JSON object:
|
803
|
+
# obj = {foo: 0, bar: 's', baz: :bat}
|
804
|
+
# json = JSON.generate(obj)
|
805
|
+
# json # => '{"foo":0,"bar":"s","baz":"bat"}'
|
806
|
+
#
|
807
|
+
# For examples of generating from other Ruby objects, see [Generating \JSON from
|
808
|
+
# Other Objects](#module-JSON-label-Generating+JSON+from+Other+Objects).
|
809
|
+
#
|
810
|
+
# ---
|
811
|
+
#
|
812
|
+
# Raises an exception if any formatting option is not a String.
|
813
|
+
#
|
814
|
+
# Raises an exception if `obj` contains circular references:
|
815
|
+
# a = []; b = []; a.push(b); b.push(a)
|
816
|
+
# # Raises JSON::NestingError (nesting of 100 is too deep):
|
817
|
+
# JSON.generate(a)
|
202
818
|
#
|
203
819
|
def self?.generate: (_ToJson obj, ?json_options opts) -> String
|
204
820
|
|
821
|
+
# <!-- rdoc-file=ext/json/lib/json/common.rb -->
|
205
822
|
# Returns the JSON generator module that is used by JSON. This is either
|
206
|
-
# JSON::Ext::Generator or JSON::Pure::Generator
|
823
|
+
# JSON::Ext::Generator or JSON::Pure::Generator:
|
824
|
+
# JSON.generator # => JSON::Ext::Generator
|
207
825
|
#
|
208
826
|
def self.generator: () -> json_generator
|
209
827
|
|
210
828
|
def self.generator=: (json_generator generator) -> void
|
211
829
|
|
212
|
-
#
|
830
|
+
# <!--
|
831
|
+
# rdoc-file=ext/json/lib/json/common.rb
|
832
|
+
# - iconv(to, from, string)
|
833
|
+
# -->
|
834
|
+
# Encodes string using String.encode.
|
213
835
|
#
|
214
836
|
def self.iconv: (encoding to, encoding from, String string) -> String
|
215
837
|
|
216
|
-
#
|
217
|
-
#
|
218
|
-
#
|
219
|
-
#
|
220
|
-
#
|
221
|
-
#
|
222
|
-
#
|
223
|
-
#
|
224
|
-
#
|
225
|
-
#
|
226
|
-
#
|
227
|
-
#
|
228
|
-
#
|
838
|
+
# <!--
|
839
|
+
# rdoc-file=ext/json/lib/json/common.rb
|
840
|
+
# - JSON.load(source, proc = nil, options = {}) -> object
|
841
|
+
# -->
|
842
|
+
# Returns the Ruby objects created by parsing the given `source`.
|
843
|
+
#
|
844
|
+
# * Argument `source` must be, or be convertible to, a String:
|
845
|
+
# * If `source` responds to instance method `to_str`, `source.to_str`
|
846
|
+
# becomes the source.
|
847
|
+
# * If `source` responds to instance method `to_io`, `source.to_io.read`
|
848
|
+
# becomes the source.
|
849
|
+
# * If `source` responds to instance method `read`, `source.read` becomes
|
850
|
+
# the source.
|
851
|
+
# * If both of the following are true, source becomes the String `'null'`:
|
852
|
+
# * Option `allow_blank` specifies a truthy value.
|
853
|
+
# * The source, as defined above, is `nil` or the empty String `''`.
|
854
|
+
#
|
855
|
+
# * Otherwise, `source` remains the source.
|
856
|
+
#
|
857
|
+
# * Argument `proc`, if given, must be a Proc that accepts one argument. It
|
858
|
+
# will be called recursively with each result (depth-first order). See
|
859
|
+
# details below. BEWARE: This method is meant to serialise data from trusted
|
860
|
+
# user input, like from your own database server or clients under your
|
861
|
+
# control, it could be dangerous to allow untrusted users to pass JSON
|
862
|
+
# sources into it.
|
863
|
+
# * Argument `opts`, if given, contains a Hash of options for the parsing. See
|
864
|
+
# [Parsing Options](#module-JSON-label-Parsing+Options). The default options
|
865
|
+
# can be changed via method JSON.load_default_options=.
|
866
|
+
#
|
867
|
+
#
|
868
|
+
# ---
|
869
|
+
#
|
870
|
+
# When no `proc` is given, modifies `source` as above and returns the result of
|
871
|
+
# `parse(source, opts)`; see #parse.
|
872
|
+
#
|
873
|
+
# Source for following examples:
|
874
|
+
# source = <<-EOT
|
875
|
+
# {
|
876
|
+
# "name": "Dave",
|
877
|
+
# "age" :40,
|
878
|
+
# "hats": [
|
879
|
+
# "Cattleman's",
|
880
|
+
# "Panama",
|
881
|
+
# "Tophat"
|
882
|
+
# ]
|
883
|
+
# }
|
884
|
+
# EOT
|
885
|
+
#
|
886
|
+
# Load a String:
|
887
|
+
# ruby = JSON.load(source)
|
888
|
+
# ruby # => {"name"=>"Dave", "age"=>40, "hats"=>["Cattleman's", "Panama", "Tophat"]}
|
889
|
+
#
|
890
|
+
# Load an IO object:
|
891
|
+
# require 'stringio'
|
892
|
+
# object = JSON.load(StringIO.new(source))
|
893
|
+
# object # => {"name"=>"Dave", "age"=>40, "hats"=>["Cattleman's", "Panama", "Tophat"]}
|
894
|
+
#
|
895
|
+
# Load a File object:
|
896
|
+
# path = 't.json'
|
897
|
+
# File.write(path, source)
|
898
|
+
# File.open(path) do |file|
|
899
|
+
# JSON.load(file)
|
900
|
+
# end # => {"name"=>"Dave", "age"=>40, "hats"=>["Cattleman's", "Panama", "Tophat"]}
|
901
|
+
#
|
902
|
+
# ---
|
903
|
+
#
|
904
|
+
# When `proc` is given:
|
905
|
+
# * Modifies `source` as above.
|
906
|
+
# * Gets the `result` from calling `parse(source, opts)`.
|
907
|
+
# * Recursively calls `proc(result)`.
|
908
|
+
# * Returns the final result.
|
909
|
+
#
|
910
|
+
#
|
911
|
+
# Example:
|
912
|
+
# require 'json'
|
913
|
+
#
|
914
|
+
# # Some classes for the example.
|
915
|
+
# class Base
|
916
|
+
# def initialize(attributes)
|
917
|
+
# @attributes = attributes
|
918
|
+
# end
|
919
|
+
# end
|
920
|
+
# class User < Base; end
|
921
|
+
# class Account < Base; end
|
922
|
+
# class Admin < Base; end
|
923
|
+
# # The JSON source.
|
924
|
+
# json = <<-EOF
|
925
|
+
# {
|
926
|
+
# "users": [
|
927
|
+
# {"type": "User", "username": "jane", "email": "jane@example.com"},
|
928
|
+
# {"type": "User", "username": "john", "email": "john@example.com"}
|
929
|
+
# ],
|
930
|
+
# "accounts": [
|
931
|
+
# {"account": {"type": "Account", "paid": true, "account_id": "1234"}},
|
932
|
+
# {"account": {"type": "Account", "paid": false, "account_id": "1235"}}
|
933
|
+
# ],
|
934
|
+
# "admins": {"type": "Admin", "password": "0wn3d"}
|
935
|
+
# }
|
936
|
+
# EOF
|
937
|
+
# # Deserializer method.
|
938
|
+
# def deserialize_obj(obj, safe_types = %w(User Account Admin))
|
939
|
+
# type = obj.is_a?(Hash) && obj["type"]
|
940
|
+
# safe_types.include?(type) ? Object.const_get(type).new(obj) : obj
|
941
|
+
# end
|
942
|
+
# # Call to JSON.load
|
943
|
+
# ruby = JSON.load(json, proc {|obj|
|
944
|
+
# case obj
|
945
|
+
# when Hash
|
946
|
+
# obj.each {|k, v| obj[k] = deserialize_obj v }
|
947
|
+
# when Array
|
948
|
+
# obj.map! {|v| deserialize_obj v }
|
949
|
+
# end
|
950
|
+
# })
|
951
|
+
# pp ruby
|
952
|
+
#
|
953
|
+
# Output:
|
954
|
+
# {"users"=>
|
955
|
+
# [#<User:0x00000000064c4c98
|
956
|
+
# @attributes=
|
957
|
+
# {"type"=>"User", "username"=>"jane", "email"=>"jane@example.com"}>,
|
958
|
+
# #<User:0x00000000064c4bd0
|
959
|
+
# @attributes=
|
960
|
+
# {"type"=>"User", "username"=>"john", "email"=>"john@example.com"}>],
|
961
|
+
# "accounts"=>
|
962
|
+
# [{"account"=>
|
963
|
+
# #<Account:0x00000000064c4928
|
964
|
+
# @attributes={"type"=>"Account", "paid"=>true, "account_id"=>"1234"}>},
|
965
|
+
# {"account"=>
|
966
|
+
# #<Account:0x00000000064c4680
|
967
|
+
# @attributes={"type"=>"Account", "paid"=>false, "account_id"=>"1235"}>}],
|
968
|
+
# "admins"=>
|
969
|
+
# #<Admin:0x00000000064c41f8
|
970
|
+
# @attributes={"type"=>"Admin", "password"=>"0wn3d"}>}
|
229
971
|
#
|
230
972
|
def self?.load: (string | _JsonReadableIO | _JsonRead source, ?Proc proc, ?json_options options) -> untyped
|
231
973
|
|
232
|
-
#
|
233
|
-
#
|
234
|
-
#
|
235
|
-
# :
|
974
|
+
# <!-- rdoc-file=ext/json/lib/json/common.rb -->
|
975
|
+
# Sets or returns default options for the JSON.load method. Initially:
|
976
|
+
# opts = JSON.load_default_options
|
977
|
+
# opts # => {:max_nesting=>false, :allow_nan=>true, :allow_blank=>true, :create_additions=>true}
|
236
978
|
#
|
237
979
|
def self.load_default_options: () -> json_options
|
238
980
|
|
981
|
+
# <!-- rdoc-file=ext/json/lib/json/common.rb -->
|
982
|
+
# Sets or returns default options for the JSON.load method. Initially:
|
983
|
+
# opts = JSON.load_default_options
|
984
|
+
# opts # => {:max_nesting=>false, :allow_nan=>true, :allow_blank=>true, :create_additions=>true}
|
985
|
+
#
|
239
986
|
def self.load_default_options=: (json_options) -> json_options
|
240
987
|
|
241
|
-
#
|
242
|
-
#
|
243
|
-
#
|
244
|
-
#
|
245
|
-
#
|
246
|
-
#
|
247
|
-
#
|
248
|
-
#
|
249
|
-
#
|
250
|
-
#
|
251
|
-
#
|
252
|
-
#
|
253
|
-
#
|
254
|
-
#
|
255
|
-
#
|
256
|
-
#
|
257
|
-
#
|
988
|
+
# <!--
|
989
|
+
# rdoc-file=ext/json/lib/json/common.rb
|
990
|
+
# - JSON.parse(source, opts) -> object
|
991
|
+
# -->
|
992
|
+
# Returns the Ruby objects created by parsing the given `source`.
|
993
|
+
#
|
994
|
+
# Argument `source` contains the String to be parsed.
|
995
|
+
#
|
996
|
+
# Argument `opts`, if given, contains a Hash of options for the parsing. See
|
997
|
+
# [Parsing Options](#module-JSON-label-Parsing+Options).
|
998
|
+
#
|
999
|
+
# ---
|
1000
|
+
#
|
1001
|
+
# When `source` is a JSON array, returns a Ruby Array:
|
1002
|
+
# source = '["foo", 1.0, true, false, null]'
|
1003
|
+
# ruby = JSON.parse(source)
|
1004
|
+
# ruby # => ["foo", 1.0, true, false, nil]
|
1005
|
+
# ruby.class # => Array
|
1006
|
+
#
|
1007
|
+
# When `source` is a JSON object, returns a Ruby Hash:
|
1008
|
+
# source = '{"a": "foo", "b": 1.0, "c": true, "d": false, "e": null}'
|
1009
|
+
# ruby = JSON.parse(source)
|
1010
|
+
# ruby # => {"a"=>"foo", "b"=>1.0, "c"=>true, "d"=>false, "e"=>nil}
|
1011
|
+
# ruby.class # => Hash
|
1012
|
+
#
|
1013
|
+
# For examples of parsing for all JSON data types, see [Parsing
|
1014
|
+
# \JSON](#module-JSON-label-Parsing+JSON).
|
1015
|
+
#
|
1016
|
+
# Parses nested JSON objects:
|
1017
|
+
# source = <<-EOT
|
1018
|
+
# {
|
1019
|
+
# "name": "Dave",
|
1020
|
+
# "age" :40,
|
1021
|
+
# "hats": [
|
1022
|
+
# "Cattleman's",
|
1023
|
+
# "Panama",
|
1024
|
+
# "Tophat"
|
1025
|
+
# ]
|
1026
|
+
# }
|
1027
|
+
# EOT
|
1028
|
+
# ruby = JSON.parse(source)
|
1029
|
+
# ruby # => {"name"=>"Dave", "age"=>40, "hats"=>["Cattleman's", "Panama", "Tophat"]}
|
1030
|
+
#
|
1031
|
+
# ---
|
1032
|
+
#
|
1033
|
+
# Raises an exception if `source` is not valid JSON:
|
1034
|
+
# # Raises JSON::ParserError (783: unexpected token at ''):
|
1035
|
+
# JSON.parse('')
|
258
1036
|
#
|
259
1037
|
def self?.parse: (string source, ?json_options opts) -> untyped
|
260
1038
|
|
261
|
-
#
|
262
|
-
#
|
263
|
-
#
|
1039
|
+
# <!--
|
1040
|
+
# rdoc-file=ext/json/lib/json/common.rb
|
1041
|
+
# - JSON.parse!(source, opts) -> object
|
1042
|
+
# -->
|
1043
|
+
# Calls
|
1044
|
+
# parse(source, opts)
|
264
1045
|
#
|
265
|
-
#
|
266
|
-
# * **max_nesting**: The maximum depth of nesting allowed in the parsed data
|
267
|
-
# structures. Enable depth checking with :max_nesting => anInteger. The
|
268
|
-
# parse! methods defaults to not doing max depth checking: This can be
|
269
|
-
# dangerous if someone wants to fill up your stack.
|
270
|
-
# * **allow_nan**: If set to true, allow NaN, Infinity, and -Infinity in
|
271
|
-
# defiance of RFC 7159 to be parsed by the Parser. This option defaults to
|
272
|
-
# true.
|
273
|
-
# * **create_additions**: If set to false, the Parser doesn't create additions
|
274
|
-
# even if a matching class and create_id was found. This option defaults to
|
275
|
-
# false.
|
1046
|
+
# with `source` and possibly modified `opts`.
|
276
1047
|
#
|
1048
|
+
# Differences from JSON.parse:
|
1049
|
+
# * Option `max_nesting`, if not provided, defaults to `false`, which disables
|
1050
|
+
# checking for nesting depth.
|
1051
|
+
# * Option `allow_nan`, if not provided, defaults to `true`.
|
277
1052
|
#
|
278
1053
|
def self?.parse!: (string source, ?json_options opts) -> untyped
|
279
1054
|
|
1055
|
+
# <!-- rdoc-file=ext/json/lib/json/common.rb -->
|
280
1056
|
# Returns the JSON parser class that is used by JSON. This is either
|
281
|
-
# JSON::Ext::Parser or JSON::Pure::Parser
|
1057
|
+
# JSON::Ext::Parser or JSON::Pure::Parser:
|
1058
|
+
# JSON.parser # => JSON::Ext::Parser
|
282
1059
|
#
|
283
1060
|
def self.parser: () -> json_parser
|
284
1061
|
|
285
1062
|
def self.parser=: (json_parser parser) -> void
|
286
1063
|
|
287
|
-
#
|
288
|
-
#
|
289
|
-
#
|
290
|
-
#
|
291
|
-
#
|
1064
|
+
# <!--
|
1065
|
+
# rdoc-file=ext/json/lib/json/common.rb
|
1066
|
+
# - JSON.pretty_generate(obj, opts = nil) -> new_string
|
1067
|
+
# -->
|
1068
|
+
# Arguments `obj` and `opts` here are the same as arguments `obj` and `opts` in
|
1069
|
+
# JSON.generate.
|
1070
|
+
#
|
1071
|
+
# Default options are:
|
1072
|
+
# {
|
1073
|
+
# indent: ' ', # Two spaces
|
1074
|
+
# space: ' ', # One space
|
1075
|
+
# array_nl: "\n", # Newline
|
1076
|
+
# object_nl: "\n" # Newline
|
1077
|
+
# }
|
1078
|
+
#
|
1079
|
+
# Example:
|
1080
|
+
# obj = {foo: [:bar, :baz], bat: {bam: 0, bad: 1}}
|
1081
|
+
# json = JSON.pretty_generate(obj)
|
1082
|
+
# puts json
|
1083
|
+
#
|
1084
|
+
# Output:
|
1085
|
+
# {
|
1086
|
+
# "foo": [
|
1087
|
+
# "bar",
|
1088
|
+
# "baz"
|
1089
|
+
# ],
|
1090
|
+
# "bat": {
|
1091
|
+
# "bam": 0,
|
1092
|
+
# "bad": 1
|
1093
|
+
# }
|
1094
|
+
# }
|
292
1095
|
#
|
293
1096
|
def self?.pretty_generate: (_ToJson obj, ?json_options opts) -> untyped
|
294
1097
|
|
295
1098
|
alias self.pretty_unparse self.pretty_generate
|
1099
|
+
|
296
1100
|
alias pretty_unparse pretty_generate
|
297
1101
|
|
298
1102
|
# Recursively calls passed *Proc* if the parsed data structure is an *Array* or
|
@@ -300,17 +1104,36 @@ module JSON
|
|
300
1104
|
#
|
301
1105
|
def self?.recurse_proc: (untyped result) { (*untyped) -> void } -> void
|
302
1106
|
|
1107
|
+
# <!--
|
1108
|
+
# rdoc-file=ext/json/lib/json/common.rb
|
1109
|
+
# - restore(source, proc = nil, options = {})
|
1110
|
+
# -->
|
1111
|
+
#
|
303
1112
|
alias self.restore self.load
|
1113
|
+
|
1114
|
+
# <!--
|
1115
|
+
# rdoc-file=ext/json/lib/json/common.rb
|
1116
|
+
# - restore(source, proc = nil, options = {})
|
1117
|
+
# -->
|
1118
|
+
#
|
304
1119
|
alias restore load
|
305
1120
|
|
306
|
-
#
|
307
|
-
#
|
1121
|
+
# <!-- rdoc-file=ext/json/lib/json/common.rb -->
|
1122
|
+
# Sets or Returns the JSON generator state class that is used by JSON. This is
|
1123
|
+
# either JSON::Ext::Generator::State or JSON::Pure::Generator::State:
|
1124
|
+
# JSON.state # => JSON::Ext::Generator::State
|
308
1125
|
#
|
309
1126
|
def self.state: () -> json_state
|
310
1127
|
|
1128
|
+
# <!-- rdoc-file=ext/json/lib/json/common.rb -->
|
1129
|
+
# Sets or Returns the JSON generator state class that is used by JSON. This is
|
1130
|
+
# either JSON::Ext::Generator::State or JSON::Pure::Generator::State:
|
1131
|
+
# JSON.state # => JSON::Ext::Generator::State
|
1132
|
+
#
|
311
1133
|
def self.state=: (json_state) -> json_state
|
312
1134
|
|
313
1135
|
alias self.unparse self.generate
|
1136
|
+
|
314
1137
|
alias unparse generate
|
315
1138
|
end
|
316
1139
|
|
@@ -328,6 +1151,7 @@ JSON::PRETTY_STATE_PROTOTYPE: json_state
|
|
328
1151
|
|
329
1152
|
JSON::SAFE_STATE_PROTOTYPE: json_state
|
330
1153
|
|
1154
|
+
# <!-- rdoc-file=ext/json/lib/json/version.rb -->
|
331
1155
|
# JSON version
|
332
1156
|
#
|
333
1157
|
JSON::VERSION: String
|
@@ -340,6 +1164,7 @@ JSON::VERSION_MAJOR: Integer
|
|
340
1164
|
|
341
1165
|
JSON::VERSION_MINOR: Integer
|
342
1166
|
|
1167
|
+
%a{annotate:rdoc:skip}
|
343
1168
|
class Object
|
344
1169
|
# Converts this object to a string (calling #to_s), converts
|
345
1170
|
# it to a JSON string, and returns the result. This is a fallback, if no
|
@@ -348,24 +1173,28 @@ class Object
|
|
348
1173
|
def to_json: (?JSON::State state) -> String
|
349
1174
|
end
|
350
1175
|
|
1176
|
+
%a{annotate:rdoc:skip}
|
351
1177
|
class NilClass
|
352
1178
|
# Returns a JSON string for nil: 'null'.
|
353
1179
|
#
|
354
1180
|
def to_json: (?JSON::State state) -> String
|
355
1181
|
end
|
356
1182
|
|
1183
|
+
%a{annotate:rdoc:skip}
|
357
1184
|
class TrueClass
|
358
1185
|
# Returns a JSON string for true: 'true'.
|
359
1186
|
#
|
360
1187
|
def to_json: (?JSON::State state) -> String
|
361
1188
|
end
|
362
1189
|
|
1190
|
+
%a{annotate:rdoc:skip}
|
363
1191
|
class FalseClass
|
364
1192
|
# Returns a JSON string for false: 'false'.
|
365
1193
|
#
|
366
1194
|
def to_json: (?JSON::State state) -> String
|
367
1195
|
end
|
368
1196
|
|
1197
|
+
%a{annotate:rdoc:skip}
|
369
1198
|
class String
|
370
1199
|
# This string should be encoded with UTF-8 A call to this method
|
371
1200
|
# returns a JSON string encoded with UTF16 big endian characters as
|
@@ -374,18 +1203,21 @@ class String
|
|
374
1203
|
def to_json: (?JSON::State state) -> String
|
375
1204
|
end
|
376
1205
|
|
1206
|
+
%a{annotate:rdoc:skip}
|
377
1207
|
class Integer
|
378
1208
|
# Returns a JSON string representation for this Integer number.
|
379
1209
|
#
|
380
1210
|
def to_json: (?JSON::State state) -> String
|
381
1211
|
end
|
382
1212
|
|
1213
|
+
%a{annotate:rdoc:skip}
|
383
1214
|
class Float
|
384
1215
|
# Returns a JSON string representation for this Float number.
|
385
1216
|
#
|
386
1217
|
def to_json: (?JSON::State state) -> String
|
387
1218
|
end
|
388
1219
|
|
1220
|
+
%a{annotate:rdoc:skip}
|
389
1221
|
class Hash[unchecked out K, unchecked out V]
|
390
1222
|
# Returns a JSON string containing a JSON object, that is generated from
|
391
1223
|
# this Hash instance.
|
@@ -395,6 +1227,7 @@ class Hash[unchecked out K, unchecked out V]
|
|
395
1227
|
def to_json: (?JSON::State state) -> String
|
396
1228
|
end
|
397
1229
|
|
1230
|
+
%a{annotate:rdoc:skip}
|
398
1231
|
class Array[unchecked out Elem]
|
399
1232
|
# Returns a JSON string containing a JSON array, that is generated from
|
400
1233
|
# this Array instance.
|
@@ -404,70 +1237,122 @@ class Array[unchecked out Elem]
|
|
404
1237
|
def to_json: (?JSON::State state) -> String
|
405
1238
|
end
|
406
1239
|
|
1240
|
+
%a{annotate:rdoc:skip}
|
407
1241
|
class BigDecimal
|
1242
|
+
# <!--
|
1243
|
+
# rdoc-file=ext/json/lib/json/add/bigdecimal.rb
|
1244
|
+
# - json_create(object)
|
1245
|
+
# -->
|
408
1246
|
# Import a JSON Marshalled object.
|
409
1247
|
#
|
410
1248
|
# method used for JSON marshalling support.
|
411
1249
|
#
|
412
1250
|
def self.json_create: (Hash[String, String] object) -> instance
|
413
1251
|
|
1252
|
+
# <!--
|
1253
|
+
# rdoc-file=ext/json/lib/json/add/bigdecimal.rb
|
1254
|
+
# - as_json(*)
|
1255
|
+
# -->
|
414
1256
|
# Marshal the object to JSON.
|
415
1257
|
#
|
416
1258
|
# method used for JSON marshalling support.
|
417
1259
|
#
|
418
1260
|
def as_json: (*untyped) -> Hash[String, String]
|
419
1261
|
|
1262
|
+
# <!--
|
1263
|
+
# rdoc-file=ext/json/lib/json/add/bigdecimal.rb
|
1264
|
+
# - to_json(*args)
|
1265
|
+
# -->
|
420
1266
|
# return the JSON value
|
421
1267
|
#
|
422
1268
|
def to_json: (?JSON::State state) -> String
|
423
1269
|
end
|
424
1270
|
|
1271
|
+
%a{annotate:rdoc:skip}
|
425
1272
|
class Complex
|
1273
|
+
# <!--
|
1274
|
+
# rdoc-file=ext/json/lib/json/add/complex.rb
|
1275
|
+
# - json_create(object)
|
1276
|
+
# -->
|
426
1277
|
# Deserializes JSON string by converting Real value `r`, imaginary value `i`, to
|
427
1278
|
# a Complex object.
|
428
1279
|
#
|
429
1280
|
def self.json_create: (Hash[String, String | Numeric] object) -> instance
|
430
1281
|
|
1282
|
+
# <!--
|
1283
|
+
# rdoc-file=ext/json/lib/json/add/complex.rb
|
1284
|
+
# - as_json(*)
|
1285
|
+
# -->
|
431
1286
|
# Returns a hash, that will be turned into a JSON object and represent this
|
432
1287
|
# object.
|
433
1288
|
#
|
434
1289
|
def as_json: (*untyped) -> Hash[String, String | Numeric]
|
435
1290
|
|
1291
|
+
# <!--
|
1292
|
+
# rdoc-file=ext/json/lib/json/add/complex.rb
|
1293
|
+
# - to_json(*args)
|
1294
|
+
# -->
|
436
1295
|
# Stores class name (Complex) along with real value `r` and imaginary value `i`
|
437
1296
|
# as JSON string
|
438
1297
|
#
|
439
1298
|
def to_json: (?JSON::State state) -> String
|
440
1299
|
end
|
441
1300
|
|
1301
|
+
%a{annotate:rdoc:skip}
|
442
1302
|
class Date
|
1303
|
+
# <!--
|
1304
|
+
# rdoc-file=ext/json/lib/json/add/date.rb
|
1305
|
+
# - json_create(object)
|
1306
|
+
# -->
|
443
1307
|
# Deserializes JSON string by converting Julian year `y`, month `m`, day `d` and
|
444
1308
|
# Day of Calendar Reform `sg` to Date.
|
445
1309
|
#
|
446
1310
|
def self.json_create: (Hash[String, String | Integer | Float] object) -> instance
|
447
1311
|
|
1312
|
+
# <!--
|
1313
|
+
# rdoc-file=ext/json/lib/json/add/date.rb
|
1314
|
+
# - as_json(*)
|
1315
|
+
# -->
|
448
1316
|
# Returns a hash, that will be turned into a JSON object and represent this
|
449
1317
|
# object.
|
450
1318
|
#
|
451
1319
|
def as_json: (*untyped) -> Hash[String, String | Integer | Float]
|
452
1320
|
|
1321
|
+
# <!--
|
1322
|
+
# rdoc-file=ext/json/lib/json/add/date.rb
|
1323
|
+
# - to_json(*args)
|
1324
|
+
# -->
|
453
1325
|
# Stores class name (Date) with Julian year `y`, month `m`, day `d` and Day of
|
454
1326
|
# Calendar Reform `sg` as JSON string
|
455
1327
|
#
|
456
1328
|
def to_json: (?JSON::State state) -> String
|
457
1329
|
end
|
458
1330
|
|
1331
|
+
%a{annotate:rdoc:skip}
|
459
1332
|
class DateTime
|
1333
|
+
# <!--
|
1334
|
+
# rdoc-file=ext/json/lib/json/add/date_time.rb
|
1335
|
+
# - json_create(object)
|
1336
|
+
# -->
|
460
1337
|
# Deserializes JSON string by converting year `y`, month `m`, day `d`, hour `H`,
|
461
1338
|
# minute `M`, second `S`, offset `of` and Day of Calendar Reform `sg` to
|
462
1339
|
# DateTime.
|
463
1340
|
#
|
464
1341
|
def self.json_create: (Hash[String, String | Integer | Float] object) -> instance
|
465
1342
|
|
1343
|
+
# <!--
|
1344
|
+
# rdoc-file=ext/json/lib/json/add/date_time.rb
|
1345
|
+
# - as_json(*)
|
1346
|
+
# -->
|
466
1347
|
# Returns a hash, that will be turned into a JSON object and represent this
|
467
1348
|
# object.
|
468
1349
|
#
|
469
1350
|
def as_json: (*untyped) -> Hash[String, String | Integer | Float]
|
470
1351
|
|
1352
|
+
# <!--
|
1353
|
+
# rdoc-file=ext/json/lib/json/add/date_time.rb
|
1354
|
+
# - to_json(*args)
|
1355
|
+
# -->
|
471
1356
|
# Stores class name (DateTime) with Julian year `y`, month `m`, day `d`, hour
|
472
1357
|
# `H`, minute `M`, second `S`, offset `of` and Day of Calendar Reform `sg` as
|
473
1358
|
# JSON string
|
@@ -475,50 +1360,89 @@ class DateTime
|
|
475
1360
|
def to_json: (?JSON::State state) -> String
|
476
1361
|
end
|
477
1362
|
|
1363
|
+
%a{annotate:rdoc:skip}
|
478
1364
|
class Exception
|
1365
|
+
# <!--
|
1366
|
+
# rdoc-file=ext/json/lib/json/add/exception.rb
|
1367
|
+
# - json_create(object)
|
1368
|
+
# -->
|
479
1369
|
# Deserializes JSON string by constructing new Exception object with message `m`
|
480
1370
|
# and backtrace `b` serialized with `to_json`
|
481
1371
|
#
|
482
1372
|
def self.json_create: (Hash[String, String | Array[String] | nil] object) -> instance
|
483
1373
|
|
1374
|
+
# <!--
|
1375
|
+
# rdoc-file=ext/json/lib/json/add/exception.rb
|
1376
|
+
# - as_json(*)
|
1377
|
+
# -->
|
484
1378
|
# Returns a hash, that will be turned into a JSON object and represent this
|
485
1379
|
# object.
|
486
1380
|
#
|
487
1381
|
def as_json: (*untyped) -> Hash[String, String | Array[String] | nil]
|
488
1382
|
|
1383
|
+
# <!--
|
1384
|
+
# rdoc-file=ext/json/lib/json/add/exception.rb
|
1385
|
+
# - to_json(*args)
|
1386
|
+
# -->
|
489
1387
|
# Stores class name (Exception) with message `m` and backtrace array `b` as JSON
|
490
1388
|
# string
|
491
1389
|
#
|
492
1390
|
def to_json: (?JSON::State state) -> String
|
493
1391
|
end
|
494
1392
|
|
1393
|
+
%a{annotate:rdoc:skip}
|
495
1394
|
class OpenStruct
|
1395
|
+
# <!--
|
1396
|
+
# rdoc-file=ext/json/lib/json/add/ostruct.rb
|
1397
|
+
# - json_create(object)
|
1398
|
+
# -->
|
496
1399
|
# Deserializes JSON string by constructing new Struct object with values `t`
|
497
1400
|
# serialized by `to_json`.
|
498
1401
|
#
|
499
1402
|
def self.json_create: (Hash[String, String | Hash[Symbol, untyped]] object) -> instance
|
500
1403
|
|
1404
|
+
# <!--
|
1405
|
+
# rdoc-file=ext/json/lib/json/add/ostruct.rb
|
1406
|
+
# - as_json(*)
|
1407
|
+
# -->
|
501
1408
|
# Returns a hash, that will be turned into a JSON object and represent this
|
502
1409
|
# object.
|
503
1410
|
#
|
504
1411
|
def as_json: (*untyped) -> Hash[String, String | Hash[Symbol, untyped]]
|
505
1412
|
|
1413
|
+
# <!--
|
1414
|
+
# rdoc-file=ext/json/lib/json/add/ostruct.rb
|
1415
|
+
# - to_json(*args)
|
1416
|
+
# -->
|
506
1417
|
# Stores class name (OpenStruct) with this struct's values `t` as a JSON string.
|
507
1418
|
#
|
508
1419
|
def to_json: (?JSON::State state) -> String
|
509
1420
|
end
|
510
1421
|
|
1422
|
+
%a{annotate:rdoc:skip}
|
511
1423
|
class Range[out Elem]
|
1424
|
+
# <!--
|
1425
|
+
# rdoc-file=ext/json/lib/json/add/range.rb
|
1426
|
+
# - json_create(object)
|
1427
|
+
# -->
|
512
1428
|
# Deserializes JSON string by constructing new Range object with arguments `a`
|
513
1429
|
# serialized by `to_json`.
|
514
1430
|
#
|
515
|
-
def self.json_create: [A] (Hash[String, String | [A, A, bool]] object) -> Range[A]
|
1431
|
+
def self.json_create: [A] (Hash[String, String | [ A, A, bool ]] object) -> Range[A]
|
516
1432
|
|
1433
|
+
# <!--
|
1434
|
+
# rdoc-file=ext/json/lib/json/add/range.rb
|
1435
|
+
# - as_json(*)
|
1436
|
+
# -->
|
517
1437
|
# Returns a hash, that will be turned into a JSON object and represent this
|
518
1438
|
# object.
|
519
1439
|
#
|
520
|
-
def as_json: (*untyped) -> Hash[String, String | [Elem, Elem, bool]]
|
1440
|
+
def as_json: (*untyped) -> Hash[String, String | [ Elem, Elem, bool ]]
|
521
1441
|
|
1442
|
+
# <!--
|
1443
|
+
# rdoc-file=ext/json/lib/json/add/range.rb
|
1444
|
+
# - to_json(*args)
|
1445
|
+
# -->
|
522
1446
|
# Stores class name (Range) with JSON array of arguments `a` which include
|
523
1447
|
# `first` (integer), `last` (integer), and `exclude_end?` (boolean) as JSON
|
524
1448
|
# string.
|
@@ -526,102 +1450,180 @@ class Range[out Elem]
|
|
526
1450
|
def to_json: (?JSON::State state) -> String
|
527
1451
|
end
|
528
1452
|
|
1453
|
+
%a{annotate:rdoc:skip}
|
529
1454
|
class Rational
|
1455
|
+
# <!--
|
1456
|
+
# rdoc-file=ext/json/lib/json/add/rational.rb
|
1457
|
+
# - json_create(object)
|
1458
|
+
# -->
|
530
1459
|
# Deserializes JSON string by converting numerator value `n`, denominator value
|
531
1460
|
# `d`, to a Rational object.
|
532
1461
|
#
|
533
1462
|
def self.json_create: (Hash[String, String | Integer] object) -> instance
|
534
1463
|
|
1464
|
+
# <!--
|
1465
|
+
# rdoc-file=ext/json/lib/json/add/rational.rb
|
1466
|
+
# - as_json(*)
|
1467
|
+
# -->
|
535
1468
|
# Returns a hash, that will be turned into a JSON object and represent this
|
536
1469
|
# object.
|
537
1470
|
#
|
538
1471
|
def as_json: (*untyped) -> Hash[String, String | Integer]
|
539
1472
|
|
1473
|
+
# <!--
|
1474
|
+
# rdoc-file=ext/json/lib/json/add/rational.rb
|
1475
|
+
# - to_json(*args)
|
1476
|
+
# -->
|
540
1477
|
# Stores class name (Rational) along with numerator value `n` and denominator
|
541
1478
|
# value `d` as JSON string
|
542
1479
|
#
|
543
1480
|
def to_json: (?JSON::State state) -> String
|
544
1481
|
end
|
545
1482
|
|
1483
|
+
%a{annotate:rdoc:skip}
|
546
1484
|
class Regexp
|
1485
|
+
# <!--
|
1486
|
+
# rdoc-file=ext/json/lib/json/add/regexp.rb
|
1487
|
+
# - json_create(object)
|
1488
|
+
# -->
|
547
1489
|
# Deserializes JSON string by constructing new Regexp object with source `s`
|
548
1490
|
# (Regexp or String) and options `o` serialized by `to_json`
|
549
1491
|
#
|
550
1492
|
def self.json_create: (Hash[String, String | Integer] object) -> instance
|
551
1493
|
|
1494
|
+
# <!--
|
1495
|
+
# rdoc-file=ext/json/lib/json/add/regexp.rb
|
1496
|
+
# - as_json(*)
|
1497
|
+
# -->
|
552
1498
|
# Returns a hash, that will be turned into a JSON object and represent this
|
553
1499
|
# object.
|
554
1500
|
#
|
555
1501
|
def as_json: (*untyped) -> Hash[String, String | Integer]
|
556
1502
|
|
1503
|
+
# <!--
|
1504
|
+
# rdoc-file=ext/json/lib/json/add/regexp.rb
|
1505
|
+
# - to_json(*args)
|
1506
|
+
# -->
|
557
1507
|
# Stores class name (Regexp) with options `o` and source `s` (Regexp or String)
|
558
1508
|
# as JSON string
|
559
1509
|
#
|
560
1510
|
def to_json: (?JSON::State state) -> String
|
561
1511
|
end
|
562
1512
|
|
1513
|
+
%a{annotate:rdoc:skip}
|
563
1514
|
class Set[A]
|
1515
|
+
# <!--
|
1516
|
+
# rdoc-file=ext/json/lib/json/add/set.rb
|
1517
|
+
# - json_create(object)
|
1518
|
+
# -->
|
564
1519
|
# Import a JSON Marshalled object.
|
565
1520
|
#
|
566
1521
|
# method used for JSON marshalling support.
|
567
1522
|
#
|
568
1523
|
def self.json_create: [A] (Hash[String, String | Array[A]] object) -> Set[A]
|
569
1524
|
|
1525
|
+
# <!--
|
1526
|
+
# rdoc-file=ext/json/lib/json/add/set.rb
|
1527
|
+
# - as_json(*)
|
1528
|
+
# -->
|
570
1529
|
# Marshal the object to JSON.
|
571
1530
|
#
|
572
1531
|
# method used for JSON marshalling support.
|
573
1532
|
#
|
574
1533
|
def as_json: (*untyped) -> Hash[String, String | Array[A]]
|
575
1534
|
|
1535
|
+
# <!--
|
1536
|
+
# rdoc-file=ext/json/lib/json/add/set.rb
|
1537
|
+
# - to_json(*args)
|
1538
|
+
# -->
|
576
1539
|
# return the JSON value
|
577
1540
|
#
|
578
1541
|
def to_json: (?JSON::State state) -> String
|
579
1542
|
end
|
580
1543
|
|
1544
|
+
%a{annotate:rdoc:skip}
|
581
1545
|
class Struct[Elem]
|
1546
|
+
# <!--
|
1547
|
+
# rdoc-file=ext/json/lib/json/add/struct.rb
|
1548
|
+
# - json_create(object)
|
1549
|
+
# -->
|
582
1550
|
# Deserializes JSON string by constructing new Struct object with values `v`
|
583
1551
|
# serialized by `to_json`.
|
584
1552
|
#
|
585
1553
|
def self.json_create: [Elem] (Hash[String, String | Array[Elem]] object) -> Struct[Elem]
|
586
1554
|
|
1555
|
+
# <!--
|
1556
|
+
# rdoc-file=ext/json/lib/json/add/struct.rb
|
1557
|
+
# - as_json(*)
|
1558
|
+
# -->
|
587
1559
|
# Returns a hash, that will be turned into a JSON object and represent this
|
588
1560
|
# object.
|
589
1561
|
#
|
590
1562
|
def as_json: (*untyped) -> Hash[String, String | Array[Elem]]
|
591
1563
|
|
1564
|
+
# <!--
|
1565
|
+
# rdoc-file=ext/json/lib/json/add/struct.rb
|
1566
|
+
# - to_json(*args)
|
1567
|
+
# -->
|
592
1568
|
# Stores class name (Struct) with Struct values `v` as a JSON string. Only named
|
593
1569
|
# structs are supported.
|
594
1570
|
#
|
595
1571
|
def to_json: (?JSON::State state) -> String
|
596
1572
|
end
|
597
1573
|
|
1574
|
+
%a{annotate:rdoc:skip}
|
598
1575
|
class Symbol
|
1576
|
+
# <!--
|
1577
|
+
# rdoc-file=ext/json/lib/json/add/symbol.rb
|
1578
|
+
# - json_create(o)
|
1579
|
+
# -->
|
599
1580
|
# Deserializes JSON string by converting the `string` value stored in the object
|
600
1581
|
# to a Symbol
|
601
1582
|
#
|
602
1583
|
def self.json_create: (Hash[String, String] object) -> instance
|
603
1584
|
|
1585
|
+
# <!--
|
1586
|
+
# rdoc-file=ext/json/lib/json/add/symbol.rb
|
1587
|
+
# - as_json(*)
|
1588
|
+
# -->
|
604
1589
|
# Returns a hash, that will be turned into a JSON object and represent this
|
605
1590
|
# object.
|
606
1591
|
#
|
607
1592
|
def as_json: (*untyped) -> Hash[String, String]
|
608
1593
|
|
1594
|
+
# <!--
|
1595
|
+
# rdoc-file=ext/json/lib/json/add/symbol.rb
|
1596
|
+
# - to_json(*a)
|
1597
|
+
# -->
|
609
1598
|
# Stores class name (Symbol) with String representation of Symbol as a JSON
|
610
1599
|
# string.
|
611
1600
|
#
|
612
1601
|
def to_json: (?JSON::State state) -> String
|
613
1602
|
end
|
614
1603
|
|
1604
|
+
%a{annotate:rdoc:skip}
|
615
1605
|
class Time
|
1606
|
+
# <!--
|
1607
|
+
# rdoc-file=ext/json/lib/json/add/time.rb
|
1608
|
+
# - json_create(object)
|
1609
|
+
# -->
|
616
1610
|
# Deserializes JSON string by converting time since epoch to Time
|
617
1611
|
#
|
618
1612
|
def self.json_create: (Hash[String, String | Integer] object) -> instance
|
619
1613
|
|
1614
|
+
# <!--
|
1615
|
+
# rdoc-file=ext/json/lib/json/add/time.rb
|
1616
|
+
# - as_json(*)
|
1617
|
+
# -->
|
620
1618
|
# Returns a hash, that will be turned into a JSON object and represent this
|
621
1619
|
# object.
|
622
1620
|
#
|
623
1621
|
def as_json: (*untyped) -> Hash[String, String | Integer]
|
624
1622
|
|
1623
|
+
# <!--
|
1624
|
+
# rdoc-file=ext/json/lib/json/add/time.rb
|
1625
|
+
# - to_json(*args)
|
1626
|
+
# -->
|
625
1627
|
# Stores class name (Time) with number of seconds since epoch and number of
|
626
1628
|
# microseconds for Time as JSON string
|
627
1629
|
#
|