ruby-paseto 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG.md +8 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/LICENSE.txt +21 -0
- data/README.md +549 -0
- data/lib/paseto/asn1/algorithm_identifier.rb +17 -0
- data/lib/paseto/asn1/curve_private_key.rb +22 -0
- data/lib/paseto/asn1/ec_private_key.rb +27 -0
- data/lib/paseto/asn1/ecdsa_full_r.rb +26 -0
- data/lib/paseto/asn1/ecdsa_sig_value.rb +23 -0
- data/lib/paseto/asn1/ecdsa_signature.rb +49 -0
- data/lib/paseto/asn1/ed25519_identifier.rb +15 -0
- data/lib/paseto/asn1/named_curve.rb +17 -0
- data/lib/paseto/asn1/one_asymmetric_key.rb +32 -0
- data/lib/paseto/asn1/private_key.rb +17 -0
- data/lib/paseto/asn1/private_key_algorithm_identifier.rb +17 -0
- data/lib/paseto/asn1/public_key.rb +17 -0
- data/lib/paseto/asn1/subject_public_key_info.rb +28 -0
- data/lib/paseto/asn1.rb +101 -0
- data/lib/paseto/asymmetric_key.rb +100 -0
- data/lib/paseto/configuration/box.rb +23 -0
- data/lib/paseto/configuration/decode_configuration.rb +68 -0
- data/lib/paseto/configuration.rb +18 -0
- data/lib/paseto/interface/i_d.rb +23 -0
- data/lib/paseto/interface/key.rb +113 -0
- data/lib/paseto/interface/pbkd.rb +83 -0
- data/lib/paseto/interface/pie.rb +59 -0
- data/lib/paseto/interface/pke.rb +86 -0
- data/lib/paseto/interface/serializer.rb +19 -0
- data/lib/paseto/interface/version.rb +161 -0
- data/lib/paseto/interface/wrapper.rb +20 -0
- data/lib/paseto/operations/i_d.rb +48 -0
- data/lib/paseto/operations/id/i_dv3.rb +20 -0
- data/lib/paseto/operations/id/i_dv4.rb +20 -0
- data/lib/paseto/operations/pbkd/p_b_k_dv3.rb +85 -0
- data/lib/paseto/operations/pbkd/p_b_k_dv4.rb +94 -0
- data/lib/paseto/operations/pbkw.rb +73 -0
- data/lib/paseto/operations/pke/p_k_ev3.rb +97 -0
- data/lib/paseto/operations/pke/p_k_ev4.rb +95 -0
- data/lib/paseto/operations/pke.rb +57 -0
- data/lib/paseto/operations/wrap.rb +29 -0
- data/lib/paseto/paserk.rb +55 -0
- data/lib/paseto/paserk_types.rb +46 -0
- data/lib/paseto/protocol/version3.rb +100 -0
- data/lib/paseto/protocol/version4.rb +99 -0
- data/lib/paseto/result.rb +9 -0
- data/lib/paseto/serializer/optional_json.rb +30 -0
- data/lib/paseto/serializer/raw.rb +23 -0
- data/lib/paseto/sodium/curve_25519.rb +46 -0
- data/lib/paseto/sodium/safe_ed25519_loader.rb +19 -0
- data/lib/paseto/sodium/stream/base.rb +82 -0
- data/lib/paseto/sodium/stream/x_cha_cha20_xor.rb +31 -0
- data/lib/paseto/sodium.rb +5 -0
- data/lib/paseto/symmetric_key.rb +119 -0
- data/lib/paseto/token.rb +127 -0
- data/lib/paseto/token_types.rb +29 -0
- data/lib/paseto/util.rb +105 -0
- data/lib/paseto/v3/local.rb +63 -0
- data/lib/paseto/v3/public.rb +204 -0
- data/lib/paseto/v4/local.rb +56 -0
- data/lib/paseto/v4/public.rb +169 -0
- data/lib/paseto/validator.rb +154 -0
- data/lib/paseto/verifiers/footer.rb +30 -0
- data/lib/paseto/verifiers/payload.rb +42 -0
- data/lib/paseto/verify.rb +48 -0
- data/lib/paseto/version.rb +6 -0
- data/lib/paseto/versions.rb +25 -0
- data/lib/paseto/wrappers/pie/pie_v3.rb +72 -0
- data/lib/paseto/wrappers/pie/pie_v4.rb +72 -0
- data/lib/paseto/wrappers/pie.rb +71 -0
- data/lib/paseto.rb +99 -0
- data/paseto.gemspec +58 -0
- data/sorbet/config +3 -0
- data/sorbet/rbi/annotations/rainbow.rbi +269 -0
- data/sorbet/rbi/gems/ast@2.4.2.rbi +584 -0
- data/sorbet/rbi/gems/diff-lcs@1.5.0.rbi +1083 -0
- data/sorbet/rbi/gems/docile@1.4.0.rbi +376 -0
- data/sorbet/rbi/gems/ffi@1.15.5.rbi +1994 -0
- data/sorbet/rbi/gems/io-console@0.5.11.rbi +8 -0
- data/sorbet/rbi/gems/irb@1.5.1.rbi +342 -0
- data/sorbet/rbi/gems/json@2.6.3.rbi +1541 -0
- data/sorbet/rbi/gems/multi_json@1.15.0.rbi +267 -0
- data/sorbet/rbi/gems/netrc@0.11.0.rbi +158 -0
- data/sorbet/rbi/gems/oj@3.13.23.rbi +603 -0
- data/sorbet/rbi/gems/openssl@3.0.1.rbi +1735 -0
- data/sorbet/rbi/gems/parallel@1.22.1.rbi +277 -0
- data/sorbet/rbi/gems/rainbow@3.1.1.rbi +407 -0
- data/sorbet/rbi/gems/rake@13.0.6.rbi +3021 -0
- data/sorbet/rbi/gems/rbnacl@7.1.1.rbi +3218 -0
- data/sorbet/rbi/gems/regexp_parser@2.6.1.rbi +3481 -0
- data/sorbet/rbi/gems/reline@0.3.1.rbi +8 -0
- data/sorbet/rbi/gems/rexml@3.2.5.rbi +4717 -0
- data/sorbet/rbi/gems/rspec-core@3.12.0.rbi +10887 -0
- data/sorbet/rbi/gems/rspec-expectations@3.12.0.rbi +8090 -0
- data/sorbet/rbi/gems/rspec-mocks@3.12.0.rbi +5300 -0
- data/sorbet/rbi/gems/rspec-support@3.12.0.rbi +1617 -0
- data/sorbet/rbi/gems/rspec@3.12.0.rbi +88 -0
- data/sorbet/rbi/gems/ruby-progressbar@1.11.0.rbi +1239 -0
- data/sorbet/rbi/gems/simplecov-html@0.12.3.rbi +219 -0
- data/sorbet/rbi/gems/simplecov@0.21.2.rbi +2135 -0
- data/sorbet/rbi/gems/simplecov_json_formatter@0.1.4.rbi +8 -0
- data/sorbet/rbi/gems/thor@1.2.1.rbi +3956 -0
- data/sorbet/rbi/gems/timecop@0.9.6.rbi +350 -0
- data/sorbet/rbi/gems/unicode-display_width@2.3.0.rbi +48 -0
- data/sorbet/rbi/gems/webrick@1.7.0.rbi +2555 -0
- data/sorbet/rbi/gems/yard-sorbet@0.7.0.rbi +391 -0
- data/sorbet/rbi/gems/yard@0.9.28.rbi +17816 -0
- data/sorbet/rbi/gems/zeitwerk@2.6.6.rbi +950 -0
- data/sorbet/rbi/shims/multi_json.rbi +19 -0
- data/sorbet/rbi/shims/openssl.rbi +111 -0
- data/sorbet/rbi/shims/rbnacl.rbi +65 -0
- data/sorbet/rbi/shims/zeitwerk.rbi +6 -0
- data/sorbet/rbi/todo.rbi +7 -0
- data/sorbet/tapioca/config.yml +30 -0
- data/sorbet/tapioca/require.rb +12 -0
- metadata +376 -0
@@ -0,0 +1,1541 @@
|
|
1
|
+
# typed: true
|
2
|
+
|
3
|
+
# DO NOT EDIT MANUALLY
|
4
|
+
# This is an autogenerated file for types exported from the `json` gem.
|
5
|
+
# Please instead update this file by running `bin/tapioca gem json`.
|
6
|
+
|
7
|
+
# Extends any Class to include _json_creatable?_ method.
|
8
|
+
#
|
9
|
+
# source://json//lib/json/common.rb#695
|
10
|
+
class Class < ::Module
|
11
|
+
# Returns true if this class can be used to create an instance
|
12
|
+
# from a serialised JSON string. The class has to implement a class
|
13
|
+
# method _json_create_ that expects a hash as first parameter. The hash
|
14
|
+
# should include the required data.
|
15
|
+
#
|
16
|
+
# @return [Boolean]
|
17
|
+
#
|
18
|
+
# source://json//lib/json/common.rb#700
|
19
|
+
def json_creatable?; end
|
20
|
+
end
|
21
|
+
|
22
|
+
# = JavaScript \Object Notation (\JSON)
|
23
|
+
#
|
24
|
+
# \JSON is a lightweight data-interchange format.
|
25
|
+
#
|
26
|
+
# A \JSON value is one of the following:
|
27
|
+
# - Double-quoted text: <tt>"foo"</tt>.
|
28
|
+
# - Number: +1+, +1.0+, +2.0e2+.
|
29
|
+
# - Boolean: +true+, +false+.
|
30
|
+
# - Null: +null+.
|
31
|
+
# - \Array: an ordered list of values, enclosed by square brackets:
|
32
|
+
# ["foo", 1, 1.0, 2.0e2, true, false, null]
|
33
|
+
#
|
34
|
+
# - \Object: a collection of name/value pairs, enclosed by curly braces;
|
35
|
+
# each name is double-quoted text;
|
36
|
+
# the values may be any \JSON values:
|
37
|
+
# {"a": "foo", "b": 1, "c": 1.0, "d": 2.0e2, "e": true, "f": false, "g": null}
|
38
|
+
#
|
39
|
+
# A \JSON array or object may contain nested arrays, objects, and scalars
|
40
|
+
# to any depth:
|
41
|
+
# {"foo": {"bar": 1, "baz": 2}, "bat": [0, 1, 2]}
|
42
|
+
# [{"foo": 0, "bar": 1}, ["baz", 2]]
|
43
|
+
#
|
44
|
+
# == Using \Module \JSON
|
45
|
+
#
|
46
|
+
# To make module \JSON available in your code, begin with:
|
47
|
+
# require 'json'
|
48
|
+
#
|
49
|
+
# All examples here assume that this has been done.
|
50
|
+
#
|
51
|
+
# === Parsing \JSON
|
52
|
+
#
|
53
|
+
# You can parse a \String containing \JSON data using
|
54
|
+
# either of two methods:
|
55
|
+
# - <tt>JSON.parse(source, opts)</tt>
|
56
|
+
# - <tt>JSON.parse!(source, opts)</tt>
|
57
|
+
#
|
58
|
+
# where
|
59
|
+
# - +source+ is a Ruby object.
|
60
|
+
# - +opts+ is a \Hash object containing options
|
61
|
+
# that control both input allowed and output formatting.
|
62
|
+
#
|
63
|
+
# The difference between the two methods
|
64
|
+
# is that JSON.parse! omits some checks
|
65
|
+
# and may not be safe for some +source+ data;
|
66
|
+
# use it only for data from trusted sources.
|
67
|
+
# Use the safer method JSON.parse for less trusted sources.
|
68
|
+
#
|
69
|
+
# ==== Parsing \JSON Arrays
|
70
|
+
#
|
71
|
+
# When +source+ is a \JSON array, JSON.parse by default returns a Ruby \Array:
|
72
|
+
# json = '["foo", 1, 1.0, 2.0e2, true, false, null]'
|
73
|
+
# ruby = JSON.parse(json)
|
74
|
+
# ruby # => ["foo", 1, 1.0, 200.0, true, false, nil]
|
75
|
+
# ruby.class # => Array
|
76
|
+
#
|
77
|
+
# The \JSON array may contain nested arrays, objects, and scalars
|
78
|
+
# to any depth:
|
79
|
+
# json = '[{"foo": 0, "bar": 1}, ["baz", 2]]'
|
80
|
+
# JSON.parse(json) # => [{"foo"=>0, "bar"=>1}, ["baz", 2]]
|
81
|
+
#
|
82
|
+
# ==== Parsing \JSON \Objects
|
83
|
+
#
|
84
|
+
# When the source is a \JSON object, JSON.parse by default returns a Ruby \Hash:
|
85
|
+
# json = '{"a": "foo", "b": 1, "c": 1.0, "d": 2.0e2, "e": true, "f": false, "g": null}'
|
86
|
+
# ruby = JSON.parse(json)
|
87
|
+
# ruby # => {"a"=>"foo", "b"=>1, "c"=>1.0, "d"=>200.0, "e"=>true, "f"=>false, "g"=>nil}
|
88
|
+
# ruby.class # => Hash
|
89
|
+
#
|
90
|
+
# The \JSON object may contain nested arrays, objects, and scalars
|
91
|
+
# to any depth:
|
92
|
+
# json = '{"foo": {"bar": 1, "baz": 2}, "bat": [0, 1, 2]}'
|
93
|
+
# JSON.parse(json) # => {"foo"=>{"bar"=>1, "baz"=>2}, "bat"=>[0, 1, 2]}
|
94
|
+
#
|
95
|
+
# ==== Parsing \JSON Scalars
|
96
|
+
#
|
97
|
+
# When the source is a \JSON scalar (not an array or object),
|
98
|
+
# JSON.parse returns a Ruby scalar.
|
99
|
+
#
|
100
|
+
# \String:
|
101
|
+
# ruby = JSON.parse('"foo"')
|
102
|
+
# ruby # => 'foo'
|
103
|
+
# ruby.class # => String
|
104
|
+
# \Integer:
|
105
|
+
# ruby = JSON.parse('1')
|
106
|
+
# ruby # => 1
|
107
|
+
# ruby.class # => Integer
|
108
|
+
# \Float:
|
109
|
+
# ruby = JSON.parse('1.0')
|
110
|
+
# ruby # => 1.0
|
111
|
+
# ruby.class # => Float
|
112
|
+
# ruby = JSON.parse('2.0e2')
|
113
|
+
# ruby # => 200
|
114
|
+
# ruby.class # => Float
|
115
|
+
# Boolean:
|
116
|
+
# ruby = JSON.parse('true')
|
117
|
+
# ruby # => true
|
118
|
+
# ruby.class # => TrueClass
|
119
|
+
# ruby = JSON.parse('false')
|
120
|
+
# ruby # => false
|
121
|
+
# ruby.class # => FalseClass
|
122
|
+
# Null:
|
123
|
+
# ruby = JSON.parse('null')
|
124
|
+
# ruby # => nil
|
125
|
+
# ruby.class # => NilClass
|
126
|
+
#
|
127
|
+
# ==== Parsing Options
|
128
|
+
#
|
129
|
+
# ====== Input Options
|
130
|
+
#
|
131
|
+
# Option +max_nesting+ (\Integer) specifies the maximum nesting depth allowed;
|
132
|
+
# defaults to +100+; specify +false+ to disable depth checking.
|
133
|
+
#
|
134
|
+
# With the default, +false+:
|
135
|
+
# source = '[0, [1, [2, [3]]]]'
|
136
|
+
# ruby = JSON.parse(source)
|
137
|
+
# ruby # => [0, [1, [2, [3]]]]
|
138
|
+
# Too deep:
|
139
|
+
# # Raises JSON::NestingError (nesting of 2 is too deep):
|
140
|
+
# JSON.parse(source, {max_nesting: 1})
|
141
|
+
# Bad value:
|
142
|
+
# # Raises TypeError (wrong argument type Symbol (expected Fixnum)):
|
143
|
+
# JSON.parse(source, {max_nesting: :foo})
|
144
|
+
#
|
145
|
+
# ---
|
146
|
+
#
|
147
|
+
# Option +allow_nan+ (boolean) specifies whether to allow
|
148
|
+
# NaN, Infinity, and MinusInfinity in +source+;
|
149
|
+
# defaults to +false+.
|
150
|
+
#
|
151
|
+
# With the default, +false+:
|
152
|
+
# # Raises JSON::ParserError (225: unexpected token at '[NaN]'):
|
153
|
+
# JSON.parse('[NaN]')
|
154
|
+
# # Raises JSON::ParserError (232: unexpected token at '[Infinity]'):
|
155
|
+
# JSON.parse('[Infinity]')
|
156
|
+
# # Raises JSON::ParserError (248: unexpected token at '[-Infinity]'):
|
157
|
+
# JSON.parse('[-Infinity]')
|
158
|
+
# Allow:
|
159
|
+
# source = '[NaN, Infinity, -Infinity]'
|
160
|
+
# ruby = JSON.parse(source, {allow_nan: true})
|
161
|
+
# ruby # => [NaN, Infinity, -Infinity]
|
162
|
+
#
|
163
|
+
# ====== Output Options
|
164
|
+
#
|
165
|
+
# Option +symbolize_names+ (boolean) specifies whether returned \Hash keys
|
166
|
+
# should be Symbols;
|
167
|
+
# defaults to +false+ (use Strings).
|
168
|
+
#
|
169
|
+
# With the default, +false+:
|
170
|
+
# source = '{"a": "foo", "b": 1.0, "c": true, "d": false, "e": null}'
|
171
|
+
# ruby = JSON.parse(source)
|
172
|
+
# ruby # => {"a"=>"foo", "b"=>1.0, "c"=>true, "d"=>false, "e"=>nil}
|
173
|
+
# Use Symbols:
|
174
|
+
# ruby = JSON.parse(source, {symbolize_names: true})
|
175
|
+
# ruby # => {:a=>"foo", :b=>1.0, :c=>true, :d=>false, :e=>nil}
|
176
|
+
#
|
177
|
+
# ---
|
178
|
+
#
|
179
|
+
# Option +object_class+ (\Class) specifies the Ruby class to be used
|
180
|
+
# for each \JSON object;
|
181
|
+
# defaults to \Hash.
|
182
|
+
#
|
183
|
+
# With the default, \Hash:
|
184
|
+
# source = '{"a": "foo", "b": 1.0, "c": true, "d": false, "e": null}'
|
185
|
+
# ruby = JSON.parse(source)
|
186
|
+
# ruby.class # => Hash
|
187
|
+
# Use class \OpenStruct:
|
188
|
+
# ruby = JSON.parse(source, {object_class: OpenStruct})
|
189
|
+
# ruby # => #<OpenStruct a="foo", b=1.0, c=true, d=false, e=nil>
|
190
|
+
#
|
191
|
+
# ---
|
192
|
+
#
|
193
|
+
# Option +array_class+ (\Class) specifies the Ruby class to be used
|
194
|
+
# for each \JSON array;
|
195
|
+
# defaults to \Array.
|
196
|
+
#
|
197
|
+
# With the default, \Array:
|
198
|
+
# source = '["foo", 1.0, true, false, null]'
|
199
|
+
# ruby = JSON.parse(source)
|
200
|
+
# ruby.class # => Array
|
201
|
+
# Use class \Set:
|
202
|
+
# ruby = JSON.parse(source, {array_class: Set})
|
203
|
+
# ruby # => #<Set: {"foo", 1.0, true, false, nil}>
|
204
|
+
#
|
205
|
+
# ---
|
206
|
+
#
|
207
|
+
# Option +create_additions+ (boolean) specifies whether to use \JSON additions in parsing.
|
208
|
+
# See {\JSON Additions}[#module-JSON-label-JSON+Additions].
|
209
|
+
#
|
210
|
+
# === Generating \JSON
|
211
|
+
#
|
212
|
+
# To generate a Ruby \String containing \JSON data,
|
213
|
+
# use method <tt>JSON.generate(source, opts)</tt>, where
|
214
|
+
# - +source+ is a Ruby object.
|
215
|
+
# - +opts+ is a \Hash object containing options
|
216
|
+
# that control both input allowed and output formatting.
|
217
|
+
#
|
218
|
+
# ==== Generating \JSON from Arrays
|
219
|
+
#
|
220
|
+
# When the source is a Ruby \Array, JSON.generate returns
|
221
|
+
# a \String containing a \JSON array:
|
222
|
+
# ruby = [0, 's', :foo]
|
223
|
+
# json = JSON.generate(ruby)
|
224
|
+
# json # => '[0,"s","foo"]'
|
225
|
+
#
|
226
|
+
# The Ruby \Array array may contain nested arrays, hashes, and scalars
|
227
|
+
# to any depth:
|
228
|
+
# ruby = [0, [1, 2], {foo: 3, bar: 4}]
|
229
|
+
# json = JSON.generate(ruby)
|
230
|
+
# json # => '[0,[1,2],{"foo":3,"bar":4}]'
|
231
|
+
#
|
232
|
+
# ==== Generating \JSON from Hashes
|
233
|
+
#
|
234
|
+
# When the source is a Ruby \Hash, JSON.generate returns
|
235
|
+
# a \String containing a \JSON object:
|
236
|
+
# ruby = {foo: 0, bar: 's', baz: :bat}
|
237
|
+
# json = JSON.generate(ruby)
|
238
|
+
# json # => '{"foo":0,"bar":"s","baz":"bat"}'
|
239
|
+
#
|
240
|
+
# The Ruby \Hash array may contain nested arrays, hashes, and scalars
|
241
|
+
# to any depth:
|
242
|
+
# ruby = {foo: [0, 1], bar: {baz: 2, bat: 3}, bam: :bad}
|
243
|
+
# json = JSON.generate(ruby)
|
244
|
+
# json # => '{"foo":[0,1],"bar":{"baz":2,"bat":3},"bam":"bad"}'
|
245
|
+
#
|
246
|
+
# ==== Generating \JSON from Other Objects
|
247
|
+
#
|
248
|
+
# When the source is neither an \Array nor a \Hash,
|
249
|
+
# the generated \JSON data depends on the class of the source.
|
250
|
+
#
|
251
|
+
# When the source is a Ruby \Integer or \Float, JSON.generate returns
|
252
|
+
# a \String containing a \JSON number:
|
253
|
+
# JSON.generate(42) # => '42'
|
254
|
+
# JSON.generate(0.42) # => '0.42'
|
255
|
+
#
|
256
|
+
# When the source is a Ruby \String, JSON.generate returns
|
257
|
+
# a \String containing a \JSON string (with double-quotes):
|
258
|
+
# JSON.generate('A string') # => '"A string"'
|
259
|
+
#
|
260
|
+
# When the source is +true+, +false+ or +nil+, JSON.generate returns
|
261
|
+
# a \String containing the corresponding \JSON token:
|
262
|
+
# JSON.generate(true) # => 'true'
|
263
|
+
# JSON.generate(false) # => 'false'
|
264
|
+
# JSON.generate(nil) # => 'null'
|
265
|
+
#
|
266
|
+
# When the source is none of the above, JSON.generate returns
|
267
|
+
# a \String containing a \JSON string representation of the source:
|
268
|
+
# JSON.generate(:foo) # => '"foo"'
|
269
|
+
# JSON.generate(Complex(0, 0)) # => '"0+0i"'
|
270
|
+
# JSON.generate(Dir.new('.')) # => '"#<Dir>"'
|
271
|
+
#
|
272
|
+
# ==== Generating Options
|
273
|
+
#
|
274
|
+
# ====== Input Options
|
275
|
+
#
|
276
|
+
# Option +allow_nan+ (boolean) specifies whether
|
277
|
+
# +NaN+, +Infinity+, and <tt>-Infinity</tt> may be generated;
|
278
|
+
# defaults to +false+.
|
279
|
+
#
|
280
|
+
# With the default, +false+:
|
281
|
+
# # Raises JSON::GeneratorError (920: NaN not allowed in JSON):
|
282
|
+
# JSON.generate(JSON::NaN)
|
283
|
+
# # Raises JSON::GeneratorError (917: Infinity not allowed in JSON):
|
284
|
+
# JSON.generate(JSON::Infinity)
|
285
|
+
# # Raises JSON::GeneratorError (917: -Infinity not allowed in JSON):
|
286
|
+
# JSON.generate(JSON::MinusInfinity)
|
287
|
+
#
|
288
|
+
# Allow:
|
289
|
+
# ruby = [Float::NaN, Float::Infinity, Float::MinusInfinity]
|
290
|
+
# JSON.generate(ruby, allow_nan: true) # => '[NaN,Infinity,-Infinity]'
|
291
|
+
#
|
292
|
+
# ---
|
293
|
+
#
|
294
|
+
# Option +max_nesting+ (\Integer) specifies the maximum nesting depth
|
295
|
+
# in +obj+; defaults to +100+.
|
296
|
+
#
|
297
|
+
# With the default, +100+:
|
298
|
+
# obj = [[[[[[0]]]]]]
|
299
|
+
# JSON.generate(obj) # => '[[[[[[0]]]]]]'
|
300
|
+
#
|
301
|
+
# Too deep:
|
302
|
+
# # Raises JSON::NestingError (nesting of 2 is too deep):
|
303
|
+
# JSON.generate(obj, max_nesting: 2)
|
304
|
+
#
|
305
|
+
# ====== Output Options
|
306
|
+
#
|
307
|
+
# The default formatting options generate the most compact
|
308
|
+
# \JSON data, all on one line and with no whitespace.
|
309
|
+
#
|
310
|
+
# You can use these formatting options to generate
|
311
|
+
# \JSON data in a more open format, using whitespace.
|
312
|
+
# See also JSON.pretty_generate.
|
313
|
+
#
|
314
|
+
# - Option +array_nl+ (\String) specifies a string (usually a newline)
|
315
|
+
# to be inserted after each \JSON array; defaults to the empty \String, <tt>''</tt>.
|
316
|
+
# - Option +object_nl+ (\String) specifies a string (usually a newline)
|
317
|
+
# to be inserted after each \JSON object; defaults to the empty \String, <tt>''</tt>.
|
318
|
+
# - Option +indent+ (\String) specifies the string (usually spaces) to be
|
319
|
+
# used for indentation; defaults to the empty \String, <tt>''</tt>;
|
320
|
+
# defaults to the empty \String, <tt>''</tt>;
|
321
|
+
# has no effect unless options +array_nl+ or +object_nl+ specify newlines.
|
322
|
+
# - Option +space+ (\String) specifies a string (usually a space) to be
|
323
|
+
# inserted after the colon in each \JSON object's pair;
|
324
|
+
# defaults to the empty \String, <tt>''</tt>.
|
325
|
+
# - Option +space_before+ (\String) specifies a string (usually a space) to be
|
326
|
+
# inserted before the colon in each \JSON object's pair;
|
327
|
+
# defaults to the empty \String, <tt>''</tt>.
|
328
|
+
#
|
329
|
+
# In this example, +obj+ is used first to generate the shortest
|
330
|
+
# \JSON data (no whitespace), then again with all formatting options
|
331
|
+
# specified:
|
332
|
+
#
|
333
|
+
# obj = {foo: [:bar, :baz], bat: {bam: 0, bad: 1}}
|
334
|
+
# json = JSON.generate(obj)
|
335
|
+
# puts 'Compact:', json
|
336
|
+
# opts = {
|
337
|
+
# array_nl: "\n",
|
338
|
+
# object_nl: "\n",
|
339
|
+
# indent: ' ',
|
340
|
+
# space_before: ' ',
|
341
|
+
# space: ' '
|
342
|
+
# }
|
343
|
+
# puts 'Open:', JSON.generate(obj, opts)
|
344
|
+
#
|
345
|
+
# Output:
|
346
|
+
# Compact:
|
347
|
+
# {"foo":["bar","baz"],"bat":{"bam":0,"bad":1}}
|
348
|
+
# Open:
|
349
|
+
# {
|
350
|
+
# "foo" : [
|
351
|
+
# "bar",
|
352
|
+
# "baz"
|
353
|
+
# ],
|
354
|
+
# "bat" : {
|
355
|
+
# "bam" : 0,
|
356
|
+
# "bad" : 1
|
357
|
+
# }
|
358
|
+
# }
|
359
|
+
#
|
360
|
+
# == \JSON Additions
|
361
|
+
#
|
362
|
+
# When you "round trip" a non-\String object from Ruby to \JSON and back,
|
363
|
+
# you have a new \String, instead of the object you began with:
|
364
|
+
# ruby0 = Range.new(0, 2)
|
365
|
+
# json = JSON.generate(ruby0)
|
366
|
+
# json # => '0..2"'
|
367
|
+
# ruby1 = JSON.parse(json)
|
368
|
+
# ruby1 # => '0..2'
|
369
|
+
# ruby1.class # => String
|
370
|
+
#
|
371
|
+
# You can use \JSON _additions_ to preserve the original object.
|
372
|
+
# The addition is an extension of a ruby class, so that:
|
373
|
+
# - \JSON.generate stores more information in the \JSON string.
|
374
|
+
# - \JSON.parse, called with option +create_additions+,
|
375
|
+
# uses that information to create a proper Ruby object.
|
376
|
+
#
|
377
|
+
# This example shows a \Range being generated into \JSON
|
378
|
+
# and parsed back into Ruby, both without and with
|
379
|
+
# the addition for \Range:
|
380
|
+
# ruby = Range.new(0, 2)
|
381
|
+
# # This passage does not use the addition for Range.
|
382
|
+
# json0 = JSON.generate(ruby)
|
383
|
+
# ruby0 = JSON.parse(json0)
|
384
|
+
# # This passage uses the addition for Range.
|
385
|
+
# require 'json/add/range'
|
386
|
+
# json1 = JSON.generate(ruby)
|
387
|
+
# ruby1 = JSON.parse(json1, create_additions: true)
|
388
|
+
# # Make a nice display.
|
389
|
+
# display = <<EOT
|
390
|
+
# Generated JSON:
|
391
|
+
# Without addition: #{json0} (#{json0.class})
|
392
|
+
# With addition: #{json1} (#{json1.class})
|
393
|
+
# Parsed JSON:
|
394
|
+
# Without addition: #{ruby0.inspect} (#{ruby0.class})
|
395
|
+
# With addition: #{ruby1.inspect} (#{ruby1.class})
|
396
|
+
# EOT
|
397
|
+
# puts display
|
398
|
+
#
|
399
|
+
# This output shows the different results:
|
400
|
+
# Generated JSON:
|
401
|
+
# Without addition: "0..2" (String)
|
402
|
+
# With addition: {"json_class":"Range","a":[0,2,false]} (String)
|
403
|
+
# Parsed JSON:
|
404
|
+
# Without addition: "0..2" (String)
|
405
|
+
# With addition: 0..2 (Range)
|
406
|
+
#
|
407
|
+
# The \JSON module includes additions for certain classes.
|
408
|
+
# You can also craft custom additions.
|
409
|
+
# See {Custom \JSON Additions}[#module-JSON-label-Custom+JSON+Additions].
|
410
|
+
#
|
411
|
+
# === Built-in Additions
|
412
|
+
#
|
413
|
+
# The \JSON module includes additions for certain classes.
|
414
|
+
# To use an addition, +require+ its source:
|
415
|
+
# - BigDecimal: <tt>require 'json/add/bigdecimal'</tt>
|
416
|
+
# - Complex: <tt>require 'json/add/complex'</tt>
|
417
|
+
# - Date: <tt>require 'json/add/date'</tt>
|
418
|
+
# - DateTime: <tt>require 'json/add/date_time'</tt>
|
419
|
+
# - Exception: <tt>require 'json/add/exception'</tt>
|
420
|
+
# - OpenStruct: <tt>require 'json/add/ostruct'</tt>
|
421
|
+
# - Range: <tt>require 'json/add/range'</tt>
|
422
|
+
# - Rational: <tt>require 'json/add/rational'</tt>
|
423
|
+
# - Regexp: <tt>require 'json/add/regexp'</tt>
|
424
|
+
# - Set: <tt>require 'json/add/set'</tt>
|
425
|
+
# - Struct: <tt>require 'json/add/struct'</tt>
|
426
|
+
# - Symbol: <tt>require 'json/add/symbol'</tt>
|
427
|
+
# - Time: <tt>require 'json/add/time'</tt>
|
428
|
+
#
|
429
|
+
# To reduce punctuation clutter, the examples below
|
430
|
+
# show the generated \JSON via +puts+, rather than the usual +inspect+,
|
431
|
+
#
|
432
|
+
# \BigDecimal:
|
433
|
+
# require 'json/add/bigdecimal'
|
434
|
+
# ruby0 = BigDecimal(0) # 0.0
|
435
|
+
# json = JSON.generate(ruby0) # {"json_class":"BigDecimal","b":"27:0.0"}
|
436
|
+
# ruby1 = JSON.parse(json, create_additions: true) # 0.0
|
437
|
+
# ruby1.class # => BigDecimal
|
438
|
+
#
|
439
|
+
# \Complex:
|
440
|
+
# require 'json/add/complex'
|
441
|
+
# ruby0 = Complex(1+0i) # 1+0i
|
442
|
+
# json = JSON.generate(ruby0) # {"json_class":"Complex","r":1,"i":0}
|
443
|
+
# ruby1 = JSON.parse(json, create_additions: true) # 1+0i
|
444
|
+
# ruby1.class # Complex
|
445
|
+
#
|
446
|
+
# \Date:
|
447
|
+
# require 'json/add/date'
|
448
|
+
# ruby0 = Date.today # 2020-05-02
|
449
|
+
# json = JSON.generate(ruby0) # {"json_class":"Date","y":2020,"m":5,"d":2,"sg":2299161.0}
|
450
|
+
# ruby1 = JSON.parse(json, create_additions: true) # 2020-05-02
|
451
|
+
# ruby1.class # Date
|
452
|
+
#
|
453
|
+
# \DateTime:
|
454
|
+
# require 'json/add/date_time'
|
455
|
+
# ruby0 = DateTime.now # 2020-05-02T10:38:13-05:00
|
456
|
+
# 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}
|
457
|
+
# ruby1 = JSON.parse(json, create_additions: true) # 2020-05-02T10:38:13-05:00
|
458
|
+
# ruby1.class # DateTime
|
459
|
+
#
|
460
|
+
# \Exception (and its subclasses including \RuntimeError):
|
461
|
+
# require 'json/add/exception'
|
462
|
+
# ruby0 = Exception.new('A message') # A message
|
463
|
+
# json = JSON.generate(ruby0) # {"json_class":"Exception","m":"A message","b":null}
|
464
|
+
# ruby1 = JSON.parse(json, create_additions: true) # A message
|
465
|
+
# ruby1.class # Exception
|
466
|
+
# ruby0 = RuntimeError.new('Another message') # Another message
|
467
|
+
# json = JSON.generate(ruby0) # {"json_class":"RuntimeError","m":"Another message","b":null}
|
468
|
+
# ruby1 = JSON.parse(json, create_additions: true) # Another message
|
469
|
+
# ruby1.class # RuntimeError
|
470
|
+
#
|
471
|
+
# \OpenStruct:
|
472
|
+
# require 'json/add/ostruct'
|
473
|
+
# ruby0 = OpenStruct.new(name: 'Matz', language: 'Ruby') # #<OpenStruct name="Matz", language="Ruby">
|
474
|
+
# json = JSON.generate(ruby0) # {"json_class":"OpenStruct","t":{"name":"Matz","language":"Ruby"}}
|
475
|
+
# ruby1 = JSON.parse(json, create_additions: true) # #<OpenStruct name="Matz", language="Ruby">
|
476
|
+
# ruby1.class # OpenStruct
|
477
|
+
#
|
478
|
+
# \Range:
|
479
|
+
# require 'json/add/range'
|
480
|
+
# ruby0 = Range.new(0, 2) # 0..2
|
481
|
+
# json = JSON.generate(ruby0) # {"json_class":"Range","a":[0,2,false]}
|
482
|
+
# ruby1 = JSON.parse(json, create_additions: true) # 0..2
|
483
|
+
# ruby1.class # Range
|
484
|
+
#
|
485
|
+
# \Rational:
|
486
|
+
# require 'json/add/rational'
|
487
|
+
# ruby0 = Rational(1, 3) # 1/3
|
488
|
+
# json = JSON.generate(ruby0) # {"json_class":"Rational","n":1,"d":3}
|
489
|
+
# ruby1 = JSON.parse(json, create_additions: true) # 1/3
|
490
|
+
# ruby1.class # Rational
|
491
|
+
#
|
492
|
+
# \Regexp:
|
493
|
+
# require 'json/add/regexp'
|
494
|
+
# ruby0 = Regexp.new('foo') # (?-mix:foo)
|
495
|
+
# json = JSON.generate(ruby0) # {"json_class":"Regexp","o":0,"s":"foo"}
|
496
|
+
# ruby1 = JSON.parse(json, create_additions: true) # (?-mix:foo)
|
497
|
+
# ruby1.class # Regexp
|
498
|
+
#
|
499
|
+
# \Set:
|
500
|
+
# require 'json/add/set'
|
501
|
+
# ruby0 = Set.new([0, 1, 2]) # #<Set: {0, 1, 2}>
|
502
|
+
# json = JSON.generate(ruby0) # {"json_class":"Set","a":[0,1,2]}
|
503
|
+
# ruby1 = JSON.parse(json, create_additions: true) # #<Set: {0, 1, 2}>
|
504
|
+
# ruby1.class # Set
|
505
|
+
#
|
506
|
+
# \Struct:
|
507
|
+
# require 'json/add/struct'
|
508
|
+
# Customer = Struct.new(:name, :address) # Customer
|
509
|
+
# ruby0 = Customer.new("Dave", "123 Main") # #<struct Customer name="Dave", address="123 Main">
|
510
|
+
# json = JSON.generate(ruby0) # {"json_class":"Customer","v":["Dave","123 Main"]}
|
511
|
+
# ruby1 = JSON.parse(json, create_additions: true) # #<struct Customer name="Dave", address="123 Main">
|
512
|
+
# ruby1.class # Customer
|
513
|
+
#
|
514
|
+
# \Symbol:
|
515
|
+
# require 'json/add/symbol'
|
516
|
+
# ruby0 = :foo # foo
|
517
|
+
# json = JSON.generate(ruby0) # {"json_class":"Symbol","s":"foo"}
|
518
|
+
# ruby1 = JSON.parse(json, create_additions: true) # foo
|
519
|
+
# ruby1.class # Symbol
|
520
|
+
#
|
521
|
+
# \Time:
|
522
|
+
# require 'json/add/time'
|
523
|
+
# ruby0 = Time.now # 2020-05-02 11:28:26 -0500
|
524
|
+
# json = JSON.generate(ruby0) # {"json_class":"Time","s":1588436906,"n":840560000}
|
525
|
+
# ruby1 = JSON.parse(json, create_additions: true) # 2020-05-02 11:28:26 -0500
|
526
|
+
# ruby1.class # Time
|
527
|
+
#
|
528
|
+
#
|
529
|
+
# === Custom \JSON Additions
|
530
|
+
#
|
531
|
+
# In addition to the \JSON additions provided,
|
532
|
+
# you can craft \JSON additions of your own,
|
533
|
+
# either for Ruby built-in classes or for user-defined classes.
|
534
|
+
#
|
535
|
+
# Here's a user-defined class +Foo+:
|
536
|
+
# class Foo
|
537
|
+
# attr_accessor :bar, :baz
|
538
|
+
# def initialize(bar, baz)
|
539
|
+
# self.bar = bar
|
540
|
+
# self.baz = baz
|
541
|
+
# end
|
542
|
+
# end
|
543
|
+
#
|
544
|
+
# Here's the \JSON addition for it:
|
545
|
+
# # Extend class Foo with JSON addition.
|
546
|
+
# class Foo
|
547
|
+
# # Serialize Foo object with its class name and arguments
|
548
|
+
# def to_json(*args)
|
549
|
+
# {
|
550
|
+
# JSON.create_id => self.class.name,
|
551
|
+
# 'a' => [ bar, baz ]
|
552
|
+
# }.to_json(*args)
|
553
|
+
# end
|
554
|
+
# # Deserialize JSON string by constructing new Foo object with arguments.
|
555
|
+
# def self.json_create(object)
|
556
|
+
# new(*object['a'])
|
557
|
+
# end
|
558
|
+
# end
|
559
|
+
#
|
560
|
+
# Demonstration:
|
561
|
+
# require 'json'
|
562
|
+
# # This Foo object has no custom addition.
|
563
|
+
# foo0 = Foo.new(0, 1)
|
564
|
+
# json0 = JSON.generate(foo0)
|
565
|
+
# obj0 = JSON.parse(json0)
|
566
|
+
# # Lood the custom addition.
|
567
|
+
# require_relative 'foo_addition'
|
568
|
+
# # This foo has the custom addition.
|
569
|
+
# foo1 = Foo.new(0, 1)
|
570
|
+
# json1 = JSON.generate(foo1)
|
571
|
+
# obj1 = JSON.parse(json1, create_additions: true)
|
572
|
+
# # Make a nice display.
|
573
|
+
# display = <<EOT
|
574
|
+
# Generated JSON:
|
575
|
+
# Without custom addition: #{json0} (#{json0.class})
|
576
|
+
# With custom addition: #{json1} (#{json1.class})
|
577
|
+
# Parsed JSON:
|
578
|
+
# Without custom addition: #{obj0.inspect} (#{obj0.class})
|
579
|
+
# With custom addition: #{obj1.inspect} (#{obj1.class})
|
580
|
+
# EOT
|
581
|
+
# puts display
|
582
|
+
#
|
583
|
+
# Output:
|
584
|
+
#
|
585
|
+
# Generated JSON:
|
586
|
+
# Without custom addition: "#<Foo:0x0000000006534e80>" (String)
|
587
|
+
# With custom addition: {"json_class":"Foo","a":[0,1]} (String)
|
588
|
+
# Parsed JSON:
|
589
|
+
# Without custom addition: "#<Foo:0x0000000006534e80>" (String)
|
590
|
+
# With custom addition: #<Foo:0x0000000006473bb8 @bar=0, @baz=1> (Foo)
|
591
|
+
#
|
592
|
+
# source://json//lib/json/version.rb#2
|
593
|
+
module JSON
|
594
|
+
private
|
595
|
+
|
596
|
+
# :call-seq:
|
597
|
+
# JSON.dump(obj, io = nil, limit = nil)
|
598
|
+
#
|
599
|
+
# Dumps +obj+ as a \JSON string, i.e. calls generate on the object and returns the result.
|
600
|
+
#
|
601
|
+
# The default options can be changed via method JSON.dump_default_options.
|
602
|
+
#
|
603
|
+
# - Argument +io+, if given, should respond to method +write+;
|
604
|
+
# the \JSON \String is written to +io+, and +io+ is returned.
|
605
|
+
# If +io+ is not given, the \JSON \String is returned.
|
606
|
+
# - Argument +limit+, if given, is passed to JSON.generate as option +max_nesting+.
|
607
|
+
#
|
608
|
+
# ---
|
609
|
+
#
|
610
|
+
# When argument +io+ is not given, returns the \JSON \String generated from +obj+:
|
611
|
+
# obj = {foo: [0, 1], bar: {baz: 2, bat: 3}, bam: :bad}
|
612
|
+
# json = JSON.dump(obj)
|
613
|
+
# json # => "{\"foo\":[0,1],\"bar\":{\"baz\":2,\"bat\":3},\"bam\":\"bad\"}"
|
614
|
+
#
|
615
|
+
# When argument +io+ is given, writes the \JSON \String to +io+ and returns +io+:
|
616
|
+
# path = 't.json'
|
617
|
+
# File.open(path, 'w') do |file|
|
618
|
+
# JSON.dump(obj, file)
|
619
|
+
# end # => #<File:t.json (closed)>
|
620
|
+
# puts File.read(path)
|
621
|
+
# Output:
|
622
|
+
# {"foo":[0,1],"bar":{"baz":2,"bat":3},"bam":"bad"}
|
623
|
+
#
|
624
|
+
# source://json//lib/json/common.rb#631
|
625
|
+
def dump(obj, anIO = T.unsafe(nil), limit = T.unsafe(nil)); end
|
626
|
+
|
627
|
+
# :call-seq:
|
628
|
+
# JSON.fast_generate(obj, opts) -> new_string
|
629
|
+
#
|
630
|
+
# Arguments +obj+ and +opts+ here are the same as
|
631
|
+
# arguments +obj+ and +opts+ in JSON.generate.
|
632
|
+
#
|
633
|
+
# By default, generates \JSON data without checking
|
634
|
+
# for circular references in +obj+ (option +max_nesting+ set to +false+, disabled).
|
635
|
+
#
|
636
|
+
# Raises an exception if +obj+ contains circular references:
|
637
|
+
# a = []; b = []; a.push(b); b.push(a)
|
638
|
+
# # Raises SystemStackError (stack level too deep):
|
639
|
+
# JSON.fast_generate(a)
|
640
|
+
#
|
641
|
+
# source://json//lib/json/common.rb#335
|
642
|
+
def fast_generate(obj, opts = T.unsafe(nil)); end
|
643
|
+
|
644
|
+
# :stopdoc:
|
645
|
+
# I want to deprecate these later, so I'll first be silent about them, and later delete them.
|
646
|
+
#
|
647
|
+
# source://json//lib/json/common.rb#335
|
648
|
+
def fast_unparse(obj, opts = T.unsafe(nil)); end
|
649
|
+
|
650
|
+
# :call-seq:
|
651
|
+
# JSON.generate(obj, opts = nil) -> new_string
|
652
|
+
#
|
653
|
+
# Returns a \String containing the generated \JSON data.
|
654
|
+
#
|
655
|
+
# See also JSON.fast_generate, JSON.pretty_generate.
|
656
|
+
#
|
657
|
+
# Argument +obj+ is the Ruby object to be converted to \JSON.
|
658
|
+
#
|
659
|
+
# Argument +opts+, if given, contains a \Hash of options for the generation.
|
660
|
+
# See {Generating Options}[#module-JSON-label-Generating+Options].
|
661
|
+
#
|
662
|
+
# ---
|
663
|
+
#
|
664
|
+
# When +obj+ is an \Array, returns a \String containing a \JSON array:
|
665
|
+
# obj = ["foo", 1.0, true, false, nil]
|
666
|
+
# json = JSON.generate(obj)
|
667
|
+
# json # => '["foo",1.0,true,false,null]'
|
668
|
+
#
|
669
|
+
# When +obj+ is a \Hash, returns a \String containing a \JSON object:
|
670
|
+
# obj = {foo: 0, bar: 's', baz: :bat}
|
671
|
+
# json = JSON.generate(obj)
|
672
|
+
# json # => '{"foo":0,"bar":"s","baz":"bat"}'
|
673
|
+
#
|
674
|
+
# For examples of generating from other Ruby objects, see
|
675
|
+
# {Generating \JSON from Other Objects}[#module-JSON-label-Generating+JSON+from+Other+Objects].
|
676
|
+
#
|
677
|
+
# ---
|
678
|
+
#
|
679
|
+
# Raises an exception if any formatting option is not a \String.
|
680
|
+
#
|
681
|
+
# Raises an exception if +obj+ contains circular references:
|
682
|
+
# a = []; b = []; a.push(b); b.push(a)
|
683
|
+
# # Raises JSON::NestingError (nesting of 100 is too deep):
|
684
|
+
# JSON.generate(a)
|
685
|
+
#
|
686
|
+
# source://json//lib/json/common.rb#296
|
687
|
+
def generate(obj, opts = T.unsafe(nil)); end
|
688
|
+
|
689
|
+
# :call-seq:
|
690
|
+
# JSON.load(source, proc = nil, options = {}) -> object
|
691
|
+
#
|
692
|
+
# Returns the Ruby objects created by parsing the given +source+.
|
693
|
+
#
|
694
|
+
# - Argument +source+ must be, or be convertible to, a \String:
|
695
|
+
# - If +source+ responds to instance method +to_str+,
|
696
|
+
# <tt>source.to_str</tt> becomes the source.
|
697
|
+
# - If +source+ responds to instance method +to_io+,
|
698
|
+
# <tt>source.to_io.read</tt> becomes the source.
|
699
|
+
# - If +source+ responds to instance method +read+,
|
700
|
+
# <tt>source.read</tt> becomes the source.
|
701
|
+
# - If both of the following are true, source becomes the \String <tt>'null'</tt>:
|
702
|
+
# - Option +allow_blank+ specifies a truthy value.
|
703
|
+
# - The source, as defined above, is +nil+ or the empty \String <tt>''</tt>.
|
704
|
+
# - Otherwise, +source+ remains the source.
|
705
|
+
# - Argument +proc+, if given, must be a \Proc that accepts one argument.
|
706
|
+
# It will be called recursively with each result (depth-first order).
|
707
|
+
# See details below.
|
708
|
+
# BEWARE: This method is meant to serialise data from trusted user input,
|
709
|
+
# like from your own database server or clients under your control, it could
|
710
|
+
# be dangerous to allow untrusted users to pass JSON sources into it.
|
711
|
+
# - Argument +opts+, if given, contains a \Hash of options for the parsing.
|
712
|
+
# See {Parsing Options}[#module-JSON-label-Parsing+Options].
|
713
|
+
# The default options can be changed via method JSON.load_default_options=.
|
714
|
+
#
|
715
|
+
# ---
|
716
|
+
#
|
717
|
+
# When no +proc+ is given, modifies +source+ as above and returns the result of
|
718
|
+
# <tt>parse(source, opts)</tt>; see #parse.
|
719
|
+
#
|
720
|
+
# Source for following examples:
|
721
|
+
# source = <<-EOT
|
722
|
+
# {
|
723
|
+
# "name": "Dave",
|
724
|
+
# "age" :40,
|
725
|
+
# "hats": [
|
726
|
+
# "Cattleman's",
|
727
|
+
# "Panama",
|
728
|
+
# "Tophat"
|
729
|
+
# ]
|
730
|
+
# }
|
731
|
+
# EOT
|
732
|
+
#
|
733
|
+
# Load a \String:
|
734
|
+
# ruby = JSON.load(source)
|
735
|
+
# ruby # => {"name"=>"Dave", "age"=>40, "hats"=>["Cattleman's", "Panama", "Tophat"]}
|
736
|
+
#
|
737
|
+
# Load an \IO object:
|
738
|
+
# require 'stringio'
|
739
|
+
# object = JSON.load(StringIO.new(source))
|
740
|
+
# object # => {"name"=>"Dave", "age"=>40, "hats"=>["Cattleman's", "Panama", "Tophat"]}
|
741
|
+
#
|
742
|
+
# Load a \File object:
|
743
|
+
# path = 't.json'
|
744
|
+
# File.write(path, source)
|
745
|
+
# File.open(path) do |file|
|
746
|
+
# JSON.load(file)
|
747
|
+
# end # => {"name"=>"Dave", "age"=>40, "hats"=>["Cattleman's", "Panama", "Tophat"]}
|
748
|
+
#
|
749
|
+
# ---
|
750
|
+
#
|
751
|
+
# When +proc+ is given:
|
752
|
+
# - Modifies +source+ as above.
|
753
|
+
# - Gets the +result+ from calling <tt>parse(source, opts)</tt>.
|
754
|
+
# - Recursively calls <tt>proc(result)</tt>.
|
755
|
+
# - Returns the final result.
|
756
|
+
#
|
757
|
+
# Example:
|
758
|
+
# require 'json'
|
759
|
+
#
|
760
|
+
# # Some classes for the example.
|
761
|
+
# class Base
|
762
|
+
# def initialize(attributes)
|
763
|
+
# @attributes = attributes
|
764
|
+
# end
|
765
|
+
# end
|
766
|
+
# class User < Base; end
|
767
|
+
# class Account < Base; end
|
768
|
+
# class Admin < Base; end
|
769
|
+
# # The JSON source.
|
770
|
+
# json = <<-EOF
|
771
|
+
# {
|
772
|
+
# "users": [
|
773
|
+
# {"type": "User", "username": "jane", "email": "jane@example.com"},
|
774
|
+
# {"type": "User", "username": "john", "email": "john@example.com"}
|
775
|
+
# ],
|
776
|
+
# "accounts": [
|
777
|
+
# {"account": {"type": "Account", "paid": true, "account_id": "1234"}},
|
778
|
+
# {"account": {"type": "Account", "paid": false, "account_id": "1235"}}
|
779
|
+
# ],
|
780
|
+
# "admins": {"type": "Admin", "password": "0wn3d"}
|
781
|
+
# }
|
782
|
+
# EOF
|
783
|
+
# # Deserializer method.
|
784
|
+
# def deserialize_obj(obj, safe_types = %w(User Account Admin))
|
785
|
+
# type = obj.is_a?(Hash) && obj["type"]
|
786
|
+
# safe_types.include?(type) ? Object.const_get(type).new(obj) : obj
|
787
|
+
# end
|
788
|
+
# # Call to JSON.load
|
789
|
+
# ruby = JSON.load(json, proc {|obj|
|
790
|
+
# case obj
|
791
|
+
# when Hash
|
792
|
+
# obj.each {|k, v| obj[k] = deserialize_obj v }
|
793
|
+
# when Array
|
794
|
+
# obj.map! {|v| deserialize_obj v }
|
795
|
+
# end
|
796
|
+
# })
|
797
|
+
# pp ruby
|
798
|
+
# Output:
|
799
|
+
# {"users"=>
|
800
|
+
# [#<User:0x00000000064c4c98
|
801
|
+
# @attributes=
|
802
|
+
# {"type"=>"User", "username"=>"jane", "email"=>"jane@example.com"}>,
|
803
|
+
# #<User:0x00000000064c4bd0
|
804
|
+
# @attributes=
|
805
|
+
# {"type"=>"User", "username"=>"john", "email"=>"john@example.com"}>],
|
806
|
+
# "accounts"=>
|
807
|
+
# [{"account"=>
|
808
|
+
# #<Account:0x00000000064c4928
|
809
|
+
# @attributes={"type"=>"Account", "paid"=>true, "account_id"=>"1234"}>},
|
810
|
+
# {"account"=>
|
811
|
+
# #<Account:0x00000000064c4680
|
812
|
+
# @attributes={"type"=>"Account", "paid"=>false, "account_id"=>"1235"}>}],
|
813
|
+
# "admins"=>
|
814
|
+
# #<Admin:0x00000000064c41f8
|
815
|
+
# @attributes={"type"=>"Admin", "password"=>"0wn3d"}>}
|
816
|
+
#
|
817
|
+
# source://json//lib/json/common.rb#557
|
818
|
+
def load(source, proc = T.unsafe(nil), options = T.unsafe(nil)); end
|
819
|
+
|
820
|
+
# :call-seq:
|
821
|
+
# JSON.load_file(path, opts={}) -> object
|
822
|
+
#
|
823
|
+
# Calls:
|
824
|
+
# parse(File.read(path), opts)
|
825
|
+
#
|
826
|
+
# See method #parse.
|
827
|
+
#
|
828
|
+
# source://json//lib/json/common.rb#245
|
829
|
+
def load_file(filespec, opts = T.unsafe(nil)); end
|
830
|
+
|
831
|
+
# :call-seq:
|
832
|
+
# JSON.load_file!(path, opts = {})
|
833
|
+
#
|
834
|
+
# Calls:
|
835
|
+
# JSON.parse!(File.read(path, opts))
|
836
|
+
#
|
837
|
+
# See method #parse!
|
838
|
+
#
|
839
|
+
# source://json//lib/json/common.rb#256
|
840
|
+
def load_file!(filespec, opts = T.unsafe(nil)); end
|
841
|
+
|
842
|
+
# :call-seq:
|
843
|
+
# JSON.parse(source, opts) -> object
|
844
|
+
#
|
845
|
+
# Returns the Ruby objects created by parsing the given +source+.
|
846
|
+
#
|
847
|
+
# Argument +source+ contains the \String to be parsed.
|
848
|
+
#
|
849
|
+
# Argument +opts+, if given, contains a \Hash of options for the parsing.
|
850
|
+
# See {Parsing Options}[#module-JSON-label-Parsing+Options].
|
851
|
+
#
|
852
|
+
# ---
|
853
|
+
#
|
854
|
+
# When +source+ is a \JSON array, returns a Ruby \Array:
|
855
|
+
# source = '["foo", 1.0, true, false, null]'
|
856
|
+
# ruby = JSON.parse(source)
|
857
|
+
# ruby # => ["foo", 1.0, true, false, nil]
|
858
|
+
# ruby.class # => Array
|
859
|
+
#
|
860
|
+
# When +source+ is a \JSON object, returns a Ruby \Hash:
|
861
|
+
# source = '{"a": "foo", "b": 1.0, "c": true, "d": false, "e": null}'
|
862
|
+
# ruby = JSON.parse(source)
|
863
|
+
# ruby # => {"a"=>"foo", "b"=>1.0, "c"=>true, "d"=>false, "e"=>nil}
|
864
|
+
# ruby.class # => Hash
|
865
|
+
#
|
866
|
+
# For examples of parsing for all \JSON data types, see
|
867
|
+
# {Parsing \JSON}[#module-JSON-label-Parsing+JSON].
|
868
|
+
#
|
869
|
+
# Parses nested JSON objects:
|
870
|
+
# source = <<-EOT
|
871
|
+
# {
|
872
|
+
# "name": "Dave",
|
873
|
+
# "age" :40,
|
874
|
+
# "hats": [
|
875
|
+
# "Cattleman's",
|
876
|
+
# "Panama",
|
877
|
+
# "Tophat"
|
878
|
+
# ]
|
879
|
+
# }
|
880
|
+
# EOT
|
881
|
+
# ruby = JSON.parse(source)
|
882
|
+
# ruby # => {"name"=>"Dave", "age"=>40, "hats"=>["Cattleman's", "Panama", "Tophat"]}
|
883
|
+
#
|
884
|
+
# ---
|
885
|
+
#
|
886
|
+
# Raises an exception if +source+ is not valid JSON:
|
887
|
+
# # Raises JSON::ParserError (783: unexpected token at ''):
|
888
|
+
# JSON.parse('')
|
889
|
+
#
|
890
|
+
# source://json//lib/json/common.rb#215
|
891
|
+
def parse(source, opts = T.unsafe(nil)); end
|
892
|
+
|
893
|
+
# :call-seq:
|
894
|
+
# JSON.parse!(source, opts) -> object
|
895
|
+
#
|
896
|
+
# Calls
|
897
|
+
# parse(source, opts)
|
898
|
+
# with +source+ and possibly modified +opts+.
|
899
|
+
#
|
900
|
+
# Differences from JSON.parse:
|
901
|
+
# - Option +max_nesting+, if not provided, defaults to +false+,
|
902
|
+
# which disables checking for nesting depth.
|
903
|
+
# - Option +allow_nan+, if not provided, defaults to +true+.
|
904
|
+
#
|
905
|
+
# source://json//lib/json/common.rb#230
|
906
|
+
def parse!(source, opts = T.unsafe(nil)); end
|
907
|
+
|
908
|
+
# :call-seq:
|
909
|
+
# JSON.pretty_generate(obj, opts = nil) -> new_string
|
910
|
+
#
|
911
|
+
# Arguments +obj+ and +opts+ here are the same as
|
912
|
+
# arguments +obj+ and +opts+ in JSON.generate.
|
913
|
+
#
|
914
|
+
# Default options are:
|
915
|
+
# {
|
916
|
+
# indent: ' ', # Two spaces
|
917
|
+
# space: ' ', # One space
|
918
|
+
# array_nl: "\n", # Newline
|
919
|
+
# object_nl: "\n" # Newline
|
920
|
+
# }
|
921
|
+
#
|
922
|
+
# Example:
|
923
|
+
# obj = {foo: [:bar, :baz], bat: {bam: 0, bad: 1}}
|
924
|
+
# json = JSON.pretty_generate(obj)
|
925
|
+
# puts json
|
926
|
+
# Output:
|
927
|
+
# {
|
928
|
+
# "foo": [
|
929
|
+
# "bar",
|
930
|
+
# "baz"
|
931
|
+
# ],
|
932
|
+
# "bat": {
|
933
|
+
# "bam": 0,
|
934
|
+
# "bad": 1
|
935
|
+
# }
|
936
|
+
# }
|
937
|
+
#
|
938
|
+
# source://json//lib/json/common.rb#390
|
939
|
+
def pretty_generate(obj, opts = T.unsafe(nil)); end
|
940
|
+
|
941
|
+
# :stopdoc:
|
942
|
+
# I want to deprecate these later, so I'll first be silent about them, and later delete them.
|
943
|
+
#
|
944
|
+
# source://json//lib/json/common.rb#390
|
945
|
+
def pretty_unparse(obj, opts = T.unsafe(nil)); end
|
946
|
+
|
947
|
+
# Recursively calls passed _Proc_ if the parsed data structure is an _Array_ or _Hash_
|
948
|
+
#
|
949
|
+
# source://json//lib/json/common.rb#575
|
950
|
+
def recurse_proc(result, &proc); end
|
951
|
+
|
952
|
+
# source://json//lib/json/common.rb#557
|
953
|
+
def restore(source, proc = T.unsafe(nil), options = T.unsafe(nil)); end
|
954
|
+
|
955
|
+
# :stopdoc:
|
956
|
+
# I want to deprecate these later, so I'll first be silent about them, and
|
957
|
+
# later delete them.
|
958
|
+
#
|
959
|
+
# source://json//lib/json/common.rb#296
|
960
|
+
def unparse(obj, opts = T.unsafe(nil)); end
|
961
|
+
|
962
|
+
class << self
|
963
|
+
# :call-seq:
|
964
|
+
# JSON[object] -> new_array or new_string
|
965
|
+
#
|
966
|
+
# If +object+ is a \String,
|
967
|
+
# calls JSON.parse with +object+ and +opts+ (see method #parse):
|
968
|
+
# json = '[0, 1, null]'
|
969
|
+
# JSON[json]# => [0, 1, nil]
|
970
|
+
#
|
971
|
+
# Otherwise, calls JSON.generate with +object+ and +opts+ (see method #generate):
|
972
|
+
# ruby = [0, 1, nil]
|
973
|
+
# JSON[ruby] # => '[0,1,null]'
|
974
|
+
#
|
975
|
+
# source://json//lib/json/common.rb#18
|
976
|
+
def [](object, opts = T.unsafe(nil)); end
|
977
|
+
|
978
|
+
# source://json//lib/json/common.rb#81
|
979
|
+
def create_fast_state; end
|
980
|
+
|
981
|
+
# Returns the current create identifier.
|
982
|
+
# See also JSON.create_id=.
|
983
|
+
#
|
984
|
+
# source://json//lib/json/common.rb#126
|
985
|
+
def create_id; end
|
986
|
+
|
987
|
+
# Sets create identifier, which is used to decide if the _json_create_
|
988
|
+
# hook of a class should be called; initial value is +json_class+:
|
989
|
+
# JSON.create_id # => 'json_class'
|
990
|
+
#
|
991
|
+
# source://json//lib/json/common.rb#120
|
992
|
+
def create_id=(new_value); end
|
993
|
+
|
994
|
+
# source://json//lib/json/common.rb#91
|
995
|
+
def create_pretty_state; end
|
996
|
+
|
997
|
+
# Return the constant located at _path_. The format of _path_ has to be
|
998
|
+
# either ::A::B::C or A::B::C. In any case, A has to be located at the top
|
999
|
+
# level (absolute namespace path?). If there doesn't exist a constant at
|
1000
|
+
# the given path, an ArgumentError is raised.
|
1001
|
+
#
|
1002
|
+
# source://json//lib/json/common.rb#42
|
1003
|
+
def deep_const_get(path); end
|
1004
|
+
|
1005
|
+
# :call-seq:
|
1006
|
+
# JSON.dump(obj, io = nil, limit = nil)
|
1007
|
+
#
|
1008
|
+
# Dumps +obj+ as a \JSON string, i.e. calls generate on the object and returns the result.
|
1009
|
+
#
|
1010
|
+
# The default options can be changed via method JSON.dump_default_options.
|
1011
|
+
#
|
1012
|
+
# - Argument +io+, if given, should respond to method +write+;
|
1013
|
+
# the \JSON \String is written to +io+, and +io+ is returned.
|
1014
|
+
# If +io+ is not given, the \JSON \String is returned.
|
1015
|
+
# - Argument +limit+, if given, is passed to JSON.generate as option +max_nesting+.
|
1016
|
+
#
|
1017
|
+
# ---
|
1018
|
+
#
|
1019
|
+
# When argument +io+ is not given, returns the \JSON \String generated from +obj+:
|
1020
|
+
# obj = {foo: [0, 1], bar: {baz: 2, bat: 3}, bam: :bad}
|
1021
|
+
# json = JSON.dump(obj)
|
1022
|
+
# json # => "{\"foo\":[0,1],\"bar\":{\"baz\":2,\"bat\":3},\"bam\":\"bad\"}"
|
1023
|
+
#
|
1024
|
+
# When argument +io+ is given, writes the \JSON \String to +io+ and returns +io+:
|
1025
|
+
# path = 't.json'
|
1026
|
+
# File.open(path, 'w') do |file|
|
1027
|
+
# JSON.dump(obj, file)
|
1028
|
+
# end # => #<File:t.json (closed)>
|
1029
|
+
# puts File.read(path)
|
1030
|
+
# Output:
|
1031
|
+
# {"foo":[0,1],"bar":{"baz":2,"bat":3},"bam":"bad"}
|
1032
|
+
#
|
1033
|
+
# source://json//lib/json/common.rb#631
|
1034
|
+
def dump(obj, anIO = T.unsafe(nil), limit = T.unsafe(nil)); end
|
1035
|
+
|
1036
|
+
# Sets or returns the default options for the JSON.dump method.
|
1037
|
+
# Initially:
|
1038
|
+
# opts = JSON.dump_default_options
|
1039
|
+
# opts # => {:max_nesting=>false, :allow_nan=>true, :escape_slash=>false}
|
1040
|
+
#
|
1041
|
+
# source://json//lib/json/common.rb#596
|
1042
|
+
def dump_default_options; end
|
1043
|
+
|
1044
|
+
# Sets or returns the default options for the JSON.dump method.
|
1045
|
+
# Initially:
|
1046
|
+
# opts = JSON.dump_default_options
|
1047
|
+
# opts # => {:max_nesting=>false, :allow_nan=>true, :escape_slash=>false}
|
1048
|
+
#
|
1049
|
+
# source://json//lib/json/common.rb#596
|
1050
|
+
def dump_default_options=(_arg0); end
|
1051
|
+
|
1052
|
+
# :call-seq:
|
1053
|
+
# JSON.fast_generate(obj, opts) -> new_string
|
1054
|
+
#
|
1055
|
+
# Arguments +obj+ and +opts+ here are the same as
|
1056
|
+
# arguments +obj+ and +opts+ in JSON.generate.
|
1057
|
+
#
|
1058
|
+
# By default, generates \JSON data without checking
|
1059
|
+
# for circular references in +obj+ (option +max_nesting+ set to +false+, disabled).
|
1060
|
+
#
|
1061
|
+
# Raises an exception if +obj+ contains circular references:
|
1062
|
+
# a = []; b = []; a.push(b); b.push(a)
|
1063
|
+
# # Raises SystemStackError (stack level too deep):
|
1064
|
+
# JSON.fast_generate(a)
|
1065
|
+
#
|
1066
|
+
# source://json//lib/json/common.rb#335
|
1067
|
+
def fast_generate(obj, opts = T.unsafe(nil)); end
|
1068
|
+
|
1069
|
+
# :stopdoc:
|
1070
|
+
# I want to deprecate these later, so I'll first be silent about them, and later delete them.
|
1071
|
+
#
|
1072
|
+
# source://json//lib/json/common.rb#335
|
1073
|
+
def fast_unparse(obj, opts = T.unsafe(nil)); end
|
1074
|
+
|
1075
|
+
# :call-seq:
|
1076
|
+
# JSON.generate(obj, opts = nil) -> new_string
|
1077
|
+
#
|
1078
|
+
# Returns a \String containing the generated \JSON data.
|
1079
|
+
#
|
1080
|
+
# See also JSON.fast_generate, JSON.pretty_generate.
|
1081
|
+
#
|
1082
|
+
# Argument +obj+ is the Ruby object to be converted to \JSON.
|
1083
|
+
#
|
1084
|
+
# Argument +opts+, if given, contains a \Hash of options for the generation.
|
1085
|
+
# See {Generating Options}[#module-JSON-label-Generating+Options].
|
1086
|
+
#
|
1087
|
+
# ---
|
1088
|
+
#
|
1089
|
+
# When +obj+ is an \Array, returns a \String containing a \JSON array:
|
1090
|
+
# obj = ["foo", 1.0, true, false, nil]
|
1091
|
+
# json = JSON.generate(obj)
|
1092
|
+
# json # => '["foo",1.0,true,false,null]'
|
1093
|
+
#
|
1094
|
+
# When +obj+ is a \Hash, returns a \String containing a \JSON object:
|
1095
|
+
# obj = {foo: 0, bar: 's', baz: :bat}
|
1096
|
+
# json = JSON.generate(obj)
|
1097
|
+
# json # => '{"foo":0,"bar":"s","baz":"bat"}'
|
1098
|
+
#
|
1099
|
+
# For examples of generating from other Ruby objects, see
|
1100
|
+
# {Generating \JSON from Other Objects}[#module-JSON-label-Generating+JSON+from+Other+Objects].
|
1101
|
+
#
|
1102
|
+
# ---
|
1103
|
+
#
|
1104
|
+
# Raises an exception if any formatting option is not a \String.
|
1105
|
+
#
|
1106
|
+
# Raises an exception if +obj+ contains circular references:
|
1107
|
+
# a = []; b = []; a.push(b); b.push(a)
|
1108
|
+
# # Raises JSON::NestingError (nesting of 100 is too deep):
|
1109
|
+
# JSON.generate(a)
|
1110
|
+
#
|
1111
|
+
# source://json//lib/json/common.rb#296
|
1112
|
+
def generate(obj, opts = T.unsafe(nil)); end
|
1113
|
+
|
1114
|
+
# Returns the JSON generator module that is used by JSON. This is
|
1115
|
+
# either JSON::Ext::Generator or JSON::Pure::Generator:
|
1116
|
+
# JSON.generator # => JSON::Ext::Generator
|
1117
|
+
#
|
1118
|
+
# source://json//lib/json/common.rb#103
|
1119
|
+
def generator; end
|
1120
|
+
|
1121
|
+
# Set the module _generator_ to be used by JSON.
|
1122
|
+
#
|
1123
|
+
# source://json//lib/json/common.rb#58
|
1124
|
+
def generator=(generator); end
|
1125
|
+
|
1126
|
+
# Encodes string using String.encode.
|
1127
|
+
#
|
1128
|
+
# source://json//lib/json/common.rb#653
|
1129
|
+
def iconv(to, from, string); end
|
1130
|
+
|
1131
|
+
# :call-seq:
|
1132
|
+
# JSON.load(source, proc = nil, options = {}) -> object
|
1133
|
+
#
|
1134
|
+
# Returns the Ruby objects created by parsing the given +source+.
|
1135
|
+
#
|
1136
|
+
# - Argument +source+ must be, or be convertible to, a \String:
|
1137
|
+
# - If +source+ responds to instance method +to_str+,
|
1138
|
+
# <tt>source.to_str</tt> becomes the source.
|
1139
|
+
# - If +source+ responds to instance method +to_io+,
|
1140
|
+
# <tt>source.to_io.read</tt> becomes the source.
|
1141
|
+
# - If +source+ responds to instance method +read+,
|
1142
|
+
# <tt>source.read</tt> becomes the source.
|
1143
|
+
# - If both of the following are true, source becomes the \String <tt>'null'</tt>:
|
1144
|
+
# - Option +allow_blank+ specifies a truthy value.
|
1145
|
+
# - The source, as defined above, is +nil+ or the empty \String <tt>''</tt>.
|
1146
|
+
# - Otherwise, +source+ remains the source.
|
1147
|
+
# - Argument +proc+, if given, must be a \Proc that accepts one argument.
|
1148
|
+
# It will be called recursively with each result (depth-first order).
|
1149
|
+
# See details below.
|
1150
|
+
# BEWARE: This method is meant to serialise data from trusted user input,
|
1151
|
+
# like from your own database server or clients under your control, it could
|
1152
|
+
# be dangerous to allow untrusted users to pass JSON sources into it.
|
1153
|
+
# - Argument +opts+, if given, contains a \Hash of options for the parsing.
|
1154
|
+
# See {Parsing Options}[#module-JSON-label-Parsing+Options].
|
1155
|
+
# The default options can be changed via method JSON.load_default_options=.
|
1156
|
+
#
|
1157
|
+
# ---
|
1158
|
+
#
|
1159
|
+
# When no +proc+ is given, modifies +source+ as above and returns the result of
|
1160
|
+
# <tt>parse(source, opts)</tt>; see #parse.
|
1161
|
+
#
|
1162
|
+
# Source for following examples:
|
1163
|
+
# source = <<-EOT
|
1164
|
+
# {
|
1165
|
+
# "name": "Dave",
|
1166
|
+
# "age" :40,
|
1167
|
+
# "hats": [
|
1168
|
+
# "Cattleman's",
|
1169
|
+
# "Panama",
|
1170
|
+
# "Tophat"
|
1171
|
+
# ]
|
1172
|
+
# }
|
1173
|
+
# EOT
|
1174
|
+
#
|
1175
|
+
# Load a \String:
|
1176
|
+
# ruby = JSON.load(source)
|
1177
|
+
# ruby # => {"name"=>"Dave", "age"=>40, "hats"=>["Cattleman's", "Panama", "Tophat"]}
|
1178
|
+
#
|
1179
|
+
# Load an \IO object:
|
1180
|
+
# require 'stringio'
|
1181
|
+
# object = JSON.load(StringIO.new(source))
|
1182
|
+
# object # => {"name"=>"Dave", "age"=>40, "hats"=>["Cattleman's", "Panama", "Tophat"]}
|
1183
|
+
#
|
1184
|
+
# Load a \File object:
|
1185
|
+
# path = 't.json'
|
1186
|
+
# File.write(path, source)
|
1187
|
+
# File.open(path) do |file|
|
1188
|
+
# JSON.load(file)
|
1189
|
+
# end # => {"name"=>"Dave", "age"=>40, "hats"=>["Cattleman's", "Panama", "Tophat"]}
|
1190
|
+
#
|
1191
|
+
# ---
|
1192
|
+
#
|
1193
|
+
# When +proc+ is given:
|
1194
|
+
# - Modifies +source+ as above.
|
1195
|
+
# - Gets the +result+ from calling <tt>parse(source, opts)</tt>.
|
1196
|
+
# - Recursively calls <tt>proc(result)</tt>.
|
1197
|
+
# - Returns the final result.
|
1198
|
+
#
|
1199
|
+
# Example:
|
1200
|
+
# require 'json'
|
1201
|
+
#
|
1202
|
+
# # Some classes for the example.
|
1203
|
+
# class Base
|
1204
|
+
# def initialize(attributes)
|
1205
|
+
# @attributes = attributes
|
1206
|
+
# end
|
1207
|
+
# end
|
1208
|
+
# class User < Base; end
|
1209
|
+
# class Account < Base; end
|
1210
|
+
# class Admin < Base; end
|
1211
|
+
# # The JSON source.
|
1212
|
+
# json = <<-EOF
|
1213
|
+
# {
|
1214
|
+
# "users": [
|
1215
|
+
# {"type": "User", "username": "jane", "email": "jane@example.com"},
|
1216
|
+
# {"type": "User", "username": "john", "email": "john@example.com"}
|
1217
|
+
# ],
|
1218
|
+
# "accounts": [
|
1219
|
+
# {"account": {"type": "Account", "paid": true, "account_id": "1234"}},
|
1220
|
+
# {"account": {"type": "Account", "paid": false, "account_id": "1235"}}
|
1221
|
+
# ],
|
1222
|
+
# "admins": {"type": "Admin", "password": "0wn3d"}
|
1223
|
+
# }
|
1224
|
+
# EOF
|
1225
|
+
# # Deserializer method.
|
1226
|
+
# def deserialize_obj(obj, safe_types = %w(User Account Admin))
|
1227
|
+
# type = obj.is_a?(Hash) && obj["type"]
|
1228
|
+
# safe_types.include?(type) ? Object.const_get(type).new(obj) : obj
|
1229
|
+
# end
|
1230
|
+
# # Call to JSON.load
|
1231
|
+
# ruby = JSON.load(json, proc {|obj|
|
1232
|
+
# case obj
|
1233
|
+
# when Hash
|
1234
|
+
# obj.each {|k, v| obj[k] = deserialize_obj v }
|
1235
|
+
# when Array
|
1236
|
+
# obj.map! {|v| deserialize_obj v }
|
1237
|
+
# end
|
1238
|
+
# })
|
1239
|
+
# pp ruby
|
1240
|
+
# Output:
|
1241
|
+
# {"users"=>
|
1242
|
+
# [#<User:0x00000000064c4c98
|
1243
|
+
# @attributes=
|
1244
|
+
# {"type"=>"User", "username"=>"jane", "email"=>"jane@example.com"}>,
|
1245
|
+
# #<User:0x00000000064c4bd0
|
1246
|
+
# @attributes=
|
1247
|
+
# {"type"=>"User", "username"=>"john", "email"=>"john@example.com"}>],
|
1248
|
+
# "accounts"=>
|
1249
|
+
# [{"account"=>
|
1250
|
+
# #<Account:0x00000000064c4928
|
1251
|
+
# @attributes={"type"=>"Account", "paid"=>true, "account_id"=>"1234"}>},
|
1252
|
+
# {"account"=>
|
1253
|
+
# #<Account:0x00000000064c4680
|
1254
|
+
# @attributes={"type"=>"Account", "paid"=>false, "account_id"=>"1235"}>}],
|
1255
|
+
# "admins"=>
|
1256
|
+
# #<Admin:0x00000000064c41f8
|
1257
|
+
# @attributes={"type"=>"Admin", "password"=>"0wn3d"}>}
|
1258
|
+
#
|
1259
|
+
# source://json//lib/json/common.rb#557
|
1260
|
+
def load(source, proc = T.unsafe(nil), options = T.unsafe(nil)); end
|
1261
|
+
|
1262
|
+
# Sets or returns default options for the JSON.load method.
|
1263
|
+
# Initially:
|
1264
|
+
# opts = JSON.load_default_options
|
1265
|
+
# opts # => {:max_nesting=>false, :allow_nan=>true, :allow_blank=>true, :create_additions=>true}
|
1266
|
+
#
|
1267
|
+
# source://json//lib/json/common.rb#420
|
1268
|
+
def load_default_options; end
|
1269
|
+
|
1270
|
+
# Sets or returns default options for the JSON.load method.
|
1271
|
+
# Initially:
|
1272
|
+
# opts = JSON.load_default_options
|
1273
|
+
# opts # => {:max_nesting=>false, :allow_nan=>true, :allow_blank=>true, :create_additions=>true}
|
1274
|
+
#
|
1275
|
+
# source://json//lib/json/common.rb#420
|
1276
|
+
def load_default_options=(_arg0); end
|
1277
|
+
|
1278
|
+
# :call-seq:
|
1279
|
+
# JSON.load_file(path, opts={}) -> object
|
1280
|
+
#
|
1281
|
+
# Calls:
|
1282
|
+
# parse(File.read(path), opts)
|
1283
|
+
#
|
1284
|
+
# See method #parse.
|
1285
|
+
#
|
1286
|
+
# source://json//lib/json/common.rb#245
|
1287
|
+
def load_file(filespec, opts = T.unsafe(nil)); end
|
1288
|
+
|
1289
|
+
# :call-seq:
|
1290
|
+
# JSON.load_file!(path, opts = {})
|
1291
|
+
#
|
1292
|
+
# Calls:
|
1293
|
+
# JSON.parse!(File.read(path, opts))
|
1294
|
+
#
|
1295
|
+
# See method #parse!
|
1296
|
+
#
|
1297
|
+
# source://json//lib/json/common.rb#256
|
1298
|
+
def load_file!(filespec, opts = T.unsafe(nil)); end
|
1299
|
+
|
1300
|
+
# :call-seq:
|
1301
|
+
# JSON.parse(source, opts) -> object
|
1302
|
+
#
|
1303
|
+
# Returns the Ruby objects created by parsing the given +source+.
|
1304
|
+
#
|
1305
|
+
# Argument +source+ contains the \String to be parsed.
|
1306
|
+
#
|
1307
|
+
# Argument +opts+, if given, contains a \Hash of options for the parsing.
|
1308
|
+
# See {Parsing Options}[#module-JSON-label-Parsing+Options].
|
1309
|
+
#
|
1310
|
+
# ---
|
1311
|
+
#
|
1312
|
+
# When +source+ is a \JSON array, returns a Ruby \Array:
|
1313
|
+
# source = '["foo", 1.0, true, false, null]'
|
1314
|
+
# ruby = JSON.parse(source)
|
1315
|
+
# ruby # => ["foo", 1.0, true, false, nil]
|
1316
|
+
# ruby.class # => Array
|
1317
|
+
#
|
1318
|
+
# When +source+ is a \JSON object, returns a Ruby \Hash:
|
1319
|
+
# source = '{"a": "foo", "b": 1.0, "c": true, "d": false, "e": null}'
|
1320
|
+
# ruby = JSON.parse(source)
|
1321
|
+
# ruby # => {"a"=>"foo", "b"=>1.0, "c"=>true, "d"=>false, "e"=>nil}
|
1322
|
+
# ruby.class # => Hash
|
1323
|
+
#
|
1324
|
+
# For examples of parsing for all \JSON data types, see
|
1325
|
+
# {Parsing \JSON}[#module-JSON-label-Parsing+JSON].
|
1326
|
+
#
|
1327
|
+
# Parses nested JSON objects:
|
1328
|
+
# source = <<-EOT
|
1329
|
+
# {
|
1330
|
+
# "name": "Dave",
|
1331
|
+
# "age" :40,
|
1332
|
+
# "hats": [
|
1333
|
+
# "Cattleman's",
|
1334
|
+
# "Panama",
|
1335
|
+
# "Tophat"
|
1336
|
+
# ]
|
1337
|
+
# }
|
1338
|
+
# EOT
|
1339
|
+
# ruby = JSON.parse(source)
|
1340
|
+
# ruby # => {"name"=>"Dave", "age"=>40, "hats"=>["Cattleman's", "Panama", "Tophat"]}
|
1341
|
+
#
|
1342
|
+
# ---
|
1343
|
+
#
|
1344
|
+
# Raises an exception if +source+ is not valid JSON:
|
1345
|
+
# # Raises JSON::ParserError (783: unexpected token at ''):
|
1346
|
+
# JSON.parse('')
|
1347
|
+
#
|
1348
|
+
# source://json//lib/json/common.rb#215
|
1349
|
+
def parse(source, opts = T.unsafe(nil)); end
|
1350
|
+
|
1351
|
+
# :call-seq:
|
1352
|
+
# JSON.parse!(source, opts) -> object
|
1353
|
+
#
|
1354
|
+
# Calls
|
1355
|
+
# parse(source, opts)
|
1356
|
+
# with +source+ and possibly modified +opts+.
|
1357
|
+
#
|
1358
|
+
# Differences from JSON.parse:
|
1359
|
+
# - Option +max_nesting+, if not provided, defaults to +false+,
|
1360
|
+
# which disables checking for nesting depth.
|
1361
|
+
# - Option +allow_nan+, if not provided, defaults to +true+.
|
1362
|
+
#
|
1363
|
+
# source://json//lib/json/common.rb#230
|
1364
|
+
def parse!(source, opts = T.unsafe(nil)); end
|
1365
|
+
|
1366
|
+
# Returns the JSON parser class that is used by JSON. This is either
|
1367
|
+
# JSON::Ext::Parser or JSON::Pure::Parser:
|
1368
|
+
# JSON.parser # => JSON::Ext::Parser
|
1369
|
+
#
|
1370
|
+
# source://json//lib/json/common.rb#29
|
1371
|
+
def parser; end
|
1372
|
+
|
1373
|
+
# Set the JSON parser class _parser_ to be used by JSON.
|
1374
|
+
#
|
1375
|
+
# source://json//lib/json/common.rb#32
|
1376
|
+
def parser=(parser); end
|
1377
|
+
|
1378
|
+
# :call-seq:
|
1379
|
+
# JSON.pretty_generate(obj, opts = nil) -> new_string
|
1380
|
+
#
|
1381
|
+
# Arguments +obj+ and +opts+ here are the same as
|
1382
|
+
# arguments +obj+ and +opts+ in JSON.generate.
|
1383
|
+
#
|
1384
|
+
# Default options are:
|
1385
|
+
# {
|
1386
|
+
# indent: ' ', # Two spaces
|
1387
|
+
# space: ' ', # One space
|
1388
|
+
# array_nl: "\n", # Newline
|
1389
|
+
# object_nl: "\n" # Newline
|
1390
|
+
# }
|
1391
|
+
#
|
1392
|
+
# Example:
|
1393
|
+
# obj = {foo: [:bar, :baz], bat: {bam: 0, bad: 1}}
|
1394
|
+
# json = JSON.pretty_generate(obj)
|
1395
|
+
# puts json
|
1396
|
+
# Output:
|
1397
|
+
# {
|
1398
|
+
# "foo": [
|
1399
|
+
# "bar",
|
1400
|
+
# "baz"
|
1401
|
+
# ],
|
1402
|
+
# "bat": {
|
1403
|
+
# "bam": 0,
|
1404
|
+
# "bad": 1
|
1405
|
+
# }
|
1406
|
+
# }
|
1407
|
+
#
|
1408
|
+
# source://json//lib/json/common.rb#390
|
1409
|
+
def pretty_generate(obj, opts = T.unsafe(nil)); end
|
1410
|
+
|
1411
|
+
# :stopdoc:
|
1412
|
+
# I want to deprecate these later, so I'll first be silent about them, and later delete them.
|
1413
|
+
#
|
1414
|
+
# source://json//lib/json/common.rb#390
|
1415
|
+
def pretty_unparse(obj, opts = T.unsafe(nil)); end
|
1416
|
+
|
1417
|
+
# Recursively calls passed _Proc_ if the parsed data structure is an _Array_ or _Hash_
|
1418
|
+
#
|
1419
|
+
# source://json//lib/json/common.rb#575
|
1420
|
+
def recurse_proc(result, &proc); end
|
1421
|
+
|
1422
|
+
# source://json//lib/json/common.rb#557
|
1423
|
+
def restore(source, proc = T.unsafe(nil), options = T.unsafe(nil)); end
|
1424
|
+
|
1425
|
+
# Sets or Returns the JSON generator state class that is used by JSON. This is
|
1426
|
+
# either JSON::Ext::Generator::State or JSON::Pure::Generator::State:
|
1427
|
+
# JSON.state # => JSON::Ext::Generator::State
|
1428
|
+
#
|
1429
|
+
# source://json//lib/json/common.rb#108
|
1430
|
+
def state; end
|
1431
|
+
|
1432
|
+
# Sets or Returns the JSON generator state class that is used by JSON. This is
|
1433
|
+
# either JSON::Ext::Generator::State or JSON::Pure::Generator::State:
|
1434
|
+
# JSON.state # => JSON::Ext::Generator::State
|
1435
|
+
#
|
1436
|
+
# source://json//lib/json/common.rb#108
|
1437
|
+
def state=(_arg0); end
|
1438
|
+
|
1439
|
+
# :stopdoc:
|
1440
|
+
# I want to deprecate these later, so I'll first be silent about them, and
|
1441
|
+
# later delete them.
|
1442
|
+
#
|
1443
|
+
# source://json//lib/json/common.rb#296
|
1444
|
+
def unparse(obj, opts = T.unsafe(nil)); end
|
1445
|
+
end
|
1446
|
+
end
|
1447
|
+
|
1448
|
+
# source://json//lib/json/common.rb#114
|
1449
|
+
JSON::CREATE_ID_TLS_KEY = T.let(T.unsafe(nil), String)
|
1450
|
+
|
1451
|
+
# source://json//lib/json/common.rb#111
|
1452
|
+
JSON::DEFAULT_CREATE_ID = T.let(T.unsafe(nil), String)
|
1453
|
+
|
1454
|
+
# source://json//lib/json/generic_object.rb#5
|
1455
|
+
class JSON::GenericObject < ::OpenStruct
|
1456
|
+
# source://json//lib/json/generic_object.rb#63
|
1457
|
+
def as_json(*_arg0); end
|
1458
|
+
|
1459
|
+
# source://json//lib/json/generic_object.rb#47
|
1460
|
+
def to_hash; end
|
1461
|
+
|
1462
|
+
# source://json//lib/json/generic_object.rb#67
|
1463
|
+
def to_json(*a); end
|
1464
|
+
|
1465
|
+
# source://json//lib/json/generic_object.rb#59
|
1466
|
+
def |(other); end
|
1467
|
+
|
1468
|
+
class << self
|
1469
|
+
# source://json//lib/json/generic_object.rb#41
|
1470
|
+
def dump(obj, *args); end
|
1471
|
+
|
1472
|
+
# source://json//lib/json/generic_object.rb#21
|
1473
|
+
def from_hash(object); end
|
1474
|
+
|
1475
|
+
# Sets the attribute json_creatable
|
1476
|
+
#
|
1477
|
+
# @param value the value to set the attribute json_creatable to.
|
1478
|
+
#
|
1479
|
+
# source://json//lib/json/generic_object.rb#13
|
1480
|
+
def json_creatable=(_arg0); end
|
1481
|
+
|
1482
|
+
# @return [Boolean]
|
1483
|
+
#
|
1484
|
+
# source://json//lib/json/generic_object.rb#9
|
1485
|
+
def json_creatable?; end
|
1486
|
+
|
1487
|
+
# source://json//lib/json/generic_object.rb#15
|
1488
|
+
def json_create(data); end
|
1489
|
+
|
1490
|
+
# source://json//lib/json/generic_object.rb#36
|
1491
|
+
def load(source, proc = T.unsafe(nil), opts = T.unsafe(nil)); end
|
1492
|
+
end
|
1493
|
+
end
|
1494
|
+
|
1495
|
+
# The base exception for JSON errors.
|
1496
|
+
#
|
1497
|
+
# source://json//lib/json/common.rb#137
|
1498
|
+
class JSON::JSONError < ::StandardError
|
1499
|
+
class << self
|
1500
|
+
# source://json//lib/json/common.rb#138
|
1501
|
+
def wrap(exception); end
|
1502
|
+
end
|
1503
|
+
end
|
1504
|
+
|
1505
|
+
# source://json//lib/json/common.rb#35
|
1506
|
+
JSON::Parser = JSON::Ext::Parser
|
1507
|
+
|
1508
|
+
# source://json//lib/json/common.rb#73
|
1509
|
+
JSON::State = JSON::Ext::Generator::State
|
1510
|
+
|
1511
|
+
# For backwards compatibility
|
1512
|
+
#
|
1513
|
+
# source://json//lib/json/common.rb#159
|
1514
|
+
JSON::UnparserError = JSON::GeneratorError
|
1515
|
+
|
1516
|
+
# source://json//lib/json/common.rb#658
|
1517
|
+
module Kernel
|
1518
|
+
private
|
1519
|
+
|
1520
|
+
# If _object_ is string-like, parse the string and return the parsed result as
|
1521
|
+
# a Ruby data structure. Otherwise, generate a JSON text from the Ruby data
|
1522
|
+
# structure object and return it.
|
1523
|
+
#
|
1524
|
+
# The _opts_ argument is passed through to generate/parse respectively. See
|
1525
|
+
# generate and parse for their documentation.
|
1526
|
+
#
|
1527
|
+
# source://json//lib/json/common.rb#685
|
1528
|
+
def JSON(object, *args); end
|
1529
|
+
|
1530
|
+
# Outputs _objs_ to STDOUT as JSON strings in the shortest form, that is in
|
1531
|
+
# one line.
|
1532
|
+
#
|
1533
|
+
# source://json//lib/json/common.rb#663
|
1534
|
+
def j(*objs); end
|
1535
|
+
|
1536
|
+
# Outputs _objs_ to STDOUT as JSON strings in a pretty format, with
|
1537
|
+
# indentation and over many lines.
|
1538
|
+
#
|
1539
|
+
# source://json//lib/json/common.rb#672
|
1540
|
+
def jj(*objs); end
|
1541
|
+
end
|