oj 2.0.0 → 3.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +17 -23
- data/README.md +74 -425
- data/ext/oj/buf.h +103 -0
- data/ext/oj/cache8.c +4 -0
- data/ext/oj/circarray.c +68 -0
- data/ext/oj/circarray.h +23 -0
- data/ext/oj/code.c +227 -0
- data/ext/oj/code.h +40 -0
- data/ext/oj/compat.c +243 -0
- data/ext/oj/custom.c +1097 -0
- data/ext/oj/dump.c +766 -1534
- data/ext/oj/dump.h +92 -0
- data/ext/oj/dump_compat.c +937 -0
- data/ext/oj/dump_leaf.c +254 -0
- data/ext/oj/dump_object.c +810 -0
- data/ext/oj/dump_rails.c +329 -0
- data/ext/oj/dump_strict.c +416 -0
- data/ext/oj/encode.h +51 -0
- data/ext/oj/err.c +57 -0
- data/ext/oj/err.h +70 -0
- data/ext/oj/extconf.rb +17 -7
- data/ext/oj/fast.c +213 -180
- data/ext/oj/hash.c +163 -0
- data/ext/oj/hash.h +46 -0
- data/ext/oj/hash_test.c +512 -0
- data/ext/oj/mimic_json.c +817 -0
- data/ext/oj/mimic_rails.c +806 -0
- data/ext/oj/mimic_rails.h +17 -0
- data/ext/oj/object.c +752 -0
- data/ext/oj/odd.c +230 -0
- data/ext/oj/odd.h +44 -0
- data/ext/oj/oj.c +1288 -929
- data/ext/oj/oj.h +240 -69
- data/ext/oj/parse.c +1014 -0
- data/ext/oj/parse.h +92 -0
- data/ext/oj/reader.c +223 -0
- data/ext/oj/reader.h +151 -0
- data/ext/oj/resolve.c +127 -0
- data/ext/oj/{cache.h → resolve.h} +6 -13
- data/ext/oj/rxclass.c +133 -0
- data/ext/oj/rxclass.h +27 -0
- data/ext/oj/saj.c +77 -175
- data/ext/oj/scp.c +224 -0
- data/ext/oj/sparse.c +911 -0
- data/ext/oj/stream_writer.c +301 -0
- data/ext/oj/strict.c +162 -0
- data/ext/oj/string_writer.c +480 -0
- data/ext/oj/val_stack.c +98 -0
- data/ext/oj/val_stack.h +188 -0
- data/lib/oj/active_support_helper.rb +41 -0
- data/lib/oj/bag.rb +6 -10
- data/lib/oj/easy_hash.rb +52 -0
- data/lib/oj/json.rb +172 -0
- data/lib/oj/mimic.rb +260 -5
- data/lib/oj/saj.rb +13 -10
- data/lib/oj/schandler.rb +142 -0
- data/lib/oj/state.rb +131 -0
- data/lib/oj/version.rb +1 -1
- data/lib/oj.rb +11 -23
- data/pages/Advanced.md +22 -0
- data/pages/Compatibility.md +25 -0
- data/pages/Custom.md +23 -0
- data/pages/Encoding.md +65 -0
- data/pages/JsonGem.md +79 -0
- data/pages/Modes.md +140 -0
- data/pages/Options.md +250 -0
- data/pages/Rails.md +60 -0
- data/pages/Security.md +20 -0
- data/test/_test_active.rb +76 -0
- data/test/_test_active_mimic.rb +96 -0
- data/test/_test_mimic_rails.rb +126 -0
- data/test/activesupport4/decoding_test.rb +105 -0
- data/test/activesupport4/encoding_test.rb +531 -0
- data/test/activesupport4/test_helper.rb +41 -0
- data/test/activesupport5/decoding_test.rb +125 -0
- data/test/activesupport5/encoding_test.rb +483 -0
- data/test/activesupport5/encoding_test_cases.rb +90 -0
- data/test/activesupport5/test_helper.rb +50 -0
- data/test/activesupport5/time_zone_test_helpers.rb +24 -0
- data/test/helper.rb +27 -0
- data/test/isolated/shared.rb +310 -0
- data/test/isolated/test_mimic_after.rb +13 -0
- data/test/isolated/test_mimic_alone.rb +12 -0
- data/test/isolated/test_mimic_as_json.rb +45 -0
- data/test/isolated/test_mimic_before.rb +13 -0
- data/test/isolated/test_mimic_define.rb +28 -0
- data/test/isolated/test_mimic_rails_after.rb +22 -0
- data/test/isolated/test_mimic_rails_before.rb +21 -0
- data/test/isolated/test_mimic_redefine.rb +15 -0
- data/test/json_gem/json_addition_test.rb +216 -0
- data/test/json_gem/json_common_interface_test.rb +143 -0
- data/test/json_gem/json_encoding_test.rb +109 -0
- data/test/json_gem/json_ext_parser_test.rb +20 -0
- data/test/json_gem/json_fixtures_test.rb +35 -0
- data/test/json_gem/json_generator_test.rb +383 -0
- data/test/json_gem/json_generic_object_test.rb +90 -0
- data/test/json_gem/json_parser_test.rb +470 -0
- data/test/json_gem/json_string_matching_test.rb +42 -0
- data/test/json_gem/test_helper.rb +18 -0
- data/test/perf_compat.rb +130 -0
- data/test/perf_fast.rb +9 -9
- data/test/perf_file.rb +64 -0
- data/test/{perf_obj.rb → perf_object.rb} +24 -10
- data/test/perf_scp.rb +151 -0
- data/test/perf_strict.rb +32 -113
- data/test/sample.rb +2 -3
- data/test/test_compat.rb +474 -0
- data/test/test_custom.rb +355 -0
- data/test/test_debian.rb +53 -0
- data/test/test_fast.rb +66 -16
- data/test/test_file.rb +237 -0
- data/test/test_gc.rb +49 -0
- data/test/test_hash.rb +29 -0
- data/test/test_null.rb +376 -0
- data/test/test_object.rb +1010 -0
- data/test/test_saj.rb +16 -16
- data/test/test_scp.rb +417 -0
- data/test/test_strict.rb +410 -0
- data/test/test_various.rb +815 -0
- data/test/test_writer.rb +308 -0
- data/test/tests.rb +9 -902
- data/test/tests_mimic.rb +14 -0
- data/test/tests_mimic_addition.rb +7 -0
- metadata +253 -38
- data/ext/oj/cache.c +0 -148
- data/ext/oj/foo.rb +0 -6
- data/ext/oj/load.c +0 -1049
- data/test/a.rb +0 -38
- data/test/perf1.rb +0 -64
- data/test/perf2.rb +0 -76
- data/test/perf_obj_old.rb +0 -213
- data/test/test_mimic.rb +0 -208
data/test/test_custom.rb
ADDED
@@ -0,0 +1,355 @@
|
|
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 'minitest'
|
11
|
+
require 'minitest/autorun'
|
12
|
+
require 'stringio'
|
13
|
+
require 'date'
|
14
|
+
require 'bigdecimal'
|
15
|
+
require 'oj'
|
16
|
+
|
17
|
+
class CustomJuice < Minitest::Test
|
18
|
+
|
19
|
+
module TestModule
|
20
|
+
end
|
21
|
+
|
22
|
+
class Jeez
|
23
|
+
attr_accessor :x, :y
|
24
|
+
|
25
|
+
def initialize(x, y)
|
26
|
+
@x = x
|
27
|
+
@y = y
|
28
|
+
end
|
29
|
+
def ==(o)
|
30
|
+
self.class == o.class && @x == o.x && @y = o.y
|
31
|
+
end
|
32
|
+
def to_json(*args)
|
33
|
+
%|{"xx":#{@x},"yy":#{y}}|
|
34
|
+
end
|
35
|
+
def as_json(*args)
|
36
|
+
{'a' => @x, :b => @y }
|
37
|
+
end
|
38
|
+
def to_hash()
|
39
|
+
{'b' => @x, 'n' => @y }
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def setup
|
44
|
+
@default_options = Oj.default_options
|
45
|
+
Oj.default_options = { :mode => :custom }
|
46
|
+
end
|
47
|
+
|
48
|
+
def teardown
|
49
|
+
Oj.default_options = @default_options
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_nil
|
53
|
+
dump_and_load(nil, false)
|
54
|
+
end
|
55
|
+
|
56
|
+
def test_true
|
57
|
+
dump_and_load(true, false)
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_false
|
61
|
+
dump_and_load(false, false)
|
62
|
+
end
|
63
|
+
|
64
|
+
def test_fixnum
|
65
|
+
dump_and_load(0, false)
|
66
|
+
dump_and_load(12345, false)
|
67
|
+
dump_and_load(-54321, false)
|
68
|
+
dump_and_load(1, false)
|
69
|
+
end
|
70
|
+
|
71
|
+
def test_float
|
72
|
+
dump_and_load(0.0, false)
|
73
|
+
dump_and_load(12345.6789, false)
|
74
|
+
dump_and_load(70.35, false)
|
75
|
+
dump_and_load(-54321.012, false)
|
76
|
+
dump_and_load(1.7775, false)
|
77
|
+
dump_and_load(2.5024, false)
|
78
|
+
dump_and_load(2.48e16, false)
|
79
|
+
dump_and_load(2.48e100 * 1.0e10, false)
|
80
|
+
dump_and_load(-2.48e100 * 1.0e10, false)
|
81
|
+
end
|
82
|
+
|
83
|
+
def test_nan_dump
|
84
|
+
assert_equal('null', Oj.dump(0/0.0, :nan => :null))
|
85
|
+
assert_equal('3.3e14159265358979323846', Oj.dump(0/0.0, :nan => :huge))
|
86
|
+
assert_equal('NaN', Oj.dump(0/0.0, :nan => :word))
|
87
|
+
begin
|
88
|
+
Oj.dump(0/0.0, :nan => :raise)
|
89
|
+
rescue Exception
|
90
|
+
assert(true)
|
91
|
+
return
|
92
|
+
end
|
93
|
+
assert(false, "*** expected an exception")
|
94
|
+
end
|
95
|
+
|
96
|
+
def test_infinity_dump
|
97
|
+
assert_equal('null', Oj.dump(1/0.0, :nan => :null))
|
98
|
+
assert_equal('3.0e14159265358979323846', Oj.dump(1/0.0, :nan => :huge))
|
99
|
+
assert_equal('Infinity', Oj.dump(1/0.0, :nan => :word))
|
100
|
+
begin
|
101
|
+
Oj.dump(1/0.0, :nan => :raise)
|
102
|
+
rescue Exception
|
103
|
+
assert(true)
|
104
|
+
return
|
105
|
+
end
|
106
|
+
assert(false, "*** expected an exception")
|
107
|
+
end
|
108
|
+
|
109
|
+
def test_neg_infinity_dump
|
110
|
+
assert_equal('null', Oj.dump(-1/0.0, :nan => :null))
|
111
|
+
assert_equal('-3.0e14159265358979323846', Oj.dump(-1/0.0, :nan => :huge))
|
112
|
+
assert_equal('-Infinity', Oj.dump(-1/0.0, :nan => :word))
|
113
|
+
begin
|
114
|
+
Oj.dump(-1/0.0, :nan => :raise)
|
115
|
+
rescue Exception
|
116
|
+
assert(true)
|
117
|
+
return
|
118
|
+
end
|
119
|
+
assert(false, "*** expected an exception")
|
120
|
+
end
|
121
|
+
|
122
|
+
def test_string
|
123
|
+
dump_and_load('', false)
|
124
|
+
dump_and_load('abc', false)
|
125
|
+
dump_and_load("abc\ndef", false)
|
126
|
+
dump_and_load("a\u0041", false)
|
127
|
+
end
|
128
|
+
|
129
|
+
def test_string_ascii
|
130
|
+
json = Oj.dump("ぴーたー", :escape_mode => :ascii)
|
131
|
+
assert_equal(%{"\\u3074\\u30fc\\u305f\\u30fc"}, json)
|
132
|
+
dump_and_load("ぴーたー", false, :escape_mode => :ascii)
|
133
|
+
end
|
134
|
+
|
135
|
+
def test_string_json
|
136
|
+
json = Oj.dump("ぴーたー", :escape_mode => :json)
|
137
|
+
assert_equal(%{"ぴーたー"}, json)
|
138
|
+
dump_and_load("ぴーたー", false, :escape_mode => :json)
|
139
|
+
end
|
140
|
+
|
141
|
+
def test_array
|
142
|
+
dump_and_load([], false)
|
143
|
+
dump_and_load([true, false], false)
|
144
|
+
dump_and_load(['a', 1, nil], false)
|
145
|
+
dump_and_load([[nil]], false)
|
146
|
+
dump_and_load([[nil], 58], false)
|
147
|
+
end
|
148
|
+
|
149
|
+
def test_array_deep
|
150
|
+
dump_and_load([1,[2,[3,[4,[5,[6,[7,[8,[9,[10,[11,[12,[13,[14,[15,[16,[17,[18,[19,[20]]]]]]]]]]]]]]]]]]]], false)
|
151
|
+
end
|
152
|
+
|
153
|
+
def test_deep_nest
|
154
|
+
begin
|
155
|
+
n = 10000
|
156
|
+
Oj.strict_load('[' * n + ']' * n)
|
157
|
+
rescue Exception => e
|
158
|
+
assert(false, e.message)
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
def test_hash
|
163
|
+
dump_and_load({}, false)
|
164
|
+
dump_and_load({ 'true' => true, 'false' => false}, false)
|
165
|
+
dump_and_load({ 'true' => true, 'array' => [], 'hash' => { }}, false)
|
166
|
+
end
|
167
|
+
|
168
|
+
def test_hash_deep
|
169
|
+
dump_and_load({'1' => {
|
170
|
+
'2' => {
|
171
|
+
'3' => {
|
172
|
+
'4' => {
|
173
|
+
'5' => {
|
174
|
+
'6' => {
|
175
|
+
'7' => {
|
176
|
+
'8' => {
|
177
|
+
'9' => {
|
178
|
+
'10' => {
|
179
|
+
'11' => {
|
180
|
+
'12' => {
|
181
|
+
'13' => {
|
182
|
+
'14' => {
|
183
|
+
'15' => {
|
184
|
+
'16' => {
|
185
|
+
'17' => {
|
186
|
+
'18' => {
|
187
|
+
'19' => {
|
188
|
+
'20' => {}}}}}}}}}}}}}}}}}}}}}, false)
|
189
|
+
end
|
190
|
+
|
191
|
+
def test_hash_escaped_key
|
192
|
+
json = %{{"a\nb":true,"c\td":false}}
|
193
|
+
obj = Oj.load(json)
|
194
|
+
assert_equal({"a\nb" => true, "c\td" => false}, obj)
|
195
|
+
end
|
196
|
+
|
197
|
+
def test_hash_non_string_key
|
198
|
+
assert_equal(%|{"1":true}|, Oj.dump({ 1 => true }, :indent => 0))
|
199
|
+
end
|
200
|
+
|
201
|
+
def test_bignum_object
|
202
|
+
dump_and_load(7 ** 55, false)
|
203
|
+
end
|
204
|
+
|
205
|
+
def test_bigdecimal
|
206
|
+
assert_equal('0.314159265358979323846e1', Oj.dump(BigDecimal.new('3.14159265358979323846'), :bigdecimal_load => true))
|
207
|
+
dump_and_load(BigDecimal.new('3.14159265358979323846'), false, :bigdecimal_load => true)
|
208
|
+
end
|
209
|
+
|
210
|
+
def test_object
|
211
|
+
obj = Jeez.new(true, 58)
|
212
|
+
json = Oj.dump(obj, :create_id => "^o", :use_to_json => false, :use_as_json => false, :use_to_hash => false)
|
213
|
+
#puts "*** #{json}"
|
214
|
+
dump_and_load(obj, false, :create_id => "^o", :create_additions => true)
|
215
|
+
end
|
216
|
+
|
217
|
+
def test_object_to_json
|
218
|
+
obj = Jeez.new(true, 58)
|
219
|
+
json = Oj.dump(obj, :use_to_json => true, :use_as_json => false, :use_to_hash => false)
|
220
|
+
assert_equal(%|{"xx":true,"yy":58}|, json)
|
221
|
+
end
|
222
|
+
|
223
|
+
def test_object_as_json
|
224
|
+
obj = Jeez.new(true, 58)
|
225
|
+
json = Oj.dump(obj, :use_to_json => false, :use_as_json => true, :use_to_hash => false)
|
226
|
+
assert_equal(%|{"a":true,"b":58}|, json)
|
227
|
+
end
|
228
|
+
|
229
|
+
def test_object_to_hash
|
230
|
+
obj = Jeez.new(true, 58)
|
231
|
+
json = Oj.dump(obj, :use_to_json => false, :use_as_json => false, :use_to_hash => true)
|
232
|
+
assert_equal(%|{"b":true,"n":58}|, json)
|
233
|
+
end
|
234
|
+
|
235
|
+
def test_symbol
|
236
|
+
json = Oj.dump(:abc)
|
237
|
+
assert_equal('"abc"', json)
|
238
|
+
end
|
239
|
+
|
240
|
+
def test_class
|
241
|
+
assert_equal(%|"CustomJuice"|, Oj.dump(CustomJuice))
|
242
|
+
end
|
243
|
+
|
244
|
+
def test_module
|
245
|
+
assert_equal(%|"CustomJuice::TestModule"|, Oj.dump(TestModule))
|
246
|
+
end
|
247
|
+
|
248
|
+
def test_symbol_keys
|
249
|
+
json = %|{
|
250
|
+
"x":true,
|
251
|
+
"y":58,
|
252
|
+
"z": [1,2,3]
|
253
|
+
}
|
254
|
+
|
|
255
|
+
obj = Oj.load(json, :symbol_keys => true)
|
256
|
+
assert_equal({ :x => true, :y => 58, :z => [1, 2, 3]}, obj)
|
257
|
+
end
|
258
|
+
|
259
|
+
def test_double
|
260
|
+
json = %{{ "x": 1}{ "y": 2}}
|
261
|
+
results = []
|
262
|
+
Oj.load(json, :mode => :strict) { |x| results << x }
|
263
|
+
|
264
|
+
assert_equal([{ 'x' => 1 }, { 'y' => 2 }], results)
|
265
|
+
end
|
266
|
+
|
267
|
+
def test_circular_hash
|
268
|
+
h = { 'a' => 7 }
|
269
|
+
h['b'] = h
|
270
|
+
json = Oj.dump(h, :indent => 2, :circular => true)
|
271
|
+
assert_equal(%|{
|
272
|
+
"a":7,
|
273
|
+
"b":null
|
274
|
+
}
|
275
|
+
|, json)
|
276
|
+
end
|
277
|
+
|
278
|
+
def test_omit_nil
|
279
|
+
json = Oj.dump({'x' => {'a' => 1, 'b' => nil }, 'y' => nil}, :omit_nil => true)
|
280
|
+
assert_equal(%|{"x":{"a":1}}|, json)
|
281
|
+
end
|
282
|
+
|
283
|
+
def test_complex
|
284
|
+
obj = Complex(2, 9)
|
285
|
+
dump_and_load(obj, false, :create_id => "^o", :create_additions => true)
|
286
|
+
end
|
287
|
+
|
288
|
+
def test_rational
|
289
|
+
obj = Rational(2, 9)
|
290
|
+
dump_and_load(obj, false, :create_id => "^o", :create_additions => true)
|
291
|
+
end
|
292
|
+
|
293
|
+
def test_range
|
294
|
+
obj = 3..8
|
295
|
+
dump_and_load(obj, false, :create_id => "^o", :create_additions => true)
|
296
|
+
end
|
297
|
+
|
298
|
+
def test_date
|
299
|
+
obj = Date.new(2017, 1, 5)
|
300
|
+
dump_and_load(obj, false, :create_id => "^o", :create_additions => true)
|
301
|
+
end
|
302
|
+
|
303
|
+
def test_datetime
|
304
|
+
obj = DateTime.new(2017, 1, 5, 10, 20, 30)
|
305
|
+
dump_and_load(obj, false, :create_id => "^o", :create_additions => true)
|
306
|
+
end
|
307
|
+
|
308
|
+
def test_regexp
|
309
|
+
# this notation must be used to get an == match later
|
310
|
+
obj = /(?ix-m:^yes$)/
|
311
|
+
dump_and_load(obj, false, :create_id => "^o", :create_additions => true)
|
312
|
+
end
|
313
|
+
|
314
|
+
def test_openstruct
|
315
|
+
obj = OpenStruct.new(:a => 1, 'b' => 2)
|
316
|
+
dump_and_load(obj, false, :create_id => "^o", :create_additions => true)
|
317
|
+
end
|
318
|
+
|
319
|
+
def test_time
|
320
|
+
obj = Time.now()
|
321
|
+
dump_and_load(obj, false, :time_format => :unix, :create_id => "^o", :create_additions => true)
|
322
|
+
dump_and_load_inspect(obj, false, :time_format => :unix_zone, :create_id => "^o", :create_additions => true)
|
323
|
+
dump_and_load_inspect(obj, false, :time_format => :xmlschema, :create_id => "^o", :create_additions => true)
|
324
|
+
dump_and_load_inspect(obj, false, :time_format => :ruby, :create_id => "^o", :create_additions => true)
|
325
|
+
end
|
326
|
+
|
327
|
+
def dump_and_load(obj, trace=false, options={})
|
328
|
+
options = options.merge(:indent => 2, :mode => :custom)
|
329
|
+
json = Oj.dump(obj, options)
|
330
|
+
puts json if trace
|
331
|
+
|
332
|
+
loaded = Oj.load(json, options);
|
333
|
+
if obj.nil?
|
334
|
+
assert_nil(loaded)
|
335
|
+
else
|
336
|
+
assert_equal(obj, loaded)
|
337
|
+
end
|
338
|
+
loaded
|
339
|
+
end
|
340
|
+
|
341
|
+
def dump_and_load_inspect(obj, trace=false, options={})
|
342
|
+
options = options.merge(:indent => 2, :mode => :custom)
|
343
|
+
json = Oj.dump(obj, options)
|
344
|
+
puts json if trace
|
345
|
+
|
346
|
+
loaded = Oj.load(json, options);
|
347
|
+
if obj.nil?
|
348
|
+
assert_nil(loaded)
|
349
|
+
else
|
350
|
+
assert_equal(obj.inspect, loaded.inspect)
|
351
|
+
end
|
352
|
+
loaded
|
353
|
+
end
|
354
|
+
|
355
|
+
end
|
data/test/test_debian.rb
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
require 'helper'
|
4
|
+
|
5
|
+
class DebJuice < Minitest::Test
|
6
|
+
class Jam
|
7
|
+
attr_accessor :x, :y
|
8
|
+
|
9
|
+
def initialize(x, y)
|
10
|
+
@x = x
|
11
|
+
@y = y
|
12
|
+
end
|
13
|
+
|
14
|
+
def eql?(o)
|
15
|
+
self.class == o.class && @x == o.x && @y == o.y
|
16
|
+
end
|
17
|
+
alias == eql?
|
18
|
+
|
19
|
+
end# Jam
|
20
|
+
|
21
|
+
# contributed by sauliusg to fix as_json
|
22
|
+
class Orange < Jam
|
23
|
+
def initialize(x, y)
|
24
|
+
super
|
25
|
+
end
|
26
|
+
|
27
|
+
def as_json()
|
28
|
+
{ :json_class => self.class,
|
29
|
+
:x => @x,
|
30
|
+
:y => @y }
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.json_create(h)
|
34
|
+
self.new(h['x'], h['y'])
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_as_json_object_compat_hash_cached
|
39
|
+
Oj.default_options = { :mode => :compat, :class_cache => true, :use_as_json => true }
|
40
|
+
obj = Orange.new(true, 58)
|
41
|
+
json = Oj.dump(obj, :indent => 2)
|
42
|
+
assert(!json.nil?)
|
43
|
+
dump_and_load(obj, true)
|
44
|
+
end
|
45
|
+
|
46
|
+
def dump_and_load(obj, trace=false)
|
47
|
+
json = Oj.dump(obj, :indent => 2)
|
48
|
+
loaded = Oj.load(json);
|
49
|
+
assert_equal(obj, loaded)
|
50
|
+
loaded
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
data/test/test_fast.rb
CHANGED
@@ -1,19 +1,11 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
# encoding: UTF-8
|
3
3
|
|
4
|
-
|
5
|
-
# required. That can be set in the RUBYOPT environment variable.
|
6
|
-
# export RUBYOPT=-w
|
4
|
+
$: << File.dirname(__FILE__)
|
7
5
|
|
8
|
-
|
6
|
+
require 'helper'
|
9
7
|
|
10
|
-
|
11
|
-
$: << File.join(File.dirname(__FILE__), "../ext")
|
12
|
-
|
13
|
-
require 'test/unit'
|
14
|
-
require 'oj'
|
15
|
-
|
16
|
-
$json1 = %{{
|
8
|
+
$json1 = %|{
|
17
9
|
"array": [
|
18
10
|
{
|
19
11
|
"num" : 3,
|
@@ -26,9 +18,17 @@ $json1 = %{{
|
|
26
18
|
}
|
27
19
|
],
|
28
20
|
"boolean" : true
|
29
|
-
}
|
21
|
+
}|
|
22
|
+
|
23
|
+
class DocTest < Minitest::Test
|
24
|
+
def setup
|
25
|
+
@default_options = Oj.default_options
|
26
|
+
end
|
27
|
+
|
28
|
+
def teardown
|
29
|
+
Oj.default_options = @default_options
|
30
|
+
end
|
30
31
|
|
31
|
-
class DocTest < ::Test::Unit::TestCase
|
32
32
|
def test_nil
|
33
33
|
json = %{null}
|
34
34
|
Oj::Doc.open(json) do |doc|
|
@@ -61,6 +61,22 @@ class DocTest < ::Test::Unit::TestCase
|
|
61
61
|
end
|
62
62
|
end
|
63
63
|
|
64
|
+
def test_encoding
|
65
|
+
json = %{"ぴーたー"}
|
66
|
+
Oj::Doc.open(json) do |doc|
|
67
|
+
assert_equal(String, doc.type)
|
68
|
+
assert_equal("ぴーたー", doc.fetch())
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def test_encoding_escaped
|
73
|
+
json = %{"\\u3074\\u30fc\\u305f\\u30fc"}
|
74
|
+
Oj::Doc.open(json) do |doc|
|
75
|
+
assert_equal(String, doc.type)
|
76
|
+
assert_equal("ぴーたー", doc.fetch())
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
64
80
|
def test_fixnum
|
65
81
|
json = %{12345}
|
66
82
|
Oj::Doc.open(json) do |doc|
|
@@ -158,6 +174,20 @@ class DocTest < ::Test::Unit::TestCase
|
|
158
174
|
end
|
159
175
|
end
|
160
176
|
|
177
|
+
def test_move_slash
|
178
|
+
Oj::Doc.open(%|{"top":{"a/b":3}}|) do |doc|
|
179
|
+
doc.move('top/a\/b')
|
180
|
+
assert_equal('/top/a\/b', doc.where?)
|
181
|
+
end
|
182
|
+
end
|
183
|
+
|
184
|
+
def test_fetch_slash
|
185
|
+
Oj::Doc.open(%|{"a/b":3}|) do |doc|
|
186
|
+
x = doc.fetch('a\/b')
|
187
|
+
assert_equal(3, x)
|
188
|
+
end
|
189
|
+
end
|
190
|
+
|
161
191
|
def test_move_relative
|
162
192
|
Oj::Doc.open($json1) do |doc|
|
163
193
|
[['/', 'array', '/array'],
|
@@ -243,6 +273,18 @@ class DocTest < ::Test::Unit::TestCase
|
|
243
273
|
end
|
244
274
|
end
|
245
275
|
|
276
|
+
def test_move_fetch_path
|
277
|
+
Oj::Doc.open($json1) do |doc|
|
278
|
+
[['/array/1', 'num', 3],
|
279
|
+
['/array/1', 'string', 'message'],
|
280
|
+
['/array/1/hash', 'h2/a', [1, 2, 3]],
|
281
|
+
].each do |path,fetch_path,val|
|
282
|
+
doc.move(path)
|
283
|
+
assert_equal(val, doc.fetch(fetch_path))
|
284
|
+
end
|
285
|
+
end
|
286
|
+
end
|
287
|
+
|
246
288
|
def test_home
|
247
289
|
Oj::Doc.open($json1) do |doc|
|
248
290
|
doc.move('/array/1/num')
|
@@ -310,7 +352,7 @@ class DocTest < ::Test::Unit::TestCase
|
|
310
352
|
end
|
311
353
|
|
312
354
|
def test_open_file
|
313
|
-
filename = 'open_file_test.json'
|
355
|
+
filename = File.join(File.dirname(__FILE__), 'open_file_test.json')
|
314
356
|
File.open(filename, 'w') { |f| f.write('{"a":[1,2,3]}') }
|
315
357
|
Oj::Doc.open_file(filename) do |doc|
|
316
358
|
assert_equal(5, doc.size)
|
@@ -336,7 +378,7 @@ class DocTest < ::Test::Unit::TestCase
|
|
336
378
|
end
|
337
379
|
|
338
380
|
def test_file_open_close
|
339
|
-
filename = 'open_file_test.json'
|
381
|
+
filename = File.join(File.dirname(__FILE__), 'open_file_test.json')
|
340
382
|
File.open(filename, 'w') { |f| f.write('{"a":[1,2,3]}') }
|
341
383
|
doc = Oj::Doc.open_file(filename)
|
342
384
|
assert_equal(Oj::Doc, doc.class)
|
@@ -354,7 +396,6 @@ class DocTest < ::Test::Unit::TestCase
|
|
354
396
|
end
|
355
397
|
end
|
356
398
|
|
357
|
-
|
358
399
|
def test_open_no_close
|
359
400
|
json = %{{"a":[1,2,3]}}
|
360
401
|
doc = Oj::Doc.open(json)
|
@@ -388,6 +429,15 @@ class DocTest < ::Test::Unit::TestCase
|
|
388
429
|
assert_equal({'/1' => 1, '/2/1' => 2, '/2/2' => 3}, results)
|
389
430
|
end
|
390
431
|
|
432
|
+
def test_each_leaf_hash
|
433
|
+
results = Oj::Doc.open('{"a":{"x":2},"b":{"y":4}}') do |doc|
|
434
|
+
h = {}
|
435
|
+
doc.each_leaf() { |d| h[d.where?] = d.fetch() }
|
436
|
+
h
|
437
|
+
end
|
438
|
+
assert_equal({'/a/x' => 2, '/b/y' => 4}, results)
|
439
|
+
end
|
440
|
+
|
391
441
|
def test_comment
|
392
442
|
json = %{{
|
393
443
|
"x"/*one*/:/*two*/true,//three
|