oj 2.18.4 → 2.18.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/ext/oj/dump.c +23 -23
- data/lib/oj/version.rb +1 -1
- data/test/curl/curl_oj.rb +46 -0
- data/test/curl/get_oj.rb +24 -0
- data/test/curl/just_curl.rb +31 -0
- data/test/curl/just_oj.rb +51 -0
- data/test/foo.rb +19 -2
- metadata +63 -66
- data/ext/oj/dump_custom.c +0 -759
- data/test/mem.rb +0 -20
- data/test/omit.rb +0 -20
- data/test/rails_datetime_test.rb +0 -24
- data/test/test_custom.rb +0 -399
- data/test/x_test.rb +0 -185
data/test/mem.rb
DELETED
@@ -1,20 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
# encoding: UTF-8
|
3
|
-
|
4
|
-
$: << File.join(File.dirname(__FILE__), "../lib")
|
5
|
-
$: << File.join(File.dirname(__FILE__), "../ext")
|
6
|
-
|
7
|
-
require 'oj'
|
8
|
-
|
9
|
-
h = {
|
10
|
-
'a' => 1,
|
11
|
-
'b' => 2,
|
12
|
-
'c' => 3,
|
13
|
-
'd' => 4,
|
14
|
-
'e' => 5,
|
15
|
-
}
|
16
|
-
|
17
|
-
10000.times do
|
18
|
-
GC.start
|
19
|
-
Oj.dump(h)
|
20
|
-
end
|
data/test/omit.rb
DELETED
@@ -1,20 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
# encoding: UTF-8
|
3
|
-
|
4
|
-
$: << File.join(File.dirname(__FILE__), "../lib")
|
5
|
-
$: << File.join(File.dirname(__FILE__), "../ext")
|
6
|
-
|
7
|
-
require 'oj'
|
8
|
-
require 'stringio'
|
9
|
-
require 'parallel'
|
10
|
-
|
11
|
-
h = { a: 1, b: nil, c: 3 }
|
12
|
-
outputs = Parallel.map((0...30).to_a, in_process: 30) do |i|
|
13
|
-
io = StringIO.new('wb+')
|
14
|
-
Oj.to_stream(io, h, omit_nil: false)
|
15
|
-
io.string
|
16
|
-
end
|
17
|
-
|
18
|
-
outputs.each do |output|
|
19
|
-
puts output
|
20
|
-
end
|
data/test/rails_datetime_test.rb
DELETED
@@ -1,24 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
# encoding: UTF-8
|
3
|
-
|
4
|
-
$: << File.dirname(__FILE__)
|
5
|
-
|
6
|
-
%w(lib ext test).each do |dir|
|
7
|
-
$LOAD_PATH.unshift File.expand_path("../../#{dir}", __FILE__)
|
8
|
-
end
|
9
|
-
|
10
|
-
require 'sqlite3'
|
11
|
-
require "rails/all"
|
12
|
-
require "active_model"
|
13
|
-
|
14
|
-
date = DateTime.now
|
15
|
-
|
16
|
-
puts date.to_json
|
17
|
-
|
18
|
-
require "oj"
|
19
|
-
|
20
|
-
puts "OJ.dump(DateTime.now)"
|
21
|
-
puts
|
22
|
-
puts Oj.dump(DateTime.now)
|
23
|
-
puts
|
24
|
-
|
data/test/test_custom.rb
DELETED
@@ -1,399 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
# encoding: UTF-8
|
3
|
-
|
4
|
-
$: << File.dirname(__FILE__)
|
5
|
-
|
6
|
-
=begin
|
7
|
-
require 'helper'
|
8
|
-
|
9
|
-
class CompatJuice < Minitest::Test
|
10
|
-
|
11
|
-
class Jeez
|
12
|
-
attr_accessor :x, :y
|
13
|
-
|
14
|
-
def initialize(x, y)
|
15
|
-
@x = x
|
16
|
-
@y = y
|
17
|
-
end
|
18
|
-
|
19
|
-
def eql?(o)
|
20
|
-
self.class == o.class && @x == o.x && @y == o.y
|
21
|
-
end
|
22
|
-
alias == eql?
|
23
|
-
|
24
|
-
def to_json()
|
25
|
-
{"json_class" => self.class.to_s,"x" => @x,"y" => @y}.to_s
|
26
|
-
end
|
27
|
-
|
28
|
-
def self.json_create(h)
|
29
|
-
self.new(h['x'], h['y'])
|
30
|
-
end
|
31
|
-
end # Jeez
|
32
|
-
|
33
|
-
module One
|
34
|
-
module Two
|
35
|
-
module Three
|
36
|
-
class Deep
|
37
|
-
|
38
|
-
def initialize()
|
39
|
-
end
|
40
|
-
|
41
|
-
def eql?(o)
|
42
|
-
self.class == o.class
|
43
|
-
end
|
44
|
-
alias == eql?
|
45
|
-
|
46
|
-
def to_json(*a)
|
47
|
-
%{{"json_class":"#{self.class.name}"}}
|
48
|
-
end
|
49
|
-
|
50
|
-
def self.json_create(h)
|
51
|
-
self.new()
|
52
|
-
end
|
53
|
-
end # Deep
|
54
|
-
end # Three
|
55
|
-
end # Two
|
56
|
-
end # One
|
57
|
-
|
58
|
-
def setup
|
59
|
-
@default_options = Oj.default_options
|
60
|
-
# in compat mode other options other than the JSON gem globals and options
|
61
|
-
# are not used.
|
62
|
-
Oj.default_options = { :mode => :compat }
|
63
|
-
end
|
64
|
-
|
65
|
-
def teardown
|
66
|
-
Oj.default_options = @default_options
|
67
|
-
end
|
68
|
-
|
69
|
-
def test_nil
|
70
|
-
dump_and_load(nil, false)
|
71
|
-
end
|
72
|
-
|
73
|
-
def test_true
|
74
|
-
dump_and_load(true, false)
|
75
|
-
end
|
76
|
-
|
77
|
-
def test_false
|
78
|
-
dump_and_load(false, false)
|
79
|
-
end
|
80
|
-
|
81
|
-
def test_fixnum
|
82
|
-
dump_and_load(0, false)
|
83
|
-
dump_and_load(12345, false)
|
84
|
-
dump_and_load(-54321, false)
|
85
|
-
dump_and_load(1, false)
|
86
|
-
end
|
87
|
-
|
88
|
-
def test_float
|
89
|
-
dump_and_load(0.0, false)
|
90
|
-
dump_and_load(12345.6789, false)
|
91
|
-
dump_and_load(70.35, false)
|
92
|
-
dump_and_load(-54321.012, false)
|
93
|
-
dump_and_load(1.7775, false)
|
94
|
-
dump_and_load(2.5024, false)
|
95
|
-
dump_and_load(2.48e16, false)
|
96
|
-
dump_and_load(2.48e100 * 1.0e10, false)
|
97
|
-
dump_and_load(-2.48e100 * 1.0e10, false)
|
98
|
-
end
|
99
|
-
|
100
|
-
def test_string
|
101
|
-
dump_and_load('', false)
|
102
|
-
dump_and_load('abc', false)
|
103
|
-
dump_and_load("abc\ndef", false)
|
104
|
-
dump_and_load("a\u0041", false)
|
105
|
-
end
|
106
|
-
|
107
|
-
def test_encode
|
108
|
-
opts = Oj.default_options
|
109
|
-
Oj.default_options = { :ascii_only => false }
|
110
|
-
unless 'jruby' == $ruby
|
111
|
-
dump_and_load("ぴーたー", false)
|
112
|
-
end
|
113
|
-
Oj.default_options = { :ascii_only => true }
|
114
|
-
json = Oj.dump("ぴーたー")
|
115
|
-
assert_equal(%{"\\u3074\\u30fc\\u305f\\u30fc"}, json)
|
116
|
-
unless 'jruby' == $ruby
|
117
|
-
dump_and_load("ぴーたー", false)
|
118
|
-
end
|
119
|
-
Oj.default_options = opts
|
120
|
-
end
|
121
|
-
|
122
|
-
def test_unicode
|
123
|
-
# hits the 3 normal ranges and one extended surrogate pair
|
124
|
-
json = %{"\\u019f\\u05e9\\u3074\\ud834\\udd1e"}
|
125
|
-
obj = Oj.load(json)
|
126
|
-
json2 = Oj.dump(obj, :ascii_only => true)
|
127
|
-
assert_equal(json, json2)
|
128
|
-
end
|
129
|
-
|
130
|
-
def test_array
|
131
|
-
dump_and_load([], false)
|
132
|
-
dump_and_load([true, false], false)
|
133
|
-
dump_and_load(['a', 1, nil], false)
|
134
|
-
dump_and_load([[nil]], false)
|
135
|
-
dump_and_load([[nil], 58], false)
|
136
|
-
end
|
137
|
-
|
138
|
-
def test_array_deep
|
139
|
-
dump_and_load([1,[2,[3,[4,[5,[6,[7,[8,[9,[10,[11,[12,[13,[14,[15,[16,[17,[18,[19,[20]]]]]]]]]]]]]]]]]]]], false)
|
140
|
-
end
|
141
|
-
|
142
|
-
# Hash
|
143
|
-
def test_hash
|
144
|
-
dump_and_load({}, false)
|
145
|
-
dump_and_load({ 'true' => true, 'false' => false}, false)
|
146
|
-
dump_and_load({ 'true' => true, 'array' => [], 'hash' => { }}, false)
|
147
|
-
end
|
148
|
-
|
149
|
-
def test_hash_deep
|
150
|
-
dump_and_load({'1' => {
|
151
|
-
'2' => {
|
152
|
-
'3' => {
|
153
|
-
'4' => {
|
154
|
-
'5' => {
|
155
|
-
'6' => {
|
156
|
-
'7' => {
|
157
|
-
'8' => {
|
158
|
-
'9' => {
|
159
|
-
'10' => {
|
160
|
-
'11' => {
|
161
|
-
'12' => {
|
162
|
-
'13' => {
|
163
|
-
'14' => {
|
164
|
-
'15' => {
|
165
|
-
'16' => {
|
166
|
-
'17' => {
|
167
|
-
'18' => {
|
168
|
-
'19' => {
|
169
|
-
'20' => {}}}}}}}}}}}}}}}}}}}}}, false)
|
170
|
-
end
|
171
|
-
|
172
|
-
def test_hash_escaped_key
|
173
|
-
json = %{{"a\nb":true,"c\td":false}}
|
174
|
-
obj = Oj.compat_load(json)
|
175
|
-
assert_equal({"a\nb" => true, "c\td" => false}, obj)
|
176
|
-
end
|
177
|
-
|
178
|
-
def test_bignum_object
|
179
|
-
dump_and_load(7 ** 55, false)
|
180
|
-
end
|
181
|
-
|
182
|
-
# BigDecimal
|
183
|
-
def test_bigdecimal_compat
|
184
|
-
dump_and_load(BigDecimal.new('3.14159265358979323846'), false)
|
185
|
-
end
|
186
|
-
|
187
|
-
def test_bigdecimal_load
|
188
|
-
orig = BigDecimal.new('80.51')
|
189
|
-
json = Oj.dump(orig, :mode => :compat, :bigdecimal_as_decimal => true)
|
190
|
-
bg = Oj.load(json, :mode => :compat, :bigdecimal_load => true)
|
191
|
-
assert_equal(BigDecimal, bg.class)
|
192
|
-
assert_equal(orig, bg)
|
193
|
-
end
|
194
|
-
|
195
|
-
# Time
|
196
|
-
def test_time_ruby
|
197
|
-
if RUBY_VERSION.start_with?('1.8')
|
198
|
-
t = Time.parse('2015-01-05T21:37:07.123456-08:00')
|
199
|
-
else
|
200
|
-
t = Time.new(2015, 1, 5, 21, 37, 7.123456, -8 * 3600)
|
201
|
-
end
|
202
|
-
expect = '"' + t.to_s + '"'
|
203
|
-
json = Oj.dump(t, :mode => :compat, :time_format => :ruby)
|
204
|
-
assert_equal(expect, json)
|
205
|
-
end
|
206
|
-
def test_time_xml
|
207
|
-
if RUBY_VERSION.start_with?('1.8')
|
208
|
-
t = Time.parse('2015-01-05T21:37:07.123456-08:00')
|
209
|
-
else
|
210
|
-
t = Time.new(2015, 1, 5, 21, 37, 7.123456, -8 * 3600)
|
211
|
-
end
|
212
|
-
json = Oj.dump(t, :mode => :compat, :time_format => :xmlschema, :second_precision => 6)
|
213
|
-
assert_equal('"2015-01-05T21:37:07.123456-08:00"', json)
|
214
|
-
end
|
215
|
-
unless RUBY_VERSION.start_with?('1.8')
|
216
|
-
def test_time_xml_12345
|
217
|
-
t = Time.new(2015, 1, 5, 21, 37, 7.123456, 12345)
|
218
|
-
json = Oj.dump(t, :mode => :compat, :time_format => :xmlschema, :second_precision => 6)
|
219
|
-
assert_equal('"2015-01-05T21:37:07.123456+03:25"', json)
|
220
|
-
end
|
221
|
-
end
|
222
|
-
def test_time_unix
|
223
|
-
if RUBY_VERSION.start_with?('1.8')
|
224
|
-
t = Time.parse('2015-01-05T21:37:07.123456-08:00')
|
225
|
-
else
|
226
|
-
t = Time.new(2015, 1, 5, 21, 37, 7.123456, -8 * 3600)
|
227
|
-
end
|
228
|
-
json = Oj.dump(t, :mode => :compat, :time_format => :unix, :second_precision => 6)
|
229
|
-
assert_equal('1420522627.123456', json)
|
230
|
-
end
|
231
|
-
def test_time_unix_zone
|
232
|
-
if RUBY_VERSION.start_with?('1.8')
|
233
|
-
t = Time.parse('2015-01-05T21:37:07.123456-08:00')
|
234
|
-
else
|
235
|
-
t = Time.new(2015, 1, 5, 21, 37, 7.123456, -8 * 3600)
|
236
|
-
end
|
237
|
-
json = Oj.dump(t, :mode => :compat, :time_format => :unix_zone, :second_precision => 6)
|
238
|
-
assert_equal('1420522627.123456e-28800', json)
|
239
|
-
end
|
240
|
-
unless RUBY_VERSION.start_with?('1.8')
|
241
|
-
def test_time_unix_zone_12345
|
242
|
-
t = Time.new(2015, 1, 5, 21, 37, 7.123456, 12345)
|
243
|
-
json = Oj.dump(t, :mode => :compat, :time_format => :unix_zone, :second_precision => 6)
|
244
|
-
assert_equal('1420481482.123456e12345', json)
|
245
|
-
end
|
246
|
-
end
|
247
|
-
def test_time_unix_zone_early
|
248
|
-
if RUBY_VERSION.start_with?('1.8')
|
249
|
-
t = Time.parse('1954-01-05T21:37:07.123456-08:00')
|
250
|
-
else
|
251
|
-
t = Time.new(1954, 1, 5, 21, 37, 7.123456, -8 * 3600)
|
252
|
-
end
|
253
|
-
json = Oj.dump(t, :mode => :compat, :time_format => :unix_zone, :second_precision => 6)
|
254
|
-
assert_equal('-504469372.876544e-28800', json)
|
255
|
-
end
|
256
|
-
|
257
|
-
# Stream IO
|
258
|
-
def test_io_string
|
259
|
-
json = %{{
|
260
|
-
"x":true,
|
261
|
-
"y":58,
|
262
|
-
"z": [1,2,3]
|
263
|
-
}
|
264
|
-
}
|
265
|
-
input = StringIO.new(json)
|
266
|
-
obj = Oj.compat_load(input)
|
267
|
-
assert_equal({ 'x' => true, 'y' => 58, 'z' => [1, 2, 3]}, obj)
|
268
|
-
end
|
269
|
-
|
270
|
-
def test_io_file
|
271
|
-
filename = File.join(File.dirname(__FILE__), 'open_file_test.json')
|
272
|
-
File.open(filename, 'w') { |f| f.write(%{{
|
273
|
-
"x":true,
|
274
|
-
"y":58,
|
275
|
-
"z": [1,2,3]
|
276
|
-
}
|
277
|
-
}) }
|
278
|
-
f = File.new(filename)
|
279
|
-
obj = Oj.compat_load(f)
|
280
|
-
f.close()
|
281
|
-
assert_equal({ 'x' => true, 'y' => 58, 'z' => [1, 2, 3]}, obj)
|
282
|
-
end
|
283
|
-
|
284
|
-
# symbol_keys option
|
285
|
-
def test_symbol_keys
|
286
|
-
json = %{{
|
287
|
-
"x":true,
|
288
|
-
"y":58,
|
289
|
-
"z": [1,2,3]
|
290
|
-
}
|
291
|
-
}
|
292
|
-
obj = Oj.compat_load(json, :symbol_keys => true)
|
293
|
-
assert_equal({ :x => true, :y => 58, :z => [1, 2, 3]}, obj)
|
294
|
-
end
|
295
|
-
|
296
|
-
# comments
|
297
|
-
def test_comment_slash
|
298
|
-
json = %{{
|
299
|
-
"x":true,//three
|
300
|
-
"y":58,
|
301
|
-
"z": [1,2,
|
302
|
-
3 // six
|
303
|
-
]}
|
304
|
-
}
|
305
|
-
obj = Oj.compat_load(json)
|
306
|
-
assert_equal({ 'x' => true, 'y' => 58, 'z' => [1, 2, 3]}, obj)
|
307
|
-
end
|
308
|
-
|
309
|
-
def test_comment_c
|
310
|
-
json = %{{
|
311
|
-
"x"/*one*/:/*two*/true,
|
312
|
-
"y":58,
|
313
|
-
"z": [1,2,3]}
|
314
|
-
}
|
315
|
-
obj = Oj.compat_load(json)
|
316
|
-
assert_equal({ 'x' => true, 'y' => 58, 'z' => [1, 2, 3]}, obj)
|
317
|
-
end
|
318
|
-
|
319
|
-
def test_comment
|
320
|
-
json = %{{
|
321
|
-
"x"/*one*/:/*two*/true,//three
|
322
|
-
"y":58/*four*/,
|
323
|
-
"z": [1,2/*five*/,
|
324
|
-
3 // six
|
325
|
-
]
|
326
|
-
}
|
327
|
-
}
|
328
|
-
obj = Oj.compat_load(json)
|
329
|
-
assert_equal({ 'x' => true, 'y' => 58, 'z' => [1, 2, 3]}, obj)
|
330
|
-
end
|
331
|
-
|
332
|
-
def test_json_object_compat
|
333
|
-
obj = Jeez.new(true, 58)
|
334
|
-
Oj.default_options = { :mode => :compat, :use_as_json => true, :use_to_json => true }
|
335
|
-
dump_and_load(obj, false)
|
336
|
-
end
|
337
|
-
|
338
|
-
def test_json_module_object
|
339
|
-
Oj.default_options = { :mode => :compat, :use_as_json => true, :use_to_json => true }
|
340
|
-
obj = One::Two::Three::Deep.new()
|
341
|
-
dump_and_load(obj, false)
|
342
|
-
end
|
343
|
-
|
344
|
-
def test_json_object_create_id
|
345
|
-
Oj.default_options = { :mode => :compat, :use_as_json => false, :use_to_json => false }
|
346
|
-
expected = Jeez.new(true, 58)
|
347
|
-
json = Oj.dump(expected, :indent => 2, :mode => :compat, :use_to_json => true, :use_as_json => true)
|
348
|
-
obj = Oj.compat_load(json)
|
349
|
-
assert_equal(expected, obj)
|
350
|
-
end
|
351
|
-
|
352
|
-
def test_json_object_bad
|
353
|
-
json = %{{"json_class":"CompatJuice::Junk","x":true}}
|
354
|
-
begin
|
355
|
-
Oj.compat_load(json)
|
356
|
-
rescue Exception => e
|
357
|
-
assert_equal("Oj::ParseError", e.class().name)
|
358
|
-
return
|
359
|
-
end
|
360
|
-
assert(false, "*** expected an exception")
|
361
|
-
end
|
362
|
-
|
363
|
-
def test_json_object_create_cache
|
364
|
-
Oj.default_options = { :mode => :compat, :use_as_json => true, :use_to_json => true }
|
365
|
-
expected = Jeez.new(true, 58)
|
366
|
-
json = Oj.dump(expected, :indent => 2, :mode => :compat, :use_to_json => true)
|
367
|
-
obj = Oj.compat_load(json, :class_cache => true)
|
368
|
-
assert_equal(expected, obj)
|
369
|
-
obj = Oj.compat_load(json, :class_cache => false)
|
370
|
-
assert_equal(expected, obj)
|
371
|
-
end
|
372
|
-
|
373
|
-
def test_json_object_create_id_other
|
374
|
-
Oj.default_options = { :mode => :compat, :use_as_json => false, :use_to_json => false }
|
375
|
-
expected = Jeez.new(true, 58)
|
376
|
-
json = Oj.dump(expected, :indent => 2, :mode => :compat, :use_as_json => true)
|
377
|
-
json.gsub!('json_class', '_class_')
|
378
|
-
obj = Oj.compat_load(json, :create_id => "_class_")
|
379
|
-
assert_equal(expected, obj)
|
380
|
-
end
|
381
|
-
|
382
|
-
def test_json_object_create_deep
|
383
|
-
Oj.default_options = { :mode => :compat, :use_as_json => true, :use_to_json => true }
|
384
|
-
expected = One::Two::Three::Deep.new()
|
385
|
-
json = Oj.dump(expected, :indent => 2, :mode => :compat)
|
386
|
-
obj = Oj.compat_load(json)
|
387
|
-
assert_equal(expected, obj)
|
388
|
-
end
|
389
|
-
|
390
|
-
def dump_and_load(obj, trace=false)
|
391
|
-
json = Oj.dump(obj, :indent => 2, :mode => :compat)
|
392
|
-
puts json if trace
|
393
|
-
loaded = Oj.compat_load(json);
|
394
|
-
assert_equal(obj, loaded)
|
395
|
-
loaded
|
396
|
-
end
|
397
|
-
|
398
|
-
end
|
399
|
-
=end
|