json 1.8.3 → 2.5.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (93) hide show
  1. checksums.yaml +5 -5
  2. data/{CHANGES → CHANGES.md} +241 -90
  3. data/Gemfile +10 -6
  4. data/{COPYING-json-jruby → LICENSE} +5 -6
  5. data/{README.rdoc → README.md} +201 -134
  6. data/VERSION +1 -1
  7. data/ext/json/ext/fbuffer/fbuffer.h +0 -3
  8. data/ext/json/ext/generator/generator.c +264 -104
  9. data/ext/json/ext/generator/generator.h +12 -4
  10. data/ext/json/ext/parser/extconf.rb +28 -0
  11. data/ext/json/ext/parser/parser.c +425 -462
  12. data/ext/json/ext/parser/parser.h +5 -5
  13. data/ext/json/ext/parser/parser.rl +181 -181
  14. data/ext/json/extconf.rb +1 -1
  15. data/json.gemspec +0 -0
  16. data/lib/json.rb +550 -29
  17. data/lib/json/add/bigdecimal.rb +3 -2
  18. data/lib/json/add/complex.rb +4 -4
  19. data/lib/json/add/core.rb +1 -0
  20. data/lib/json/add/date.rb +1 -1
  21. data/lib/json/add/date_time.rb +1 -1
  22. data/lib/json/add/exception.rb +1 -1
  23. data/lib/json/add/ostruct.rb +3 -3
  24. data/lib/json/add/range.rb +1 -1
  25. data/lib/json/add/rational.rb +3 -3
  26. data/lib/json/add/regexp.rb +3 -3
  27. data/lib/json/add/set.rb +29 -0
  28. data/lib/json/add/struct.rb +1 -1
  29. data/lib/json/add/symbol.rb +1 -1
  30. data/lib/json/add/time.rb +1 -1
  31. data/lib/json/common.rb +381 -162
  32. data/lib/json/ext.rb +0 -6
  33. data/lib/json/generic_object.rb +5 -4
  34. data/lib/json/pure.rb +2 -8
  35. data/lib/json/pure/generator.rb +83 -126
  36. data/lib/json/pure/parser.rb +62 -84
  37. data/lib/json/version.rb +2 -1
  38. data/tests/fixtures/fail29.json +1 -0
  39. data/tests/fixtures/fail30.json +1 -0
  40. data/tests/fixtures/fail31.json +1 -0
  41. data/tests/fixtures/fail32.json +1 -0
  42. data/tests/fixtures/obsolete_fail1.json +1 -0
  43. data/tests/{test_json_addition.rb → json_addition_test.rb} +28 -25
  44. data/tests/json_common_interface_test.rb +169 -0
  45. data/tests/json_encoding_test.rb +107 -0
  46. data/tests/json_ext_parser_test.rb +15 -0
  47. data/tests/{test_json_fixtures.rb → json_fixtures_test.rb} +13 -8
  48. data/tests/{test_json_generate.rb → json_generator_test.rb} +109 -47
  49. data/tests/{test_json_generic_object.rb → json_generic_object_test.rb} +15 -8
  50. data/tests/json_parser_test.rb +497 -0
  51. data/tests/json_string_matching_test.rb +38 -0
  52. data/tests/lib/core_assertions.rb +763 -0
  53. data/tests/lib/envutil.rb +365 -0
  54. data/tests/lib/find_executable.rb +22 -0
  55. data/tests/lib/helper.rb +4 -0
  56. data/tests/ractor_test.rb +30 -0
  57. data/tests/test_helper.rb +17 -0
  58. metadata +48 -76
  59. data/.gitignore +0 -16
  60. data/.travis.yml +0 -26
  61. data/COPYING +0 -58
  62. data/GPL +0 -340
  63. data/README-json-jruby.markdown +0 -33
  64. data/Rakefile +0 -412
  65. data/TODO +0 -1
  66. data/data/example.json +0 -1
  67. data/data/index.html +0 -38
  68. data/data/prototype.js +0 -4184
  69. data/diagrams/.keep +0 -0
  70. data/install.rb +0 -23
  71. data/java/src/json/ext/ByteListTranscoder.java +0 -167
  72. data/java/src/json/ext/Generator.java +0 -444
  73. data/java/src/json/ext/GeneratorMethods.java +0 -232
  74. data/java/src/json/ext/GeneratorService.java +0 -43
  75. data/java/src/json/ext/GeneratorState.java +0 -543
  76. data/java/src/json/ext/OptionsReader.java +0 -114
  77. data/java/src/json/ext/Parser.java +0 -2645
  78. data/java/src/json/ext/Parser.rl +0 -969
  79. data/java/src/json/ext/ParserService.java +0 -35
  80. data/java/src/json/ext/RuntimeInfo.java +0 -121
  81. data/java/src/json/ext/StringDecoder.java +0 -167
  82. data/java/src/json/ext/StringEncoder.java +0 -106
  83. data/java/src/json/ext/Utils.java +0 -89
  84. data/json-java.gemspec +0 -23
  85. data/json_pure.gemspec +0 -40
  86. data/tests/fixtures/fail1.json +0 -1
  87. data/tests/setup_variant.rb +0 -11
  88. data/tests/test_json.rb +0 -553
  89. data/tests/test_json_encoding.rb +0 -65
  90. data/tests/test_json_string_matching.rb +0 -39
  91. data/tests/test_json_unicode.rb +0 -72
  92. data/tools/fuzz.rb +0 -139
  93. data/tools/server.rb +0 -62
data/lib/json/version.rb CHANGED
@@ -1,6 +1,7 @@
1
+ # frozen_string_literal: false
1
2
  module JSON
2
3
  # JSON version
3
- VERSION = '1.8.3'
4
+ VERSION = '2.5.1'
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
+ {
@@ -0,0 +1 @@
1
+ [
@@ -0,0 +1 @@
1
+ [1, 2, 3,
@@ -0,0 +1 @@
1
+ {"foo": "bar"
@@ -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,7 +71,7 @@ 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, :create_additions => true)
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
@@ -82,7 +80,7 @@ class TestJSONAddition < Test::Unit::TestCase
82
80
  a = A.new(666)
83
81
  assert A.json_creatable?
84
82
  json = generate(a)
85
- a_hash = JSON.parse(json)
83
+ a_hash = parse(json)
86
84
  assert_kind_of Hash, a_hash
87
85
  end
88
86
 
@@ -90,13 +88,13 @@ class TestJSONAddition < Test::Unit::TestCase
90
88
  a = A.new(666)
91
89
  assert A.json_creatable?
92
90
  json = generate(a)
93
- a_again = JSON.parse(json, :create_additions => true)
91
+ a_again = parse(json, :create_additions => true)
94
92
  assert_kind_of a.class, a_again
95
93
  assert_equal a, a_again
96
- a_hash = JSON.parse(json, :create_additions => false)
94
+ a_hash = parse(json, :create_additions => false)
97
95
  assert_kind_of Hash, a_hash
98
96
  assert_equal(
99
- {"args"=>[666], "json_class"=>"TestJSONAddition::A"}.sort_by { |k,| k },
97
+ {"args"=>[666], "json_class"=>"JSONAdditionTest::A"}.sort_by { |k,| k },
100
98
  a_hash.sort_by { |k,| k }
101
99
  )
102
100
  end
@@ -105,14 +103,14 @@ class TestJSONAddition < Test::Unit::TestCase
105
103
  b = B.new
106
104
  assert !B.json_creatable?
107
105
  json = generate(b)
108
- assert_equal({ "json_class"=>"TestJSONAddition::B" }, JSON.parse(json))
106
+ assert_equal({ "json_class"=>"JSONAdditionTest::B" }, parse(json))
109
107
  end
110
108
 
111
109
  def test_extended_json_fail2
112
110
  c = C.new
113
111
  assert !C.json_creatable?
114
112
  json = generate(c)
115
- assert_raises(ArgumentError, NameError) { JSON.parse(json, :create_additions => true) }
113
+ assert_raise(ArgumentError, NameError) { parse(json, :create_additions => true) }
116
114
  end
117
115
 
118
116
  def test_raw_strings
@@ -130,7 +128,7 @@ class TestJSONAddition < Test::Unit::TestCase
130
128
  assert_match(/\A\{.*\}\z/, json)
131
129
  assert_match(/"json_class":"String"/, json)
132
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)
133
- raw_again = JSON.parse(json, :create_additions => true)
131
+ raw_again = parse(json, :create_additions => true)
134
132
  assert_equal raw, raw_again
135
133
  end
136
134
 
@@ -151,7 +149,7 @@ class TestJSONAddition < Test::Unit::TestCase
151
149
  assert_equal s, JSON(JSON(s), :create_additions => true)
152
150
  struct = Struct.new :foo, :bar
153
151
  s = struct.new 4711, 'foot'
154
- assert_raises(JSONError) { JSON(s) }
152
+ assert_raise(JSONError) { JSON(s) }
155
153
  begin
156
154
  raise TypeError, "test me"
157
155
  rescue TypeError => e
@@ -167,19 +165,19 @@ class TestJSONAddition < Test::Unit::TestCase
167
165
 
168
166
  def test_utc_datetime
169
167
  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)
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)
174
172
  d = DateTime.civil(2008, 6, 17, 11, 48, 32, Rational(1,24))
175
- assert_equal d, JSON.parse(d.to_json, :create_additions => true)
173
+ assert_equal d, parse(d.to_json, :create_additions => true)
176
174
  d = DateTime.civil(2008, 6, 17, 11, 48, 32, Rational(12,24))
177
- assert_equal d, JSON.parse(d.to_json, :create_additions => true)
175
+ assert_equal d, parse(d.to_json, :create_additions => true)
178
176
  end
179
177
 
180
178
  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)
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)
183
181
  end
184
182
 
185
183
  def test_bigdecimal
@@ -191,6 +189,11 @@ class TestJSONAddition < Test::Unit::TestCase
191
189
  o = OpenStruct.new
192
190
  # XXX this won't work; o.foo = { :bar => true }
193
191
  o.foo = { 'bar' => true }
194
- assert_equal o, JSON.parse(JSON(o), :create_additions => true)
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)
195
198
  end
196
199
  end
@@ -0,0 +1,169 @@
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
+
127
+ def test_load_file
128
+ test_load_shared(:load_file)
129
+ end
130
+
131
+ def test_load_file!
132
+ test_load_shared(:load_file!)
133
+ end
134
+
135
+ def test_load_file_with_option
136
+ test_load_file_with_option_shared(:load_file)
137
+ end
138
+
139
+ def test_load_file_with_option!
140
+ test_load_file_with_option_shared(:load_file!)
141
+ end
142
+
143
+ private
144
+
145
+ def test_load_shared(method_name)
146
+ temp_file_containing(@json) do |filespec|
147
+ assert_equal JSON.public_send(method_name, filespec), @hash
148
+ end
149
+ end
150
+
151
+ def test_load_file_with_option_shared(method_name)
152
+ temp_file_containing(@json) do |filespec|
153
+ parsed_object = JSON.public_send(method_name, filespec, symbolize_names: true)
154
+ key_classes = parsed_object.keys.map(&:class)
155
+ assert_include(key_classes, Symbol)
156
+ assert_not_include(key_classes, String)
157
+ end
158
+ end
159
+
160
+ def temp_file_containing(text, file_prefix = '')
161
+ raise "This method must be called with a code block." unless block_given?
162
+
163
+ Tempfile.create(file_prefix) do |file|
164
+ file << text
165
+ file.close
166
+ yield file.path
167
+ end
168
+ end
169
+ 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