json_pure 1.0.0 → 1.4.6

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 (93) hide show
  1. data/CHANGES +155 -1
  2. data/COPYING +58 -0
  3. data/GPL +7 -7
  4. data/README +324 -45
  5. data/Rakefile +166 -124
  6. data/TODO +1 -1
  7. data/VERSION +1 -1
  8. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkComparison.log +52 -0
  9. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_fast-autocorrelation.dat +1000 -0
  10. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_fast.dat +1001 -0
  11. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_pretty-autocorrelation.dat +900 -0
  12. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_pretty.dat +901 -0
  13. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_safe-autocorrelation.dat +1000 -0
  14. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_safe.dat +1001 -0
  15. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt.log +261 -0
  16. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_fast-autocorrelation.dat +1000 -0
  17. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_fast.dat +1001 -0
  18. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_pretty-autocorrelation.dat +1000 -0
  19. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_pretty.dat +1001 -0
  20. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_safe-autocorrelation.dat +1000 -0
  21. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_safe.dat +1001 -0
  22. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure.log +262 -0
  23. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkRails#generator-autocorrelation.dat +1000 -0
  24. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkRails#generator.dat +1001 -0
  25. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkRails.log +82 -0
  26. data/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkComparison.log +34 -0
  27. data/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkExt#parser-autocorrelation.dat +900 -0
  28. data/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkExt#parser.dat +901 -0
  29. data/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkExt.log +81 -0
  30. data/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkPure#parser-autocorrelation.dat +1000 -0
  31. data/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkPure#parser.dat +1001 -0
  32. data/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkPure.log +82 -0
  33. data/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkRails#parser-autocorrelation.dat +1000 -0
  34. data/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkRails#parser.dat +1001 -0
  35. data/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkRails.log +82 -0
  36. data/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkYAML#parser-autocorrelation.dat +1000 -0
  37. data/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkYAML#parser.dat +1001 -0
  38. data/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkYAML.log +82 -0
  39. data/benchmarks/generator2_benchmark.rb +222 -0
  40. data/benchmarks/generator_benchmark.rb +224 -0
  41. data/benchmarks/ohai.json +1216 -0
  42. data/benchmarks/ohai.ruby +1 -0
  43. data/benchmarks/parser2_benchmark.rb +251 -0
  44. data/benchmarks/parser_benchmark.rb +259 -0
  45. data/bin/edit_json.rb +1 -3
  46. data/bin/prettify_json.rb +75 -0
  47. data/data/index.html +5 -4
  48. data/data/prototype.js +2764 -1095
  49. data/ext/json/ext/generator/extconf.rb +14 -3
  50. data/ext/json/ext/generator/generator.c +1022 -334
  51. data/ext/json/ext/generator/generator.h +197 -0
  52. data/ext/json/ext/parser/extconf.rb +9 -3
  53. data/ext/json/ext/parser/parser.c +961 -577
  54. data/ext/json/ext/parser/parser.h +71 -0
  55. data/ext/json/ext/parser/parser.rl +400 -123
  56. data/install.rb +0 -0
  57. data/lib/json/add/core.rb +148 -0
  58. data/lib/json/add/rails.rb +58 -0
  59. data/lib/json/common.rb +254 -47
  60. data/lib/json/editor.rb +236 -72
  61. data/lib/json/ext.rb +2 -0
  62. data/lib/json/pure/generator.rb +235 -117
  63. data/lib/json/pure/parser.rb +124 -25
  64. data/lib/json/pure.rb +5 -3
  65. data/lib/json/version.rb +1 -1
  66. data/lib/json.rb +2 -197
  67. data/tests/fixtures/fail18.json +1 -0
  68. data/tests/test_json.rb +181 -22
  69. data/tests/test_json_addition.rb +84 -16
  70. data/tests/test_json_encoding.rb +68 -0
  71. data/tests/test_json_fixtures.rb +9 -5
  72. data/tests/test_json_generate.rb +114 -14
  73. data/tests/test_json_rails.rb +144 -0
  74. data/tests/test_json_unicode.rb +35 -14
  75. data/tools/fuzz.rb +13 -7
  76. data/tools/server.rb +0 -1
  77. metadata +156 -122
  78. data/benchmarks/benchmark.txt +0 -133
  79. data/benchmarks/benchmark_generator.rb +0 -44
  80. data/benchmarks/benchmark_parser.rb +0 -22
  81. data/benchmarks/benchmark_rails.rb +0 -26
  82. data/ext/json/ext/generator/Makefile +0 -149
  83. data/ext/json/ext/generator/unicode.c +0 -184
  84. data/ext/json/ext/generator/unicode.h +0 -40
  85. data/ext/json/ext/parser/Makefile +0 -149
  86. data/ext/json/ext/parser/unicode.c +0 -156
  87. data/ext/json/ext/parser/unicode.h +0 -44
  88. data/tests/fixtures/pass18.json +0 -1
  89. data/tests/runner.rb +0 -24
  90. /data/tests/fixtures/{fail15.json → pass15.json} +0 -0
  91. /data/tests/fixtures/{fail16.json → pass16.json} +0 -0
  92. /data/tests/fixtures/{fail17.json → pass17.json} +0 -0
  93. /data/tests/fixtures/{fail26.json → pass26.json} +0 -0
@@ -0,0 +1,68 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- coding: utf-8 -*-
3
+
4
+ require 'test/unit'
5
+ case ENV['JSON']
6
+ when 'pure' then require 'json/pure'
7
+ when 'ext' then require 'json/ext'
8
+ else require 'json'
9
+ end
10
+ require 'iconv'
11
+
12
+ class TC_JSONEncoding < Test::Unit::TestCase
13
+ include JSON
14
+
15
+ def setup
16
+ @utf_8 = '["© ≠ €!"]'
17
+ @parsed = [ "© ≠ €!" ]
18
+ @utf_16_data = Iconv.iconv('utf-16be', 'utf-8', @parsed.first)
19
+ @generated = '["\u00a9 \u2260 \u20ac!"]'
20
+ if defined?(::Encoding)
21
+ @utf_8_ascii_8bit = @utf_8.dup.force_encoding(Encoding::ASCII_8BIT)
22
+ @utf_16be, = Iconv.iconv('utf-16be', 'utf-8', @utf_8)
23
+ @utf_16be_ascii_8bit = @utf_16be.dup.force_encoding(Encoding::ASCII_8BIT)
24
+ @utf_16le, = Iconv.iconv('utf-16le', 'utf-8', @utf_8)
25
+ @utf_16le_ascii_8bit = @utf_16le.dup.force_encoding(Encoding::ASCII_8BIT)
26
+ @utf_32be, = Iconv.iconv('utf-32be', 'utf-8', @utf_8)
27
+ @utf_32be_ascii_8bit = @utf_32be.dup.force_encoding(Encoding::ASCII_8BIT)
28
+ @utf_32le, = Iconv.iconv('utf-32le', 'utf-8', @utf_8)
29
+ @utf_32le_ascii_8bit = @utf_32le.dup.force_encoding(Encoding::ASCII_8BIT)
30
+ else
31
+ @utf_8_ascii_8bit = @utf_8.dup
32
+ @utf_16be, = Iconv.iconv('utf-16be', 'utf-8', @utf_8)
33
+ @utf_16be_ascii_8bit = @utf_16be.dup
34
+ @utf_16le, = Iconv.iconv('utf-16le', 'utf-8', @utf_8)
35
+ @utf_16le_ascii_8bit = @utf_16le.dup
36
+ @utf_32be, = Iconv.iconv('utf-32be', 'utf-8', @utf_8)
37
+ @utf_32be_ascii_8bit = @utf_32be.dup
38
+ @utf_32le, = Iconv.iconv('utf-32le', 'utf-8', @utf_8)
39
+ @utf_32le_ascii_8bit = @utf_32le.dup
40
+ end
41
+ end
42
+
43
+ def test_parse
44
+ assert_equal @parsed, JSON.parse(@utf_8)
45
+ assert_equal @parsed, JSON.parse(@utf_16be)
46
+ assert_equal @parsed, JSON.parse(@utf_16le)
47
+ assert_equal @parsed, JSON.parse(@utf_32be)
48
+ assert_equal @parsed, JSON.parse(@utf_32le)
49
+ end
50
+
51
+ def test_parse_ascii_8bit
52
+ assert_equal @parsed, JSON.parse(@utf_8_ascii_8bit)
53
+ assert_equal @parsed, JSON.parse(@utf_16be_ascii_8bit)
54
+ assert_equal @parsed, JSON.parse(@utf_16le_ascii_8bit)
55
+ assert_equal @parsed, JSON.parse(@utf_32be_ascii_8bit)
56
+ assert_equal @parsed, JSON.parse(@utf_32le_ascii_8bit)
57
+ end
58
+
59
+ def test_generate
60
+ assert_equal @generated, JSON.generate(@parsed, :ascii_only => true)
61
+ if defined?(::Encoding)
62
+ assert_equal @generated, JSON.generate(@utf_16_data, :ascii_only => true)
63
+ else
64
+ # XXX checking of correct utf8 data is not as strict (yet?) without :ascii_only
65
+ assert_raises(JSON::GeneratorError) { JSON.generate(@utf_16_data, :ascii_only => true) }
66
+ end
67
+ end
68
+ end
@@ -1,11 +1,15 @@
1
1
  #!/usr/bin/env ruby
2
+ # -*- coding: utf-8 -*-
2
3
 
3
4
  require 'test/unit'
4
- require 'json'
5
+ case ENV['JSON']
6
+ when 'pure' then require 'json/pure'
7
+ when 'ext' then require 'json/ext'
8
+ else require 'json'
9
+ end
5
10
 
6
11
  class TC_JSONFixtures < Test::Unit::TestCase
7
12
  def setup
8
- $KCODE = 'UTF8'
9
13
  fixtures = File.join(File.dirname(__FILE__), 'fixtures/*.json')
10
14
  passed, failed = Dir[fixtures].partition { |f| f['pass'] }
11
15
  @passed = passed.inject([]) { |a, f| a << [ f, File.read(f) ] }.sort
@@ -13,15 +17,15 @@ class TC_JSONFixtures < Test::Unit::TestCase
13
17
  end
14
18
 
15
19
  def test_passing
16
- for (name, source) in @passed
20
+ for name, source in @passed
17
21
  assert JSON.parse(source),
18
22
  "Did not pass for fixture '#{name}'"
19
23
  end
20
24
  end
21
25
 
22
26
  def test_failing
23
- for (name, source) in @failed
24
- assert_raises(JSON::ParserError,
27
+ for name, source in @failed
28
+ assert_raises(JSON::ParserError, JSON::NestingError,
25
29
  "Did not fail for fixture '#{name}'") do
26
30
  JSON.parse(source)
27
31
  end
@@ -1,11 +1,17 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- coding: utf-8 -*-
3
+
1
4
  require 'test/unit'
2
- require 'json'
5
+ case ENV['JSON']
6
+ when 'pure' then require 'json/pure'
7
+ when 'ext' then require 'json/ext'
8
+ else require 'json'
9
+ end
3
10
 
4
11
  class TC_JSONGenerate < Test::Unit::TestCase
5
12
  include JSON
6
13
 
7
14
  def setup
8
- $KCODE = 'UTF8'
9
15
  @hash = {
10
16
  'a' => 2,
11
17
  'b' => 3.141,
@@ -38,20 +44,21 @@ class TC_JSONGenerate < Test::Unit::TestCase
38
44
  EOT
39
45
  end
40
46
 
41
- def test_unparse
42
- json = unparse(@hash)
43
- assert_equal(@json2, json)
47
+ def test_generate
48
+ json = generate(@hash)
49
+ assert_equal(JSON.parse(@json2), JSON.parse(json))
44
50
  parsed_json = parse(json)
45
51
  assert_equal(@hash, parsed_json)
46
52
  json = generate({1=>2})
47
53
  assert_equal('{"1":2}', json)
48
54
  parsed_json = parse(json)
49
55
  assert_equal({"1"=>2}, parsed_json)
56
+ assert_raise(GeneratorError) { generate(666) }
50
57
  end
51
58
 
52
- def test_unparse_pretty
53
- json = pretty_unparse(@hash)
54
- assert_equal(@json3, json)
59
+ def test_generate_pretty
60
+ json = pretty_generate(@hash)
61
+ assert_equal(JSON.parse(@json3), JSON.parse(json))
55
62
  parsed_json = parse(json)
56
63
  assert_equal(@hash, parsed_json)
57
64
  json = pretty_generate({1=>2})
@@ -62,20 +69,113 @@ EOT
62
69
  EOT
63
70
  parsed_json = parse(json)
64
71
  assert_equal({"1"=>2}, parsed_json)
72
+ assert_raise(GeneratorError) { pretty_generate(666) }
73
+ end
74
+
75
+ def test_fast_generate
76
+ json = fast_generate(@hash)
77
+ assert_equal(JSON.parse(@json2), JSON.parse(json))
78
+ parsed_json = parse(json)
79
+ assert_equal(@hash, parsed_json)
80
+ json = fast_generate({1=>2})
81
+ assert_equal('{"1":2}', json)
82
+ parsed_json = parse(json)
83
+ assert_equal({"1"=>2}, parsed_json)
84
+ assert_raise(GeneratorError) { fast_generate(666) }
65
85
  end
66
86
 
67
87
  def test_states
68
88
  json = generate({1=>2}, nil)
69
89
  assert_equal('{"1":2}', json)
70
- s = JSON.state.new(:check_circular => true)
71
- #assert s.check_circular
90
+ s = JSON.state.new
91
+ assert s.check_circular?
92
+ assert s[:check_circular?]
72
93
  h = { 1=>2 }
73
94
  h[3] = h
74
- assert_raises(JSON::CircularDatastructure) { generate(h, s) }
75
- s = JSON.state.new(:check_circular => true)
76
- #assert s.check_circular
95
+ assert_raises(JSON::NestingError) { generate(h) }
96
+ assert_raises(JSON::NestingError) { generate(h, s) }
97
+ s = JSON.state.new
77
98
  a = [ 1, 2 ]
78
99
  a << a
79
- assert_raises(JSON::CircularDatastructure) { generate(a, s) }
100
+ assert_raises(JSON::NestingError) { generate(a, s) }
101
+ assert s.check_circular?
102
+ assert s[:check_circular?]
103
+ end
104
+
105
+ def test_pretty_state
106
+ state = PRETTY_STATE_PROTOTYPE.dup
107
+ assert_equal({
108
+ :allow_nan => false,
109
+ :array_nl => "\n",
110
+ :ascii_only => false,
111
+ :depth => 0,
112
+ :indent => " ",
113
+ :max_nesting => 19,
114
+ :object_nl => "\n",
115
+ :space => " ",
116
+ :space_before => "",
117
+ }.sort_by { |n,| n.to_s }, state.to_h.sort_by { |n,| n.to_s })
118
+ end
119
+
120
+ def test_safe_state
121
+ state = SAFE_STATE_PROTOTYPE.dup
122
+ assert_equal({
123
+ :allow_nan => false,
124
+ :array_nl => "",
125
+ :ascii_only => false,
126
+ :depth => 0,
127
+ :indent => "",
128
+ :max_nesting => 19,
129
+ :object_nl => "",
130
+ :space => "",
131
+ :space_before => "",
132
+ }.sort_by { |n,| n.to_s }, state.to_h.sort_by { |n,| n.to_s })
133
+ end
134
+
135
+ def test_fast_state
136
+ state = FAST_STATE_PROTOTYPE.dup
137
+ assert_equal({
138
+ :allow_nan => false,
139
+ :array_nl => "",
140
+ :ascii_only => false,
141
+ :depth => 0,
142
+ :indent => "",
143
+ :max_nesting => 0,
144
+ :object_nl => "",
145
+ :space => "",
146
+ :space_before => "",
147
+ }.sort_by { |n,| n.to_s }, state.to_h.sort_by { |n,| n.to_s })
148
+ end
149
+
150
+ def test_allow_nan
151
+ assert_raises(GeneratorError) { generate([JSON::NaN]) }
152
+ assert_equal '[NaN]', generate([JSON::NaN], :allow_nan => true)
153
+ assert_raises(GeneratorError) { fast_generate([JSON::NaN]) }
154
+ assert_raises(GeneratorError) { pretty_generate([JSON::NaN]) }
155
+ assert_equal "[\n NaN\n]", pretty_generate([JSON::NaN], :allow_nan => true)
156
+ assert_raises(GeneratorError) { generate([JSON::Infinity]) }
157
+ assert_equal '[Infinity]', generate([JSON::Infinity], :allow_nan => true)
158
+ assert_raises(GeneratorError) { fast_generate([JSON::Infinity]) }
159
+ assert_raises(GeneratorError) { pretty_generate([JSON::Infinity]) }
160
+ assert_equal "[\n Infinity\n]", pretty_generate([JSON::Infinity], :allow_nan => true)
161
+ assert_raises(GeneratorError) { generate([JSON::MinusInfinity]) }
162
+ assert_equal '[-Infinity]', generate([JSON::MinusInfinity], :allow_nan => true)
163
+ assert_raises(GeneratorError) { fast_generate([JSON::MinusInfinity]) }
164
+ assert_raises(GeneratorError) { pretty_generate([JSON::MinusInfinity]) }
165
+ assert_equal "[\n -Infinity\n]", pretty_generate([JSON::MinusInfinity], :allow_nan => true)
166
+ end
167
+
168
+ def test_depth
169
+ ary = []; ary << ary
170
+ assert_equal 0, JSON::SAFE_STATE_PROTOTYPE.depth
171
+ assert_raises(JSON::NestingError) { JSON.generate(ary) }
172
+ assert_equal 0, JSON::SAFE_STATE_PROTOTYPE.depth
173
+ assert_equal 0, JSON::PRETTY_STATE_PROTOTYPE.depth
174
+ assert_raises(JSON::NestingError) { JSON.pretty_generate(ary) }
175
+ assert_equal 0, JSON::PRETTY_STATE_PROTOTYPE.depth
176
+ s = JSON.state.new
177
+ assert_equal 0, s.depth
178
+ assert_raises(JSON::NestingError) { ary.to_json(s) }
179
+ assert_equal 19, s.depth
80
180
  end
81
181
  end
@@ -0,0 +1,144 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- coding: utf-8 -*-
3
+
4
+ require 'test/unit'
5
+ case ENV['JSON']
6
+ when 'pure' then require 'json/pure'
7
+ when 'ext' then require 'json/ext'
8
+ else require 'json'
9
+ end
10
+ require 'json/add/rails'
11
+ require 'date'
12
+
13
+ class TC_JSONRails < Test::Unit::TestCase
14
+ include JSON
15
+
16
+ class A
17
+ def initialize(a)
18
+ @a = a
19
+ end
20
+
21
+ attr_reader :a
22
+
23
+ def ==(other)
24
+ a == other.a
25
+ end
26
+
27
+ def self.json_create(object)
28
+ new(*object['args'])
29
+ end
30
+
31
+ def to_json(*args)
32
+ {
33
+ 'json_class' => self.class.name,
34
+ 'args' => [ @a ],
35
+ }.to_json(*args)
36
+ end
37
+ end
38
+
39
+ class B
40
+ def self.json_creatable?
41
+ false
42
+ end
43
+
44
+ def to_json(*args)
45
+ {
46
+ 'json_class' => self.class.name,
47
+ }.to_json(*args)
48
+ end
49
+ end
50
+
51
+ class C
52
+ def to_json(*args)
53
+ {
54
+ 'json_class' => 'TC_JSONRails::Nix',
55
+ }.to_json(*args)
56
+ end
57
+ end
58
+
59
+ class D
60
+ def initialize
61
+ @foo = 666
62
+ end
63
+
64
+ attr_reader :foo
65
+
66
+ def ==(other)
67
+ foo == other.foo
68
+ end
69
+ end
70
+
71
+ def test_extended_json
72
+ a = A.new(666)
73
+ assert A.json_creatable?
74
+ assert_equal 666, a.a
75
+ json = generate(a)
76
+ a_again = JSON.parse(json)
77
+ assert_kind_of a.class, a_again
78
+ assert_equal a, a_again
79
+ assert_equal 666, a_again.a
80
+ end
81
+
82
+ def test_extended_json_generic_object
83
+ d = D.new
84
+ assert D.json_creatable?
85
+ assert_equal 666, d.foo
86
+ json = generate(d)
87
+ d_again = JSON.parse(json)
88
+ assert_kind_of d.class, d_again
89
+ assert_equal d, d_again
90
+ assert_equal 666, d_again.foo
91
+ end
92
+
93
+ def test_extended_json_disabled
94
+ a = A.new(666)
95
+ assert A.json_creatable?
96
+ json = generate(a)
97
+ a_again = JSON.parse(json, :create_additions => true)
98
+ assert_kind_of a.class, a_again
99
+ assert_equal a, a_again
100
+ a_hash = JSON.parse(json, :create_additions => false)
101
+ assert_kind_of Hash, a_hash
102
+ assert_equal(
103
+ {"args"=>[666], "json_class"=>"TC_JSONRails::A"}.sort_by { |k,| k },
104
+ a_hash.sort_by { |k,| k }
105
+ )
106
+ end
107
+
108
+ def test_extended_json_fail1
109
+ b = B.new
110
+ assert !B.json_creatable?
111
+ json = generate(b)
112
+ assert_equal({ 'json_class' => B.name }, JSON.parse(json))
113
+ end
114
+
115
+ def test_extended_json_fail2
116
+ c = C.new # with rails addition all objects are theoretically creatable
117
+ assert C.json_creatable?
118
+ json = generate(c)
119
+ assert_raises(ArgumentError, NameError) { JSON.parse(json) }
120
+ end
121
+
122
+ def test_raw_strings
123
+ raw = ''
124
+ raw.respond_to?(:encode!) and raw.encode!(Encoding::ASCII_8BIT)
125
+ raw_array = []
126
+ for i in 0..255
127
+ raw << i
128
+ raw_array << i
129
+ end
130
+ json = raw.to_json_raw
131
+ json_raw_object = raw.to_json_raw_object
132
+ hash = { 'json_class' => 'String', 'raw'=> raw_array }
133
+ assert_equal hash, json_raw_object
134
+ assert_match /\A\{.*\}\Z/, json
135
+ assert_match /"json_class":"String"/, json
136
+ 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
137
+ raw_again = JSON.parse(json)
138
+ assert_equal raw, raw_again
139
+ end
140
+
141
+ def test_symbol
142
+ assert_equal '"foo"', :foo.to_json # we don't want an object here
143
+ end
144
+ end
@@ -1,15 +1,16 @@
1
1
  #!/usr/bin/env ruby
2
+ # -*- coding: utf-8 -*-
2
3
 
3
4
  require 'test/unit'
4
- require 'json'
5
+ case ENV['JSON']
6
+ when 'pure' then require 'json/pure'
7
+ when 'ext' then require 'json/ext'
8
+ else require 'json'
9
+ end
5
10
 
6
11
  class TC_JSONUnicode < Test::Unit::TestCase
7
12
  include JSON
8
13
 
9
- def setup
10
- $KCODE = 'UTF8'
11
- end
12
-
13
14
  def test_unicode
14
15
  assert_equal '""', ''.to_json
15
16
  assert_equal '"\\b"', "\b".to_json
@@ -18,37 +19,57 @@ class TC_JSONUnicode < Test::Unit::TestCase
18
19
  assert_equal '" "', ' '.to_json
19
20
  assert_equal "\"#{0x7f.chr}\"", 0x7f.chr.to_json
20
21
  utf8 = [ "© ≠ €! \01" ]
22
+ json = '["© ≠ €! \u0001"]'
23
+ assert_equal json, utf8.to_json(:ascii_only => false)
24
+ assert_equal utf8, parse(json)
21
25
  json = '["\u00a9 \u2260 \u20ac! \u0001"]'
22
- assert_equal json, utf8.to_json
26
+ assert_equal json, utf8.to_json(:ascii_only => true)
23
27
  assert_equal utf8, parse(json)
24
28
  utf8 = ["\343\201\202\343\201\204\343\201\206\343\201\210\343\201\212"]
29
+ json = "[\"\343\201\202\343\201\204\343\201\206\343\201\210\343\201\212\"]"
30
+ assert_equal utf8, parse(json)
31
+ assert_equal json, utf8.to_json(:ascii_only => false)
32
+ utf8 = ["\343\201\202\343\201\204\343\201\206\343\201\210\343\201\212"]
33
+ assert_equal utf8, parse(json)
25
34
  json = "[\"\\u3042\\u3044\\u3046\\u3048\\u304a\"]"
26
- assert_equal json, utf8.to_json
35
+ assert_equal json, utf8.to_json(:ascii_only => true)
27
36
  assert_equal utf8, parse(json)
28
37
  utf8 = ['საქართველო']
38
+ json = '["საქართველო"]'
39
+ assert_equal json, utf8.to_json(:ascii_only => false)
29
40
  json = "[\"\\u10e1\\u10d0\\u10e5\\u10d0\\u10e0\\u10d7\\u10d5\\u10d4\\u10da\\u10dd\"]"
30
- assert_equal json, utf8.to_json
41
+ assert_equal json, utf8.to_json(:ascii_only => true)
31
42
  assert_equal utf8, parse(json)
32
- assert_equal '["\\u00c3"]', JSON.generate(["Ã"])
43
+ assert_equal '["Ã"]', JSON.generate(["Ã"], :ascii_only => false)
44
+ assert_equal '["\\u00c3"]', JSON.generate(["Ã"], :ascii_only => true)
33
45
  assert_equal ["€"], JSON.parse('["\u20ac"]')
34
46
  utf8 = ["\xf0\xa0\x80\x81"]
47
+ json = "[\"\xf0\xa0\x80\x81\"]"
48
+ assert_equal json, JSON.generate(utf8, :ascii_only => false)
49
+ assert_equal utf8, JSON.parse(json)
35
50
  json = '["\ud840\udc01"]'
36
- assert_equal json, JSON.generate(utf8)
51
+ assert_equal json, JSON.generate(utf8, :ascii_only => true)
37
52
  assert_equal utf8, JSON.parse(json)
38
53
  end
39
54
 
40
55
  def test_chars
41
56
  (0..0x7f).each do |i|
42
57
  json = '["\u%04x"]' % i
58
+ if RUBY_VERSION >= "1.9."
59
+ i = i.chr
60
+ end
43
61
  assert_equal i, JSON.parse(json).first[0]
44
- if [?\b, ?\n, ?\r, ?\t, ?\f].include?(i)
62
+ if i == ?\b
63
+ generated = JSON.generate(["" << i])
64
+ assert '["\b"]' == generated || '["\10"]' == generated
65
+ elsif [?\n, ?\r, ?\t, ?\f].include?(i)
45
66
  assert_equal '[' << ('' << i).dump << ']', JSON.generate(["" << i])
46
- elsif i < 0x20
67
+ elsif i.chr < 0x20.chr
47
68
  assert_equal json, JSON.generate(["" << i])
48
69
  end
49
70
  end
50
- assert_raises(JSON::GeneratorError) do
51
- JSON.generate(["" << 0x80])
71
+ assert_raise(JSON::GeneratorError) do
72
+ JSON.generate(["\x80"], :ascii_only => true)
52
73
  end
53
74
  assert_equal "\302\200", JSON.parse('["\u0080"]').first
54
75
  end
data/tools/fuzz.rb CHANGED
@@ -1,7 +1,12 @@
1
- $KCODE='UTF8'
2
1
  require 'json'
3
- require 'irb'
4
- require 'pp'
2
+
3
+ require 'iconv'
4
+ ISO_8859_1_TO_UTF8 = Iconv.new('utf-8', 'iso-8859-15')
5
+ class ::String
6
+ def to_utf8
7
+ ISO_8859_1_TO_UTF8.iconv self
8
+ end
9
+ end
5
10
 
6
11
  class Fuzzer
7
12
  def initialize(n, freqs = {})
@@ -14,13 +19,13 @@ class Fuzzer
14
19
  end
15
20
  @freqs = freqs
16
21
  @n = n
17
- @alpha = (0..0x7f).to_a
22
+ @alpha = (0..0xff).to_a
18
23
  end
19
24
 
20
25
  def random_string
21
26
  s = ''
22
27
  30.times { s << @alpha[rand(@alpha.size)] }
23
- s
28
+ s.to_utf8
24
29
  end
25
30
 
26
31
  def pick
@@ -86,7 +91,8 @@ class MyState < JSON.state
86
91
  :space => make_spaces,
87
92
  :space_before => make_spaces,
88
93
  :object_nl => make_spaces,
89
- :array_nl => make_spaces
94
+ :array_nl => make_spaces,
95
+ :max_nesting => false
90
96
  )
91
97
  end
92
98
 
@@ -118,7 +124,7 @@ loop do
118
124
  puts json.size
119
125
  end
120
126
  begin
121
- o2 = JSON.parse(json)
127
+ o2 = JSON.parse(json, :max_nesting => false)
122
128
  rescue JSON::ParserError => e
123
129
  puts "Caught #{e.class}: #{e.message}\n#{e.backtrace * "\n"}"
124
130
  puts "o1 = #{o1.inspect}", "json = #{json}", "json_str = #{json.inspect}"
data/tools/server.rb CHANGED
@@ -1,6 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- $KCODE='UTF8'
4
3
  require 'webrick'
5
4
  include WEBrick
6
5
  $:.unshift 'ext'