json_pure 2.0.4 → 2.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. checksums.yaml +5 -5
  2. data/.gitignore +1 -0
  3. data/.travis.yml +9 -5
  4. data/CHANGES.md +39 -0
  5. data/Gemfile +1 -3
  6. data/LICENSE +56 -0
  7. data/README.md +54 -21
  8. data/Rakefile +19 -93
  9. data/VERSION +1 -1
  10. data/ext/json/ext/generator/generator.c +214 -45
  11. data/ext/json/ext/generator/generator.h +5 -2
  12. data/ext/json/ext/parser/extconf.rb +25 -0
  13. data/ext/json/ext/parser/parser.c +155 -83
  14. data/ext/json/ext/parser/parser.h +2 -0
  15. data/ext/json/ext/parser/parser.rl +79 -7
  16. data/ext/json/extconf.rb +1 -0
  17. data/java/src/json/ext/Generator.java +28 -24
  18. data/java/src/json/ext/GeneratorState.java +30 -0
  19. data/java/src/json/ext/Parser.java +109 -82
  20. data/java/src/json/ext/Parser.rl +39 -12
  21. data/java/src/json/ext/StringEncoder.java +8 -2
  22. data/json-java.gemspec +22 -22
  23. data/json.gemspec +0 -0
  24. data/json_pure.gemspec +9 -14
  25. data/lib/json.rb +549 -29
  26. data/lib/json/add/bigdecimal.rb +2 -2
  27. data/lib/json/add/complex.rb +2 -3
  28. data/lib/json/add/ostruct.rb +1 -1
  29. data/lib/json/add/rational.rb +2 -3
  30. data/lib/json/add/regexp.rb +2 -2
  31. data/lib/json/add/set.rb +29 -0
  32. data/lib/json/common.rb +341 -115
  33. data/lib/json/pure/generator.rb +31 -10
  34. data/lib/json/pure/parser.rb +35 -5
  35. data/lib/json/version.rb +1 -1
  36. data/tests/json_addition_test.rb +6 -0
  37. data/tests/json_common_interface_test.rb +47 -4
  38. data/tests/json_encoding_test.rb +2 -2
  39. data/tests/json_fixtures_test.rb +9 -1
  40. data/tests/json_generator_test.rb +55 -0
  41. data/tests/json_parser_test.rb +43 -12
  42. data/tests/test_helper.rb +3 -7
  43. metadata +17 -13
  44. data/data/example.json +0 -1
  45. data/data/index.html +0 -38
  46. data/data/prototype.js +0 -4184
@@ -23,7 +23,7 @@ class BigDecimal
23
23
  end
24
24
 
25
25
  # return the JSON value
26
- def to_json(*)
27
- as_json.to_json
26
+ def to_json(*args)
27
+ as_json.to_json(*args)
28
28
  end
29
29
  end
@@ -2,7 +2,6 @@
2
2
  unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
3
3
  require 'json'
4
4
  end
5
- defined?(::Complex) or require 'complex'
6
5
 
7
6
  class Complex
8
7
 
@@ -23,7 +22,7 @@ class Complex
23
22
  end
24
23
 
25
24
  # Stores class name (Complex) along with real value <tt>r</tt> and imaginary value <tt>i</tt> as JSON string
26
- def to_json(*)
27
- as_json.to_json
25
+ def to_json(*args)
26
+ as_json.to_json(*args)
28
27
  end
29
28
  end
@@ -23,7 +23,7 @@ class OpenStruct
23
23
  }
24
24
  end
25
25
 
26
- # Stores class name (OpenStruct) with this struct's values <tt>v</tt> as a
26
+ # Stores class name (OpenStruct) with this struct's values <tt>t</tt> as a
27
27
  # JSON string.
28
28
  def to_json(*args)
29
29
  as_json.to_json(*args)
@@ -2,7 +2,6 @@
2
2
  unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
3
3
  require 'json'
4
4
  end
5
- defined?(::Rational) or require 'rational'
6
5
 
7
6
  class Rational
8
7
  # Deserializes JSON string by converting numerator value <tt>n</tt>,
@@ -22,7 +21,7 @@ class Rational
22
21
  end
23
22
 
24
23
  # Stores class name (Rational) along with numerator value <tt>n</tt> and denominator value <tt>d</tt> as JSON string
25
- def to_json(*)
26
- as_json.to_json
24
+ def to_json(*args)
25
+ as_json.to_json(*args)
27
26
  end
28
27
  end
@@ -24,7 +24,7 @@ class Regexp
24
24
 
25
25
  # Stores class name (Regexp) with options <tt>o</tt> and source <tt>s</tt>
26
26
  # (Regexp or String) as JSON string
27
- def to_json(*)
28
- as_json.to_json
27
+ def to_json(*args)
28
+ as_json.to_json(*args)
29
29
  end
30
30
  end
@@ -0,0 +1,29 @@
1
+ unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
2
+ require 'json'
3
+ end
4
+ defined?(::Set) or require 'set'
5
+
6
+ class Set
7
+ # Import a JSON Marshalled object.
8
+ #
9
+ # method used for JSON marshalling support.
10
+ def self.json_create(object)
11
+ new object['a']
12
+ end
13
+
14
+ # Marshal the object to JSON.
15
+ #
16
+ # method used for JSON marshalling support.
17
+ def as_json(*)
18
+ {
19
+ JSON.create_id => self.class.name,
20
+ 'a' => to_a,
21
+ }
22
+ end
23
+
24
+ # return the JSON value
25
+ def to_json(*args)
26
+ as_json.to_json(*args)
27
+ end
28
+ end
29
+
@@ -4,12 +4,17 @@ require 'json/generic_object'
4
4
 
5
5
  module JSON
6
6
  class << self
7
- # If _object_ is string-like, parse the string and return the parsed
8
- # result as a Ruby data structure. Otherwise generate a JSON text from the
9
- # Ruby data structure object and return it.
7
+ # :call-seq:
8
+ # JSON[object] -> new_array or new_string
10
9
  #
11
- # The _opts_ argument is passed through to generate/parse respectively.
12
- # See 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]'
13
18
  def [](object, opts = {})
14
19
  if object.respond_to? :to_str
15
20
  JSON.parse(object.to_str, opts)
@@ -19,7 +24,8 @@ module JSON
19
24
  end
20
25
 
21
26
  # Returns the JSON parser class that is used by JSON. This is either
22
- # JSON::Ext::Parser or JSON::Pure::Parser.
27
+ # JSON::Ext::Parser or JSON::Pure::Parser:
28
+ # JSON.parser # => JSON::Ext::Parser
23
29
  attr_reader :parser
24
30
 
25
31
  # Set the JSON parser class _parser_ to be used by JSON.
@@ -84,15 +90,18 @@ module JSON
84
90
  end
85
91
 
86
92
  # Returns the JSON generator module that is used by JSON. This is
87
- # either JSON::Ext::Generator or JSON::Pure::Generator.
93
+ # either JSON::Ext::Generator or JSON::Pure::Generator:
94
+ # JSON.generator # => JSON::Ext::Generator
88
95
  attr_reader :generator
89
96
 
90
- # Returns the JSON generator state class that is used by JSON. This is
91
- # 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
92
100
  attr_accessor :state
93
101
 
94
- # This is create identifier, which is used to decide if the _json_create_
95
- # 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'
96
105
  attr_accessor :create_id
97
106
  end
98
107
  self.create_id = 'json_class'
@@ -126,7 +135,7 @@ module JSON
126
135
  # This exception is raised if a generator or unparser error occurs.
127
136
  class GeneratorError < JSONError; end
128
137
  # For backwards compatibility
129
- UnparserError = GeneratorError
138
+ UnparserError = GeneratorError # :nodoc:
130
139
 
131
140
  # This exception is raised if the required unicode support is missing on the
132
141
  # system. Usually this means that the iconv library is not installed.
@@ -134,77 +143,135 @@ module JSON
134
143
 
135
144
  module_function
136
145
 
137
- # Parse the JSON document _source_ into a Ruby data structure and return it.
138
- #
139
- # _opts_ can have the following
140
- # keys:
141
- # * *max_nesting*: The maximum depth of nesting allowed in the parsed data
142
- # structures. Disable depth checking with :max_nesting => false. It
143
- # defaults to 100.
144
- # * *allow_nan*: If set to true, allow NaN, Infinity and -Infinity in
145
- # defiance of RFC 7159 to be parsed by the Parser. This option defaults
146
- # to false.
147
- # * *symbolize_names*: If set to true, returns symbols for the names
148
- # (keys) in a JSON object. Otherwise strings are returned. Strings are
149
- # the default.
150
- # * *create_additions*: If set to false, the Parser doesn't create
151
- # additions even if a matching class and create_id was found. This option
152
- # defaults to false.
153
- # * *object_class*: Defaults to Hash
154
- # * *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
+ #
155
194
  def parse(source, opts = {})
156
- Parser.new(source, opts).parse
195
+ Parser.new(source, **(opts||{})).parse
157
196
  end
158
197
 
159
- # Parse the JSON document _source_ into a Ruby data structure and return it.
160
- # The bang version of the parse method defaults to the more dangerous values
161
- # for the _opts_ hash, so be sure only to parse trusted _source_ documents.
162
- #
163
- # _opts_ can have the following keys:
164
- # * *max_nesting*: The maximum depth of nesting allowed in the parsed data
165
- # structures. Enable depth checking with :max_nesting => anInteger. The
166
- # parse! methods defaults to not doing max depth checking: This can be
167
- # dangerous if someone wants to fill up your stack.
168
- # * *allow_nan*: If set to true, allow NaN, Infinity, and -Infinity in
169
- # defiance of RFC 7159 to be parsed by the Parser. This option defaults
170
- # to true.
171
- # * *create_additions*: If set to false, the Parser doesn't create
172
- # additions even if a matching class and create_id was found. This option
173
- # 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+.
174
209
  def parse!(source, opts = {})
175
210
  opts = {
176
211
  :max_nesting => false,
177
212
  :allow_nan => true
178
213
  }.merge(opts)
179
- Parser.new(source, opts).parse
214
+ Parser.new(source, **(opts||{})).parse
180
215
  end
181
216
 
182
- # Generate a JSON document from the Ruby data structure _obj_ and return
183
- # it. _state_ is * a JSON::State object,
184
- # * or a Hash like object (responding to to_hash),
185
- # * an object convertible into a hash by a to_h method,
186
- # that is used as or to configure a State object.
187
- #
188
- # It defaults to a state object, that creates the shortest possible JSON text
189
- # in one line, checks for circular data structures and doesn't allow NaN,
190
- # Infinity, and -Infinity.
191
- #
192
- # A _state_ hash can have the following keys:
193
- # * *indent*: a string used to indent levels (default: ''),
194
- # * *space*: a string that is put after, a : or , delimiter (default: ''),
195
- # * *space_before*: a string that is put before a : pair delimiter (default: ''),
196
- # * *object_nl*: a string that is put at the end of a JSON object (default: ''),
197
- # * *array_nl*: a string that is put at the end of a JSON array (default: ''),
198
- # * *allow_nan*: true if NaN, Infinity, and -Infinity should be
199
- # generated, otherwise an exception is thrown if these values are
200
- # encountered. This options defaults to false.
201
- # * *max_nesting*: The maximum depth of nesting allowed in the data
202
- # structures from which JSON is to be generated. Disable depth checking
203
- # with :max_nesting => false, it defaults to 100.
204
- #
205
- # See also the fast_generate for the fastest creation method with the least
206
- # amount of sanity checks, and the pretty_generate method for some
207
- # 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
+ #
208
275
  def generate(obj, opts = nil)
209
276
  if State === opts
210
277
  state, opts = opts, nil
@@ -231,11 +298,19 @@ module JSON
231
298
  module_function :unparse
232
299
  # :startdoc:
233
300
 
234
- # Generate a JSON document from the Ruby data structure _obj_ and return it.
235
- # 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.
306
+ #
307
+ # By default, generates \JSON data without checking
308
+ # for circular references in +obj+ (option +max_nesting+ set to +false+, disabled).
236
309
  #
237
- # *WARNING*: Be careful not to pass any Ruby data structures with circles as
238
- # _obj_ argument because this will cause JSON to go into an infinite loop.
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)
239
314
  def fast_generate(obj, opts = nil)
240
315
  if State === opts
241
316
  state, opts = opts, nil
@@ -261,12 +336,36 @@ module JSON
261
336
  module_function :fast_unparse
262
337
  # :startdoc:
263
338
 
264
- # Generate a JSON document from the Ruby data structure _obj_ and return it.
265
- # The returned document is a prettier form of the document returned by
266
- # #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
+ # }
267
368
  #
268
- # The _opts_ argument can be used to configure the generator. See the
269
- # generate method for a more detailed explanation.
270
369
  def pretty_generate(obj, opts = nil)
271
370
  if State === opts
272
371
  state, opts = opts, nil
@@ -293,10 +392,10 @@ module JSON
293
392
  # :startdoc:
294
393
 
295
394
  class << self
296
- # The global default options for the JSON.load method:
297
- # :max_nesting: false
298
- # :allow_nan: true
299
- # :allow_blank: 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}
300
399
  attr_accessor :load_default_options
301
400
  end
302
401
  self.load_default_options = {
@@ -306,20 +405,134 @@ module JSON
306
405
  :create_additions => true,
307
406
  }
308
407
 
309
- # Load a ruby data structure from a JSON _source_ and return it. A source can
310
- # either be a string-like object, an IO-like object, or an object responding
311
- # to the read method. If _proc_ was given, it will be called with any nested
312
- # Ruby object as an argument recursively in depth first order. To modify the
313
- # default options pass in the optional _options_ argument as well.
408
+ # :call-seq:
409
+ # JSON.load(source, proc = nil, options = {}) -> object
314
410
  #
315
- # BEWARE: This method is meant to serialise data from trusted user input,
316
- # like from your own database server or clients under your control, it could
317
- # be dangerous to allow untrusted users to pass JSON sources into it. The
318
- # default options for the parser can be changed via the load_default_options
319
- # method.
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"]}
460
+ #
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"}>}
320
535
  #
321
- # This method is part of the implementation of the load/dump interface of
322
- # Marshal and YAML.
323
536
  def load(source, proc = nil, options = {})
324
537
  opts = load_default_options.merge options
325
538
  if source.respond_to? :to_str
@@ -338,7 +551,7 @@ module JSON
338
551
  end
339
552
 
340
553
  # Recursively calls passed _Proc_ if the parsed data structure is an _Array_ or _Hash_
341
- def recurse_proc(result, &proc)
554
+ def recurse_proc(result, &proc) # :nodoc:
342
555
  case result
343
556
  when Array
344
557
  result.each { |x| recurse_proc x, &proc }
@@ -355,32 +568,45 @@ module JSON
355
568
  module_function :restore
356
569
 
357
570
  class << self
358
- # The global default options for the JSON.dump method:
359
- # :max_nesting: false
360
- # :allow_nan: true
361
- # :allow_blank: 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}
362
575
  attr_accessor :dump_default_options
363
576
  end
364
577
  self.dump_default_options = {
365
578
  :max_nesting => false,
366
579
  :allow_nan => 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)
585
+ #
586
+ # Dumps +obj+ as a \JSON string, i.e. calls generate on the object and returns the result.
587
+ #
588
+ # The default options can be changed via method JSON.dump_default_options.
371
589
  #
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.
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+.
374
594
  #
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.
595
+ # ---
378
596
  #
379
- # The default options for the generator can be changed via the
380
- # dump_default_options method.
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\"}"
381
601
  #
382
- # This method is part of the implementation of the load/dump interface of
383
- # Marshal and YAML.
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,7 +628,7 @@ module JSON
402
628
  raise ArgumentError, "exceed depth limit"
403
629
  end
404
630
 
405
- # Encodes string using Ruby's _String.encode_
631
+ # Encodes string using String.encode.
406
632
  def self.iconv(to, from, string)
407
633
  string.encode(to, from)
408
634
  end