json 1.4.4-java → 1.4.5-java
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of json might be problematic. Click here for more details.
- data/lib/json/common.rb +11 -17
- data/lib/json/ext/generator.jar +0 -0
- data/lib/json/ext/parser.jar +0 -0
- data/lib/json/pure/generator.rb +6 -1
- data/lib/json/pure/parser.rb +28 -27
- data/lib/json/version.rb +1 -1
- data/tests/test_json_generate.rb +45 -0
- metadata +3 -3
data/lib/json/common.rb
CHANGED
@@ -11,9 +11,9 @@ module JSON
|
|
11
11
|
# generate and parse for their documentation.
|
12
12
|
def [](object, opts = {})
|
13
13
|
if object.respond_to? :to_str
|
14
|
-
JSON.parse(object.to_str, opts
|
14
|
+
JSON.parse(object.to_str, opts)
|
15
15
|
else
|
16
|
-
JSON.generate(object, opts
|
16
|
+
JSON.generate(object, opts)
|
17
17
|
end
|
18
18
|
end
|
19
19
|
|
@@ -63,20 +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
|
66
|
+
const_set :SAFE_STATE_PROTOTYPE, State.new
|
67
67
|
const_set :FAST_STATE_PROTOTYPE, State.new(
|
68
68
|
:indent => '',
|
69
69
|
:space => '',
|
70
70
|
:object_nl => "",
|
71
71
|
:array_nl => "",
|
72
72
|
:max_nesting => false
|
73
|
-
)
|
73
|
+
)
|
74
74
|
const_set :PRETTY_STATE_PROTOTYPE, State.new(
|
75
75
|
:indent => ' ',
|
76
76
|
:space => ' ',
|
77
77
|
:object_nl => "\n",
|
78
78
|
:array_nl => "\n"
|
79
|
-
)
|
79
|
+
)
|
80
80
|
end
|
81
81
|
|
82
82
|
# Returns the JSON generator modul, that is used by JSON. This might be
|
@@ -196,6 +196,7 @@ module JSON
|
|
196
196
|
# amount of sanity checks, and the pretty_generate method for some
|
197
197
|
# defaults for a pretty output.
|
198
198
|
def generate(obj, opts = nil)
|
199
|
+
state = SAFE_STATE_PROTOTYPE.dup
|
199
200
|
if opts
|
200
201
|
if opts.respond_to? :to_hash
|
201
202
|
opts = opts.to_hash
|
@@ -204,10 +205,7 @@ module JSON
|
|
204
205
|
else
|
205
206
|
raise TypeError, "can't convert #{opts.class} into Hash"
|
206
207
|
end
|
207
|
-
state = SAFE_STATE_PROTOTYPE.dup
|
208
208
|
state = state.configure(opts)
|
209
|
-
else
|
210
|
-
state = SAFE_STATE_PROTOTYPE
|
211
209
|
end
|
212
210
|
state.generate(obj)
|
213
211
|
end
|
@@ -225,6 +223,7 @@ module JSON
|
|
225
223
|
# *WARNING*: Be careful not to pass any Ruby data structures with circles as
|
226
224
|
# _obj_ argument, because this will cause JSON to go into an infinite loop.
|
227
225
|
def fast_generate(obj, opts = nil)
|
226
|
+
state = FAST_STATE_PROTOTYPE.dup
|
228
227
|
if opts
|
229
228
|
if opts.respond_to? :to_hash
|
230
229
|
opts = opts.to_hash
|
@@ -233,10 +232,7 @@ module JSON
|
|
233
232
|
else
|
234
233
|
raise TypeError, "can't convert #{opts.class} into Hash"
|
235
234
|
end
|
236
|
-
state = FAST_STATE_PROTOTYPE.dup
|
237
235
|
state.configure(opts)
|
238
|
-
else
|
239
|
-
state = FAST_STATE_PROTOTYPE
|
240
236
|
end
|
241
237
|
state.generate(obj)
|
242
238
|
end
|
@@ -254,6 +250,7 @@ module JSON
|
|
254
250
|
# The _opts_ argument can be used to configure the generator, see the
|
255
251
|
# generate method for a more detailed explanation.
|
256
252
|
def pretty_generate(obj, opts = nil)
|
253
|
+
state = PRETTY_STATE_PROTOTYPE.dup
|
257
254
|
if opts
|
258
255
|
if opts.respond_to? :to_hash
|
259
256
|
opts = opts.to_hash
|
@@ -262,10 +259,7 @@ module JSON
|
|
262
259
|
else
|
263
260
|
raise TypeError, "can't convert #{opts.class} into Hash"
|
264
261
|
end
|
265
|
-
state = PRETTY_STATE_PROTOTYPE.dup
|
266
262
|
state.configure(opts)
|
267
|
-
else
|
268
|
-
state = PRETTY_STATE_PROTOTYPE
|
269
263
|
end
|
270
264
|
state.generate(obj)
|
271
265
|
end
|
@@ -377,11 +371,11 @@ module ::Kernel
|
|
377
371
|
#
|
378
372
|
# The _opts_ argument is passed through to generate/parse respectively, see
|
379
373
|
# generate and parse for their documentation.
|
380
|
-
def JSON(object,
|
374
|
+
def JSON(object, *args)
|
381
375
|
if object.respond_to? :to_str
|
382
|
-
JSON.parse(object.to_str,
|
376
|
+
JSON.parse(object.to_str, args.first)
|
383
377
|
else
|
384
|
-
JSON.generate(object,
|
378
|
+
JSON.generate(object, args.first)
|
385
379
|
end
|
386
380
|
end
|
387
381
|
end
|
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
@@ -163,6 +163,10 @@ module JSON
|
|
163
163
|
# the generated JSON, max_nesting = 0 if no maximum is checked.
|
164
164
|
attr_accessor :max_nesting
|
165
165
|
|
166
|
+
# This integer returns the current depth data structure nesting in the
|
167
|
+
# generated JSON.
|
168
|
+
attr_accessor :depth
|
169
|
+
|
166
170
|
def check_max_nesting(depth) # :nodoc:
|
167
171
|
return if @max_nesting.zero?
|
168
172
|
current_nesting = depth + 1
|
@@ -196,6 +200,7 @@ module JSON
|
|
196
200
|
@array_nl = opts[:array_nl] if opts.key?(:array_nl)
|
197
201
|
@allow_nan = !!opts[:allow_nan] if opts.key?(:allow_nan)
|
198
202
|
@ascii_only = opts[:ascii_only] if opts.key?(:ascii_only)
|
203
|
+
@depth = opts[:depth] || 0
|
199
204
|
if !opts.key?(:max_nesting) # defaults to 19
|
200
205
|
@max_nesting = 19
|
201
206
|
elsif opts[:max_nesting]
|
@@ -210,7 +215,7 @@ module JSON
|
|
210
215
|
# passed to the configure method.
|
211
216
|
def to_h
|
212
217
|
result = {}
|
213
|
-
for iv in %w[indent space space_before object_nl array_nl allow_nan max_nesting]
|
218
|
+
for iv in %w[indent space space_before object_nl array_nl allow_nan max_nesting ascii_only depth]
|
214
219
|
result[iv.intern] = instance_variable_get("@#{iv}")
|
215
220
|
end
|
216
221
|
result
|
data/lib/json/pure/parser.rb
CHANGED
@@ -69,41 +69,42 @@ module JSON
|
|
69
69
|
# * *object_class*: Defaults to Hash
|
70
70
|
# * *array_class*: Defaults to Array
|
71
71
|
def initialize(source, opts = {})
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
when b.size >= 4 && b[0] == 0 && b[1] == 0 && b[2] == 0
|
77
|
-
source.dup.force_encoding(::Encoding::UTF_32BE).encode!(::Encoding::UTF_8)
|
78
|
-
when b.size >= 4 && b[0] == 0 && b[2] == 0
|
79
|
-
source.dup.force_encoding(::Encoding::UTF_16BE).encode!(::Encoding::UTF_8)
|
80
|
-
when b.size >= 4 && b[1] == 0 && b[2] == 0 && b[3] == 0
|
81
|
-
source.dup.force_encoding(::Encoding::UTF_32LE).encode!(::Encoding::UTF_8)
|
82
|
-
|
83
|
-
when b.size >= 4 && b[1] == 0 && b[3] == 0
|
84
|
-
source.dup.force_encoding(::Encoding::UTF_16LE).encode!(::Encoding::UTF_8)
|
85
|
-
else
|
86
|
-
source.dup
|
87
|
-
end
|
88
|
-
else
|
89
|
-
source = source.encode(::Encoding::UTF_8)
|
90
|
-
end
|
91
|
-
source.force_encoding(::Encoding::ASCII_8BIT)
|
92
|
-
else
|
93
|
-
b = source
|
72
|
+
opts ||= {}
|
73
|
+
if defined?(::Encoding)
|
74
|
+
if source.encoding == ::Encoding::ASCII_8BIT
|
75
|
+
b = source[0, 4].bytes.to_a
|
94
76
|
source = case
|
95
77
|
when b.size >= 4 && b[0] == 0 && b[1] == 0 && b[2] == 0
|
96
|
-
|
78
|
+
source.dup.force_encoding(::Encoding::UTF_32BE).encode!(::Encoding::UTF_8)
|
97
79
|
when b.size >= 4 && b[0] == 0 && b[2] == 0
|
98
|
-
|
80
|
+
source.dup.force_encoding(::Encoding::UTF_16BE).encode!(::Encoding::UTF_8)
|
99
81
|
when b.size >= 4 && b[1] == 0 && b[2] == 0 && b[3] == 0
|
100
|
-
|
82
|
+
source.dup.force_encoding(::Encoding::UTF_32LE).encode!(::Encoding::UTF_8)
|
83
|
+
|
101
84
|
when b.size >= 4 && b[1] == 0 && b[3] == 0
|
102
|
-
|
85
|
+
source.dup.force_encoding(::Encoding::UTF_16LE).encode!(::Encoding::UTF_8)
|
103
86
|
else
|
104
|
-
|
87
|
+
source.dup
|
105
88
|
end
|
89
|
+
else
|
90
|
+
source = source.encode(::Encoding::UTF_8)
|
106
91
|
end
|
92
|
+
source.force_encoding(::Encoding::ASCII_8BIT)
|
93
|
+
else
|
94
|
+
b = source
|
95
|
+
source = case
|
96
|
+
when b.size >= 4 && b[0] == 0 && b[1] == 0 && b[2] == 0
|
97
|
+
JSON.iconv('utf-8', 'utf-32be', b)
|
98
|
+
when b.size >= 4 && b[0] == 0 && b[2] == 0
|
99
|
+
JSON.iconv('utf-8', 'utf-16be', b)
|
100
|
+
when b.size >= 4 && b[1] == 0 && b[2] == 0 && b[3] == 0
|
101
|
+
JSON.iconv('utf-8', 'utf-32le', b)
|
102
|
+
when b.size >= 4 && b[1] == 0 && b[3] == 0
|
103
|
+
JSON.iconv('utf-8', 'utf-16le', b)
|
104
|
+
else
|
105
|
+
b
|
106
|
+
end
|
107
|
+
end
|
107
108
|
super source
|
108
109
|
if !opts.key?(:max_nesting) # defaults to 19
|
109
110
|
@max_nesting = 19
|
data/lib/json/version.rb
CHANGED
data/tests/test_json_generate.rb
CHANGED
@@ -102,6 +102,51 @@ EOT
|
|
102
102
|
assert s[:check_circular?]
|
103
103
|
end
|
104
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
|
+
|
105
150
|
def test_allow_nan
|
106
151
|
assert_raises(GeneratorError) { generate([JSON::NaN]) }
|
107
152
|
assert_equal '[NaN]', generate([JSON::NaN], :allow_nan => true)
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 1
|
7
7
|
- 4
|
8
|
-
-
|
9
|
-
version: 1.4.
|
8
|
+
- 5
|
9
|
+
version: 1.4.5
|
10
10
|
platform: java
|
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-08-
|
17
|
+
date: 2010-08-07 00:00:00 -05:00
|
18
18
|
default_executable:
|
19
19
|
dependencies: []
|
20
20
|
|