json-jruby 1.2.4-universal-java-1.6 → 1.4.0-universal-java-1.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.
- data/lib/json/common.rb +53 -36
- data/lib/json/ext/generator.jar +0 -0
- data/lib/json/ext/parser.jar +0 -0
- data/lib/json/pure/generator.rb +85 -81
- data/lib/json/version.rb +1 -1
- data/tests/test_json_encoding.rb +4 -3
- data/tests/test_json_generate.rb +7 -7
- data/tests/test_json_unicode.rb +20 -6
- metadata +3 -3
data/lib/json/common.rb
CHANGED
@@ -63,6 +63,20 @@ module JSON
|
|
63
63
|
end
|
64
64
|
self.state = generator::State
|
65
65
|
const_set :State, self.state
|
66
|
+
const_set :SAFE_STATE_PROTOTYPE, State.new.freeze
|
67
|
+
const_set :FAST_STATE_PROTOTYPE, State.new(
|
68
|
+
:indent => '',
|
69
|
+
:space => '',
|
70
|
+
:object_nl => "",
|
71
|
+
:array_nl => "",
|
72
|
+
:max_nesting => false
|
73
|
+
).freeze
|
74
|
+
const_set :PRETTY_STATE_PROTOTYPE, State.new(
|
75
|
+
:indent => ' ',
|
76
|
+
:space => ' ',
|
77
|
+
:object_nl => "\n",
|
78
|
+
:array_nl => "\n"
|
79
|
+
).freeze
|
66
80
|
end
|
67
81
|
|
68
82
|
# Returns the JSON generator modul, that is used by JSON. This might be
|
@@ -95,15 +109,15 @@ module JSON
|
|
95
109
|
# deep.
|
96
110
|
class NestingError < ParserError; end
|
97
111
|
|
112
|
+
# :stopdoc:
|
113
|
+
class CircularDatastructure < NestingError; end
|
114
|
+
# :startdoc:
|
115
|
+
|
98
116
|
# This exception is raised, if a generator or unparser error occurs.
|
99
117
|
class GeneratorError < JSONError; end
|
100
118
|
# For backwards compatibility
|
101
119
|
UnparserError = GeneratorError
|
102
120
|
|
103
|
-
# If a circular data structure is encountered while unparsing
|
104
|
-
# this exception is raised.
|
105
|
-
class CircularDatastructure < GeneratorError; end
|
106
|
-
|
107
121
|
# This exception is raised, if the required unicode support is missing on the
|
108
122
|
# system. Usually this means, that the iconv library is not installed.
|
109
123
|
class MissingUnicodeSupport < JSONError; end
|
@@ -129,7 +143,7 @@ module JSON
|
|
129
143
|
# * *object_class*: Defaults to Hash
|
130
144
|
# * *array_class*: Defaults to Array
|
131
145
|
def parse(source, opts = {})
|
132
|
-
|
146
|
+
Parser.new(source, opts).parse
|
133
147
|
end
|
134
148
|
|
135
149
|
# Parse the JSON document _source_ into a Ruby data structure and return it.
|
@@ -149,10 +163,10 @@ module JSON
|
|
149
163
|
# defaults to true.
|
150
164
|
def parse!(source, opts = {})
|
151
165
|
opts = {
|
152
|
-
:max_nesting
|
153
|
-
:allow_nan
|
166
|
+
:max_nesting => false,
|
167
|
+
:allow_nan => true
|
154
168
|
}.update(opts)
|
155
|
-
|
169
|
+
Parser.new(source, opts).parse
|
156
170
|
end
|
157
171
|
|
158
172
|
# Generate a JSON document from the Ruby data structure _obj_ and return
|
@@ -171,8 +185,6 @@ module JSON
|
|
171
185
|
# * *space_before*: a string that is put before a : pair delimiter (default: ''),
|
172
186
|
# * *object_nl*: a string that is put at the end of a JSON object (default: ''),
|
173
187
|
# * *array_nl*: a string that is put at the end of a JSON array (default: ''),
|
174
|
-
# * *check_circular*: true if checking for circular data structures
|
175
|
-
# should be done (the default), false otherwise.
|
176
188
|
# * *allow_nan*: true if NaN, Infinity, and -Infinity should be
|
177
189
|
# generated, otherwise an exception is thrown, if these values are
|
178
190
|
# encountered. This options defaults to false.
|
@@ -183,17 +195,21 @@ module JSON
|
|
183
195
|
# See also the fast_generate for the fastest creation method with the least
|
184
196
|
# amount of sanity checks, and the pretty_generate method for some
|
185
197
|
# defaults for a pretty output.
|
186
|
-
def generate(obj,
|
187
|
-
if
|
188
|
-
|
198
|
+
def generate(obj, opts = nil)
|
199
|
+
if opts
|
200
|
+
if opts.respond_to? :to_hash
|
201
|
+
opts = opts.to_hash
|
202
|
+
elsif opts.respond_to? :to_h
|
203
|
+
opts = opts.to_h
|
204
|
+
else
|
205
|
+
raise TypeError, "can't convert #{opts.class} into Hash"
|
206
|
+
end
|
207
|
+
state = SAFE_STATE_PROTOTYPE.dup
|
208
|
+
state = state.configure(opts)
|
189
209
|
else
|
190
|
-
state =
|
191
|
-
end
|
192
|
-
result = obj.to_json(state)
|
193
|
-
if result !~ /\A\s*(?:\[.*\]|\{.*\})\s*\Z/m
|
194
|
-
raise GeneratorError, "only generation of JSON objects or arrays allowed"
|
210
|
+
state = SAFE_STATE_PROTOTYPE
|
195
211
|
end
|
196
|
-
|
212
|
+
state.generate(obj)
|
197
213
|
end
|
198
214
|
|
199
215
|
# :stopdoc:
|
@@ -208,12 +224,21 @@ module JSON
|
|
208
224
|
#
|
209
225
|
# *WARNING*: Be careful not to pass any Ruby data structures with circles as
|
210
226
|
# _obj_ argument, because this will cause JSON to go into an infinite loop.
|
211
|
-
def fast_generate(obj)
|
212
|
-
|
213
|
-
|
214
|
-
|
227
|
+
def fast_generate(obj, opts = nil)
|
228
|
+
if opts
|
229
|
+
if opts.respond_to? :to_hash
|
230
|
+
opts = opts.to_hash
|
231
|
+
elsif opts.respond_to? :to_h
|
232
|
+
opts = opts.to_h
|
233
|
+
else
|
234
|
+
raise TypeError, "can't convert #{opts.class} into Hash"
|
235
|
+
end
|
236
|
+
state = FAST_STATE_PROTOTYPE.dup
|
237
|
+
state.configure(opts)
|
238
|
+
else
|
239
|
+
state = FAST_STATE_PROTOTYPE
|
215
240
|
end
|
216
|
-
|
241
|
+
state.generate(obj)
|
217
242
|
end
|
218
243
|
|
219
244
|
# :stopdoc:
|
@@ -229,13 +254,6 @@ module JSON
|
|
229
254
|
# The _opts_ argument can be used to configure the generator, see the
|
230
255
|
# generate method for a more detailed explanation.
|
231
256
|
def pretty_generate(obj, opts = nil)
|
232
|
-
state = JSON.state.new(
|
233
|
-
:indent => ' ',
|
234
|
-
:space => ' ',
|
235
|
-
:object_nl => "\n",
|
236
|
-
:array_nl => "\n",
|
237
|
-
:check_circular => true
|
238
|
-
)
|
239
257
|
if opts
|
240
258
|
if opts.respond_to? :to_hash
|
241
259
|
opts = opts.to_hash
|
@@ -244,13 +262,12 @@ module JSON
|
|
244
262
|
else
|
245
263
|
raise TypeError, "can't convert #{opts.class} into Hash"
|
246
264
|
end
|
265
|
+
state = PRETTY_STATE_PROTOTYPE.dup
|
247
266
|
state.configure(opts)
|
267
|
+
else
|
268
|
+
state = PRETTY_STATE_PROTOTYPE
|
248
269
|
end
|
249
|
-
|
250
|
-
if result !~ /\A\s*(?:\[.*\]|\{.*\})\s*\Z/m
|
251
|
-
raise GeneratorError, "only generation of JSON objects or arrays allowed"
|
252
|
-
end
|
253
|
-
result
|
270
|
+
state.generate(obj)
|
254
271
|
end
|
255
272
|
|
256
273
|
# :stopdoc:
|
data/lib/json/ext/generator.jar
CHANGED
Binary file
|
data/lib/json/ext/parser.jar
CHANGED
Binary file
|
data/lib/json/pure/generator.rb
CHANGED
@@ -40,6 +40,15 @@ module JSON
|
|
40
40
|
# UTF16 big endian characters as \u????, and return it.
|
41
41
|
if defined?(::Encoding)
|
42
42
|
def utf8_to_json(string) # :nodoc:
|
43
|
+
string = string.dup
|
44
|
+
string << '' # XXX workaround: avoid buffer sharing
|
45
|
+
string.force_encoding(::Encoding::ASCII_8BIT)
|
46
|
+
string.gsub!(/["\\\x0-\x1f]/) { MAP[$&] }
|
47
|
+
string.force_encoding(::Encoding::UTF_8)
|
48
|
+
string
|
49
|
+
end
|
50
|
+
|
51
|
+
def utf8_to_json_ascii(string) # :nodoc:
|
43
52
|
string = string.dup
|
44
53
|
string << '' # XXX workaround: avoid buffer sharing
|
45
54
|
string.force_encoding(::Encoding::ASCII_8BIT)
|
@@ -63,6 +72,10 @@ module JSON
|
|
63
72
|
end
|
64
73
|
else
|
65
74
|
def utf8_to_json(string) # :nodoc:
|
75
|
+
string.gsub(/["\\\x0-\x1f]/) { MAP[$&] }
|
76
|
+
end
|
77
|
+
|
78
|
+
def utf8_to_json_ascii(string) # :nodoc:
|
66
79
|
string = string.gsub(/["\\\x0-\x1f]/) { MAP[$&] }
|
67
80
|
string.gsub!(/(
|
68
81
|
(?:
|
@@ -81,7 +94,7 @@ module JSON
|
|
81
94
|
raise GeneratorError, "Caught #{e.class}: #{e}"
|
82
95
|
end
|
83
96
|
end
|
84
|
-
module_function :utf8_to_json
|
97
|
+
module_function :utf8_to_json, :utf8_to_json_ascii
|
85
98
|
|
86
99
|
module Pure
|
87
100
|
module Generator
|
@@ -99,7 +112,7 @@ module JSON
|
|
99
112
|
when Hash
|
100
113
|
new(opts)
|
101
114
|
else
|
102
|
-
|
115
|
+
SAFE_STATE_PROTOTYPE
|
103
116
|
end
|
104
117
|
end
|
105
118
|
|
@@ -112,22 +125,20 @@ module JSON
|
|
112
125
|
# * *space_before*: a string that is put before a : pair delimiter (default: ''),
|
113
126
|
# * *object_nl*: a string that is put at the end of a JSON object (default: ''),
|
114
127
|
# * *array_nl*: a string that is put at the end of a JSON array (default: ''),
|
115
|
-
# * *check_circular*:
|
116
|
-
#
|
117
|
-
#
|
118
|
-
# should be done, false (the default) otherwise.
|
128
|
+
# * *check_circular*: is deprecated now, use the :max_nesting option instead,
|
129
|
+
# * *max_nesting*: sets the maximum level of data structure nesting in
|
130
|
+
# the generated JSON, max_nesting = 0 if no maximum should be checked.
|
119
131
|
# * *allow_nan*: true if NaN, Infinity, and -Infinity should be
|
120
132
|
# generated, otherwise an exception is thrown, if these values are
|
121
133
|
# encountered. This options defaults to false.
|
122
134
|
def initialize(opts = {})
|
123
|
-
@seen = {}
|
124
135
|
@indent = ''
|
125
136
|
@space = ''
|
126
137
|
@space_before = ''
|
127
138
|
@object_nl = ''
|
128
139
|
@array_nl = ''
|
129
|
-
@check_circular = true
|
130
140
|
@allow_nan = false
|
141
|
+
@ascii_only = false
|
131
142
|
configure opts
|
132
143
|
end
|
133
144
|
|
@@ -159,10 +170,10 @@ module JSON
|
|
159
170
|
raise NestingError, "nesting of #{current_nesting} is too deep"
|
160
171
|
end
|
161
172
|
|
162
|
-
# Returns true, if circular data structures
|
173
|
+
# Returns true, if circular data structures are checked,
|
163
174
|
# otherwise returns false.
|
164
175
|
def check_circular?
|
165
|
-
|
176
|
+
!@max_nesting.zero?
|
166
177
|
end
|
167
178
|
|
168
179
|
# Returns true if NaN, Infinity, and -Infinity should be considered as
|
@@ -171,21 +182,8 @@ module JSON
|
|
171
182
|
@allow_nan
|
172
183
|
end
|
173
184
|
|
174
|
-
|
175
|
-
|
176
|
-
def seen?(object)
|
177
|
-
@seen.key?(object.__id__)
|
178
|
-
end
|
179
|
-
|
180
|
-
# Remember _object_, to find out if it was already encountered (if a
|
181
|
-
# cyclic data structure is if a cyclic data structure is rendered).
|
182
|
-
def remember(object)
|
183
|
-
@seen[object.__id__] = true
|
184
|
-
end
|
185
|
-
|
186
|
-
# Forget _object_ for this generating run.
|
187
|
-
def forget(object)
|
188
|
-
@seen.delete object.__id__
|
185
|
+
def ascii_only?
|
186
|
+
@ascii_only
|
189
187
|
end
|
190
188
|
|
191
189
|
# Configure this State instance with the Hash _opts_, and return
|
@@ -196,8 +194,8 @@ module JSON
|
|
196
194
|
@space_before = opts[:space_before] if opts.key?(:space_before)
|
197
195
|
@object_nl = opts[:object_nl] if opts.key?(:object_nl)
|
198
196
|
@array_nl = opts[:array_nl] if opts.key?(:array_nl)
|
199
|
-
@check_circular = !!opts[:check_circular] if opts.key?(:check_circular)
|
200
197
|
@allow_nan = !!opts[:allow_nan] if opts.key?(:allow_nan)
|
198
|
+
@ascii_only = opts[:ascii_only] if opts.key?(:ascii_only)
|
201
199
|
if !opts.key?(:max_nesting) # defaults to 19
|
202
200
|
@max_nesting = 19
|
203
201
|
elsif opts[:max_nesting]
|
@@ -212,12 +210,23 @@ module JSON
|
|
212
210
|
# passed to the configure method.
|
213
211
|
def to_h
|
214
212
|
result = {}
|
215
|
-
for iv in %w[indent space space_before object_nl array_nl
|
213
|
+
for iv in %w[indent space space_before object_nl array_nl allow_nan max_nesting]
|
216
214
|
result[iv.intern] = instance_variable_get("@#{iv}")
|
217
215
|
end
|
218
216
|
result
|
219
217
|
end
|
220
218
|
|
219
|
+
# Generates a valid JSON document from object +obj+ and returns the
|
220
|
+
# result. If no valid JSON document can be created this method raises a
|
221
|
+
# GeneratorError exception.
|
222
|
+
def generate(obj)
|
223
|
+
result = obj.to_json(self)
|
224
|
+
if result !~ /\A\s*(?:\[.*\]|\{.*\})\s*\Z/m
|
225
|
+
raise GeneratorError, "only generation of JSON objects or arrays allowed"
|
226
|
+
end
|
227
|
+
result
|
228
|
+
end
|
229
|
+
|
221
230
|
# Return the value returned by method +name+.
|
222
231
|
def [](name)
|
223
232
|
__send__ name
|
@@ -240,27 +249,14 @@ module JSON
|
|
240
249
|
# _depth_ is used to find out nesting depth, to indent accordingly.
|
241
250
|
def to_json(state = nil, depth = 0, *)
|
242
251
|
if state
|
243
|
-
state =
|
252
|
+
state = State.from_state(state)
|
244
253
|
state.check_max_nesting(depth)
|
245
|
-
json_check_circular(state) { json_transform(state, depth) }
|
246
|
-
else
|
247
|
-
json_transform(state, depth)
|
248
254
|
end
|
255
|
+
json_transform(state, depth)
|
249
256
|
end
|
250
257
|
|
251
258
|
private
|
252
259
|
|
253
|
-
def json_check_circular(state)
|
254
|
-
if state and state.check_circular?
|
255
|
-
state.seen?(self) and raise JSON::CircularDatastructure,
|
256
|
-
"circular data structures not supported!"
|
257
|
-
state.remember self
|
258
|
-
end
|
259
|
-
yield
|
260
|
-
ensure
|
261
|
-
state and state.forget self
|
262
|
-
end
|
263
|
-
|
264
260
|
def json_shift(state, depth)
|
265
261
|
state and not state.object_nl.empty? or return ''
|
266
262
|
state.indent * depth
|
@@ -272,16 +268,22 @@ module JSON
|
|
272
268
|
delim << state.object_nl
|
273
269
|
result = '{'
|
274
270
|
result << state.object_nl
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
271
|
+
depth += 1
|
272
|
+
first = true
|
273
|
+
indent = state && !state.object_nl.empty?
|
274
|
+
each { |key,value|
|
275
|
+
result << delim unless first
|
276
|
+
result << state.indent * depth if indent
|
277
|
+
result << key.to_s.to_json(state, depth)
|
278
|
+
result << state.space_before
|
279
|
+
result << ':'
|
280
|
+
result << state.space
|
281
|
+
result << value.to_json(state, depth)
|
282
|
+
first = false
|
283
|
+
}
|
284
|
+
depth -= 1
|
283
285
|
result << state.object_nl
|
284
|
-
result <<
|
286
|
+
result << state.indent * depth if indent if indent
|
285
287
|
result << '}'
|
286
288
|
else
|
287
289
|
result = '{'
|
@@ -302,43 +304,32 @@ module JSON
|
|
302
304
|
# _depth_ is used to find out nesting depth, to indent accordingly.
|
303
305
|
def to_json(state = nil, depth = 0, *)
|
304
306
|
if state
|
305
|
-
state =
|
307
|
+
state = State.from_state(state)
|
306
308
|
state.check_max_nesting(depth)
|
307
|
-
json_check_circular(state) { json_transform(state, depth) }
|
308
|
-
else
|
309
|
-
json_transform(state, depth)
|
310
309
|
end
|
310
|
+
json_transform(state, depth)
|
311
311
|
end
|
312
312
|
|
313
313
|
private
|
314
314
|
|
315
|
-
def json_check_circular(state)
|
316
|
-
if state and state.check_circular?
|
317
|
-
state.seen?(self) and raise JSON::CircularDatastructure,
|
318
|
-
"circular data structures not supported!"
|
319
|
-
state.remember self
|
320
|
-
end
|
321
|
-
yield
|
322
|
-
ensure
|
323
|
-
state and state.forget self
|
324
|
-
end
|
325
|
-
|
326
|
-
def json_shift(state, depth)
|
327
|
-
state and not state.array_nl.empty? or return ''
|
328
|
-
state.indent * depth
|
329
|
-
end
|
330
|
-
|
331
315
|
def json_transform(state, depth)
|
332
316
|
delim = ','
|
333
317
|
if state
|
334
318
|
delim << state.array_nl
|
335
319
|
result = '['
|
336
320
|
result << state.array_nl
|
337
|
-
|
338
|
-
|
339
|
-
|
321
|
+
depth += 1
|
322
|
+
first = true
|
323
|
+
indent = state && !state.array_nl.empty?
|
324
|
+
each { |value|
|
325
|
+
result << delim unless first
|
326
|
+
result << state.indent * depth if indent
|
327
|
+
result << value.to_json(state, depth)
|
328
|
+
first = false
|
329
|
+
}
|
330
|
+
depth -= 1
|
340
331
|
result << state.array_nl
|
341
|
-
result <<
|
332
|
+
result << state.indent * depth if indent
|
342
333
|
result << ']'
|
343
334
|
else
|
344
335
|
'[' << map { |value| value.to_json }.join(delim) << ']'
|
@@ -378,11 +369,17 @@ module JSON
|
|
378
369
|
# This string should be encoded with UTF-8 A call to this method
|
379
370
|
# returns a JSON string encoded with UTF16 big endian characters as
|
380
371
|
# \u????.
|
381
|
-
def to_json(*)
|
372
|
+
def to_json(*args)
|
373
|
+
state, = *args
|
374
|
+
state ||= State.from_state(state)
|
382
375
|
if encoding == ::Encoding::UTF_8
|
383
|
-
|
376
|
+
string = self
|
384
377
|
else
|
385
378
|
string = encode(::Encoding::UTF_8)
|
379
|
+
end
|
380
|
+
if state.ascii_only?
|
381
|
+
'"' << JSON.utf8_to_json_ascii(string) << '"'
|
382
|
+
else
|
386
383
|
'"' << JSON.utf8_to_json(string) << '"'
|
387
384
|
end
|
388
385
|
end
|
@@ -390,16 +387,23 @@ module JSON
|
|
390
387
|
# This string should be encoded with UTF-8 A call to this method
|
391
388
|
# returns a JSON string encoded with UTF16 big endian characters as
|
392
389
|
# \u????.
|
393
|
-
def to_json(*)
|
394
|
-
|
390
|
+
def to_json(*args)
|
391
|
+
state, = *args
|
392
|
+
state ||= State.from_state(state)
|
393
|
+
if state.ascii_only?
|
394
|
+
'"' << JSON.utf8_to_json_ascii(self) << '"'
|
395
|
+
else
|
396
|
+
'"' << JSON.utf8_to_json(self) << '"'
|
397
|
+
end
|
395
398
|
end
|
396
399
|
end
|
397
400
|
|
398
401
|
# Module that holds the extinding methods if, the String module is
|
399
402
|
# included.
|
400
403
|
module Extend
|
401
|
-
# Raw Strings are JSON Objects (the raw bytes are stored in an
|
402
|
-
# key "raw"). The Ruby String can be created by this
|
404
|
+
# Raw Strings are JSON Objects (the raw bytes are stored in an
|
405
|
+
# array for the key "raw"). The Ruby String can be created by this
|
406
|
+
# module method.
|
403
407
|
def json_create(o)
|
404
408
|
o['raw'].pack('C*')
|
405
409
|
end
|
data/lib/json/version.rb
CHANGED
data/tests/test_json_encoding.rb
CHANGED
@@ -57,11 +57,12 @@ class TC_JSONEncoding < Test::Unit::TestCase
|
|
57
57
|
end
|
58
58
|
|
59
59
|
def test_generate
|
60
|
-
assert_equal @generated, JSON.generate(@parsed)
|
60
|
+
assert_equal @generated, JSON.generate(@parsed, :ascii_only => true)
|
61
61
|
if defined?(::Encoding)
|
62
|
-
assert_equal @generated, JSON.generate(@utf_16_data)
|
62
|
+
assert_equal @generated, JSON.generate(@utf_16_data, :ascii_only => true)
|
63
63
|
else
|
64
|
-
|
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) }
|
65
66
|
end
|
66
67
|
end
|
67
68
|
end
|
data/tests/test_json_generate.rb
CHANGED
@@ -87,19 +87,19 @@ EOT
|
|
87
87
|
def test_states
|
88
88
|
json = generate({1=>2}, nil)
|
89
89
|
assert_equal('{"1":2}', json)
|
90
|
-
s = JSON.state.new
|
90
|
+
s = JSON.state.new
|
91
91
|
assert s.check_circular?
|
92
92
|
assert s[:check_circular?]
|
93
93
|
h = { 1=>2 }
|
94
94
|
h[3] = h
|
95
|
-
assert_raises(JSON::
|
96
|
-
assert_raises(JSON::
|
97
|
-
s = JSON.state.new
|
98
|
-
assert s.check_circular?
|
99
|
-
assert s[:check_circular?]
|
95
|
+
assert_raises(JSON::NestingError) { generate(h) }
|
96
|
+
assert_raises(JSON::NestingError) { generate(h, s) }
|
97
|
+
s = JSON.state.new
|
100
98
|
a = [ 1, 2 ]
|
101
99
|
a << a
|
102
|
-
assert_raises(JSON::
|
100
|
+
assert_raises(JSON::NestingError) { generate(a, s) }
|
101
|
+
assert s.check_circular?
|
102
|
+
assert s[:check_circular?]
|
103
103
|
end
|
104
104
|
|
105
105
|
def test_allow_nan
|
data/tests/test_json_unicode.rb
CHANGED
@@ -19,22 +19,36 @@ class TC_JSONUnicode < Test::Unit::TestCase
|
|
19
19
|
assert_equal '" "', ' '.to_json
|
20
20
|
assert_equal "\"#{0x7f.chr}\"", 0x7f.chr.to_json
|
21
21
|
utf8 = [ "© ≠ €! \01" ]
|
22
|
+
json = '["© ≠ €! \u0001"]'
|
23
|
+
assert_equal json, utf8.to_json(:ascii_only => false)
|
24
|
+
assert_equal utf8, parse(json)
|
22
25
|
json = '["\u00a9 \u2260 \u20ac! \u0001"]'
|
23
|
-
assert_equal json, utf8.to_json
|
26
|
+
assert_equal json, utf8.to_json(:ascii_only => true)
|
27
|
+
assert_equal utf8, parse(json)
|
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\"]"
|
24
30
|
assert_equal utf8, parse(json)
|
31
|
+
assert_equal json, utf8.to_json(:ascii_only => false)
|
25
32
|
utf8 = ["\343\201\202\343\201\204\343\201\206\343\201\210\343\201\212"]
|
33
|
+
assert_equal utf8, parse(json)
|
26
34
|
json = "[\"\\u3042\\u3044\\u3046\\u3048\\u304a\"]"
|
27
|
-
assert_equal json, utf8.to_json
|
35
|
+
assert_equal json, utf8.to_json(:ascii_only => true)
|
28
36
|
assert_equal utf8, parse(json)
|
29
37
|
utf8 = ['საქართველო']
|
38
|
+
json = '["საქართველო"]'
|
39
|
+
assert_equal json, utf8.to_json(:ascii_only => false)
|
30
40
|
json = "[\"\\u10e1\\u10d0\\u10e5\\u10d0\\u10e0\\u10d7\\u10d5\\u10d4\\u10da\\u10dd\"]"
|
31
|
-
assert_equal json, utf8.to_json
|
41
|
+
assert_equal json, utf8.to_json(:ascii_only => true)
|
32
42
|
assert_equal utf8, parse(json)
|
33
|
-
assert_equal '["
|
43
|
+
assert_equal '["Ã"]', JSON.generate(["Ã"], :ascii_only => false)
|
44
|
+
assert_equal '["\\u00c3"]', JSON.generate(["Ã"], :ascii_only => true)
|
34
45
|
assert_equal ["€"], JSON.parse('["\u20ac"]')
|
35
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)
|
36
50
|
json = '["\ud840\udc01"]'
|
37
|
-
assert_equal json, JSON.generate(utf8)
|
51
|
+
assert_equal json, JSON.generate(utf8, :ascii_only => true)
|
38
52
|
assert_equal utf8, JSON.parse(json)
|
39
53
|
end
|
40
54
|
|
@@ -55,7 +69,7 @@ class TC_JSONUnicode < Test::Unit::TestCase
|
|
55
69
|
end
|
56
70
|
end
|
57
71
|
assert_raise(JSON::GeneratorError) do
|
58
|
-
JSON.generate(["\x80"])
|
72
|
+
JSON.generate(["\x80"], :ascii_only => true)
|
59
73
|
end
|
60
74
|
assert_equal "\302\200", JSON.parse('["\u0080"]').first
|
61
75
|
end
|
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 1
|
7
|
-
- 2
|
8
7
|
- 4
|
9
|
-
|
8
|
+
- 0
|
9
|
+
version: 1.4.0
|
10
10
|
platform: universal-java-1.6
|
11
11
|
authors:
|
12
12
|
- Daniel Luz
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-04-
|
17
|
+
date: 2010-04-23 00:00:00 -03:00
|
18
18
|
default_executable:
|
19
19
|
dependencies: []
|
20
20
|
|