oj 3.7.12 → 3.8.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +10 -2
- data/ext/oj/custom.c +15 -12
- data/ext/oj/dump.c +46 -26
- data/ext/oj/dump.h +2 -0
- data/ext/oj/dump_compat.c +13 -9
- data/ext/oj/mimic_json.c +7 -2
- data/ext/oj/oj.c +34 -24
- data/ext/oj/oj.h +6 -2
- data/ext/oj/parse.c +2 -1
- data/ext/oj/rails.c +8 -0
- data/ext/oj/sparse.c +2 -2
- data/ext/oj/string_writer.c +25 -3
- data/lib/oj/json.rb +1 -1
- data/lib/oj/version.rb +1 -1
- data/pages/Modes.md +3 -2
- data/pages/Options.md +19 -2
- data/pages/Security.md +1 -1
- data/test/bar.rb +25 -0
- data/test/foo.rb +160 -26
- data/test/test_custom.rb +99 -2
- data/test/test_various.rb +2 -0
- data/test/zoo.rb +13 -0
- metadata +71 -71
- data/test/big.rb +0 -15
- data/test/mem.rb +0 -35
data/lib/oj/json.rb
CHANGED
data/lib/oj/version.rb
CHANGED
data/pages/Modes.md
CHANGED
@@ -14,7 +14,7 @@ modes are:
|
|
14
14
|
- `:object`
|
15
15
|
- `:custom`
|
16
16
|
|
17
|
-
Since modes
|
17
|
+
Since modes determine what the JSON output will look like and alternatively
|
18
18
|
what Oj expects when the `Oj.load()` method is called, mixing the output and
|
19
19
|
input mode formats will most likely not behave as intended. If the object mode
|
20
20
|
is used for producing JSON then use object mode for reading. The same is true
|
@@ -117,6 +117,7 @@ information.
|
|
117
117
|
| :object_nl | String | | | x | x | | x | |
|
118
118
|
| :omit_nil | Boolean | x | x | x | x | x | x | |
|
119
119
|
| :quirks_mode | Boolean | | | 6 | | | x | |
|
120
|
+
| :safe | String | | | x | | | | |
|
120
121
|
| :second_precision | Fixnum | | | | | x | x | |
|
121
122
|
| :space | String | | | x | x | | x | |
|
122
123
|
| :space_before | String | | | x | x | | x | |
|
@@ -124,6 +125,7 @@ information.
|
|
124
125
|
| :trace | Boolean | x | x | x | x | x | x | x |
|
125
126
|
| :time_format | Symbol | | | | | x | x | |
|
126
127
|
| :use_as_json | Boolean | | | | | | x | |
|
128
|
+
| :use_raw_json | Boolean | | | x | x | x | x | |
|
127
129
|
| :use_to_hash | Boolean | | | | | | x | |
|
128
130
|
| :use_to_json | Boolean | | | | | | x | |
|
129
131
|
--------------------------------------------------------------------------------------------------------
|
@@ -151,4 +153,3 @@ information.
|
|
151
153
|
6. The quirks mode option is no longer supported in the most recent json
|
152
154
|
gem. It is supported by Oj for backward compatibility with older json gem
|
153
155
|
versions.
|
154
|
-
|
data/pages/Options.md
CHANGED
@@ -149,7 +149,7 @@ integer gives better performance.
|
|
149
149
|
|
150
150
|
### :integer_range [Range]
|
151
151
|
|
152
|
-
Dump integers outside range as strings.
|
152
|
+
Dump integers outside range as strings.
|
153
153
|
Note: range bounds must be Fixnum.
|
154
154
|
|
155
155
|
### :match_string
|
@@ -211,6 +211,12 @@ Allow single JSON values instead of documents, default is true (allow). This
|
|
211
211
|
can also be used in :compat mode to be backward compatible with older versions
|
212
212
|
of the json gem.
|
213
213
|
|
214
|
+
### :safe
|
215
|
+
|
216
|
+
The JSON gem includes the complete JSON in parse errors with no limit
|
217
|
+
on size. To break from the JSON gem behavior for this case set `:safe`
|
218
|
+
to true.
|
219
|
+
|
214
220
|
### :second_precision [Fixnum]
|
215
221
|
|
216
222
|
The number of digits after the decimal when dumping the seconds of time.
|
@@ -254,6 +260,18 @@ The :time_format when dumping.
|
|
254
260
|
Call `as_json()` methods on dump, default is false. The option is ignored in
|
255
261
|
the :compat and :rails mode.
|
256
262
|
|
263
|
+
|
264
|
+
### :use_raw_json [Boolean]
|
265
|
+
|
266
|
+
Call `raw_json()` methods on dump, default is false. The option is
|
267
|
+
accepted in the :compat and :rails mode even though it is not
|
268
|
+
supported by other JSON gems. It provides a means to optimize dump or
|
269
|
+
generate performance. The `raw_json(depth, indent)` method should be
|
270
|
+
called only by Oj. It is not intended for any other use. This is mean
|
271
|
+
to replace the abused `to_json` methods. Calling `Oj.dump` inside the
|
272
|
+
`raw_json` with the object itself when `:use_raw_json` is true will
|
273
|
+
result in an infinite loop.
|
274
|
+
|
257
275
|
### :use_to_hash [Boolean]
|
258
276
|
|
259
277
|
Call `to_hash()` methods on dump, default is false. The option is ignored in
|
@@ -263,4 +281,3 @@ the :compat and :rails mode.
|
|
263
281
|
|
264
282
|
Call `to_json()` methods on dump, default is false. The option is ignored in
|
265
283
|
the :compat and :rails mode.
|
266
|
-
|
data/pages/Security.md
CHANGED
@@ -7,7 +7,7 @@ Symbols. The same is true for auto defining classes in all versions of ruby;
|
|
7
7
|
memory will also be exhausted if too many classes are automatically
|
8
8
|
defined. Auto defining is a useful feature during development and from trusted
|
9
9
|
sources but it allows too many classes to be created in the object load mode and
|
10
|
-
auto defined is used with an untrusted source. The `Oj.
|
10
|
+
auto defined is used with an untrusted source. The `Oj.safe_load()` method
|
11
11
|
sets and uses the most strict and safest options. It should be used by
|
12
12
|
developers who find it difficult to understand the options available in Oj.
|
13
13
|
|
data/test/bar.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# encoding: UTF-8
|
3
|
+
|
4
|
+
$: << File.dirname(__FILE__)
|
5
|
+
$oj_dir = File.dirname(File.expand_path(File.dirname(__FILE__)))
|
6
|
+
%w(lib ext).each do |dir|
|
7
|
+
$: << File.join($oj_dir, dir)
|
8
|
+
end
|
9
|
+
|
10
|
+
require 'oj'
|
11
|
+
|
12
|
+
#Oj.load_file(ARGV[0], mode: :strict) { |obj|
|
13
|
+
# puts Oj.dump(obj, indent: 2)
|
14
|
+
#}
|
15
|
+
|
16
|
+
data = open('invalid_unicode.data').read
|
17
|
+
|
18
|
+
puts data
|
19
|
+
|
20
|
+
puts Oj.dump(data)
|
21
|
+
|
22
|
+
Oj.mimic_JSON
|
23
|
+
puts Oj.dump(data, escape_mode: :json)
|
24
|
+
|
25
|
+
puts Oj.default_options
|
data/test/foo.rb
CHANGED
@@ -1,33 +1,167 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
+
# encoding: UTF-8
|
2
3
|
|
3
|
-
$: <<
|
4
|
-
|
5
|
-
|
4
|
+
$: << File.dirname(__FILE__)
|
5
|
+
$oj_dir = File.dirname(File.expand_path(File.dirname(__FILE__)))
|
6
|
+
%w(lib ext).each do |dir|
|
7
|
+
$: << File.join($oj_dir, dir)
|
8
|
+
end
|
6
9
|
|
10
|
+
#require 'json'
|
7
11
|
require 'oj'
|
8
12
|
|
9
|
-
|
10
|
-
100_000.times do
|
11
|
-
obj = { created_at: DateTime.new(2001,2,3,4,5,6) }
|
12
|
-
Oj.to_stream(f, obj)
|
13
|
-
f.puts
|
14
|
-
f.flush
|
15
|
-
end
|
16
|
-
f.close
|
13
|
+
Oj.mimic_JSON
|
17
14
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
15
|
+
obj = {
|
16
|
+
ab: {
|
17
|
+
cbbb: {
|
18
|
+
tilbeb: [
|
19
|
+
{
|
20
|
+
coob: {
|
21
|
+
uijwts: [
|
22
|
+
{
|
23
|
+
prrrrr: {
|
24
|
+
yakj: "pvebbx",
|
25
|
+
lbhqy: {
|
26
|
+
uhyw: {
|
27
|
+
uijwts: [
|
28
|
+
{
|
29
|
+
jangi: {
|
30
|
+
ubentg7haineued8atnr8w: {
|
31
|
+
abc: "uejdncbncnamnasdasdasdasd",
|
32
|
+
cde: "skfjskdfjskdfjsdkfjsdkfjs"
|
33
|
+
}
|
34
|
+
}
|
35
|
+
}
|
36
|
+
]
|
37
|
+
}
|
38
|
+
}
|
39
|
+
}
|
40
|
+
},
|
41
|
+
{
|
42
|
+
kdncg: {
|
43
|
+
lvbnt8b9ounv: {
|
44
|
+
qk: 9
|
45
|
+
}
|
46
|
+
}
|
47
|
+
}
|
48
|
+
],
|
49
|
+
jenfjbhe: {}
|
50
|
+
}
|
51
|
+
}
|
52
|
+
]
|
53
|
+
}
|
54
|
+
},
|
55
|
+
ijbh: {
|
56
|
+
jsnbrpbnunt: {
|
57
|
+
b88dibalbvp: {
|
58
|
+
mnbvd: "9uhbqlpiev"
|
59
|
+
}
|
60
|
+
},
|
61
|
+
ncnwkl: {
|
62
|
+
ksdfsf: {
|
63
|
+
mjln: "mnklkn"
|
64
|
+
},
|
65
|
+
kbrh: {
|
66
|
+
sdfn83nnalbmgnansdd: {
|
67
|
+
uijwts: {
|
68
|
+
ibha: {
|
69
|
+
uijwts: [
|
70
|
+
{
|
71
|
+
lnrbf: {
|
72
|
+
nbvtmqbhap9ebeb7btnnaw: {
|
73
|
+
ksb: "sdfksdfjsdfsb39242dnasddd",
|
74
|
+
mnm: "1293dsfnsdmfnsdfsd,fmnsd,"
|
75
|
+
}
|
76
|
+
}
|
77
|
+
}
|
78
|
+
]
|
79
|
+
}
|
80
|
+
},
|
81
|
+
kbrh: {
|
82
|
+
bo8libts: {
|
83
|
+
nag40n: {
|
84
|
+
kyen: "sdfasnc92nsn"
|
85
|
+
},
|
86
|
+
kbrh: {
|
87
|
+
nbwyu26snfcbajsdkj8: {
|
88
|
+
uijwts: {
|
89
|
+
mdfnkjsdd: {}
|
90
|
+
},
|
91
|
+
kbrh: {
|
92
|
+
kneahce: {
|
93
|
+
uijwts: {
|
94
|
+
kwnb: {
|
95
|
+
uijwts: [
|
96
|
+
{
|
97
|
+
fhfd: {
|
98
|
+
sfasdnfmasndfamsdnfajsmdf: false
|
99
|
+
}
|
100
|
+
}
|
101
|
+
],
|
102
|
+
asdfsdff: [
|
103
|
+
{
|
104
|
+
cwdf: {
|
105
|
+
sddlkfajsdkfjabskdfjalsdkfjansdkfjf: ""
|
106
|
+
}
|
107
|
+
},
|
108
|
+
{
|
109
|
+
bsdj: {
|
110
|
+
sdfsjdlfkasy8kljsfsdf83jlkjfals: true
|
111
|
+
}
|
112
|
+
}
|
113
|
+
]
|
114
|
+
}
|
115
|
+
},
|
116
|
+
kbrh: {
|
117
|
+
sdfsdfsddfk: {
|
118
|
+
uijwts: {
|
119
|
+
sdfsd: {
|
120
|
+
sdfsadf89mnlrrrqurqwvdnff: {
|
121
|
+
"kj": 8
|
122
|
+
}
|
123
|
+
}
|
124
|
+
},
|
125
|
+
kbrh: {
|
126
|
+
dkdjd: {
|
127
|
+
dfeteu: {
|
128
|
+
sdfd: "sdfasdfjlkjslrsdbb"
|
129
|
+
},
|
130
|
+
kbrh: {
|
131
|
+
sdfskjdfldk: {
|
132
|
+
buqpen: {
|
133
|
+
kjlkj: {
|
134
|
+
sdflskdjfalsdkrjalwkjfsrlfjasdf: {
|
135
|
+
sd: 0
|
136
|
+
}
|
137
|
+
}
|
138
|
+
},
|
139
|
+
kbrh: {
|
140
|
+
sdfksljdlfksdfl: {
|
141
|
+
sdfsdkfjssd: {
|
142
|
+
ksdjf: "sdflsdkfjasdkaufs;ldkfjsdlf",
|
143
|
+
sdfsdfsl: [5]
|
144
|
+
}
|
145
|
+
}
|
146
|
+
}
|
147
|
+
}
|
148
|
+
}
|
149
|
+
}
|
150
|
+
}
|
151
|
+
}
|
152
|
+
}
|
153
|
+
}
|
154
|
+
}
|
155
|
+
}
|
156
|
+
}
|
157
|
+
}
|
158
|
+
}
|
159
|
+
}
|
160
|
+
}
|
161
|
+
}
|
162
|
+
}
|
163
|
+
}
|
29
164
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
end
|
165
|
+
#Oj.dump(obj)
|
166
|
+
JSON.pretty_generate(obj)
|
167
|
+
#JSON.generate(obj)
|
data/test/test_custom.rb
CHANGED
@@ -32,6 +32,9 @@ class CustomJuice < Minitest::Test
|
|
32
32
|
def to_json(*args)
|
33
33
|
%|{"xx":#{@x},"yy":#{y}}|
|
34
34
|
end
|
35
|
+
def raw_json(depth, indent)
|
36
|
+
%|{"xxx":#{@x},"yyy":#{y}}|
|
37
|
+
end
|
35
38
|
def as_json(*args)
|
36
39
|
{'a' => @x, :b => @y }
|
37
40
|
end
|
@@ -40,6 +43,40 @@ class CustomJuice < Minitest::Test
|
|
40
43
|
end
|
41
44
|
end
|
42
45
|
|
46
|
+
class AsJson
|
47
|
+
attr_accessor :x, :y
|
48
|
+
|
49
|
+
def initialize(x, y)
|
50
|
+
@x = x
|
51
|
+
@y = y
|
52
|
+
end
|
53
|
+
def ==(o)
|
54
|
+
self.class == o.class && @x == o.x && @y = o.y
|
55
|
+
end
|
56
|
+
def as_json(*args)
|
57
|
+
{'a' => @x, :b => @y }
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
class AsRails
|
62
|
+
attr_accessor :x, :y
|
63
|
+
|
64
|
+
def initialize(x, y)
|
65
|
+
@x = x
|
66
|
+
@y = y
|
67
|
+
end
|
68
|
+
def ==(o)
|
69
|
+
self.class == o.class && @x == o.x && @y = o.y
|
70
|
+
end
|
71
|
+
def as_json(*args)
|
72
|
+
a = @x
|
73
|
+
a = a.as_json if a.respond_to?('as_json')
|
74
|
+
b = @y
|
75
|
+
b = b.as_json if b.respond_to?('as_json')
|
76
|
+
{'a' => a, :b => b }
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
43
80
|
def setup
|
44
81
|
@default_options = Oj.default_options
|
45
82
|
Oj.default_options = { :mode => :custom }
|
@@ -92,7 +129,7 @@ class CustomJuice < Minitest::Test
|
|
92
129
|
end
|
93
130
|
assert(false, "*** expected an exception")
|
94
131
|
end
|
95
|
-
|
132
|
+
|
96
133
|
def test_infinity_dump
|
97
134
|
assert_equal('null', Oj.dump(1/0.0, :nan => :null))
|
98
135
|
assert_equal('3.0e14159265358979323846', Oj.dump(1/0.0, :nan => :huge))
|
@@ -187,7 +224,7 @@ class CustomJuice < Minitest::Test
|
|
187
224
|
'19' => {
|
188
225
|
'20' => {}}}}}}}}}}}}}}}}}}}}}, false)
|
189
226
|
end
|
190
|
-
|
227
|
+
|
191
228
|
def test_hash_escaped_key
|
192
229
|
json = %{{"a\nb":true,"c\td":false}}
|
193
230
|
obj = Oj.load(json)
|
@@ -232,6 +269,66 @@ class CustomJuice < Minitest::Test
|
|
232
269
|
assert_equal(%|{"b":true,"n":58}|, json)
|
233
270
|
end
|
234
271
|
|
272
|
+
def test_object_raw_json
|
273
|
+
obj = Jeez.new(true, 58)
|
274
|
+
json = Oj.dump(obj, :use_to_json => true, :use_as_json => false, :use_raw_json => true, :use_to_hash => false)
|
275
|
+
assert_equal(%|{"xxx":true,"yyy":58}|, json)
|
276
|
+
end
|
277
|
+
|
278
|
+
def test_raw_json_stringwriter
|
279
|
+
obj = Oj::StringWriter.new(:indent => 0)
|
280
|
+
obj.push_array()
|
281
|
+
obj.pop()
|
282
|
+
json = Oj.dump(obj, :use_raw_json => true)
|
283
|
+
assert_equal(%|[]|, json)
|
284
|
+
end
|
285
|
+
|
286
|
+
def test_as_raw_json_stringwriter
|
287
|
+
obj = Oj::StringWriter.new(:indent => 0)
|
288
|
+
obj.push_array()
|
289
|
+
obj.push_value(3)
|
290
|
+
obj.pop()
|
291
|
+
j = AsJson.new(1, obj)
|
292
|
+
|
293
|
+
json = Oj.dump(j, use_raw_json: true, use_as_json: true, indent: 2)
|
294
|
+
assert_equal(%|{
|
295
|
+
"a":1,
|
296
|
+
"b":[3]
|
297
|
+
}
|
298
|
+
|, json)
|
299
|
+
|
300
|
+
json = Oj.dump(j, use_raw_json: false, use_as_json: true, indent: 2)
|
301
|
+
assert_equal(%|{
|
302
|
+
"a":1,
|
303
|
+
"b":{}
|
304
|
+
}
|
305
|
+
|, json)
|
306
|
+
end
|
307
|
+
|
308
|
+
def test_rails_as_raw_json_stringwriter
|
309
|
+
obj = Oj::StringWriter.new(:indent => 0)
|
310
|
+
obj.push_array()
|
311
|
+
obj.push_value(3)
|
312
|
+
obj.pop()
|
313
|
+
j = AsRails.new(1, obj)
|
314
|
+
json = Oj.dump(j, mode: :rails, use_raw_json: true, indent: 2)
|
315
|
+
assert_equal(%|{
|
316
|
+
"a":1,
|
317
|
+
"b":{}
|
318
|
+
}
|
319
|
+
|, json)
|
320
|
+
|
321
|
+
Oj::Rails.optimize
|
322
|
+
json = Oj.dump(j, mode: :rails, use_raw_json: true, indent: 2)
|
323
|
+
Oj::Rails.deoptimize
|
324
|
+
assert_equal(%|{
|
325
|
+
"a":1,
|
326
|
+
"b":[3]
|
327
|
+
}
|
328
|
+
|, json)
|
329
|
+
|
330
|
+
end
|
331
|
+
|
235
332
|
def test_symbol
|
236
333
|
json = Oj.dump(:abc)
|
237
334
|
assert_equal('"abc"', json)
|