json 1.8.6-java → 2.0.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.

Files changed (42) hide show
  1. checksums.yaml +4 -4
  2. data/lib/json.rb +1 -0
  3. data/lib/json/add/bigdecimal.rb +1 -0
  4. data/lib/json/add/complex.rb +2 -1
  5. data/lib/json/add/core.rb +1 -0
  6. data/lib/json/add/date.rb +1 -1
  7. data/lib/json/add/date_time.rb +1 -1
  8. data/lib/json/add/exception.rb +1 -1
  9. data/lib/json/add/ostruct.rb +1 -1
  10. data/lib/json/add/range.rb +1 -1
  11. data/lib/json/add/rational.rb +1 -0
  12. data/lib/json/add/regexp.rb +1 -1
  13. data/lib/json/add/struct.rb +1 -1
  14. data/lib/json/add/symbol.rb +1 -1
  15. data/lib/json/add/time.rb +1 -1
  16. data/lib/json/common.rb +24 -52
  17. data/lib/json/ext.rb +0 -6
  18. data/lib/json/ext/generator.jar +0 -0
  19. data/lib/json/ext/parser.jar +0 -0
  20. data/lib/json/generic_object.rb +5 -4
  21. data/lib/json/pure.rb +2 -8
  22. data/lib/json/pure/generator.rb +51 -123
  23. data/lib/json/pure/parser.rb +28 -80
  24. data/lib/json/version.rb +2 -1
  25. data/tests/fixtures/obsolete_fail1.json +1 -0
  26. data/tests/{test_json_addition.rb → json_addition_test.rb} +22 -25
  27. data/tests/json_common_interface_test.rb +126 -0
  28. data/tests/json_encoding_test.rb +105 -0
  29. data/tests/json_ext_parser_test.rb +15 -0
  30. data/tests/{test_json_fixtures.rb → json_fixtures_test.rb} +5 -8
  31. data/tests/{test_json_generate.rb → json_generator_test.rb} +65 -37
  32. data/tests/{test_json_generic_object.rb → json_generic_object_test.rb} +15 -8
  33. data/tests/json_parser_test.rb +448 -0
  34. data/tests/json_string_matching_test.rb +38 -0
  35. data/tests/test_helper.rb +23 -0
  36. metadata +14 -13
  37. data/tests/fixtures/fail1.json +0 -1
  38. data/tests/setup_variant.rb +0 -11
  39. data/tests/test_json.rb +0 -519
  40. data/tests/test_json_encoding.rb +0 -65
  41. data/tests/test_json_string_matching.rb +0 -39
  42. data/tests/test_json_unicode.rb +0 -72
@@ -1,3 +1,4 @@
1
+ #frozen_string_literal: false
1
2
  require 'strscan'
2
3
 
3
4
  module JSON
@@ -48,7 +49,7 @@ module JSON
48
49
  )+
49
50
  )mx
50
51
 
51
- UNPARSED = Object.new
52
+ UNPARSED = Object.new.freeze
52
53
 
53
54
  # Creates a new JSON::Pure::Parser instance for the string _source_.
54
55
  #
@@ -58,23 +59,20 @@ module JSON
58
59
  # structures. Disable depth checking with :max_nesting => false|nil|0,
59
60
  # it defaults to 100.
60
61
  # * *allow_nan*: If set to true, allow NaN, Infinity and -Infinity in
61
- # defiance of RFC 4627 to be parsed by the Parser. This option defaults
62
+ # defiance of RFC 7159 to be parsed by the Parser. This option defaults
62
63
  # to false.
63
64
  # * *symbolize_names*: If set to true, returns symbols for the names
64
- # (keys) in a JSON object. Otherwise strings are returned, which is also
65
- # the default.
65
+ # (keys) in a JSON object. Otherwise strings are returned, which is
66
+ # also the default. It's not possible to use this option in
67
+ # conjunction with the *create_additions* option.
66
68
  # * *create_additions*: If set to true, the Parser creates
67
69
  # additions when if a matching class and create_id was found. This
68
70
  # option defaults to false.
69
71
  # * *object_class*: Defaults to Hash
70
72
  # * *array_class*: Defaults to Array
71
- # * *quirks_mode*: Enables quirks_mode for parser, that is for example
72
- # parsing single JSON values instead of documents is possible.
73
73
  def initialize(source, opts = {})
74
74
  opts ||= {}
75
- unless @quirks_mode = opts[:quirks_mode]
76
- source = convert_encoding source
77
- end
75
+ source = convert_encoding source
78
76
  super source
79
77
  if !opts.key?(:max_nesting) # defaults to 100
80
78
  @max_nesting = 100
@@ -90,6 +88,9 @@ module JSON
90
88
  else
91
89
  @create_additions = false
92
90
  end
91
+ @symbolize_names && @create_additions and raise ArgumentError,
92
+ 'options :symbolize_names and :create_additions cannot be used '\
93
+ 'in conjunction'
93
94
  @create_id = @create_additions ? JSON.create_id : nil
94
95
  @object_class = opts[:object_class] || Hash
95
96
  @array_class = opts[:array_class] || Array
@@ -98,48 +99,26 @@ module JSON
98
99
 
99
100
  alias source string
100
101
 
101
- def quirks_mode?
102
- !!@quirks_mode
103
- end
104
-
105
102
  def reset
106
103
  super
107
104
  @current_nesting = 0
108
105
  end
109
106
 
110
- # Parses the current JSON string _source_ and returns the complete data
111
- # structure as a result.
107
+ # Parses the current JSON string _source_ and returns the
108
+ # complete data structure as a result.
112
109
  def parse
113
110
  reset
114
111
  obj = nil
115
- if @quirks_mode
116
- while !eos? && skip(IGNORE)
117
- end
118
- if eos?
119
- raise ParserError, "source did not contain any JSON!"
120
- else
121
- obj = parse_value
122
- obj == UNPARSED and raise ParserError, "source did not contain any JSON!"
123
- end
112
+ while !eos? && skip(IGNORE) do end
113
+ if eos?
114
+ raise ParserError, "source is not valid JSON!"
124
115
  else
125
- until eos?
126
- case
127
- when scan(OBJECT_OPEN)
128
- obj and raise ParserError, "source '#{peek(20)}' not in JSON!"
129
- @current_nesting = 1
130
- obj = parse_object
131
- when scan(ARRAY_OPEN)
132
- obj and raise ParserError, "source '#{peek(20)}' not in JSON!"
133
- @current_nesting = 1
134
- obj = parse_array
135
- when skip(IGNORE)
136
- ;
137
- else
138
- raise ParserError, "source '#{peek(20)}' not in JSON!"
139
- end
140
- end
141
- obj or raise ParserError, "source did not contain any JSON!"
116
+ obj = parse_value
117
+ UNPARSED.equal?(obj) and raise ParserError,
118
+ "source is not valid JSON!"
142
119
  end
120
+ while !eos? && skip(IGNORE) do end
121
+ eos? or raise ParserError, "source is not valid JSON!"
143
122
  obj
144
123
  end
145
124
 
@@ -149,43 +128,12 @@ module JSON
149
128
  if source.respond_to?(:to_str)
150
129
  source = source.to_str
151
130
  else
152
- raise TypeError, "#{source.inspect} is not like a string"
131
+ raise TypeError,
132
+ "#{source.inspect} is not like a string"
153
133
  end
154
- if defined?(::Encoding)
155
- if source.encoding == ::Encoding::ASCII_8BIT
156
- b = source[0, 4].bytes.to_a
157
- source =
158
- case
159
- when b.size >= 4 && b[0] == 0 && b[1] == 0 && b[2] == 0
160
- source.dup.force_encoding(::Encoding::UTF_32BE).encode!(::Encoding::UTF_8)
161
- when b.size >= 4 && b[0] == 0 && b[2] == 0
162
- source.dup.force_encoding(::Encoding::UTF_16BE).encode!(::Encoding::UTF_8)
163
- when b.size >= 4 && b[1] == 0 && b[2] == 0 && b[3] == 0
164
- source.dup.force_encoding(::Encoding::UTF_32LE).encode!(::Encoding::UTF_8)
165
- when b.size >= 4 && b[1] == 0 && b[3] == 0
166
- source.dup.force_encoding(::Encoding::UTF_16LE).encode!(::Encoding::UTF_8)
167
- else
168
- source.dup
169
- end
170
- else
171
- source = source.encode(::Encoding::UTF_8)
172
- end
134
+ if source.encoding != ::Encoding::ASCII_8BIT
135
+ source = source.encode(::Encoding::UTF_8)
173
136
  source.force_encoding(::Encoding::ASCII_8BIT)
174
- else
175
- b = source
176
- source =
177
- case
178
- when b.size >= 4 && b[0] == 0 && b[1] == 0 && b[2] == 0
179
- JSON.iconv('utf-8', 'utf-32be', b)
180
- when b.size >= 4 && b[0] == 0 && b[2] == 0
181
- JSON.iconv('utf-8', 'utf-16be', b)
182
- when b.size >= 4 && b[1] == 0 && b[2] == 0 && b[3] == 0
183
- JSON.iconv('utf-8', 'utf-32le', b)
184
- when b.size >= 4 && b[1] == 0 && b[3] == 0
185
- JSON.iconv('utf-8', 'utf-16le', b)
186
- else
187
- b
188
- end
189
137
  end
190
138
  source
191
139
  end
@@ -254,7 +202,7 @@ module JSON
254
202
  false
255
203
  when scan(NULL)
256
204
  nil
257
- when (string = parse_string) != UNPARSED
205
+ when !UNPARSED.equal?(string = parse_string)
258
206
  string
259
207
  when scan(ARRAY_OPEN)
260
208
  @current_nesting += 1
@@ -284,7 +232,7 @@ module JSON
284
232
  delim = false
285
233
  until eos?
286
234
  case
287
- when (value = parse_value) != UNPARSED
235
+ when !UNPARSED.equal?(value = parse_value)
288
236
  delim = false
289
237
  result << value
290
238
  skip(IGNORE)
@@ -316,13 +264,13 @@ module JSON
316
264
  delim = false
317
265
  until eos?
318
266
  case
319
- when (string = parse_string) != UNPARSED
267
+ when !UNPARSED.equal?(string = parse_string)
320
268
  skip(IGNORE)
321
269
  unless scan(PAIR_DELIMITER)
322
270
  raise ParserError, "expected ':' in object at '#{peek(20)}'!"
323
271
  end
324
272
  skip(IGNORE)
325
- unless (value = parse_value).equal? UNPARSED
273
+ unless UNPARSED.equal?(value = parse_value)
326
274
  result[@symbolize_names ? string.to_sym : string] = value
327
275
  delim = false
328
276
  skip(IGNORE)
@@ -1,6 +1,7 @@
1
+ # frozen_string_literal: false
1
2
  module JSON
2
3
  # JSON version
3
- VERSION = '1.8.6'
4
+ VERSION = '2.0.0'
4
5
  VERSION_ARRAY = VERSION.split(/\./).map { |x| x.to_i } # :nodoc:
5
6
  VERSION_MAJOR = VERSION_ARRAY[0] # :nodoc:
6
7
  VERSION_MINOR = VERSION_ARRAY[1] # :nodoc:
@@ -0,0 +1 @@
1
+ "A JSON payload should be an object or array, not a string."
@@ -1,8 +1,5 @@
1
- #!/usr/bin/env ruby
2
- # -*- coding:utf-8 -*-
3
-
4
- require 'test/unit'
5
- require File.join(File.dirname(__FILE__), 'setup_variant')
1
+ #frozen_string_literal: false
2
+ require 'test_helper'
6
3
  require 'json/add/core'
7
4
  require 'json/add/complex'
8
5
  require 'json/add/rational'
@@ -10,7 +7,7 @@ require 'json/add/bigdecimal'
10
7
  require 'json/add/ostruct'
11
8
  require 'date'
12
9
 
13
- class TestJSONAddition < Test::Unit::TestCase
10
+ class JSONAdditionTest < Test::Unit::TestCase
14
11
  include JSON
15
12
 
16
13
  class A
@@ -64,7 +61,7 @@ class TestJSONAddition < Test::Unit::TestCase
64
61
 
65
62
  def to_json(*args)
66
63
  {
67
- 'json_class' => 'TestJSONAddition::Nix',
64
+ 'json_class' => 'JSONAdditionTest::Nix',
68
65
  }.to_json(*args)
69
66
  end
70
67
  end
@@ -73,7 +70,7 @@ class TestJSONAddition < Test::Unit::TestCase
73
70
  a = A.new(666)
74
71
  assert A.json_creatable?
75
72
  json = generate(a)
76
- a_again = JSON.parse(json, :create_additions => true)
73
+ a_again = parse(json, :create_additions => true)
77
74
  assert_kind_of a.class, a_again
78
75
  assert_equal a, a_again
79
76
  end
@@ -82,7 +79,7 @@ class TestJSONAddition < Test::Unit::TestCase
82
79
  a = A.new(666)
83
80
  assert A.json_creatable?
84
81
  json = generate(a)
85
- a_hash = JSON.parse(json)
82
+ a_hash = parse(json)
86
83
  assert_kind_of Hash, a_hash
87
84
  end
88
85
 
@@ -90,13 +87,13 @@ class TestJSONAddition < Test::Unit::TestCase
90
87
  a = A.new(666)
91
88
  assert A.json_creatable?
92
89
  json = generate(a)
93
- a_again = JSON.parse(json, :create_additions => true)
90
+ a_again = parse(json, :create_additions => true)
94
91
  assert_kind_of a.class, a_again
95
92
  assert_equal a, a_again
96
- a_hash = JSON.parse(json, :create_additions => false)
93
+ a_hash = parse(json, :create_additions => false)
97
94
  assert_kind_of Hash, a_hash
98
95
  assert_equal(
99
- {"args"=>[666], "json_class"=>"TestJSONAddition::A"}.sort_by { |k,| k },
96
+ {"args"=>[666], "json_class"=>"JSONAdditionTest::A"}.sort_by { |k,| k },
100
97
  a_hash.sort_by { |k,| k }
101
98
  )
102
99
  end
@@ -105,14 +102,14 @@ class TestJSONAddition < Test::Unit::TestCase
105
102
  b = B.new
106
103
  assert !B.json_creatable?
107
104
  json = generate(b)
108
- assert_equal({ "json_class"=>"TestJSONAddition::B" }, JSON.parse(json))
105
+ assert_equal({ "json_class"=>"JSONAdditionTest::B" }, parse(json))
109
106
  end
110
107
 
111
108
  def test_extended_json_fail2
112
109
  c = C.new
113
110
  assert !C.json_creatable?
114
111
  json = generate(c)
115
- assert_raises(ArgumentError, NameError) { JSON.parse(json, :create_additions => true) }
112
+ assert_raise(ArgumentError, NameError) { parse(json, :create_additions => true) }
116
113
  end
117
114
 
118
115
  def test_raw_strings
@@ -130,7 +127,7 @@ class TestJSONAddition < Test::Unit::TestCase
130
127
  assert_match(/\A\{.*\}\z/, json)
131
128
  assert_match(/"json_class":"String"/, json)
132
129
  assert_match(/"raw":\[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255\]/, json)
133
- raw_again = JSON.parse(json, :create_additions => true)
130
+ raw_again = parse(json, :create_additions => true)
134
131
  assert_equal raw, raw_again
135
132
  end
136
133
 
@@ -151,7 +148,7 @@ class TestJSONAddition < Test::Unit::TestCase
151
148
  assert_equal s, JSON(JSON(s), :create_additions => true)
152
149
  struct = Struct.new :foo, :bar
153
150
  s = struct.new 4711, 'foot'
154
- assert_raises(JSONError) { JSON(s) }
151
+ assert_raise(JSONError) { JSON(s) }
155
152
  begin
156
153
  raise TypeError, "test me"
157
154
  rescue TypeError => e
@@ -167,19 +164,19 @@ class TestJSONAddition < Test::Unit::TestCase
167
164
 
168
165
  def test_utc_datetime
169
166
  now = Time.now
170
- d = DateTime.parse(now.to_s, :create_additions => true) # usual case
171
- assert_equal d, JSON.parse(d.to_json, :create_additions => true)
172
- d = DateTime.parse(now.utc.to_s) # of = 0
173
- assert_equal d, JSON.parse(d.to_json, :create_additions => true)
167
+ d = DateTime.parse(now.to_s, :create_additions => true) # usual case
168
+ assert_equal d, parse(d.to_json, :create_additions => true)
169
+ d = DateTime.parse(now.utc.to_s) # of = 0
170
+ assert_equal d, parse(d.to_json, :create_additions => true)
174
171
  d = DateTime.civil(2008, 6, 17, 11, 48, 32, Rational(1,24))
175
- assert_equal d, JSON.parse(d.to_json, :create_additions => true)
172
+ assert_equal d, parse(d.to_json, :create_additions => true)
176
173
  d = DateTime.civil(2008, 6, 17, 11, 48, 32, Rational(12,24))
177
- assert_equal d, JSON.parse(d.to_json, :create_additions => true)
174
+ assert_equal d, parse(d.to_json, :create_additions => true)
178
175
  end
179
176
 
180
177
  def test_rational_complex
181
- assert_equal Rational(2, 9), JSON.parse(JSON(Rational(2, 9)), :create_additions => true)
182
- assert_equal Complex(2, 9), JSON.parse(JSON(Complex(2, 9)), :create_additions => true)
178
+ assert_equal Rational(2, 9), parse(JSON(Rational(2, 9)), :create_additions => true)
179
+ assert_equal Complex(2, 9), parse(JSON(Complex(2, 9)), :create_additions => true)
183
180
  end
184
181
 
185
182
  def test_bigdecimal
@@ -191,6 +188,6 @@ class TestJSONAddition < Test::Unit::TestCase
191
188
  o = OpenStruct.new
192
189
  # XXX this won't work; o.foo = { :bar => true }
193
190
  o.foo = { 'bar' => true }
194
- assert_equal o, JSON.parse(JSON(o), :create_additions => true)
191
+ assert_equal o, parse(JSON(o), :create_additions => true)
195
192
  end
196
193
  end
@@ -0,0 +1,126 @@
1
+ #frozen_string_literal: false
2
+ require 'test_helper'
3
+ require 'stringio'
4
+ require 'tempfile'
5
+
6
+ class JSONCommonInterfaceTest < Test::Unit::TestCase
7
+ include JSON
8
+
9
+ def setup
10
+ @hash = {
11
+ 'a' => 2,
12
+ 'b' => 3.141,
13
+ 'c' => 'c',
14
+ 'd' => [ 1, "b", 3.14 ],
15
+ 'e' => { 'foo' => 'bar' },
16
+ 'g' => "\"\0\037",
17
+ 'h' => 1000.0,
18
+ 'i' => 0.001
19
+ }
20
+ @json = '{"a":2,"b":3.141,"c":"c","d":[1,"b",3.14],"e":{"foo":"bar"},'\
21
+ '"g":"\\"\\u0000\\u001f","h":1000.0,"i":0.001}'
22
+ end
23
+
24
+ def test_index
25
+ assert_equal @json, JSON[@hash]
26
+ assert_equal @hash, JSON[@json]
27
+ end
28
+
29
+ def test_parser
30
+ assert_match /::Parser\z/, JSON.parser.name
31
+ end
32
+
33
+ def test_generator
34
+ assert_match /::Generator\z/, JSON.generator.name
35
+ end
36
+
37
+ def test_state
38
+ assert_match /::Generator::State\z/, JSON.state.name
39
+ end
40
+
41
+ def test_create_id
42
+ assert_equal 'json_class', JSON.create_id
43
+ JSON.create_id = 'foo_bar'
44
+ assert_equal 'foo_bar', JSON.create_id
45
+ ensure
46
+ JSON.create_id = 'json_class'
47
+ end
48
+
49
+ def test_deep_const_get
50
+ assert_raises(ArgumentError) { JSON.deep_const_get('Nix::Da') }
51
+ assert_equal File::SEPARATOR, JSON.deep_const_get('File::SEPARATOR')
52
+ end
53
+
54
+ def test_parse
55
+ assert_equal [ 1, 2, 3, ], JSON.parse('[ 1, 2, 3 ]')
56
+ end
57
+
58
+ def test_parse_bang
59
+ assert_equal [ 1, NaN, 3, ], JSON.parse!('[ 1, NaN, 3 ]')
60
+ end
61
+
62
+ def test_generate
63
+ assert_equal '[1,2,3]', JSON.generate([ 1, 2, 3 ])
64
+ end
65
+
66
+ def test_fast_generate
67
+ assert_equal '[1,2,3]', JSON.generate([ 1, 2, 3 ])
68
+ end
69
+
70
+ def test_pretty_generate
71
+ assert_equal "[\n 1,\n 2,\n 3\n]", JSON.pretty_generate([ 1, 2, 3 ])
72
+ end
73
+
74
+ def test_load
75
+ assert_equal @hash, JSON.load(@json)
76
+ tempfile = Tempfile.open('@json')
77
+ tempfile.write @json
78
+ tempfile.rewind
79
+ assert_equal @hash, JSON.load(tempfile)
80
+ stringio = StringIO.new(@json)
81
+ stringio.rewind
82
+ assert_equal @hash, JSON.load(stringio)
83
+ assert_equal nil, JSON.load(nil)
84
+ assert_equal nil, JSON.load('')
85
+ ensure
86
+ tempfile.close!
87
+ end
88
+
89
+ def test_load_with_options
90
+ json = '{ "foo": NaN }'
91
+ assert JSON.load(json, nil, :allow_nan => true)['foo'].nan?
92
+ end
93
+
94
+ def test_load_null
95
+ assert_equal nil, JSON.load(nil, nil, :allow_blank => true)
96
+ assert_raises(TypeError) { JSON.load(nil, nil, :allow_blank => false) }
97
+ assert_raises(JSON::ParserError) { JSON.load('', nil, :allow_blank => false) }
98
+ end
99
+
100
+ def test_dump
101
+ too_deep = '[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]'
102
+ assert_equal too_deep, dump(eval(too_deep))
103
+ assert_kind_of String, Marshal.dump(eval(too_deep))
104
+ assert_raise(ArgumentError) { dump(eval(too_deep), 100) }
105
+ assert_raise(ArgumentError) { Marshal.dump(eval(too_deep), 100) }
106
+ assert_equal too_deep, dump(eval(too_deep), 101)
107
+ assert_kind_of String, Marshal.dump(eval(too_deep), 101)
108
+ output = StringIO.new
109
+ dump(eval(too_deep), output)
110
+ assert_equal too_deep, output.string
111
+ output = StringIO.new
112
+ dump(eval(too_deep), output, 101)
113
+ assert_equal too_deep, output.string
114
+ end
115
+
116
+ def test_dump_should_modify_defaults
117
+ max_nesting = JSON.dump_default_options[:max_nesting]
118
+ dump([], StringIO.new, 10)
119
+ assert_equal max_nesting, JSON.dump_default_options[:max_nesting]
120
+ end
121
+
122
+ def test_JSON
123
+ assert_equal @json, JSON(@hash)
124
+ assert_equal @hash, JSON(@json)
125
+ end
126
+ end