json 1.6.1-java → 1.6.2-java

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of json might be problematic. Click here for more details.

@@ -0,0 +1,21 @@
1
+ unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
2
+ require 'json'
3
+ end
4
+ defined?(::BigDecimal) or require 'bigdecimal'
5
+
6
+ class BigDecimal
7
+ def self.json_create(object)
8
+ BigDecimal._load object['b']
9
+ end
10
+
11
+ def as_json(*)
12
+ {
13
+ JSON.create_id => self.class.name,
14
+ 'b' => _dump,
15
+ }
16
+ end
17
+
18
+ def to_json(*)
19
+ as_json.to_json
20
+ end
21
+ end
@@ -0,0 +1,31 @@
1
+ unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
2
+ require 'json'
3
+ end
4
+ require 'ostruct'
5
+
6
+ # OpenStruct serialization/deserialization
7
+ class OpenStruct
8
+
9
+ # Deserializes JSON string by constructing new Struct object with values
10
+ # <tt>v</tt> serialized by <tt>to_json</tt>.
11
+ def self.json_create(object)
12
+ new(object['t'] || object[:t])
13
+ end
14
+
15
+ # Returns a hash, that will be turned into a JSON object and represent this
16
+ # object.
17
+ def as_json(*)
18
+ klass = self.class.name
19
+ klass.to_s.empty? and raise JSON::JSONError, "Only named structs are supported!"
20
+ {
21
+ JSON.create_id => klass,
22
+ 't' => table,
23
+ }
24
+ end
25
+
26
+ # Stores class name (OpenStruct) with this struct's values <tt>v</tt> as a
27
+ # JSON string.
28
+ def to_json(*args)
29
+ as_json.to_json(*args)
30
+ end
31
+ end
@@ -10,8 +10,8 @@ class Time
10
10
  if usec = object.delete('u') # used to be tv_usec -> tv_nsec
11
11
  object['n'] = usec * 1000
12
12
  end
13
- if respond_to?(:tv_nsec)
14
- at(*object.values_at('s', 'n'))
13
+ if instance_methods.include?(:tv_nsec)
14
+ at(object['s'], Rational(object['n'], 1000))
15
15
  else
16
16
  at(object['s'], object['n'] / 1000)
17
17
  end
@@ -284,22 +284,39 @@ module JSON
284
284
  module_function :pretty_unparse
285
285
  # :startdoc:
286
286
 
287
+ class << self
288
+ # The global default options for the JSON.load method:
289
+ # :max_nesting: false
290
+ # :allow_nan: true
291
+ # :quirks_mode: true
292
+ attr_accessor :load_default_options
293
+ end
294
+ self.load_default_options = {
295
+ :max_nesting => false,
296
+ :allow_nan => true,
297
+ :quirks_mode => true,
298
+ }
299
+
287
300
  # Load a ruby data structure from a JSON _source_ and return it. A source can
288
301
  # either be a string-like object, an IO-like object, or an object responding
289
302
  # to the read method. If _proc_ was given, it will be called with any nested
290
- # Ruby object as an argument recursively in depth first order.
303
+ # Ruby object as an argument recursively in depth first order. The default
304
+ # options for the parser can be changed via the load_default_options method.
291
305
  #
292
306
  # This method is part of the implementation of the load/dump interface of
293
307
  # Marshal and YAML.
294
308
  def load(source, proc = nil)
309
+ opts = load_default_options
295
310
  if source.respond_to? :to_str
296
311
  source = source.to_str
297
312
  elsif source.respond_to? :to_io
298
313
  source = source.to_io.read
299
- else
314
+ elsif source.respond_to?(:read)
300
315
  source = source.read
316
+ elsif source.nil? && opts[:quirks_mode]
317
+ source = 'null'
301
318
  end
302
- result = parse(source, :max_nesting => false, :allow_nan => true)
319
+ result = parse(source, opts)
303
320
  recurse_proc(result, &proc) if proc
304
321
  result
305
322
  end
@@ -321,6 +338,19 @@ module JSON
321
338
  alias restore load
322
339
  module_function :restore
323
340
 
341
+ class << self
342
+ # The global default options for the JSON.dump method:
343
+ # :max_nesting: false
344
+ # :allow_nan: true
345
+ # :quirks_mode: true
346
+ attr_accessor :dump_default_options
347
+ end
348
+ self.dump_default_options = {
349
+ :max_nesting => false,
350
+ :allow_nan => true,
351
+ :quirks_mode => true,
352
+ }
353
+
324
354
  # Dumps _obj_ as a JSON string, i.e. calls generate on the object and returns
325
355
  # the result.
326
356
  #
@@ -331,6 +361,9 @@ module JSON
331
361
  # exception is raised. This argument is similar (but not exactly the
332
362
  # same!) to the _limit_ argument in Marshal.dump.
333
363
  #
364
+ # The default options for the generator can be changed via the
365
+ # dump_default_options method.
366
+ #
334
367
  # This method is part of the implementation of the load/dump interface of
335
368
  # Marshal and YAML.
336
369
  def dump(obj, anIO = nil, limit = nil)
@@ -341,8 +374,9 @@ module JSON
341
374
  anIO = nil
342
375
  end
343
376
  end
344
- limit ||= 0
345
- result = generate(obj, :allow_nan => true, :max_nesting => limit)
377
+ opts = JSON.dump_default_options
378
+ limit and opts.update(:max_nesting => limit)
379
+ result = generate(obj, opts)
346
380
  if anIO
347
381
  anIO.write result
348
382
  anIO
@@ -1,3 +1,9 @@
1
+ if ENV['SIMPLECOV_COVERAGE'].to_i == 1
2
+ require 'simplecov'
3
+ SimpleCov.start do
4
+ add_filter "/tests/"
5
+ end
6
+ end
1
7
  require 'json/common'
2
8
 
3
9
  module JSON
Binary file
Binary file
@@ -1,3 +1,9 @@
1
+ if ENV['SIMPLECOV_COVERAGE'].to_i == 1
2
+ require 'simplecov'
3
+ SimpleCov.start do
4
+ add_filter "/tests/"
5
+ end
6
+ end
1
7
  require 'json/common'
2
8
  require 'json/pure/parser'
3
9
  require 'json/pure/generator'
@@ -136,14 +136,15 @@ module JSON
136
136
  # * *quirks_mode*: Enables quirks_mode for parser, that is for example
137
137
  # generating single JSON values instead of documents is possible.
138
138
  def initialize(opts = {})
139
- @indent = ''
140
- @space = ''
141
- @space_before = ''
142
- @object_nl = ''
143
- @array_nl = ''
144
- @allow_nan = false
145
- @ascii_only = false
146
- @quirks_mode = false
139
+ @indent = ''
140
+ @space = ''
141
+ @space_before = ''
142
+ @object_nl = ''
143
+ @array_nl = ''
144
+ @allow_nan = false
145
+ @ascii_only = false
146
+ @quirks_mode = false
147
+ @buffer_initial_length = 1024
147
148
  configure opts
148
149
  end
149
150
 
@@ -172,6 +173,16 @@ module JSON
172
173
  # it's disabled.
173
174
  attr_accessor :quirks_mode
174
175
 
176
+ # :stopdoc:
177
+ attr_reader :buffer_initial_length
178
+
179
+ def buffer_initial_length=(length)
180
+ if length > 0
181
+ @buffer_initial_length = length
182
+ end
183
+ end
184
+ # :startdoc:
185
+
175
186
  # This integer returns the current depth data structure nesting in the
176
187
  # generated JSON.
177
188
  attr_accessor :depth
@@ -233,7 +244,7 @@ module JSON
233
244
  # passed to the configure method.
234
245
  def to_h
235
246
  result = {}
236
- for iv in %w[indent space space_before object_nl array_nl allow_nan max_nesting ascii_only quirks_mode depth]
247
+ for iv in %w[indent space space_before object_nl array_nl allow_nan max_nesting ascii_only quirks_mode buffer_initial_length depth]
237
248
  result[iv.intern] = instance_variable_get("@#{iv}")
238
249
  end
239
250
  result
@@ -73,7 +73,7 @@ module JSON
73
73
  def initialize(source, opts = {})
74
74
  opts ||= {}
75
75
  unless @quirks_mode = opts[:quirks_mode]
76
- source = determine_encoding source
76
+ source = convert_encoding source
77
77
  end
78
78
  super source
79
79
  if !opts.key?(:max_nesting) # defaults to 19
@@ -145,7 +145,12 @@ module JSON
145
145
 
146
146
  private
147
147
 
148
- def determine_encoding(source)
148
+ def convert_encoding(source)
149
+ if source.respond_to?(:to_str)
150
+ source = source.to_str
151
+ else
152
+ raise TypeError, "#{source.inspect} is not like a string"
153
+ end
149
154
  if defined?(::Encoding)
150
155
  if source.encoding == ::Encoding::ASCII_8BIT
151
156
  b = source[0, 4].bytes.to_a
@@ -1,6 +1,6 @@
1
1
  module JSON
2
2
  # JSON version
3
- VERSION = '1.6.1'
3
+ VERSION = '1.6.2'
4
4
  VERSION_ARRAY = VERSION.split(/\./).map { |x| x.to_i } # :nodoc:
5
5
  VERSION_MAJOR = VERSION_ARRAY[0] # :nodoc:
6
6
  VERSION_MINOR = VERSION_ARRAY[1] # :nodoc:
@@ -4,6 +4,7 @@
4
4
  require 'test/unit'
5
5
  require File.join(File.dirname(__FILE__), 'setup_variant')
6
6
  require 'stringio'
7
+ require 'tempfile'
7
8
 
8
9
  unless Array.method_defined?(:permutation)
9
10
  begin
@@ -107,6 +108,8 @@ class TC_JSON < Test::Unit::TestCase
107
108
  def test_parse_json_primitive_values
108
109
  assert_raise(JSON::ParserError) { JSON.parse('') }
109
110
  assert_raise(JSON::ParserError) { JSON.parse('', :quirks_mode => true) }
111
+ assert_raise(TypeError) { JSON.parse(nil) }
112
+ assert_raise(TypeError) { JSON.parse(nil, :quirks_mode => true) }
110
113
  assert_raise(JSON::ParserError) { JSON.parse(' /* foo */ ') }
111
114
  assert_raise(JSON::ParserError) { JSON.parse(' /* foo */ ', :quirks_mode => true) }
112
115
  parser = JSON::Parser.new('null')
@@ -414,7 +417,19 @@ EOT
414
417
  JSON.parse('{"foo":"bar", "baz":"quux"}', :symbolize_names => true))
415
418
  end
416
419
 
417
- def test_load_dump
420
+ def test_load
421
+ assert_equal @hash, JSON.load(@json)
422
+ tempfile = Tempfile.open('json')
423
+ tempfile.write @json
424
+ tempfile.rewind
425
+ assert_equal @hash, JSON.load(tempfile)
426
+ stringio = StringIO.new(@json)
427
+ stringio.rewind
428
+ assert_equal @hash, JSON.load(stringio)
429
+ assert_equal nil, JSON.load(nil)
430
+ end
431
+
432
+ def test_dump
418
433
  too_deep = '[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]'
419
434
  assert_equal too_deep, JSON.dump(eval(too_deep))
420
435
  assert_kind_of String, Marshal.dump(eval(too_deep))
@@ -6,6 +6,8 @@ require File.join(File.dirname(__FILE__), 'setup_variant')
6
6
  require 'json/add/core'
7
7
  require 'json/add/complex'
8
8
  require 'json/add/rational'
9
+ require 'json/add/bigdecimal'
10
+ require 'json/add/ostruct'
9
11
  require 'date'
10
12
 
11
13
  class TC_JSONAddition < Test::Unit::TestCase
@@ -128,7 +130,7 @@ class TC_JSONAddition < Test::Unit::TestCase
128
130
 
129
131
  def test_core
130
132
  t = Time.now
131
- assert_equal t.inspect, JSON(JSON(t)).inspect
133
+ assert_equal t, JSON(JSON(t))
132
134
  d = Date.today
133
135
  assert_equal d, JSON(JSON(d))
134
136
  d = DateTime.civil(2007, 6, 14, 14, 57, 10, Rational(1, 12), 2299161)
@@ -171,4 +173,16 @@ class TC_JSONAddition < Test::Unit::TestCase
171
173
  assert_equal Rational(2, 9), JSON(JSON(Rational(2, 9)))
172
174
  assert_equal Complex(2, 9), JSON(JSON(Complex(2, 9)))
173
175
  end
176
+
177
+ def test_bigdecimal
178
+ assert_equal BigDecimal('3.141', 23), JSON(JSON(BigDecimal('3.141', 23)))
179
+ assert_equal BigDecimal('3.141', 666), JSON(JSON(BigDecimal('3.141', 666)))
180
+ end
181
+
182
+ def test_ostruct
183
+ o = OpenStruct.new
184
+ # XXX this won't work; o.foo = { :bar => true }
185
+ o.foo = { 'bar' => true }
186
+ assert_equal o, JSON(JSON(o))
187
+ end
174
188
  end
@@ -43,6 +43,8 @@ EOT
43
43
  def test_generate
44
44
  json = generate(@hash)
45
45
  assert_equal(JSON.parse(@json2), JSON.parse(json))
46
+ json = JSON[@hash]
47
+ assert_equal(JSON.parse(@json2), JSON.parse(json))
46
48
  parsed_json = parse(json)
47
49
  assert_equal(@hash, parsed_json)
48
50
  json = generate({1=>2})
@@ -121,48 +123,51 @@ EOT
121
123
  def test_pretty_state
122
124
  state = PRETTY_STATE_PROTOTYPE.dup
123
125
  assert_equal({
124
- :allow_nan => false,
125
- :array_nl => "\n",
126
- :ascii_only => false,
127
- :quirks_mode => false,
128
- :depth => 0,
129
- :indent => " ",
130
- :max_nesting => 19,
131
- :object_nl => "\n",
132
- :space => " ",
133
- :space_before => "",
126
+ :allow_nan => false,
127
+ :array_nl => "\n",
128
+ :ascii_only => false,
129
+ :buffer_initial_length => 1024,
130
+ :quirks_mode => false,
131
+ :depth => 0,
132
+ :indent => " ",
133
+ :max_nesting => 19,
134
+ :object_nl => "\n",
135
+ :space => " ",
136
+ :space_before => "",
134
137
  }.sort_by { |n,| n.to_s }, state.to_h.sort_by { |n,| n.to_s })
135
138
  end
136
139
 
137
140
  def test_safe_state
138
141
  state = SAFE_STATE_PROTOTYPE.dup
139
142
  assert_equal({
140
- :allow_nan => false,
141
- :array_nl => "",
142
- :ascii_only => false,
143
- :quirks_mode => false,
144
- :depth => 0,
145
- :indent => "",
146
- :max_nesting => 19,
147
- :object_nl => "",
148
- :space => "",
149
- :space_before => "",
143
+ :allow_nan => false,
144
+ :array_nl => "",
145
+ :ascii_only => false,
146
+ :buffer_initial_length => 1024,
147
+ :quirks_mode => false,
148
+ :depth => 0,
149
+ :indent => "",
150
+ :max_nesting => 19,
151
+ :object_nl => "",
152
+ :space => "",
153
+ :space_before => "",
150
154
  }.sort_by { |n,| n.to_s }, state.to_h.sort_by { |n,| n.to_s })
151
155
  end
152
156
 
153
157
  def test_fast_state
154
158
  state = FAST_STATE_PROTOTYPE.dup
155
159
  assert_equal({
156
- :allow_nan => false,
157
- :array_nl => "",
158
- :ascii_only => false,
159
- :quirks_mode => false,
160
- :depth => 0,
161
- :indent => "",
162
- :max_nesting => 0,
163
- :object_nl => "",
164
- :space => "",
165
- :space_before => "",
160
+ :allow_nan => false,
161
+ :array_nl => "",
162
+ :ascii_only => false,
163
+ :buffer_initial_length => 1024,
164
+ :quirks_mode => false,
165
+ :depth => 0,
166
+ :indent => "",
167
+ :max_nesting => 0,
168
+ :object_nl => "",
169
+ :space => "",
170
+ :space_before => "",
166
171
  }.sort_by { |n,| n.to_s }, state.to_h.sort_by { |n,| n.to_s })
167
172
  end
168
173
 
@@ -198,6 +203,17 @@ EOT
198
203
  assert_equal 19, s.depth
199
204
  end
200
205
 
206
+ def test_buffer_initial_length
207
+ s = JSON.state.new
208
+ assert_equal 1024, s.buffer_initial_length
209
+ s.buffer_initial_length = 0
210
+ assert_equal 1024, s.buffer_initial_length
211
+ s.buffer_initial_length = -1
212
+ assert_equal 1024, s.buffer_initial_length
213
+ s.buffer_initial_length = 128
214
+ assert_equal 128, s.buffer_initial_length
215
+ end
216
+
201
217
  def test_gc
202
218
  bignum_too_long_to_embed_as_string = 1234567890123456789012345
203
219
  expect = bignum_too_long_to_embed_as_string.to_s
@@ -210,4 +226,24 @@ EOT
210
226
  ensure
211
227
  GC.stress = stress
212
228
  end if GC.respond_to?(:stress=)
229
+
230
+ def test_broken_bignum # [ruby-core:38867]
231
+ pid = fork do
232
+ Bignum.class_eval do
233
+ def to_s
234
+ end
235
+ end
236
+ begin
237
+ JSON::Ext::Generator::State.new.generate(1<<64)
238
+ exit 1
239
+ rescue TypeError
240
+ exit 0
241
+ end
242
+ end
243
+ _, status = Process.waitpid2(pid)
244
+ assert status.success?
245
+ rescue NotImplementedError
246
+ # forking to avoid modifying core class of a parent process and
247
+ # introducing race conditions of tests are run in parallel
248
+ end if defined?(JSON::Ext)
213
249
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: json
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 1.6.1
5
+ version: 1.6.2
6
6
  platform: java
7
7
  authors:
8
8
  - Daniel Luz
@@ -10,8 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-09-18 00:00:00 +02:00
14
- default_executable:
13
+ date: 2011-11-28 00:00:00 Z
15
14
  dependencies: []
16
15
 
17
16
  description: A JSON implementation as a JRuby extension.
@@ -34,11 +33,13 @@ files:
34
33
  - lib/json/add/rational.rb
35
34
  - lib/json/add/exception.rb
36
35
  - lib/json/add/time.rb
36
+ - lib/json/add/bigdecimal.rb
37
37
  - lib/json/add/date_time.rb
38
38
  - lib/json/add/core.rb
39
39
  - lib/json/add/range.rb
40
40
  - lib/json/add/date.rb
41
41
  - lib/json/add/regexp.rb
42
+ - lib/json/add/ostruct.rb
42
43
  - lib/json/pure/generator.rb
43
44
  - lib/json/pure/parser.rb
44
45
  - lib/json/ext/generator.jar
@@ -82,7 +83,6 @@ files:
82
83
  - tests/fixtures/fail22.json
83
84
  - tests/fixtures/fail27.json
84
85
  - tests/fixtures/fail19.json
85
- has_rdoc: true
86
86
  homepage: http://json-jruby.rubyforge.org/
87
87
  licenses: []
88
88
 
@@ -106,7 +106,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
106
106
  requirements: []
107
107
 
108
108
  rubyforge_project: json-jruby
109
- rubygems_version: 1.5.1
109
+ rubygems_version: 1.8.9
110
110
  signing_key:
111
111
  specification_version: 3
112
112
  summary: JSON implementation for JRuby