json 1.7.0 → 2.3.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 (88) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +7 -0
  3. data/.travis.yml +18 -10
  4. data/{CHANGES → CHANGES.md} +217 -69
  5. data/Gemfile +11 -12
  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} +188 -137
  9. data/Rakefile +52 -37
  10. data/VERSION +1 -1
  11. data/ext/json/ext/fbuffer/fbuffer.h +38 -7
  12. data/ext/json/ext/generator/depend +1 -0
  13. data/ext/json/ext/generator/extconf.rb +1 -10
  14. data/ext/json/ext/generator/generator.c +239 -133
  15. data/ext/json/ext/generator/generator.h +35 -26
  16. data/ext/json/ext/parser/depend +1 -0
  17. data/ext/json/ext/parser/extconf.rb +2 -9
  18. data/ext/json/ext/parser/parser.c +446 -514
  19. data/ext/json/ext/parser/parser.h +23 -9
  20. data/ext/json/ext/parser/parser.rl +177 -208
  21. data/ext/json/extconf.rb +2 -0
  22. data/java/src/json/ext/ByteListTranscoder.java +1 -2
  23. data/java/src/json/ext/Generator.java +49 -20
  24. data/java/src/json/ext/GeneratorMethods.java +1 -2
  25. data/java/src/json/ext/GeneratorService.java +1 -2
  26. data/java/src/json/ext/GeneratorState.java +25 -57
  27. data/java/src/json/ext/OptionsReader.java +5 -5
  28. data/java/src/json/ext/Parser.java +141 -419
  29. data/java/src/json/ext/Parser.rl +57 -128
  30. data/java/src/json/ext/ParserService.java +1 -2
  31. data/java/src/json/ext/RuntimeInfo.java +1 -6
  32. data/java/src/json/ext/StringDecoder.java +1 -2
  33. data/java/src/json/ext/StringEncoder.java +5 -0
  34. data/java/src/json/ext/Utils.java +1 -2
  35. data/json-java.gemspec +17 -2
  36. data/json.gemspec +0 -0
  37. data/json_pure.gemspec +25 -26
  38. data/lib/json.rb +3 -2
  39. data/lib/json/add/bigdecimal.rb +10 -2
  40. data/lib/json/add/complex.rb +9 -2
  41. data/lib/json/add/core.rb +1 -0
  42. data/lib/json/add/date.rb +1 -1
  43. data/lib/json/add/date_time.rb +1 -1
  44. data/lib/json/add/exception.rb +1 -1
  45. data/lib/json/add/ostruct.rb +3 -3
  46. data/lib/json/add/range.rb +1 -1
  47. data/lib/json/add/rational.rb +8 -2
  48. data/lib/json/add/regexp.rb +3 -3
  49. data/lib/json/add/set.rb +29 -0
  50. data/lib/json/add/struct.rb +1 -1
  51. data/lib/json/add/symbol.rb +1 -1
  52. data/lib/json/add/time.rb +6 -3
  53. data/lib/json/common.rb +51 -66
  54. data/lib/json/ext.rb +0 -6
  55. data/lib/json/generic_object.rb +36 -4
  56. data/lib/json/pure.rb +2 -8
  57. data/lib/json/pure/generator.rb +106 -119
  58. data/lib/json/pure/parser.rb +48 -88
  59. data/lib/json/version.rb +2 -1
  60. data/references/rfc7159.txt +899 -0
  61. data/tests/fixtures/fail18.json +1 -1
  62. data/tests/fixtures/obsolete_fail1.json +1 -0
  63. data/tests/{test_json_addition.rb → json_addition_test.rb} +53 -38
  64. data/tests/json_common_interface_test.rb +126 -0
  65. data/tests/json_encoding_test.rb +107 -0
  66. data/tests/json_ext_parser_test.rb +15 -0
  67. data/tests/{test_json_fixtures.rb → json_fixtures_test.rb} +5 -8
  68. data/tests/json_generator_test.rb +421 -0
  69. data/tests/json_generic_object_test.rb +82 -0
  70. data/tests/json_parser_test.rb +472 -0
  71. data/tests/json_string_matching_test.rb +38 -0
  72. data/tests/{setup_variant.rb → test_helper.rb} +6 -0
  73. data/tools/diff.sh +18 -0
  74. data/tools/fuzz.rb +1 -9
  75. metadata +49 -72
  76. data/COPYING +0 -58
  77. data/GPL +0 -340
  78. data/TODO +0 -1
  79. data/data/example.json +0 -1
  80. data/data/index.html +0 -38
  81. data/data/prototype.js +0 -4184
  82. data/tests/fixtures/fail1.json +0 -1
  83. data/tests/test_json.rb +0 -539
  84. data/tests/test_json_encoding.rb +0 -65
  85. data/tests/test_json_generate.rb +0 -251
  86. data/tests/test_json_generic_object.rb +0 -35
  87. data/tests/test_json_string_matching.rb +0 -40
  88. data/tests/test_json_unicode.rb +0 -72
@@ -1 +1 @@
1
- [[[[[[[[[[[[[[[[[[[["Too deep"]]]]]]]]]]]]]]]]]]]]
1
+ [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[["Too deep"]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]
@@ -0,0 +1 @@
1
+ "A JSON payload should be an object or array, not a string."
@@ -1,16 +1,14 @@
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'
9
6
  require 'json/add/bigdecimal'
10
7
  require 'json/add/ostruct'
8
+ require 'json/add/set'
11
9
  require 'date'
12
10
 
13
- class TestJSONAddition < Test::Unit::TestCase
11
+ class JSONAdditionTest < Test::Unit::TestCase
14
12
  include JSON
15
13
 
16
14
  class A
@@ -64,7 +62,7 @@ class TestJSONAddition < Test::Unit::TestCase
64
62
 
65
63
  def to_json(*args)
66
64
  {
67
- 'json_class' => 'TestJSONAddition::Nix',
65
+ 'json_class' => 'JSONAdditionTest::Nix',
68
66
  }.to_json(*args)
69
67
  end
70
68
  end
@@ -73,22 +71,30 @@ class TestJSONAddition < Test::Unit::TestCase
73
71
  a = A.new(666)
74
72
  assert A.json_creatable?
75
73
  json = generate(a)
76
- a_again = JSON.parse(json)
74
+ a_again = parse(json, :create_additions => true)
77
75
  assert_kind_of a.class, a_again
78
76
  assert_equal a, a_again
79
77
  end
80
78
 
79
+ def test_extended_json_default
80
+ a = A.new(666)
81
+ assert A.json_creatable?
82
+ json = generate(a)
83
+ a_hash = parse(json)
84
+ assert_kind_of Hash, a_hash
85
+ end
86
+
81
87
  def test_extended_json_disabled
82
88
  a = A.new(666)
83
89
  assert A.json_creatable?
84
90
  json = generate(a)
85
- a_again = JSON.parse(json, :create_additions => true)
91
+ a_again = parse(json, :create_additions => true)
86
92
  assert_kind_of a.class, a_again
87
93
  assert_equal a, a_again
88
- a_hash = JSON.parse(json, :create_additions => false)
94
+ a_hash = parse(json, :create_additions => false)
89
95
  assert_kind_of Hash, a_hash
90
96
  assert_equal(
91
- {"args"=>[666], "json_class"=>"TestJSONAddition::A"}.sort_by { |k,| k },
97
+ {"args"=>[666], "json_class"=>"JSONAdditionTest::A"}.sort_by { |k,| k },
92
98
  a_hash.sort_by { |k,| k }
93
99
  )
94
100
  end
@@ -97,14 +103,14 @@ class TestJSONAddition < Test::Unit::TestCase
97
103
  b = B.new
98
104
  assert !B.json_creatable?
99
105
  json = generate(b)
100
- assert_equal({ "json_class"=>"TestJSONAddition::B" }, JSON.parse(json))
106
+ assert_equal({ "json_class"=>"JSONAdditionTest::B" }, parse(json))
101
107
  end
102
108
 
103
109
  def test_extended_json_fail2
104
110
  c = C.new
105
111
  assert !C.json_creatable?
106
112
  json = generate(c)
107
- assert_raises(ArgumentError, NameError) { JSON.parse(json) }
113
+ assert_raise(ArgumentError, NameError) { parse(json, :create_additions => true) }
108
114
  end
109
115
 
110
116
  def test_raw_strings
@@ -119,10 +125,10 @@ class TestJSONAddition < Test::Unit::TestCase
119
125
  json_raw_object = raw.to_json_raw_object
120
126
  hash = { 'json_class' => 'String', 'raw'=> raw_array }
121
127
  assert_equal hash, json_raw_object
122
- assert_match(/\A\{.*\}\Z/, json)
128
+ assert_match(/\A\{.*\}\z/, json)
123
129
  assert_match(/"json_class":"String"/, json)
124
130
  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)
125
- raw_again = JSON.parse(json)
131
+ raw_again = parse(json, :create_additions => true)
126
132
  assert_equal raw, raw_again
127
133
  end
128
134
 
@@ -130,59 +136,68 @@ class TestJSONAddition < Test::Unit::TestCase
130
136
 
131
137
  def test_core
132
138
  t = Time.now
133
- assert_equal t, JSON(JSON(t))
139
+ assert_equal t, JSON(JSON(t), :create_additions => true)
134
140
  d = Date.today
135
- assert_equal d, JSON(JSON(d))
141
+ assert_equal d, JSON(JSON(d), :create_additions => true)
136
142
  d = DateTime.civil(2007, 6, 14, 14, 57, 10, Rational(1, 12), 2299161)
137
- assert_equal d, JSON(JSON(d))
138
- assert_equal 1..10, JSON(JSON(1..10))
139
- assert_equal 1...10, JSON(JSON(1...10))
140
- assert_equal "a".."c", JSON(JSON("a".."c"))
141
- assert_equal "a"..."c", JSON(JSON("a"..."c"))
143
+ assert_equal d, JSON(JSON(d), :create_additions => true)
144
+ assert_equal 1..10, JSON(JSON(1..10), :create_additions => true)
145
+ assert_equal 1...10, JSON(JSON(1...10), :create_additions => true)
146
+ assert_equal "a".."c", JSON(JSON("a".."c"), :create_additions => true)
147
+ assert_equal "a"..."c", JSON(JSON("a"..."c"), :create_additions => true)
142
148
  s = MyJsonStruct.new 4711, 'foot'
143
- assert_equal s, JSON(JSON(s))
149
+ assert_equal s, JSON(JSON(s), :create_additions => true)
144
150
  struct = Struct.new :foo, :bar
145
151
  s = struct.new 4711, 'foot'
146
- assert_raises(JSONError) { JSON(s) }
152
+ assert_raise(JSONError) { JSON(s) }
147
153
  begin
148
154
  raise TypeError, "test me"
149
155
  rescue TypeError => e
150
156
  e_json = JSON.generate e
151
- e_again = JSON e_json
157
+ e_again = JSON e_json, :create_additions => true
152
158
  assert_kind_of TypeError, e_again
153
159
  assert_equal e.message, e_again.message
154
160
  assert_equal e.backtrace, e_again.backtrace
155
161
  end
156
- assert_equal(/foo/, JSON(JSON(/foo/)))
157
- assert_equal(/foo/i, JSON(JSON(/foo/i)))
162
+ assert_equal(/foo/, JSON(JSON(/foo/), :create_additions => true))
163
+ assert_equal(/foo/i, JSON(JSON(/foo/i), :create_additions => true))
158
164
  end
159
165
 
160
166
  def test_utc_datetime
161
167
  now = Time.now
162
- d = DateTime.parse(now.to_s) # usual case
163
- assert_equal d, JSON.parse(d.to_json)
164
- d = DateTime.parse(now.utc.to_s) # of = 0
165
- assert_equal d, JSON.parse(d.to_json)
168
+ d = DateTime.parse(now.to_s, :create_additions => true) # usual case
169
+ assert_equal d, parse(d.to_json, :create_additions => true)
170
+ d = DateTime.parse(now.utc.to_s) # of = 0
171
+ assert_equal d, parse(d.to_json, :create_additions => true)
166
172
  d = DateTime.civil(2008, 6, 17, 11, 48, 32, Rational(1,24))
167
- assert_equal d, JSON.parse(d.to_json)
173
+ assert_equal d, parse(d.to_json, :create_additions => true)
168
174
  d = DateTime.civil(2008, 6, 17, 11, 48, 32, Rational(12,24))
169
- assert_equal d, JSON.parse(d.to_json)
175
+ assert_equal d, parse(d.to_json, :create_additions => true)
170
176
  end
171
177
 
172
178
  def test_rational_complex
173
- assert_equal Rational(2, 9), JSON(JSON(Rational(2, 9)))
174
- assert_equal Complex(2, 9), JSON(JSON(Complex(2, 9)))
179
+ assert_equal Rational(2, 9), parse(JSON(Rational(2, 9)), :create_additions => true)
180
+ assert_equal Complex(2, 9), parse(JSON(Complex(2, 9)), :create_additions => true)
175
181
  end
176
182
 
177
183
  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)))
184
+ assert_equal BigDecimal('3.141', 23), JSON(JSON(BigDecimal('3.141', 23)), :create_additions => true)
185
+ assert_equal BigDecimal('3.141', 666), JSON(JSON(BigDecimal('3.141', 666)), :create_additions => true)
180
186
  end
181
187
 
182
188
  def test_ostruct
183
189
  o = OpenStruct.new
184
190
  # XXX this won't work; o.foo = { :bar => true }
185
191
  o.foo = { 'bar' => true }
186
- assert_equal o, JSON(JSON(o))
192
+ assert_equal o, parse(JSON(o), :create_additions => true)
193
+ end
194
+
195
+ def test_set
196
+ s = Set.new([:a, :b, :c, :a])
197
+ assert_equal s, JSON.parse(JSON(s), :create_additions => true)
198
+ ss = SortedSet.new([:d, :b, :a, :c])
199
+ ss_again = JSON.parse(JSON(ss), :create_additions => true)
200
+ assert_kind_of ss.class, ss_again
201
+ assert_equal ss, ss_again
187
202
  end
188
203
  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_raise(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, Infinity, 3, ], JSON.parse!('[ 1, Infinity, 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_raise(TypeError) { JSON.load(nil, nil, :allow_blank => false) }
97
+ assert_raise(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
@@ -0,0 +1,107 @@
1
+ # encoding: utf-8
2
+ #frozen_string_literal: false
3
+ require 'test_helper'
4
+
5
+ class JSONEncodingTest < Test::Unit::TestCase
6
+ include JSON
7
+
8
+ def setup
9
+ @utf_8 = '"© ≠ €!"'
10
+ @ascii_8bit = @utf_8.dup.force_encoding('ascii-8bit')
11
+ @parsed = "© ≠ €!"
12
+ @generated = '"\u00a9 \u2260 \u20ac!"'
13
+ if String.method_defined?(:encode)
14
+ @utf_16_data = @parsed.encode('utf-16be', 'utf-8')
15
+ @utf_16be = @utf_8.encode('utf-16be', 'utf-8')
16
+ @utf_16le = @utf_8.encode('utf-16le', 'utf-8')
17
+ @utf_32be = @utf_8.encode('utf-32be', 'utf-8')
18
+ @utf_32le = @utf_8.encode('utf-32le', 'utf-8')
19
+ else
20
+ require 'iconv'
21
+ @utf_16_data, = Iconv.iconv('utf-16be', 'utf-8', @parsed)
22
+ @utf_16be, = Iconv.iconv('utf-16be', 'utf-8', @utf_8)
23
+ @utf_16le, = Iconv.iconv('utf-16le', 'utf-8', @utf_8)
24
+ @utf_32be, = Iconv.iconv('utf-32be', 'utf-8', @utf_8)
25
+ @utf_32le, = Iconv.iconv('utf-32le', 'utf-8', @utf_8)
26
+ end
27
+ end
28
+
29
+ def test_parse
30
+ assert_equal @parsed, JSON.parse(@ascii_8bit)
31
+ assert_equal @parsed, JSON.parse(@utf_8)
32
+ assert_equal @parsed, JSON.parse(@utf_16be)
33
+ assert_equal @parsed, JSON.parse(@utf_16le)
34
+ assert_equal @parsed, JSON.parse(@utf_32be)
35
+ assert_equal @parsed, JSON.parse(@utf_32le)
36
+ end
37
+
38
+ def test_generate
39
+ assert_equal @generated, JSON.generate(@parsed, :ascii_only => true)
40
+ assert_equal @generated, JSON.generate(@utf_16_data, :ascii_only => true)
41
+ end
42
+
43
+ def test_unicode
44
+ assert_equal '""', ''.to_json
45
+ assert_equal '"\\b"', "\b".to_json
46
+ assert_equal '"\u0001"', 0x1.chr.to_json
47
+ assert_equal '"\u001f"', 0x1f.chr.to_json
48
+ assert_equal '" "', ' '.to_json
49
+ assert_equal "\"#{0x7f.chr}\"", 0x7f.chr.to_json
50
+ utf8 = [ "© ≠ €! \01" ]
51
+ json = '["© ≠ €! \u0001"]'
52
+ assert_equal json, utf8.to_json(:ascii_only => false)
53
+ assert_equal utf8, parse(json)
54
+ json = '["\u00a9 \u2260 \u20ac! \u0001"]'
55
+ assert_equal json, utf8.to_json(:ascii_only => true)
56
+ assert_equal utf8, parse(json)
57
+ utf8 = ["\343\201\202\343\201\204\343\201\206\343\201\210\343\201\212"]
58
+ json = "[\"\343\201\202\343\201\204\343\201\206\343\201\210\343\201\212\"]"
59
+ assert_equal utf8, parse(json)
60
+ assert_equal json, utf8.to_json(:ascii_only => false)
61
+ utf8 = ["\343\201\202\343\201\204\343\201\206\343\201\210\343\201\212"]
62
+ assert_equal utf8, parse(json)
63
+ json = "[\"\\u3042\\u3044\\u3046\\u3048\\u304a\"]"
64
+ assert_equal json, utf8.to_json(:ascii_only => true)
65
+ assert_equal utf8, parse(json)
66
+ utf8 = ['საქართველო']
67
+ json = '["საქართველო"]'
68
+ assert_equal json, utf8.to_json(:ascii_only => false)
69
+ json = "[\"\\u10e1\\u10d0\\u10e5\\u10d0\\u10e0\\u10d7\\u10d5\\u10d4\\u10da\\u10dd\"]"
70
+ assert_equal json, utf8.to_json(:ascii_only => true)
71
+ assert_equal utf8, parse(json)
72
+ assert_equal '["Ã"]', generate(["Ã"], :ascii_only => false)
73
+ assert_equal '["\\u00c3"]', generate(["Ã"], :ascii_only => true)
74
+ assert_equal ["€"], parse('["\u20ac"]')
75
+ utf8 = ["\xf0\xa0\x80\x81"]
76
+ json = "[\"\xf0\xa0\x80\x81\"]"
77
+ assert_equal json, generate(utf8, :ascii_only => false)
78
+ assert_equal utf8, parse(json)
79
+ json = '["\ud840\udc01"]'
80
+ assert_equal json, generate(utf8, :ascii_only => true)
81
+ assert_equal utf8, parse(json)
82
+ assert_raise(JSON::ParserError) { parse('"\u"') }
83
+ assert_raise(JSON::ParserError) { parse('"\ud800"') }
84
+ end
85
+
86
+ def test_chars
87
+ (0..0x7f).each do |i|
88
+ json = '["\u%04x"]' % i
89
+ if RUBY_VERSION >= "1.9."
90
+ i = i.chr
91
+ end
92
+ assert_equal i, parse(json).first[0]
93
+ if i == ?\b
94
+ generated = generate(["" << i])
95
+ assert '["\b"]' == generated || '["\10"]' == generated
96
+ elsif [?\n, ?\r, ?\t, ?\f].include?(i)
97
+ assert_equal '[' << ('' << i).dump << ']', generate(["" << i])
98
+ elsif i.chr < 0x20.chr
99
+ assert_equal json, generate(["" << i])
100
+ end
101
+ end
102
+ assert_raise(JSON::GeneratorError) do
103
+ generate(["\x80"], :ascii_only => true)
104
+ end
105
+ assert_equal "\302\200", parse('["\u0080"]').first
106
+ end
107
+ end
@@ -0,0 +1,15 @@
1
+ #frozen_string_literal: false
2
+ require 'test_helper'
3
+
4
+ class JSONExtParserTest < Test::Unit::TestCase
5
+ if defined?(JSON::Ext::Parser)
6
+ def test_allocate
7
+ parser = JSON::Ext::Parser.new("{}")
8
+ assert_raise(TypeError, '[ruby-core:35079]') do
9
+ parser.__send__(:initialize, "{}")
10
+ end
11
+ parser = JSON::Ext::Parser.allocate
12
+ assert_raise(TypeError, '[ruby-core:35079]') { parser.source }
13
+ end
14
+ end
15
+ end
@@ -1,12 +1,9 @@
1
- #!/usr/bin/env ruby
2
- # -*- coding: utf-8 -*-
1
+ #frozen_string_literal: false
2
+ require 'test_helper'
3
3
 
4
- require 'test/unit'
5
- require File.join(File.dirname(__FILE__), 'setup_variant')
6
-
7
- class TestJSONFixtures < Test::Unit::TestCase
4
+ class JSONFixturesTest < Test::Unit::TestCase
8
5
  def setup
9
- fixtures = File.join(File.dirname(__FILE__), 'fixtures/*.json')
6
+ fixtures = File.join(File.dirname(__FILE__), 'fixtures/{fail,pass}.json')
10
7
  passed, failed = Dir[fixtures].partition { |f| f['pass'] }
11
8
  @passed = passed.inject([]) { |a, f| a << [ f, File.read(f) ] }.sort
12
9
  @failed = failed.inject([]) { |a, f| a << [ f, File.read(f) ] }.sort
@@ -26,7 +23,7 @@ class TestJSONFixtures < Test::Unit::TestCase
26
23
 
27
24
  def test_failing
28
25
  for name, source in @failed
29
- assert_raises(JSON::ParserError, JSON::NestingError,
26
+ assert_raise(JSON::ParserError, JSON::NestingError,
30
27
  "Did not fail for fixture '#{name}': #{source.inspect}") do
31
28
  JSON.parse(source)
32
29
  end