json 1.7.7-java → 1.8.0-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.

Binary file
Binary file
@@ -31,6 +31,15 @@ module JSON
31
31
  object
32
32
  end
33
33
  end
34
+
35
+ def load(source, proc = nil, opts = {})
36
+ result = ::JSON.load(source, proc, opts.merge(:object_class => self))
37
+ result.nil? ? new : result
38
+ end
39
+
40
+ def dump(obj, *args)
41
+ ::JSON.dump(obj, *args)
42
+ end
34
43
  end
35
44
  self.json_creatable = false
36
45
 
@@ -70,6 +70,13 @@ module JSON
70
70
  rescue => e
71
71
  raise GeneratorError.wrap(e)
72
72
  end
73
+
74
+ def valid_utf8?(string)
75
+ encoding = string.encoding
76
+ (encoding == Encoding::UTF_8 || encoding == Encoding::ASCII) &&
77
+ string.valid_encoding?
78
+ end
79
+ module_function :valid_utf8?
73
80
  else
74
81
  def utf8_to_json(string) # :nodoc:
75
82
  string.gsub(/["\\\x0-\x1f]/n) { MAP[$&] }
@@ -93,8 +100,22 @@ module JSON
93
100
  rescue => e
94
101
  raise GeneratorError.wrap(e)
95
102
  end
103
+
104
+ def valid_utf8?(string)
105
+ string =~
106
+ /\A( [\x09\x0a\x0d\x20-\x7e] # ASCII
107
+ | [\xc2-\xdf][\x80-\xbf] # non-overlong 2-byte
108
+ | \xe0[\xa0-\xbf][\x80-\xbf] # excluding overlongs
109
+ | [\xe1-\xec\xee\xef][\x80-\xbf]{2} # straight 3-byte
110
+ | \xed[\x80-\x9f][\x80-\xbf] # excluding surrogates
111
+ | \xf0[\x90-\xbf][\x80-\xbf]{2} # planes 1-3
112
+ | [\xf1-\xf3][\x80-\xbf]{3} # planes 4-15
113
+ | \xf4[\x80-\x8f][\x80-\xbf]{2} # plane 16
114
+ )*\z/nx
115
+ end
96
116
  end
97
- module_function :utf8_to_json, :utf8_to_json_ascii
117
+ module_function :utf8_to_json, :utf8_to_json_ascii, :valid_utf8?
118
+
98
119
 
99
120
  module Pure
100
121
  module Generator
@@ -220,6 +241,13 @@ module JSON
220
241
  # Configure this State instance with the Hash _opts_, and return
221
242
  # itself.
222
243
  def configure(opts)
244
+ if opts.respond_to?(:to_hash)
245
+ opts = opts.to_hash
246
+ elsif opts.respond_to?(:to_h)
247
+ opts = opts.to_h
248
+ else
249
+ raise TypeError, "can't convert #{opts.class} into Hash"
250
+ end
223
251
  for key, value in opts
224
252
  instance_variable_set "@#{key}", value
225
253
  end
@@ -263,6 +291,8 @@ module JSON
263
291
  # GeneratorError exception.
264
292
  def generate(obj)
265
293
  result = obj.to_json(self)
294
+ JSON.valid_utf8?(result) or raise GeneratorError,
295
+ "source sequence #{result.inspect} is illegal/malformed utf-8"
266
296
  unless @quirks_mode
267
297
  unless result =~ /\A\s*\[/ && result =~ /\]\s*\Z/ ||
268
298
  result =~ /\A\s*\{/ && result =~ /\}\s*\Z/
@@ -338,7 +368,7 @@ module JSON
338
368
  }
339
369
  depth = state.depth -= 1
340
370
  result << state.object_nl
341
- result << state.indent * depth if indent if indent
371
+ result << state.indent * depth if indent
342
372
  result << '}'
343
373
  result
344
374
  end
@@ -1,6 +1,6 @@
1
1
  module JSON
2
2
  # JSON version
3
- VERSION = '1.7.7'
3
+ VERSION = '1.8.0'
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:
@@ -251,6 +251,22 @@ EOT
251
251
  assert_equal '5', state2.array_nl
252
252
  end
253
253
 
254
+ def test_configure_hash_conversion
255
+ state = JSON.state.new
256
+ state.configure(:indent => '1')
257
+ assert_equal '1', state.indent
258
+ state = JSON.state.new
259
+ foo = 'foo'
260
+ assert_raise(TypeError) do
261
+ state.configure(foo)
262
+ end
263
+ def foo.to_h
264
+ { :indent => '2' }
265
+ end
266
+ state.configure(foo)
267
+ assert_equal '2', state.indent
268
+ end
269
+
254
270
  if defined?(JSON::Ext::Generator)
255
271
  def test_broken_bignum # [ruby-core:38867]
256
272
  pid = fork do
@@ -297,4 +313,10 @@ EOT
297
313
  assert_kind_of Hash, state_hash
298
314
  assert_equal :bar, state_hash[:foo]
299
315
  end
316
+
317
+ def test_json_generate
318
+ assert_raise JSON::GeneratorError do
319
+ assert_equal true, JSON.generate(["\xea"])
320
+ end
321
+ end
300
322
  end
@@ -49,6 +49,21 @@ class TestJSONGenericObject < Test::Unit::TestCase
49
49
  assert_equal true, GenericObject.from_hash(true)
50
50
  end
51
51
 
52
+ def test_json_generic_object_load
53
+ empty = JSON::GenericObject.load(nil)
54
+ assert_kind_of JSON::GenericObject, empty
55
+ simple_json = '{"json_class":"JSON::GenericObject","hello":"world"}'
56
+ simple = JSON::GenericObject.load(simple_json)
57
+ assert_kind_of JSON::GenericObject, simple
58
+ assert_equal "world", simple.hello
59
+ converting = JSON::GenericObject.load('{ "hello": "world" }')
60
+ assert_kind_of JSON::GenericObject, converting
61
+ assert_equal "world", converting.hello
62
+
63
+ json = JSON::GenericObject.dump(JSON::GenericObject[:hello => 'world'])
64
+ assert_equal JSON(json), JSON('{"json_class":"JSON::GenericObject","hello":"world"}')
65
+ end
66
+
52
67
  private
53
68
 
54
69
  def switch_json_creatable
metadata CHANGED
@@ -2,14 +2,14 @@
2
2
  name: json
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 1.7.7
5
+ version: 1.8.0
6
6
  platform: java
7
7
  authors:
8
8
  - Daniel Luz
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-02-11 00:00:00.000000000 Z
12
+ date: 2013-05-13 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: A JSON implementation as a JRuby extension.
15
15
  email: dev+ruby@mernen.com