json-jruby 1.1.3-universal-java-1.6 → 1.1.6-universal-java-1.6

Sign up to get free protection for your applications and to get access to all the features.
data/lib/json.rb CHANGED
@@ -230,6 +230,4 @@ module JSON
230
230
  require 'json/pure'
231
231
  end
232
232
  end
233
-
234
- JSON_LOADED = true
235
233
  end
data/lib/json/add/core.rb CHANGED
@@ -96,7 +96,7 @@ class Struct
96
96
 
97
97
  def to_json(*args)
98
98
  klass = self.class.name
99
- klass.empty? and raise JSON::JSONError, "Only named structs are supported!"
99
+ klass.to_s.empty? and raise JSON::JSONError, "Only named structs are supported!"
100
100
  {
101
101
  'json_class' => klass,
102
102
  'v' => values,
data/lib/json/ext.rb CHANGED
@@ -10,4 +10,6 @@ module JSON
10
10
  JSON.parser = Parser
11
11
  JSON.generator = Generator
12
12
  end
13
+
14
+ JSON_LOADED = true
13
15
  end
Binary file
Binary file
data/lib/json/pure.rb CHANGED
@@ -72,4 +72,6 @@ module JSON
72
72
  JSON.parser = Parser
73
73
  JSON.generator = Generator
74
74
  end
75
+
76
+ JSON_LOADED = true
75
77
  end
@@ -39,23 +39,48 @@ module JSON
39
39
 
40
40
  # Convert a UTF8 encoded Ruby string _string_ to a JSON string, encoded with
41
41
  # UTF16 big endian characters as \u????, and return it.
42
- def utf8_to_json(string) # :nodoc:
43
- string = string.gsub(/["\\\/\x0-\x1f]/) { MAP[$&] }
44
- string.gsub!(/(
45
- (?:
46
- [\xc2-\xdf][\x80-\xbf] |
47
- [\xe0-\xef][\x80-\xbf]{2} |
48
- [\xf0-\xf4][\x80-\xbf]{3}
49
- )+ |
50
- [\x80-\xc1\xf5-\xff] # invalid
51
- )/nx) { |c|
52
- c.size == 1 and raise GeneratorError, "invalid utf8 byte: '#{c}'"
53
- s = JSON::UTF8toUTF16.iconv(c).unpack('H*')[0]
54
- s.gsub!(/.{4}/n, '\\\\u\&')
55
- }
56
- string
57
- rescue Iconv::Failure => e
58
- raise GeneratorError, "Caught #{e.class}: #{e}"
42
+ if String.method_defined?(:force_encoding)
43
+ def utf8_to_json(string) # :nodoc:
44
+ string = string.dup
45
+ string << '' # XXX workaround: avoid buffer sharing
46
+ string.force_encoding(Encoding::ASCII_8BIT)
47
+ string.gsub!(/["\\\/\x0-\x1f]/) { MAP[$&] }
48
+ string.gsub!(/(
49
+ (?:
50
+ [\xc2-\xdf][\x80-\xbf] |
51
+ [\xe0-\xef][\x80-\xbf]{2} |
52
+ [\xf0-\xf4][\x80-\xbf]{3}
53
+ )+ |
54
+ [\x80-\xc1\xf5-\xff] # invalid
55
+ )/nx) { |c|
56
+ c.size == 1 and raise GeneratorError, "invalid utf8 byte: '#{c}'"
57
+ s = JSON::UTF8toUTF16.iconv(c).unpack('H*')[0]
58
+ s.gsub!(/.{4}/n, '\\\\u\&')
59
+ }
60
+ string.force_encoding(Encoding::UTF_8)
61
+ string
62
+ rescue Iconv::Failure => e
63
+ raise GeneratorError, "Caught #{e.class}: #{e}"
64
+ end
65
+ else
66
+ def utf8_to_json(string) # :nodoc:
67
+ string = string.gsub(/["\\\/\x0-\x1f]/) { MAP[$&] }
68
+ string.gsub!(/(
69
+ (?:
70
+ [\xc2-\xdf][\x80-\xbf] |
71
+ [\xe0-\xef][\x80-\xbf]{2} |
72
+ [\xf0-\xf4][\x80-\xbf]{3}
73
+ )+ |
74
+ [\x80-\xc1\xf5-\xff] # invalid
75
+ )/nx) { |c|
76
+ c.size == 1 and raise GeneratorError, "invalid utf8 byte: '#{c}'"
77
+ s = JSON::UTF8toUTF16.iconv(c).unpack('H*')[0]
78
+ s.gsub!(/.{4}/n, '\\\\u\&')
79
+ }
80
+ string
81
+ rescue Iconv::Failure => e
82
+ raise GeneratorError, "Caught #{e.class}: #{e}"
83
+ end
59
84
  end
60
85
  module_function :utf8_to_json
61
86
 
@@ -239,20 +264,28 @@ module JSON
239
264
 
240
265
  def json_transform(state, depth)
241
266
  delim = ','
242
- delim << state.object_nl if state
243
- result = '{'
244
- result << state.object_nl if state
245
- result << map { |key,value|
246
- s = json_shift(state, depth + 1)
247
- s << key.to_s.to_json(state, depth + 1)
248
- s << state.space_before if state
249
- s << ':'
250
- s << state.space if state
251
- s << value.to_json(state, depth + 1)
252
- }.join(delim)
253
- result << state.object_nl if state
254
- result << json_shift(state, depth)
255
- result << '}'
267
+ if state
268
+ delim << state.object_nl
269
+ result = '{'
270
+ result << state.object_nl
271
+ result << map { |key,value|
272
+ s = json_shift(state, depth + 1)
273
+ s << key.to_s.to_json(state, depth + 1)
274
+ s << state.space_before
275
+ s << ':'
276
+ s << state.space
277
+ s << value.to_json(state, depth + 1)
278
+ }.join(delim)
279
+ result << state.object_nl
280
+ result << json_shift(state, depth)
281
+ result << '}'
282
+ else
283
+ result = '{'
284
+ result << map { |key,value|
285
+ key.to_s.to_json << ':' << value.to_json
286
+ }.join(delim)
287
+ result << '}'
288
+ end
256
289
  result
257
290
  end
258
291
  end
@@ -293,16 +326,19 @@ module JSON
293
326
 
294
327
  def json_transform(state, depth)
295
328
  delim = ','
296
- delim << state.array_nl if state
297
- result = '['
298
- result << state.array_nl if state
299
- result << map { |value|
300
- json_shift(state, depth + 1) << value.to_json(state, depth + 1)
301
- }.join(delim)
302
- result << state.array_nl if state
303
- result << json_shift(state, depth)
304
- result << ']'
305
- result
329
+ if state
330
+ delim << state.array_nl
331
+ result = '['
332
+ result << state.array_nl
333
+ result << map { |value|
334
+ json_shift(state, depth + 1) << value.to_json(state, depth + 1)
335
+ }.join(delim)
336
+ result << state.array_nl
337
+ result << json_shift(state, depth)
338
+ result << ']'
339
+ else
340
+ '[' << map { |value| value.to_json }.join(delim) << ']'
341
+ end
306
342
  end
307
343
  end
308
344
 
@@ -61,6 +61,8 @@ module JSON
61
61
  # * *create_additions*: If set to false, the Parser doesn't create
62
62
  # additions even if a matchin class and create_id was found. This option
63
63
  # defaults to true.
64
+ # * *object_class*: Defaults to Hash
65
+ # * *array_class*: Defaults to Array
64
66
  def initialize(source, opts = {})
65
67
  super
66
68
  if !opts.key?(:max_nesting) # defaults to 19
@@ -74,6 +76,8 @@ module JSON
74
76
  ca = true
75
77
  ca = opts[:create_additions] if opts.key?(:create_additions)
76
78
  @create_id = ca ? JSON.create_id : nil
79
+ @object_class = opts[:object_class] || Hash
80
+ @array_class = opts[:array_class] || Array
77
81
  end
78
82
 
79
83
  alias source string
@@ -122,19 +126,23 @@ module JSON
122
126
  def parse_string
123
127
  if scan(STRING)
124
128
  return '' if self[1].empty?
125
- self[1].gsub(%r((?:\\[\\bfnrt"/]|(?:\\u(?:[A-Fa-f\d]{4}))+|\\[\x20-\xff]))n) do |c|
129
+ string = self[1].gsub(%r((?:\\[\\bfnrt"/]|(?:\\u(?:[A-Fa-f\d]{4}))+|\\[\x20-\xff]))n) do |c|
126
130
  if u = UNESCAPE_MAP[$&[1]]
127
131
  u
128
132
  else # \uXXXX
129
133
  bytes = ''
130
134
  i = 0
131
- while c[6 * i] == ?\\ && c[6 * i + 1] == ?u
135
+ while c[6 * i] == ?\\ && c[6 * i + 1] == ?u
132
136
  bytes << c[6 * i + 2, 2].to_i(16) << c[6 * i + 4, 2].to_i(16)
133
137
  i += 1
134
138
  end
135
139
  JSON::UTF16toUTF8.iconv(bytes)
136
140
  end
137
141
  end
142
+ if string.respond_to?(:force_encoding)
143
+ string.force_encoding(Encoding::UTF_8)
144
+ end
145
+ string
138
146
  else
139
147
  UNPARSED
140
148
  end
@@ -180,7 +188,7 @@ module JSON
180
188
  def parse_array
181
189
  raise NestingError, "nesting of #@current_nesting is to deep" if
182
190
  @max_nesting.nonzero? && @current_nesting > @max_nesting
183
- result = []
191
+ result = @array_class.new
184
192
  delim = false
185
193
  until eos?
186
194
  case
@@ -212,7 +220,7 @@ module JSON
212
220
  def parse_object
213
221
  raise NestingError, "nesting of #@current_nesting is to deep" if
214
222
  @max_nesting.nonzero? && @current_nesting > @max_nesting
215
- result = {}
223
+ result = @object_class.new
216
224
  delim = false
217
225
  until eos?
218
226
  case
data/lib/json/version.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  module JSON
2
2
  # JSON version
3
- VERSION = '1.1.3'
3
+ VERSION = '1.1.6'
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:
data/tests/runner.rb CHANGED
@@ -10,6 +10,7 @@ require 'test_json_unicode'
10
10
  require 'test_json_addition'
11
11
  require 'test_json_rails'
12
12
  require 'test_json_fixtures'
13
+ require 'test_jjrb_offsets'
13
14
 
14
15
  class TS_AllTests
15
16
  def self.suite
@@ -20,6 +21,7 @@ class TS_AllTests
20
21
  suite << TC_JSONAddition.suite
21
22
  suite << TC_JSONRails.suite
22
23
  suite << TC_JSONFixtures.suite
24
+ suite << TestJjrbOffsets.suite
23
25
  end
24
26
  end
25
27
  Test::Unit::UI::Console::TestRunner.run(TS_AllTests)
@@ -0,0 +1,308 @@
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
+
11
+ class TestJjrbOffsets < Test::Unit::TestCase
12
+ # Test non-zero ByteList offsets.
13
+ # Yes, this is a very ad-hoc, copy-pasted test suite.
14
+
15
+ include JSON
16
+
17
+ def o(string, offset = 1)
18
+ full = "!" * offset + string
19
+ return full[offset, string.length]
20
+ end
21
+
22
+ def parse(string, *args)
23
+ return JSON.parse(o(string), *args)
24
+ end
25
+
26
+ def setup
27
+ @ary = [1, "foo", 3.14, 4711.0, 2.718, nil, [1,-2,3], false, true].map do
28
+ |x| [x]
29
+ end
30
+ @ary_to_parse = ["1", '"foo"', "3.14", "4711.0", "2.718", "null",
31
+ "[1,-2,3]", "false", "true"].map do
32
+ |x| "[#{x}]"
33
+ end
34
+ @hash = {
35
+ 'a' => 2,
36
+ 'b' => 3.141,
37
+ 'c' => 'c',
38
+ 'd' => [ 1, "b", 3.14 ],
39
+ 'e' => { 'foo' => 'bar' },
40
+ 'g' => "\"\0\037",
41
+ 'h' => 1000.0,
42
+ 'i' => 0.001
43
+ }
44
+ @json = '{"a":2,"b":3.141,"c":"c","d":[1,"b",3.14],"e":{"foo":"bar"},'\
45
+ '"g":"\\"\\u0000\\u001f","h":1.0E3,"i":1.0E-3}'
46
+ end
47
+
48
+ def test_parser_source_offset
49
+ parser = JSON::Parser.new(o("test"))
50
+ assert_equal 'test', parser.source
51
+ assert_equal [], parse("[]")
52
+ end
53
+
54
+ def assert_equal_float(expected, is)
55
+ assert_in_delta(expected.first, is.first, 1e-2)
56
+ end
57
+
58
+ def test_parse_simple_arrays
59
+ assert_equal([], parse('[]'))
60
+ assert_equal([], parse(' [ ] '))
61
+ assert_equal([nil], parse('[null]'))
62
+ assert_equal([false], parse('[false]'))
63
+ assert_equal([true], parse('[true]'))
64
+ assert_equal([-23], parse('[-23]'))
65
+ assert_equal([23], parse('[23]'))
66
+ assert_equal([0.23], parse('[0.23]'))
67
+ assert_equal([0.0], parse('[0e0]'))
68
+ assert_raises(JSON::ParserError) { parse('[+23.2]') }
69
+ assert_raises(JSON::ParserError) { parse('[+23]') }
70
+ assert_raises(JSON::ParserError) { parse('[.23]') }
71
+ assert_raises(JSON::ParserError) { parse('[023]') }
72
+ assert_equal_float [3.141], parse('[3.141]')
73
+ assert_equal_float [-3.141], parse('[-3.141]')
74
+ assert_equal_float [3.141], parse('[3141e-3]')
75
+ assert_equal_float [3.141], parse('[3141.1e-3]')
76
+ assert_equal_float [3.141], parse('[3141E-3]')
77
+ assert_equal_float [3.141], parse('[3141.0E-3]')
78
+ assert_equal_float [-3.141], parse('[-3141.0e-3]')
79
+ assert_equal_float [-3.141], parse('[-3141e-3]')
80
+ assert_raises(ParserError) { parse('[NaN]') }
81
+ assert parse('[NaN]', :allow_nan => true).first.nan?
82
+ assert_raises(ParserError) { parse('[Infinity]') }
83
+ assert_equal [1.0/0], parse('[Infinity]', :allow_nan => true)
84
+ assert_raises(ParserError) { parse('[-Infinity]') }
85
+ assert_equal [-1.0/0], parse('[-Infinity]', :allow_nan => true)
86
+ assert_equal([""], parse('[""]'))
87
+ assert_equal(["foobar"], parse('["foobar"]'))
88
+ assert_equal([{}], parse('[{}]'))
89
+ end
90
+
91
+ def test_parse_simple_objects
92
+ assert_equal({}, parse('{}'))
93
+ assert_equal({}, parse(' { } '))
94
+ assert_equal({ "a" => nil }, parse('{ "a" : null}'))
95
+ assert_equal({ "a" => nil }, parse('{"a":null}'))
96
+ assert_equal({ "a" => false }, parse('{ "a" : false } '))
97
+ assert_equal({ "a" => false }, parse('{"a":false}'))
98
+ assert_raises(JSON::ParserError) { parse('{false}') }
99
+ assert_equal({ "a" => true }, parse('{"a":true}'))
100
+ assert_equal({ "a" => true }, parse(' { "a" : true } '))
101
+ assert_equal({ "a" => -23 }, parse(' { "a" : -23 } '))
102
+ assert_equal({ "a" => -23 }, parse(' { "a" : -23 } '))
103
+ assert_equal({ "a" => 23 }, parse('{"a":23 } '))
104
+ assert_equal({ "a" => 23 }, parse(' { "a" : 23 } '))
105
+ assert_equal({ "a" => 0.23 }, parse(' { "a" : 0.23 } '))
106
+ assert_equal({ "a" => 0.23 }, parse(' { "a" : 0.23 } '))
107
+ end
108
+
109
+ begin
110
+ require 'permutation'
111
+ def test_parse_more_complex_arrays
112
+ a = [ nil, false, true, "foßbar", [ "n€st€d", true ], { "nested" => true, "n€ßt€ð2" => {} }]
113
+ perms = Permutation.for a
114
+ perms.each do |perm|
115
+ orig_ary = perm.project
116
+ json = pretty_generate(orig_ary)
117
+ assert_equal orig_ary, parse(json)
118
+ end
119
+ end
120
+
121
+ def test_parse_complex_objects
122
+ a = [ nil, false, true, "foßbar", [ "n€st€d", true ], { "nested" => true, "n€ßt€ð2" => {} }]
123
+ perms = Permutation.for a
124
+ perms.each do |perm|
125
+ s = "a"
126
+ orig_obj = perm.project.inject({}) { |h, x| h[s.dup] = x; s = s.succ; h }
127
+ json = pretty_generate(orig_obj)
128
+ assert_equal orig_obj, parse(json)
129
+ end
130
+ end
131
+ rescue LoadError
132
+ warn "Skipping permutation tests."
133
+ end
134
+
135
+ def test_parse_arrays
136
+ assert_equal([1,2,3], parse('[1,2,3]'))
137
+ assert_equal([1.2,2,3], parse('[1.2,2,3]'))
138
+ assert_equal([[],[[],[]]], parse('[[],[[],[]]]'))
139
+ end
140
+
141
+ def test_parse_values
142
+ assert_equal([""], parse('[""]'))
143
+ assert_equal(["\\"], parse('["\\\\"]'))
144
+ assert_equal(['"'], parse('["\""]'))
145
+ assert_equal(['\\"\\'], parse('["\\\\\\"\\\\"]'))
146
+ assert_equal(["\"\b\n\r\t\0\037"],
147
+ parse('["\"\b\n\r\t\u0000\u001f"]'))
148
+ for i in 0 ... @ary.size
149
+ assert_equal(@ary[i], parse(@ary_to_parse[i]))
150
+ end
151
+ end
152
+
153
+ def test_parse_array
154
+ assert_equal([], parse('[]'))
155
+ assert_equal([], parse(' [ ] '))
156
+ assert_equal([1], parse('[1]'))
157
+ assert_equal([1], parse(' [ 1 ] '))
158
+ assert_equal(@ary,
159
+ parse('[[1],["foo"],[3.14],[47.11e+2],[2718.0E-3],[null],[[1,-2,3]]'\
160
+ ',[false],[true]]'))
161
+ assert_equal(@ary, parse(%Q{ [ [1] , ["foo"] , [3.14] \t , [47.11e+2]
162
+ , [2718.0E-3 ],\r[ null] , [[1, -2, 3 ]], [false ],[ true]\n ] }))
163
+ end
164
+
165
+ def test_parse_object
166
+ assert_equal({}, parse('{}'))
167
+ assert_equal({}, parse(' { } '))
168
+ assert_equal({'foo'=>'bar'}, parse('{"foo":"bar"}'))
169
+ assert_equal({'foo'=>'bar'}, parse(' { "foo" : "bar" } '))
170
+ end
171
+
172
+ def test_parser_reset
173
+ parser = Parser.new(@json)
174
+ assert_equal(@hash, parser.parse)
175
+ assert_equal(@hash, parser.parse)
176
+ end
177
+
178
+ def test_comments
179
+ json = <<EOT
180
+ {
181
+ "key1":"value1", // eol comment
182
+ "key2":"value2" /* multi line
183
+ * comment */,
184
+ "key3":"value3" /* multi line
185
+ // nested eol comment
186
+ * comment */
187
+ }
188
+ EOT
189
+ assert_equal(
190
+ { "key1" => "value1", "key2" => "value2", "key3" => "value3" },
191
+ parse(json))
192
+ json = <<EOT
193
+ {
194
+ "key1":"value1" /* multi line
195
+ // nested eol comment
196
+ /* illegal nested multi line comment */
197
+ * comment */
198
+ }
199
+ EOT
200
+ assert_raises(ParserError) { parse(json) }
201
+ json = <<EOT
202
+ {
203
+ "key1":"value1" /* multi line
204
+ // nested eol comment
205
+ closed multi comment */
206
+ and again, throw an Error */
207
+ }
208
+ EOT
209
+ assert_raises(ParserError) { parse(json) }
210
+ json = <<EOT
211
+ {
212
+ "key1":"value1" /*/*/
213
+ }
214
+ EOT
215
+ assert_equal({ "key1" => "value1" }, parse(json))
216
+ end
217
+
218
+ def test_backslash
219
+ data = [ '\\.(?i:gif|jpe?g|png)$' ]
220
+ json = '["\\\\.(?i:gif|jpe?g|png)$"]'
221
+ assert_equal json, JSON.unparse(data)
222
+ assert_equal data, JSON.parse(json)
223
+ #
224
+ data = [ '\\"' ]
225
+ json = '["\\\\\""]'
226
+ assert_equal json, JSON.unparse(data)
227
+ assert_equal data, JSON.parse(json)
228
+ #
229
+ json = '["\/"]'
230
+ data = JSON.parse(json)
231
+ assert_equal ['/'], data
232
+ assert_equal json, JSON.unparse(data)
233
+ #
234
+ json = '["\""]'
235
+ data = JSON.parse(json)
236
+ assert_equal ['"'], data
237
+ assert_equal json, JSON.unparse(data)
238
+ json = '["\\\'"]'
239
+ data = JSON.parse(json)
240
+ assert_equal ["'"], data
241
+ assert_equal '["\'"]', JSON.unparse(data)
242
+ end
243
+
244
+ def test_wrong_inputs
245
+ assert_raises(ParserError) { JSON.parse('"foo"') }
246
+ assert_raises(ParserError) { JSON.parse('123') }
247
+ assert_raises(ParserError) { JSON.parse('[] bla') }
248
+ assert_raises(ParserError) { JSON.parse('[] 1') }
249
+ assert_raises(ParserError) { JSON.parse('[] []') }
250
+ assert_raises(ParserError) { JSON.parse('[] {}') }
251
+ assert_raises(ParserError) { JSON.parse('{} []') }
252
+ assert_raises(ParserError) { JSON.parse('{} {}') }
253
+ assert_raises(ParserError) { JSON.parse('[NULL]') }
254
+ assert_raises(ParserError) { JSON.parse('[FALSE]') }
255
+ assert_raises(ParserError) { JSON.parse('[TRUE]') }
256
+ assert_raises(ParserError) { JSON.parse('[07] ') }
257
+ assert_raises(ParserError) { JSON.parse('[0a]') }
258
+ assert_raises(ParserError) { JSON.parse('[1.]') }
259
+ assert_raises(ParserError) { JSON.parse(' ') }
260
+ end
261
+
262
+ def test_nesting
263
+ assert_raises(JSON::NestingError) { JSON.parse '[[]]', :max_nesting => 1 }
264
+ assert_raises(JSON::NestingError) { JSON.parser.new('[[]]', :max_nesting => 1).parse }
265
+ assert_equal [[]], JSON.parse('[[]]', :max_nesting => 2)
266
+ too_deep = '[[[[[[[[[[[[[[[[[[[["Too deep"]]]]]]]]]]]]]]]]]]]]'
267
+ too_deep_ary = eval too_deep
268
+ assert_raises(JSON::NestingError) { JSON.parse too_deep }
269
+ assert_raises(JSON::NestingError) { JSON.parser.new(too_deep).parse }
270
+ assert_raises(JSON::NestingError) { JSON.parse too_deep, :max_nesting => 19 }
271
+ ok = JSON.parse too_deep, :max_nesting => 20
272
+ assert_equal too_deep_ary, ok
273
+ ok = JSON.parse too_deep, :max_nesting => nil
274
+ assert_equal too_deep_ary, ok
275
+ ok = JSON.parse too_deep, :max_nesting => false
276
+ assert_equal too_deep_ary, ok
277
+ ok = JSON.parse too_deep, :max_nesting => 0
278
+ assert_equal too_deep_ary, ok
279
+ assert_raises(JSON::NestingError) { JSON.generate [[]], :max_nesting => 1 }
280
+ assert_equal '[[]]', JSON.generate([[]], :max_nesting => 2)
281
+ assert_raises(JSON::NestingError) { JSON.generate too_deep_ary }
282
+ assert_raises(JSON::NestingError) { JSON.generate too_deep_ary, :max_nesting => 19 }
283
+ ok = JSON.generate too_deep_ary, :max_nesting => 20
284
+ assert_equal too_deep, ok
285
+ ok = JSON.generate too_deep_ary, :max_nesting => nil
286
+ assert_equal too_deep, ok
287
+ ok = JSON.generate too_deep_ary, :max_nesting => false
288
+ assert_equal too_deep, ok
289
+ ok = JSON.generate too_deep_ary, :max_nesting => 0
290
+ assert_equal too_deep, ok
291
+ end
292
+
293
+ def test_load_dump
294
+ too_deep = '[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]'
295
+ assert_equal too_deep, JSON.dump(eval(too_deep))
296
+ assert_kind_of String, Marshal.dump(eval(too_deep))
297
+ assert_raises(ArgumentError) { JSON.dump(eval(too_deep), 19) }
298
+ assert_raises(ArgumentError) { Marshal.dump(eval(too_deep), 19) }
299
+ assert_equal too_deep, JSON.dump(eval(too_deep), 20)
300
+ assert_kind_of String, Marshal.dump(eval(too_deep), 20)
301
+ output = StringIO.new
302
+ JSON.dump(eval(too_deep), output)
303
+ assert_equal too_deep, output.string
304
+ output = StringIO.new
305
+ JSON.dump(eval(too_deep), output, 20)
306
+ assert_equal too_deep, output.string
307
+ end
308
+ end
data/tests/test_json.rb CHANGED
@@ -1,14 +1,18 @@
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
  require 'stringio'
6
11
 
7
12
  class TC_JSON < Test::Unit::TestCase
8
13
  include JSON
9
14
 
10
15
  def setup
11
- $KCODE = 'UTF8'
12
16
  @ary = [1, "foo", 3.14, 4711.0, 2.718, nil, [1,-2,3], false, true].map do
13
17
  |x| [x]
14
18
  end
@@ -26,10 +30,9 @@ class TC_JSON < Test::Unit::TestCase
26
30
  'h' => 1000.0,
27
31
  'i' => 0.001
28
32
  }
29
- @json = '{"a":2,"b":3.141,"c":"c","d":[1,"b",3.14],"e":{"foo":"bar"},' +
33
+ @json = '{"a":2,"b":3.141,"c":"c","d":[1,"b",3.14],"e":{"foo":"bar"},'\
30
34
  '"g":"\\"\\u0000\\u001f","h":1.0E3,"i":1.0E-3}'
31
35
  end
32
- suite << TC_JSON.suite
33
36
 
34
37
  def test_construction
35
38
  parser = JSON::Parser.new('test')
@@ -147,6 +150,14 @@ class TC_JSON < Test::Unit::TestCase
147
150
  , [2718.0E-3 ],\r[ null] , [[1, -2, 3 ]], [false ],[ true]\n ] }))
148
151
  end
149
152
 
153
+ class SubArray < Array; end
154
+
155
+ def test_parse_array_custom_class
156
+ res = parse('[]', :array_class => SubArray)
157
+ assert_equal([], res)
158
+ assert_equal(SubArray, res.class)
159
+ end
160
+
150
161
  def test_parse_object
151
162
  assert_equal({}, parse('{}'))
152
163
  assert_equal({}, parse(' { } '))
@@ -154,6 +165,14 @@ class TC_JSON < Test::Unit::TestCase
154
165
  assert_equal({'foo'=>'bar'}, parse(' { "foo" : "bar" } '))
155
166
  end
156
167
 
168
+ class SubHash < Hash; end
169
+
170
+ def test_parse_object_custom_class
171
+ res = parse('{}', :object_class => SubHash)
172
+ assert_equal({}, res)
173
+ assert_equal(SubHash, res.class)
174
+ end
175
+
157
176
  def test_parser_reset
158
177
  parser = Parser.new(@json)
159
178
  assert_equal(@hash, parser.parse)
@@ -1,6 +1,12 @@
1
1
  #!/usr/bin/env ruby
2
+ # -*- coding:utf-8 -*-
2
3
 
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
4
10
  require 'json/add/core'
5
11
  require 'date'
6
12
 
@@ -54,10 +60,6 @@ class TC_JSONAddition < Test::Unit::TestCase
54
60
  end
55
61
  end
56
62
 
57
- def setup
58
- $KCODE = 'UTF8'
59
- end
60
-
61
63
  def test_extended_json
62
64
  a = A.new(666)
63
65
  assert A.json_creatable?
@@ -98,6 +100,7 @@ class TC_JSONAddition < Test::Unit::TestCase
98
100
 
99
101
  def test_raw_strings
100
102
  raw = ''
103
+ raw.respond_to?(:encode!) and raw.encode!(Encoding::ASCII_8BIT)
101
104
  raw_array = []
102
105
  for i in 0..255
103
106
  raw << i
@@ -120,7 +123,7 @@ EOT
120
123
 
121
124
  def test_core
122
125
  t = Time.now
123
- assert_equal t, JSON(JSON(t))
126
+ assert_equal t.inspect, JSON(JSON(t)).inspect
124
127
  d = Date.today
125
128
  assert_equal d, JSON(JSON(d))
126
129
  d = DateTime.civil(2007, 6, 14, 14, 57, 10, Rational(1, 12), 2299161)
@@ -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
@@ -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,
@@ -1,6 +1,12 @@
1
1
  #!/usr/bin/env ruby
2
+ # -*- coding: utf-8 -*-
2
3
 
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
4
10
  require 'json/add/rails'
5
11
  require 'date'
6
12
 
@@ -50,17 +56,38 @@ class TC_JSONRails < Test::Unit::TestCase
50
56
  end
51
57
  end
52
58
 
53
- def setup
54
- $KCODE = 'UTF8'
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
55
69
  end
56
70
 
57
71
  def test_extended_json
58
72
  a = A.new(666)
59
73
  assert A.json_creatable?
74
+ assert_equal 666, a.a
60
75
  json = generate(a)
61
76
  a_again = JSON.parse(json)
62
77
  assert_kind_of a.class, a_again
63
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
64
91
  end
65
92
 
66
93
  def test_extended_json_disabled
@@ -94,6 +121,7 @@ class TC_JSONRails < Test::Unit::TestCase
94
121
 
95
122
  def test_raw_strings
96
123
  raw = ''
124
+ raw.respond_to?(:encode!) and raw.encode!(Encoding::ASCII_8BIT)
97
125
  raw_array = []
98
126
  for i in 0..255
99
127
  raw << i
@@ -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
@@ -53,8 +54,8 @@ class TC_JSONUnicode < Test::Unit::TestCase
53
54
  assert_equal json, JSON.generate(["" << i])
54
55
  end
55
56
  end
56
- assert_raises(JSON::GeneratorError) do
57
- JSON.generate(["" << 0x80])
57
+ assert_raise(JSON::GeneratorError) do
58
+ JSON.generate(["\x80"])
58
59
  end
59
60
  assert_equal "\302\200", JSON.parse('["\u0080"]').first
60
61
  end
metadata CHANGED
@@ -17,23 +17,21 @@ signing_key:
17
17
  name: json-jruby
18
18
  rdoc_options: []
19
19
 
20
- autorequire:
21
20
  rubyforge_project: json-jruby
21
+ autorequire:
22
+ licenses: []
23
+
22
24
  executables: []
23
25
 
24
26
  description:
25
- specification_version: 2
27
+ specification_version: 3
26
28
  default_executable:
27
29
  files:
28
30
  - lib/json.rb
29
- - lib/json
30
31
  - lib/json/version.rb
31
- - lib/json/add
32
- - lib/json/ext
33
32
  - lib/json/ext.rb
34
33
  - lib/json/pure.rb
35
34
  - lib/json/common.rb
36
- - lib/json/pure
37
35
  - lib/json/add/rails.rb
38
36
  - lib/json/add/core.rb
39
37
  - lib/json/ext/generator.jar
@@ -42,7 +40,7 @@ files:
42
40
  - lib/json/pure/parser.rb
43
41
  - tests/test_json_generate.rb
44
42
  - tests/test_json_unicode.rb
45
- - tests/fixtures
43
+ - tests/test_jjrb_offsets.rb
46
44
  - tests/test_json_addition.rb
47
45
  - tests/test_json_fixtures.rb
48
46
  - tests/test_json_rails.rb
@@ -87,20 +85,20 @@ required_rubygems_version: !ruby/object:Gem::Requirement
87
85
  version:
88
86
  extensions: []
89
87
 
90
- rubygems_version: 1.3.1
88
+ rubygems_version: 1.3.3
91
89
  requirements: []
92
90
 
93
91
  authors:
94
92
  - Daniel Luz
95
- date: 2009-01-04 02:00:00 +00:00
93
+ date: 2009-05-13 03:00:00 +00:00
96
94
  platform: universal-java-1.6
97
95
  test_files:
98
96
  - tests/runner.rb
99
97
  version: !ruby/object:Gem::Version
100
- version: 1.1.3
98
+ version: 1.1.6
101
99
  require_paths:
102
100
  - lib
103
101
  dependencies: []
104
102
 
105
103
  bindir: bin
106
- has_rdoc: false
104
+ has_rdoc: true