json 1.8.3 → 2.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (83) hide show
  1. checksums.yaml +5 -5
  2. data/.gitignore +2 -0
  3. data/.travis.yml +9 -12
  4. data/{CHANGES → CHANGES.md} +219 -90
  5. data/Gemfile +10 -6
  6. data/{COPYING-json-jruby → LICENSE} +5 -6
  7. data/{README-json-jruby.markdown → README-json-jruby.md} +0 -0
  8. data/{README.rdoc → README.md} +201 -134
  9. data/Rakefile +35 -113
  10. data/VERSION +1 -1
  11. data/ext/json/ext/fbuffer/fbuffer.h +0 -3
  12. data/ext/json/ext/generator/generator.c +255 -101
  13. data/ext/json/ext/generator/generator.h +12 -4
  14. data/ext/json/ext/parser/extconf.rb +28 -0
  15. data/ext/json/ext/parser/parser.c +410 -462
  16. data/ext/json/ext/parser/parser.h +5 -5
  17. data/ext/json/ext/parser/parser.rl +166 -181
  18. data/ext/json/extconf.rb +1 -1
  19. data/java/src/json/ext/ByteListTranscoder.java +1 -2
  20. data/java/src/json/ext/Generator.java +39 -36
  21. data/java/src/json/ext/GeneratorMethods.java +1 -2
  22. data/java/src/json/ext/GeneratorService.java +1 -2
  23. data/java/src/json/ext/GeneratorState.java +33 -56
  24. data/java/src/json/ext/OptionsReader.java +2 -3
  25. data/java/src/json/ext/Parser.java +146 -417
  26. data/java/src/json/ext/Parser.rl +62 -126
  27. data/java/src/json/ext/ParserService.java +1 -2
  28. data/java/src/json/ext/RuntimeInfo.java +1 -6
  29. data/java/src/json/ext/StringDecoder.java +1 -2
  30. data/java/src/json/ext/StringEncoder.java +13 -2
  31. data/java/src/json/ext/Utils.java +1 -2
  32. data/json-java.gemspec +22 -7
  33. data/json.gemspec +0 -0
  34. data/json_pure.gemspec +22 -29
  35. data/lib/json/add/bigdecimal.rb +3 -2
  36. data/lib/json/add/complex.rb +4 -4
  37. data/lib/json/add/core.rb +1 -0
  38. data/lib/json/add/date.rb +1 -1
  39. data/lib/json/add/date_time.rb +1 -1
  40. data/lib/json/add/exception.rb +1 -1
  41. data/lib/json/add/ostruct.rb +3 -3
  42. data/lib/json/add/range.rb +1 -1
  43. data/lib/json/add/rational.rb +3 -3
  44. data/lib/json/add/regexp.rb +3 -3
  45. data/lib/json/add/set.rb +29 -0
  46. data/lib/json/add/struct.rb +1 -1
  47. data/lib/json/add/symbol.rb +1 -1
  48. data/lib/json/add/time.rb +1 -1
  49. data/lib/json/common.rb +350 -152
  50. data/lib/json/ext.rb +0 -6
  51. data/lib/json/generic_object.rb +5 -4
  52. data/lib/json/pure/generator.rb +83 -126
  53. data/lib/json/pure/parser.rb +62 -84
  54. data/lib/json/pure.rb +2 -8
  55. data/lib/json/version.rb +2 -1
  56. data/lib/json.rb +550 -29
  57. data/references/rfc7159.txt +899 -0
  58. data/tests/fixtures/obsolete_fail1.json +1 -0
  59. data/tests/{test_json_addition.rb → json_addition_test.rb} +28 -25
  60. data/tests/json_common_interface_test.rb +169 -0
  61. data/tests/json_encoding_test.rb +107 -0
  62. data/tests/json_ext_parser_test.rb +15 -0
  63. data/tests/{test_json_fixtures.rb → json_fixtures_test.rb} +13 -8
  64. data/tests/{test_json_generate.rb → json_generator_test.rb} +134 -39
  65. data/tests/{test_json_generic_object.rb → json_generic_object_test.rb} +15 -8
  66. data/tests/json_parser_test.rb +497 -0
  67. data/tests/json_string_matching_test.rb +38 -0
  68. data/tests/test_helper.rb +17 -0
  69. data/tools/diff.sh +18 -0
  70. data/tools/fuzz.rb +1 -9
  71. metadata +47 -53
  72. data/COPYING +0 -58
  73. data/GPL +0 -340
  74. data/TODO +0 -1
  75. data/data/example.json +0 -1
  76. data/data/index.html +0 -38
  77. data/data/prototype.js +0 -4184
  78. data/tests/fixtures/fail1.json +0 -1
  79. data/tests/setup_variant.rb +0 -11
  80. data/tests/test_json.rb +0 -553
  81. data/tests/test_json_encoding.rb +0 -65
  82. data/tests/test_json_string_matching.rb +0 -39
  83. data/tests/test_json_unicode.rb +0 -72
data/lib/json/common.rb CHANGED
@@ -1,14 +1,20 @@
1
+ #frozen_string_literal: false
1
2
  require 'json/version'
2
3
  require 'json/generic_object'
3
4
 
4
5
  module JSON
5
6
  class << self
6
- # If _object_ is string-like, parse the string and return the parsed result
7
- # as a Ruby data structure. Otherwise generate a JSON text from the Ruby
8
- # data structure object and return it.
7
+ # :call-seq:
8
+ # JSON[object] -> new_array or new_string
9
9
  #
10
- # The _opts_ argument is passed through to generate/parse respectively. See
11
- # generate and parse for their documentation.
10
+ # If +object+ is a \String,
11
+ # calls JSON.parse with +object+ and +opts+ (see method #parse):
12
+ # json = '[0, 1, null]'
13
+ # JSON[json]# => [0, 1, nil]
14
+ #
15
+ # Otherwise, calls JSON.generate with +object+ and +opts+ (see method #generate):
16
+ # ruby = [0, 1, nil]
17
+ # JSON[ruby] # => '[0,1,null]'
12
18
  def [](object, opts = {})
13
19
  if object.respond_to? :to_str
14
20
  JSON.parse(object.to_str, opts)
@@ -18,13 +24,14 @@ module JSON
18
24
  end
19
25
 
20
26
  # Returns the JSON parser class that is used by JSON. This is either
21
- # JSON::Ext::Parser or JSON::Pure::Parser.
27
+ # JSON::Ext::Parser or JSON::Pure::Parser:
28
+ # JSON.parser # => JSON::Ext::Parser
22
29
  attr_reader :parser
23
30
 
24
31
  # Set the JSON parser class _parser_ to be used by JSON.
25
32
  def parser=(parser) # :nodoc:
26
33
  @parser = parser
27
- remove_const :Parser if JSON.const_defined_in?(self, :Parser)
34
+ remove_const :Parser if const_defined?(:Parser, false)
28
35
  const_set :Parser, parser
29
36
  end
30
37
 
@@ -35,8 +42,8 @@ module JSON
35
42
  def deep_const_get(path) # :nodoc:
36
43
  path.to_s.split(/::/).inject(Object) do |p, c|
37
44
  case
38
- when c.empty? then p
39
- when JSON.const_defined_in?(p, c) then p.const_get(c)
45
+ when c.empty? then p
46
+ when p.const_defined?(c, true) then p.const_get(c)
40
47
  else
41
48
  begin
42
49
  p.const_missing(c)
@@ -83,15 +90,18 @@ module JSON
83
90
  end
84
91
 
85
92
  # Returns the JSON generator module that is used by JSON. This is
86
- # either JSON::Ext::Generator or JSON::Pure::Generator.
93
+ # either JSON::Ext::Generator or JSON::Pure::Generator:
94
+ # JSON.generator # => JSON::Ext::Generator
87
95
  attr_reader :generator
88
96
 
89
- # Returns the JSON generator state class that is used by JSON. This is
90
- # either JSON::Ext::Generator::State or JSON::Pure::Generator::State.
97
+ # Sets or Returns the JSON generator state class that is used by JSON. This is
98
+ # either JSON::Ext::Generator::State or JSON::Pure::Generator::State:
99
+ # JSON.state # => JSON::Ext::Generator::State
91
100
  attr_accessor :state
92
101
 
93
- # This is create identifier, which is used to decide if the _json_create_
94
- # hook of a class should be called. It defaults to 'json_class'.
102
+ # Sets or returns create identifier, which is used to decide if the _json_create_
103
+ # hook of a class should be called; initial value is +json_class+:
104
+ # JSON.create_id # => 'json_class'
95
105
  attr_accessor :create_id
96
106
  end
97
107
  self.create_id = 'json_class'
@@ -125,7 +135,7 @@ module JSON
125
135
  # This exception is raised if a generator or unparser error occurs.
126
136
  class GeneratorError < JSONError; end
127
137
  # For backwards compatibility
128
- UnparserError = GeneratorError
138
+ UnparserError = GeneratorError # :nodoc:
129
139
 
130
140
  # This exception is raised if the required unicode support is missing on the
131
141
  # system. Usually this means that the iconv library is not installed.
@@ -133,77 +143,135 @@ module JSON
133
143
 
134
144
  module_function
135
145
 
136
- # Parse the JSON document _source_ into a Ruby data structure and return it.
137
- #
138
- # _opts_ can have the following
139
- # keys:
140
- # * *max_nesting*: The maximum depth of nesting allowed in the parsed data
141
- # structures. Disable depth checking with :max_nesting => false. It defaults
142
- # to 100.
143
- # * *allow_nan*: If set to true, allow NaN, Infinity and -Infinity in
144
- # defiance of RFC 4627 to be parsed by the Parser. This option defaults
145
- # to false.
146
- # * *symbolize_names*: If set to true, returns symbols for the names
147
- # (keys) in a JSON object. Otherwise strings are returned. Strings are
148
- # the default.
149
- # * *create_additions*: If set to false, the Parser doesn't create
150
- # additions even if a matching class and create_id was found. This option
151
- # defaults to false.
152
- # * *object_class*: Defaults to Hash
153
- # * *array_class*: Defaults to Array
146
+ # :call-seq:
147
+ # JSON.parse(source, opts) -> object
148
+ #
149
+ # Returns the Ruby objects created by parsing the given +source+.
150
+ #
151
+ # Argument +source+ contains the \String to be parsed.
152
+ #
153
+ # Argument +opts+, if given, contains a \Hash of options for the parsing.
154
+ # See {Parsing Options}[#module-JSON-label-Parsing+Options].
155
+ #
156
+ # ---
157
+ #
158
+ # When +source+ is a \JSON array, returns a Ruby \Array:
159
+ # source = '["foo", 1.0, true, false, null]'
160
+ # ruby = JSON.parse(source)
161
+ # ruby # => ["foo", 1.0, true, false, nil]
162
+ # ruby.class # => Array
163
+ #
164
+ # When +source+ is a \JSON object, returns a Ruby \Hash:
165
+ # source = '{"a": "foo", "b": 1.0, "c": true, "d": false, "e": null}'
166
+ # ruby = JSON.parse(source)
167
+ # ruby # => {"a"=>"foo", "b"=>1.0, "c"=>true, "d"=>false, "e"=>nil}
168
+ # ruby.class # => Hash
169
+ #
170
+ # For examples of parsing for all \JSON data types, see
171
+ # {Parsing \JSON}[#module-JSON-label-Parsing+JSON].
172
+ #
173
+ # Parses nested JSON objects:
174
+ # source = <<-EOT
175
+ # {
176
+ # "name": "Dave",
177
+ # "age" :40,
178
+ # "hats": [
179
+ # "Cattleman's",
180
+ # "Panama",
181
+ # "Tophat"
182
+ # ]
183
+ # }
184
+ # EOT
185
+ # ruby = JSON.parse(source)
186
+ # ruby # => {"name"=>"Dave", "age"=>40, "hats"=>["Cattleman's", "Panama", "Tophat"]}
187
+ #
188
+ # ---
189
+ #
190
+ # Raises an exception if +source+ is not valid JSON:
191
+ # # Raises JSON::ParserError (783: unexpected token at ''):
192
+ # JSON.parse('')
193
+ #
154
194
  def parse(source, opts = {})
155
- Parser.new(source, opts).parse
195
+ Parser.new(source, **(opts||{})).parse
156
196
  end
157
197
 
158
- # Parse the JSON document _source_ into a Ruby data structure and return it.
159
- # The bang version of the parse method defaults to the more dangerous values
160
- # for the _opts_ hash, so be sure only to parse trusted _source_ documents.
161
- #
162
- # _opts_ can have the following keys:
163
- # * *max_nesting*: The maximum depth of nesting allowed in the parsed data
164
- # structures. Enable depth checking with :max_nesting => anInteger. The parse!
165
- # methods defaults to not doing max depth checking: This can be dangerous
166
- # if someone wants to fill up your stack.
167
- # * *allow_nan*: If set to true, allow NaN, Infinity, and -Infinity in
168
- # defiance of RFC 4627 to be parsed by the Parser. This option defaults
169
- # to true.
170
- # * *create_additions*: If set to false, the Parser doesn't create
171
- # additions even if a matching class and create_id was found. This option
172
- # defaults to false.
198
+ # :call-seq:
199
+ # JSON.parse!(source, opts) -> object
200
+ #
201
+ # Calls
202
+ # parse(source, opts)
203
+ # with +source+ and possibly modified +opts+.
204
+ #
205
+ # Differences from JSON.parse:
206
+ # - Option +max_nesting+, if not provided, defaults to +false+,
207
+ # which disables checking for nesting depth.
208
+ # - Option +allow_nan+, if not provided, defaults to +true+.
173
209
  def parse!(source, opts = {})
174
210
  opts = {
175
211
  :max_nesting => false,
176
212
  :allow_nan => true
177
- }.update(opts)
178
- Parser.new(source, opts).parse
213
+ }.merge(opts)
214
+ Parser.new(source, **(opts||{})).parse
179
215
  end
180
216
 
181
- # Generate a JSON document from the Ruby data structure _obj_ and return
182
- # it. _state_ is * a JSON::State object,
183
- # * or a Hash like object (responding to to_hash),
184
- # * an object convertible into a hash by a to_h method,
185
- # that is used as or to configure a State object.
186
- #
187
- # It defaults to a state object, that creates the shortest possible JSON text
188
- # in one line, checks for circular data structures and doesn't allow NaN,
189
- # Infinity, and -Infinity.
190
- #
191
- # A _state_ hash can have the following keys:
192
- # * *indent*: a string used to indent levels (default: ''),
193
- # * *space*: a string that is put after, a : or , delimiter (default: ''),
194
- # * *space_before*: a string that is put before a : pair delimiter (default: ''),
195
- # * *object_nl*: a string that is put at the end of a JSON object (default: ''),
196
- # * *array_nl*: a string that is put at the end of a JSON array (default: ''),
197
- # * *allow_nan*: true if NaN, Infinity, and -Infinity should be
198
- # generated, otherwise an exception is thrown if these values are
199
- # encountered. This options defaults to false.
200
- # * *max_nesting*: The maximum depth of nesting allowed in the data
201
- # structures from which JSON is to be generated. Disable depth checking
202
- # with :max_nesting => false, it defaults to 100.
203
- #
204
- # See also the fast_generate for the fastest creation method with the least
205
- # amount of sanity checks, and the pretty_generate method for some
206
- # defaults for pretty output.
217
+ # :call-seq:
218
+ # JSON.load_file(path, opts={}) -> object
219
+ #
220
+ # Calls:
221
+ # parse(File.read(path), opts)
222
+ #
223
+ # See method #parse.
224
+ def load_file(filespec, opts = {})
225
+ parse(File.read(filespec), opts)
226
+ end
227
+
228
+ # :call-seq:
229
+ # JSON.load_file!(path, opts = {})
230
+ #
231
+ # Calls:
232
+ # JSON.parse!(File.read(path, opts))
233
+ #
234
+ # See method #parse!
235
+ def load_file!(filespec, opts = {})
236
+ parse!(File.read(filespec), opts)
237
+ end
238
+
239
+ # :call-seq:
240
+ # JSON.generate(obj, opts = nil) -> new_string
241
+ #
242
+ # Returns a \String containing the generated \JSON data.
243
+ #
244
+ # See also JSON.fast_generate, JSON.pretty_generate.
245
+ #
246
+ # Argument +obj+ is the Ruby object to be converted to \JSON.
247
+ #
248
+ # Argument +opts+, if given, contains a \Hash of options for the generation.
249
+ # See {Generating Options}[#module-JSON-label-Generating+Options].
250
+ #
251
+ # ---
252
+ #
253
+ # When +obj+ is an \Array, returns a \String containing a \JSON array:
254
+ # obj = ["foo", 1.0, true, false, nil]
255
+ # json = JSON.generate(obj)
256
+ # json # => '["foo",1.0,true,false,null]'
257
+ #
258
+ # When +obj+ is a \Hash, returns a \String containing a \JSON object:
259
+ # obj = {foo: 0, bar: 's', baz: :bat}
260
+ # json = JSON.generate(obj)
261
+ # json # => '{"foo":0,"bar":"s","baz":"bat"}'
262
+ #
263
+ # For examples of generating from other Ruby objects, see
264
+ # {Generating \JSON from Other Objects}[#module-JSON-label-Generating+JSON+from+Other+Objects].
265
+ #
266
+ # ---
267
+ #
268
+ # Raises an exception if any formatting option is not a \String.
269
+ #
270
+ # Raises an exception if +obj+ contains circular references:
271
+ # a = []; b = []; a.push(b); b.push(a)
272
+ # # Raises JSON::NestingError (nesting of 100 is too deep):
273
+ # JSON.generate(a)
274
+ #
207
275
  def generate(obj, opts = nil)
208
276
  if State === opts
209
277
  state, opts = opts, nil
@@ -230,11 +298,19 @@ module JSON
230
298
  module_function :unparse
231
299
  # :startdoc:
232
300
 
233
- # Generate a JSON document from the Ruby data structure _obj_ and return it.
234
- # This method disables the checks for circles in Ruby objects.
301
+ # :call-seq:
302
+ # JSON.fast_generate(obj, opts) -> new_string
303
+ #
304
+ # Arguments +obj+ and +opts+ here are the same as
305
+ # arguments +obj+ and +opts+ in JSON.generate.
235
306
  #
236
- # *WARNING*: Be careful not to pass any Ruby data structures with circles as
237
- # _obj_ argument because this will cause JSON to go into an infinite loop.
307
+ # By default, generates \JSON data without checking
308
+ # for circular references in +obj+ (option +max_nesting+ set to +false+, disabled).
309
+ #
310
+ # Raises an exception if +obj+ contains circular references:
311
+ # a = []; b = []; a.push(b); b.push(a)
312
+ # # Raises SystemStackError (stack level too deep):
313
+ # JSON.fast_generate(a)
238
314
  def fast_generate(obj, opts = nil)
239
315
  if State === opts
240
316
  state, opts = opts, nil
@@ -260,12 +336,36 @@ module JSON
260
336
  module_function :fast_unparse
261
337
  # :startdoc:
262
338
 
263
- # Generate a JSON document from the Ruby data structure _obj_ and return it.
264
- # The returned document is a prettier form of the document returned by
265
- # #unparse.
339
+ # :call-seq:
340
+ # JSON.pretty_generate(obj, opts = nil) -> new_string
341
+ #
342
+ # Arguments +obj+ and +opts+ here are the same as
343
+ # arguments +obj+ and +opts+ in JSON.generate.
344
+ #
345
+ # Default options are:
346
+ # {
347
+ # indent: ' ', # Two spaces
348
+ # space: ' ', # One space
349
+ # array_nl: "\n", # Newline
350
+ # object_nl: "\n" # Newline
351
+ # }
352
+ #
353
+ # Example:
354
+ # obj = {foo: [:bar, :baz], bat: {bam: 0, bad: 1}}
355
+ # json = JSON.pretty_generate(obj)
356
+ # puts json
357
+ # Output:
358
+ # {
359
+ # "foo": [
360
+ # "bar",
361
+ # "baz"
362
+ # ],
363
+ # "bat": {
364
+ # "bam": 0,
365
+ # "bad": 1
366
+ # }
367
+ # }
266
368
  #
267
- # The _opts_ argument can be used to configure the generator. See the
268
- # generate method for a more detailed explanation.
269
369
  def pretty_generate(obj, opts = nil)
270
370
  if State === opts
271
371
  state, opts = opts, nil
@@ -292,33 +392,147 @@ module JSON
292
392
  # :startdoc:
293
393
 
294
394
  class << self
295
- # The global default options for the JSON.load method:
296
- # :max_nesting: false
297
- # :allow_nan: true
298
- # :quirks_mode: true
395
+ # Sets or returns default options for the JSON.load method.
396
+ # Initially:
397
+ # opts = JSON.load_default_options
398
+ # opts # => {:max_nesting=>false, :allow_nan=>true, :allow_blank=>true, :create_additions=>true}
299
399
  attr_accessor :load_default_options
300
400
  end
301
401
  self.load_default_options = {
302
402
  :max_nesting => false,
303
403
  :allow_nan => true,
304
- :quirks_mode => true,
404
+ :allow_blank => true,
305
405
  :create_additions => true,
306
406
  }
307
407
 
308
- # Load a ruby data structure from a JSON _source_ and return it. A source can
309
- # either be a string-like object, an IO-like object, or an object responding
310
- # to the read method. If _proc_ was given, it will be called with any nested
311
- # Ruby object as an argument recursively in depth first order. To modify the
312
- # default options pass in the optional _options_ argument as well.
408
+ # :call-seq:
409
+ # JSON.load(source, proc = nil, options = {}) -> object
410
+ #
411
+ # Returns the Ruby objects created by parsing the given +source+.
412
+ #
413
+ # - Argument +source+ must be, or be convertible to, a \String:
414
+ # - If +source+ responds to instance method +to_str+,
415
+ # <tt>source.to_str</tt> becomes the source.
416
+ # - If +source+ responds to instance method +to_io+,
417
+ # <tt>source.to_io.read</tt> becomes the source.
418
+ # - If +source+ responds to instance method +read+,
419
+ # <tt>source.read</tt> becomes the source.
420
+ # - If both of the following are true, source becomes the \String <tt>'null'</tt>:
421
+ # - Option +allow_blank+ specifies a truthy value.
422
+ # - The source, as defined above, is +nil+ or the empty \String <tt>''</tt>.
423
+ # - Otherwise, +source+ remains the source.
424
+ # - Argument +proc+, if given, must be a \Proc that accepts one argument.
425
+ # It will be called recursively with each result (depth-first order).
426
+ # See details below.
427
+ # BEWARE: This method is meant to serialise data from trusted user input,
428
+ # like from your own database server or clients under your control, it could
429
+ # be dangerous to allow untrusted users to pass JSON sources into it.
430
+ # - Argument +opts+, if given, contains a \Hash of options for the parsing.
431
+ # See {Parsing Options}[#module-JSON-label-Parsing+Options].
432
+ # The default options can be changed via method JSON.load_default_options=.
433
+ #
434
+ # ---
435
+ #
436
+ # When no +proc+ is given, modifies +source+ as above and returns the result of
437
+ # <tt>parse(source, opts)</tt>; see #parse.
438
+ #
439
+ # Source for following examples:
440
+ # source = <<-EOT
441
+ # {
442
+ # "name": "Dave",
443
+ # "age" :40,
444
+ # "hats": [
445
+ # "Cattleman's",
446
+ # "Panama",
447
+ # "Tophat"
448
+ # ]
449
+ # }
450
+ # EOT
451
+ #
452
+ # Load a \String:
453
+ # ruby = JSON.load(source)
454
+ # ruby # => {"name"=>"Dave", "age"=>40, "hats"=>["Cattleman's", "Panama", "Tophat"]}
455
+ #
456
+ # Load an \IO object:
457
+ # require 'stringio'
458
+ # object = JSON.load(StringIO.new(source))
459
+ # object # => {"name"=>"Dave", "age"=>40, "hats"=>["Cattleman's", "Panama", "Tophat"]}
313
460
  #
314
- # BEWARE: This method is meant to serialise data from trusted user input,
315
- # like from your own database server or clients under your control, it could
316
- # be dangerous to allow untrusted users to pass JSON sources into it. The
317
- # default options for the parser can be changed via the load_default_options
318
- # method.
461
+ # Load a \File object:
462
+ # path = 't.json'
463
+ # File.write(path, source)
464
+ # File.open(path) do |file|
465
+ # JSON.load(file)
466
+ # end # => {"name"=>"Dave", "age"=>40, "hats"=>["Cattleman's", "Panama", "Tophat"]}
467
+ #
468
+ # ---
469
+ #
470
+ # When +proc+ is given:
471
+ # - Modifies +source+ as above.
472
+ # - Gets the +result+ from calling <tt>parse(source, opts)</tt>.
473
+ # - Recursively calls <tt>proc(result)</tt>.
474
+ # - Returns the final result.
475
+ #
476
+ # Example:
477
+ # require 'json'
478
+ #
479
+ # # Some classes for the example.
480
+ # class Base
481
+ # def initialize(attributes)
482
+ # @attributes = attributes
483
+ # end
484
+ # end
485
+ # class User < Base; end
486
+ # class Account < Base; end
487
+ # class Admin < Base; end
488
+ # # The JSON source.
489
+ # json = <<-EOF
490
+ # {
491
+ # "users": [
492
+ # {"type": "User", "username": "jane", "email": "jane@example.com"},
493
+ # {"type": "User", "username": "john", "email": "john@example.com"}
494
+ # ],
495
+ # "accounts": [
496
+ # {"account": {"type": "Account", "paid": true, "account_id": "1234"}},
497
+ # {"account": {"type": "Account", "paid": false, "account_id": "1235"}}
498
+ # ],
499
+ # "admins": {"type": "Admin", "password": "0wn3d"}
500
+ # }
501
+ # EOF
502
+ # # Deserializer method.
503
+ # def deserialize_obj(obj, safe_types = %w(User Account Admin))
504
+ # type = obj.is_a?(Hash) && obj["type"]
505
+ # safe_types.include?(type) ? Object.const_get(type).new(obj) : obj
506
+ # end
507
+ # # Call to JSON.load
508
+ # ruby = JSON.load(json, proc {|obj|
509
+ # case obj
510
+ # when Hash
511
+ # obj.each {|k, v| obj[k] = deserialize_obj v }
512
+ # when Array
513
+ # obj.map! {|v| deserialize_obj v }
514
+ # end
515
+ # })
516
+ # pp ruby
517
+ # Output:
518
+ # {"users"=>
519
+ # [#<User:0x00000000064c4c98
520
+ # @attributes=
521
+ # {"type"=>"User", "username"=>"jane", "email"=>"jane@example.com"}>,
522
+ # #<User:0x00000000064c4bd0
523
+ # @attributes=
524
+ # {"type"=>"User", "username"=>"john", "email"=>"john@example.com"}>],
525
+ # "accounts"=>
526
+ # [{"account"=>
527
+ # #<Account:0x00000000064c4928
528
+ # @attributes={"type"=>"Account", "paid"=>true, "account_id"=>"1234"}>},
529
+ # {"account"=>
530
+ # #<Account:0x00000000064c4680
531
+ # @attributes={"type"=>"Account", "paid"=>false, "account_id"=>"1235"}>}],
532
+ # "admins"=>
533
+ # #<Admin:0x00000000064c41f8
534
+ # @attributes={"type"=>"Admin", "password"=>"0wn3d"}>}
319
535
  #
320
- # This method is part of the implementation of the load/dump interface of
321
- # Marshal and YAML.
322
536
  def load(source, proc = nil, options = {})
323
537
  opts = load_default_options.merge options
324
538
  if source.respond_to? :to_str
@@ -328,7 +542,7 @@ module JSON
328
542
  elsif source.respond_to?(:read)
329
543
  source = source.read
330
544
  end
331
- if opts[:quirks_mode] && (source.nil? || source.empty?)
545
+ if opts[:allow_blank] && (source.nil? || source.empty?)
332
546
  source = 'null'
333
547
  end
334
548
  result = parse(source, opts)
@@ -337,7 +551,7 @@ module JSON
337
551
  end
338
552
 
339
553
  # Recursively calls passed _Proc_ if the parsed data structure is an _Array_ or _Hash_
340
- def recurse_proc(result, &proc)
554
+ def recurse_proc(result, &proc) # :nodoc:
341
555
  case result
342
556
  when Array
343
557
  result.each { |x| recurse_proc x, &proc }
@@ -354,33 +568,45 @@ module JSON
354
568
  module_function :restore
355
569
 
356
570
  class << self
357
- # The global default options for the JSON.dump method:
358
- # :max_nesting: false
359
- # :allow_nan: true
360
- # :quirks_mode: true
571
+ # Sets or returns the default options for the JSON.dump method.
572
+ # Initially:
573
+ # opts = JSON.dump_default_options
574
+ # opts # => {:max_nesting=>false, :allow_nan=>true, :escape_slash=>false}
361
575
  attr_accessor :dump_default_options
362
576
  end
363
577
  self.dump_default_options = {
364
578
  :max_nesting => false,
365
579
  :allow_nan => true,
366
- :quirks_mode => true,
580
+ :escape_slash => false,
367
581
  }
368
582
 
369
- # Dumps _obj_ as a JSON string, i.e. calls generate on the object and returns
370
- # the result.
583
+ # :call-seq:
584
+ # JSON.dump(obj, io = nil, limit = nil)
371
585
  #
372
- # If anIO (an IO-like object or an object that responds to the write method)
373
- # was given, the resulting JSON is written to it.
586
+ # Dumps +obj+ as a \JSON string, i.e. calls generate on the object and returns the result.
374
587
  #
375
- # If the number of nested arrays or objects exceeds _limit_, an ArgumentError
376
- # exception is raised. This argument is similar (but not exactly the
377
- # same!) to the _limit_ argument in Marshal.dump.
588
+ # The default options can be changed via method JSON.dump_default_options.
378
589
  #
379
- # The default options for the generator can be changed via the
380
- # dump_default_options method.
590
+ # - Argument +io+, if given, should respond to method +write+;
591
+ # the \JSON \String is written to +io+, and +io+ is returned.
592
+ # If +io+ is not given, the \JSON \String is returned.
593
+ # - Argument +limit+, if given, is passed to JSON.generate as option +max_nesting+.
381
594
  #
382
- # This method is part of the implementation of the load/dump interface of
383
- # Marshal and YAML.
595
+ # ---
596
+ #
597
+ # When argument +io+ is not given, returns the \JSON \String generated from +obj+:
598
+ # obj = {foo: [0, 1], bar: {baz: 2, bat: 3}, bam: :bad}
599
+ # json = JSON.dump(obj)
600
+ # json # => "{\"foo\":[0,1],\"bar\":{\"baz\":2,\"bat\":3},\"bam\":\"bad\"}"
601
+ #
602
+ # When argument +io+ is given, writes the \JSON \String to +io+ and returns +io+:
603
+ # path = 't.json'
604
+ # File.open(path, 'w') do |file|
605
+ # JSON.dump(obj, file)
606
+ # end # => #<File:t.json (closed)>
607
+ # puts File.read(path)
608
+ # Output:
609
+ # {"foo":[0,1],"bar":{"baz":2,"bat":3},"bam":"bad"}
384
610
  def dump(obj, anIO = nil, limit = nil)
385
611
  if anIO and limit.nil?
386
612
  anIO = anIO.to_io if anIO.respond_to?(:to_io)
@@ -402,37 +628,9 @@ module JSON
402
628
  raise ArgumentError, "exceed depth limit"
403
629
  end
404
630
 
405
- # Swap consecutive bytes of _string_ in place.
406
- def self.swap!(string) # :nodoc:
407
- 0.upto(string.size / 2) do |i|
408
- break unless string[2 * i + 1]
409
- string[2 * i], string[2 * i + 1] = string[2 * i + 1], string[2 * i]
410
- end
411
- string
412
- end
413
-
414
- # Shortcut for iconv.
415
- if ::String.method_defined?(:encode)
416
- # Encodes string using Ruby's _String.encode_
417
- def self.iconv(to, from, string)
418
- string.encode(to, from)
419
- end
420
- else
421
- require 'iconv'
422
- # Encodes string using _iconv_ library
423
- def self.iconv(to, from, string)
424
- Iconv.conv(to, from, string)
425
- end
426
- end
427
-
428
- if ::Object.method(:const_defined?).arity == 1
429
- def self.const_defined_in?(modul, constant)
430
- modul.const_defined?(constant)
431
- end
432
- else
433
- def self.const_defined_in?(modul, constant)
434
- modul.const_defined?(constant, false)
435
- end
631
+ # Encodes string using String.encode.
632
+ def self.iconv(to, from, string)
633
+ string.encode(to, from)
436
634
  end
437
635
  end
438
636
 
data/lib/json/ext.rb CHANGED
@@ -1,9 +1,3 @@
1
- if ENV['SIMPLECOV_COVERAGE'].to_i == 1
2
- require 'simplecov'
3
- SimpleCov.start do
4
- add_filter "/tests/"
5
- end
6
- end
7
1
  require 'json/common'
8
2
 
9
3
  module JSON
@@ -1,3 +1,4 @@
1
+ #frozen_string_literal: false
1
2
  require 'ostruct'
2
3
 
3
4
  module JSON
@@ -48,12 +49,12 @@ module JSON
48
49
  end
49
50
 
50
51
  def [](name)
51
- table[name.to_sym]
52
- end
52
+ __send__(name)
53
+ end unless method_defined?(:[])
53
54
 
54
55
  def []=(name, value)
55
- __send__ "#{name}=", value
56
- end
56
+ __send__("#{name}=", value)
57
+ end unless method_defined?(:[]=)
57
58
 
58
59
  def |(other)
59
60
  self.class[other.to_hash.merge(to_hash)]