Skip to content

Commit 380699d

Browse files
authored
Merge pull request #430 from BurdetteLamar/master
Move options from #generate and #parse to common area
2 parents 0a76a1f + 20d7be6 commit 380699d

File tree

2 files changed

+181
-177
lines changed

2 files changed

+181
-177
lines changed

lib/json.rb

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,89 @@
107107
# ruby # => nil
108108
# ruby.class # => NilClass
109109
#
110+
# ==== Parsing Options
111+
#
112+
# ====== Input Options
113+
#
114+
# Option +max_nesting+ (\Integer) specifies the maximum nesting depth allowed;
115+
# defaults to +100+; specify +false+ to disable depth checking.
116+
#
117+
# With the default, +false+:
118+
# source = '[0, [1, [2, [3]]]]'
119+
# ruby = JSON.parse(source)
120+
# ruby # => [0, [1, [2, [3]]]]
121+
# Too deep:
122+
# # Raises JSON::NestingError (nesting of 2 is too deep):
123+
# JSON.parse(source, {max_nesting: 1})
124+
# Bad value:
125+
# # Raises TypeError (wrong argument type Symbol (expected Fixnum)):
126+
# JSON.parse(source, {max_nesting: :foo})
127+
#
128+
# ---
129+
#
130+
# Option +allow_nan+ (boolean) specifies whether to allow
131+
# NaN, Infinity, and MinusInfinity in +source+;
132+
# defaults to +false+.
133+
#
134+
# With the default, +false+:
135+
# # Raises JSON::ParserError (225: unexpected token at '[NaN]'):
136+
# JSON.parse('[NaN]')
137+
# # Raises JSON::ParserError (232: unexpected token at '[Infinity]'):
138+
# JSON.parse('[Infinity]')
139+
# # Raises JSON::ParserError (248: unexpected token at '[-Infinity]'):
140+
# JSON.parse('[-Infinity]')
141+
# Allow:
142+
# source = '[NaN, Infinity, -Infinity]'
143+
# ruby = JSON.parse(source, {allow_nan: true})
144+
# ruby # => [NaN, Infinity, -Infinity]
145+
#
146+
# ====== Output Options
147+
#
148+
# Option +symbolize_names+ (boolean) specifies whether returned \Hash keys
149+
# should be Symbols;
150+
# defaults to +false+ (use Strings).
151+
#
152+
# With the default, +false+:
153+
# source = '{"a": "foo", "b": 1.0, "c": true, "d": false, "e": null}'
154+
# ruby = JSON.parse(source)
155+
# ruby # => {"a"=>"foo", "b"=>1.0, "c"=>true, "d"=>false, "e"=>nil}
156+
# Use Symbols:
157+
# ruby = JSON.parse(source, {symbolize_names: true})
158+
# ruby # => {:a=>"foo", :b=>1.0, :c=>true, :d=>false, :e=>nil}
159+
#
160+
# ---
161+
#
162+
# Option +object_class+ (\Class) specifies the Ruby class to be used
163+
# for each \JSON object;
164+
# defaults to \Hash.
165+
#
166+
# With the default, \Hash:
167+
# source = '{"a": "foo", "b": 1.0, "c": true, "d": false, "e": null}'
168+
# ruby = JSON.parse(source)
169+
# ruby.class # => Hash
170+
# Use class \OpenStruct:
171+
# ruby = JSON.parse(source, {object_class: OpenStruct})
172+
# ruby # => #<OpenStruct a="foo", b=1.0, c=true, d=false, e=nil>
173+
#
174+
# ---
175+
#
176+
# Option +array_class+ (\Class) specifies the Ruby class to be used
177+
# for each \JSON array;
178+
# defaults to \Array.
179+
#
180+
# With the default, \Array:
181+
# source = '["foo", 1.0, true, false, null]'
182+
# ruby = JSON.parse(source)
183+
# ruby.class # => Array
184+
# Use class \Set:
185+
# ruby = JSON.parse(source, {array_class: Set})
186+
# ruby # => #<Set: {"foo", 1.0, true, false, nil}>
187+
#
188+
# ---
189+
#
190+
# Option +create_additions+ (boolean) specifies whether to use \JSON additions in parsing.
191+
# See {\JSON Additions}[#module-JSON-label-JSON+Additions].
192+
#
110193
# === Generating \JSON
111194
#
112195
# To generate a Ruby \String containing \JSON data,
@@ -169,6 +252,94 @@
169252
# JSON.generate(Complex(0, 0)) # => '"0+0i"'
170253
# JSON.generate(Dir.new('.')) # => '"#<Dir>"'
171254
#
255+
# ==== Generating Options
256+
#
257+
# ====== Input Options
258+
#
259+
# Option +allow_nan+ (boolean) specifies whether
260+
# +NaN+, +Infinity+, and <tt>-Infinity</tt> may be generated;
261+
# defaults to +false+.
262+
#
263+
# With the default, +false+:
264+
# # Raises JSON::GeneratorError (920: NaN not allowed in JSON):
265+
# JSON.generate(JSON::NaN)
266+
# # Raises JSON::GeneratorError (917: Infinity not allowed in JSON):
267+
# JSON.generate(JSON::Infinity)
268+
# # Raises JSON::GeneratorError (917: -Infinity not allowed in JSON):
269+
# JSON.generate(JSON::MinusInfinity)
270+
#
271+
# Allow:
272+
# ruby = [Float::NaN, Float::Infinity, Float::MinusInfinity]
273+
# JSON.generate(ruby, allow_nan: true) # => '[NaN,Infinity,-Infinity]'
274+
#
275+
# ---
276+
#
277+
# Option +max_nesting+ (\Integer) specifies the maximum nesting depth
278+
# in +obj+; defaults to +100+.
279+
#
280+
# With the default, +100+:
281+
# obj = [[[[[[0]]]]]]
282+
# JSON.generate(obj) # => '[[[[[[0]]]]]]'
283+
#
284+
# Too deep:
285+
# # Raises JSON::NestingError (nesting of 2 is too deep):
286+
# JSON.generate(obj, max_nesting: 2)
287+
#
288+
# ====== Output Options
289+
#
290+
# The default formatting options generate the most compact
291+
# \JSON data, all on one line and with no whitespace.
292+
#
293+
# You can use these formatting options to generate
294+
# \JSON data in a more open format, using whitespace.
295+
# See also JSON.pretty_generate.
296+
#
297+
# - Option +array_nl+ (\String) specifies a string (usually a newline)
298+
# to be inserted after each \JSON array; defaults to the empty \String, <tt>''</tt>.
299+
# - Option +object_nl+ (\String) specifies a string (usually a newline)
300+
# to be inserted after each \JSON object; defaults to the empty \String, <tt>''</tt>.
301+
# - Option +indent+ (\String) specifies the string (usually spaces) to be
302+
# used for indentation; defaults to the empty \String, <tt>''</tt>;
303+
# defaults to the empty \String, <tt>''</tt>;
304+
# has no effect unless options +array_nl+ or +object_nl+ specify newlines.
305+
# - Option +space+ (\String) specifies a string (usually a space) to be
306+
# inserted after the colon in each \JSON object's pair;
307+
# defaults to the empty \String, <tt>''</tt>.
308+
# - Option +space_before+ (\String) specifies a string (usually a space) to be
309+
# inserted before the colon in each \JSON object's pair;
310+
# defaults to the empty \String, <tt>''</tt>.
311+
#
312+
# In this example, +obj+ is used first to generate the shortest
313+
# \JSON data (no whitespace), then again with all formatting options
314+
# specified:
315+
#
316+
# obj = {foo: [:bar, :baz], bat: {bam: 0, bad: 1}}
317+
# json = JSON.generate(obj)
318+
# puts 'Compact:', json
319+
# opts = {
320+
# array_nl: "\n",
321+
# object_nl: "\n",
322+
# indent+: ' ',
323+
# space_before: ' ',
324+
# space: ' '
325+
# }
326+
# puts 'Open:', JSON.generate(obj, opts)
327+
#
328+
# Output:
329+
# Compact:
330+
# {"foo":["bar","baz"],"bat":{"bam":0,"bad":1}}
331+
# Open:
332+
# {
333+
# "foo" : [
334+
# "bar",
335+
# "baz"
336+
# ],
337+
# "bat" : {
338+
# "bam" : 0,
339+
# "bad" : 1
340+
# }
341+
# }
342+
#
172343
# == \JSON Additions
173344
#
174345
# When you "round trip" a non-\String object from Ruby to \JSON and back,

0 commit comments

Comments
 (0)