oj 3.8.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +21 -0
- data/README.md +104 -0
- data/ext/oj/buf.h +103 -0
- data/ext/oj/cache8.c +107 -0
- data/ext/oj/cache8.h +48 -0
- data/ext/oj/circarray.c +68 -0
- data/ext/oj/circarray.h +23 -0
- data/ext/oj/code.c +235 -0
- data/ext/oj/code.h +42 -0
- data/ext/oj/compat.c +299 -0
- data/ext/oj/custom.c +1191 -0
- data/ext/oj/dump.c +1252 -0
- data/ext/oj/dump.h +96 -0
- data/ext/oj/dump_compat.c +977 -0
- data/ext/oj/dump_leaf.c +252 -0
- data/ext/oj/dump_object.c +837 -0
- data/ext/oj/dump_strict.c +433 -0
- data/ext/oj/encode.h +45 -0
- data/ext/oj/err.c +57 -0
- data/ext/oj/err.h +70 -0
- data/ext/oj/extconf.rb +47 -0
- data/ext/oj/fast.c +1771 -0
- 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 +878 -0
- data/ext/oj/object.c +771 -0
- data/ext/oj/odd.c +231 -0
- data/ext/oj/odd.h +44 -0
- data/ext/oj/oj.c +1704 -0
- data/ext/oj/oj.h +385 -0
- data/ext/oj/parse.c +1086 -0
- data/ext/oj/parse.h +111 -0
- data/ext/oj/rails.c +1493 -0
- data/ext/oj/rails.h +21 -0
- data/ext/oj/reader.c +231 -0
- data/ext/oj/reader.h +151 -0
- data/ext/oj/resolve.c +102 -0
- data/ext/oj/resolve.h +14 -0
- data/ext/oj/rxclass.c +147 -0
- data/ext/oj/rxclass.h +27 -0
- data/ext/oj/saj.c +714 -0
- data/ext/oj/scp.c +224 -0
- data/ext/oj/sparse.c +910 -0
- data/ext/oj/stream_writer.c +363 -0
- data/ext/oj/strict.c +212 -0
- data/ext/oj/string_writer.c +534 -0
- data/ext/oj/trace.c +79 -0
- data/ext/oj/trace.h +28 -0
- data/ext/oj/util.c +136 -0
- data/ext/oj/util.h +19 -0
- data/ext/oj/val_stack.c +118 -0
- data/ext/oj/val_stack.h +185 -0
- data/ext/oj/wab.c +631 -0
- data/lib/oj.rb +21 -0
- data/lib/oj/active_support_helper.rb +41 -0
- data/lib/oj/bag.rb +88 -0
- data/lib/oj/easy_hash.rb +52 -0
- data/lib/oj/error.rb +22 -0
- data/lib/oj/json.rb +176 -0
- data/lib/oj/mimic.rb +267 -0
- data/lib/oj/saj.rb +66 -0
- data/lib/oj/schandler.rb +142 -0
- data/lib/oj/state.rb +131 -0
- data/lib/oj/version.rb +5 -0
- 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 +155 -0
- data/pages/Options.md +283 -0
- data/pages/Rails.md +116 -0
- data/pages/Security.md +20 -0
- data/pages/WAB.md +13 -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/activerecord/result_test.rb +27 -0
- data/test/activesupport4/decoding_test.rb +108 -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 +485 -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/bar.rb +25 -0
- data/test/files.rb +29 -0
- data/test/foo.rb +167 -0
- data/test/helper.rb +26 -0
- data/test/isolated/shared.rb +308 -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 +148 -0
- data/test/json_gem/json_encoding_test.rb +107 -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.rb +107 -0
- data/test/perf_compat.rb +130 -0
- data/test/perf_fast.rb +164 -0
- data/test/perf_file.rb +64 -0
- data/test/perf_object.rb +138 -0
- data/test/perf_saj.rb +109 -0
- data/test/perf_scp.rb +151 -0
- data/test/perf_simple.rb +287 -0
- data/test/perf_strict.rb +145 -0
- data/test/perf_wab.rb +131 -0
- data/test/sample.rb +54 -0
- data/test/sample/change.rb +14 -0
- data/test/sample/dir.rb +19 -0
- data/test/sample/doc.rb +36 -0
- data/test/sample/file.rb +48 -0
- data/test/sample/group.rb +16 -0
- data/test/sample/hasprops.rb +16 -0
- data/test/sample/layer.rb +12 -0
- data/test/sample/line.rb +20 -0
- data/test/sample/oval.rb +10 -0
- data/test/sample/rect.rb +10 -0
- data/test/sample/shape.rb +35 -0
- data/test/sample/text.rb +20 -0
- data/test/sample_json.rb +37 -0
- data/test/test_compat.rb +509 -0
- data/test/test_custom.rb +503 -0
- data/test/test_debian.rb +53 -0
- data/test/test_fast.rb +470 -0
- data/test/test_file.rb +239 -0
- data/test/test_gc.rb +49 -0
- data/test/test_hash.rb +29 -0
- data/test/test_integer_range.rb +73 -0
- data/test/test_null.rb +376 -0
- data/test/test_object.rb +1018 -0
- data/test/test_saj.rb +186 -0
- data/test/test_scp.rb +433 -0
- data/test/test_strict.rb +410 -0
- data/test/test_various.rb +741 -0
- data/test/test_wab.rb +307 -0
- data/test/test_writer.rb +380 -0
- data/test/tests.rb +24 -0
- data/test/tests_mimic.rb +14 -0
- data/test/tests_mimic_addition.rb +7 -0
- data/test/zoo.rb +13 -0
- metadata +359 -0
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
ADDED
@@ -0,0 +1,470 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# encoding: UTF-8
|
3
|
+
|
4
|
+
$: << File.dirname(__FILE__)
|
5
|
+
|
6
|
+
require 'helper'
|
7
|
+
|
8
|
+
class DocTest < Minitest::Test
|
9
|
+
def setup
|
10
|
+
@default_options = Oj.default_options
|
11
|
+
@json1 = %|{
|
12
|
+
"array": [
|
13
|
+
{
|
14
|
+
"num" : 3,
|
15
|
+
"string": "message",
|
16
|
+
"hash" : {
|
17
|
+
"h2" : {
|
18
|
+
"a" : [ 1, 2, 3 ]
|
19
|
+
}
|
20
|
+
}
|
21
|
+
}
|
22
|
+
],
|
23
|
+
"boolean" : true
|
24
|
+
}|
|
25
|
+
end
|
26
|
+
|
27
|
+
def teardown
|
28
|
+
Oj.default_options = @default_options
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_nil
|
32
|
+
json = %{null}
|
33
|
+
Oj::Doc.open(json) do |doc|
|
34
|
+
assert_equal(NilClass, doc.type)
|
35
|
+
assert_nil(doc.fetch())
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_true
|
40
|
+
json = %{true}
|
41
|
+
Oj::Doc.open(json) do |doc|
|
42
|
+
assert_equal(TrueClass, doc.type)
|
43
|
+
assert_equal(true, doc.fetch())
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_false
|
48
|
+
json = %{false}
|
49
|
+
Oj::Doc.open(json) do |doc|
|
50
|
+
assert_equal(FalseClass, doc.type)
|
51
|
+
assert_equal(false, doc.fetch())
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_string
|
56
|
+
json = %{"a string"}
|
57
|
+
Oj::Doc.open(json) do |doc|
|
58
|
+
assert_equal(String, doc.type)
|
59
|
+
assert_equal('a string', doc.fetch())
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_encoding
|
64
|
+
json = %{"ぴーたー"}
|
65
|
+
Oj::Doc.open(json) do |doc|
|
66
|
+
assert_equal(String, doc.type)
|
67
|
+
assert_equal("ぴーたー", doc.fetch())
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def test_encoding_escaped
|
72
|
+
json = %{"\\u3074\\u30fc\\u305f\\u30fc"}
|
73
|
+
Oj::Doc.open(json) do |doc|
|
74
|
+
assert_equal(String, doc.type)
|
75
|
+
assert_equal("ぴーたー", doc.fetch())
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def test_fixnum
|
80
|
+
json = %{12345}
|
81
|
+
Oj::Doc.open(json) do |doc|
|
82
|
+
if '2.4.0' <= RUBY_VERSION
|
83
|
+
assert_equal(Integer, doc.type)
|
84
|
+
else
|
85
|
+
assert_equal(Fixnum, doc.type)
|
86
|
+
end
|
87
|
+
assert_equal(12345, doc.fetch())
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
def test_float
|
92
|
+
json = %{12345.6789}
|
93
|
+
Oj::Doc.open(json) do |doc|
|
94
|
+
assert_equal(Float, doc.type)
|
95
|
+
assert_equal(12345.6789, doc.fetch())
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
def test_float_exp
|
100
|
+
json = %{12345.6789e7}
|
101
|
+
Oj::Doc.open(json) do |doc|
|
102
|
+
assert_equal(Float, doc.type)
|
103
|
+
#assert_equal(12345.6789e7, doc.fetch())
|
104
|
+
assert_equal(12345.6789e7.to_i, doc.fetch().to_i)
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
def test_array_empty
|
109
|
+
json = %{[]}
|
110
|
+
Oj::Doc.open(json) do |doc|
|
111
|
+
assert_equal(Array, doc.type)
|
112
|
+
assert_equal([], doc.fetch())
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
def test_array
|
117
|
+
json = %{[true,false]}
|
118
|
+
Oj::Doc.open(json) do |doc|
|
119
|
+
assert_equal(Array, doc.type)
|
120
|
+
assert_equal([true, false], doc.fetch())
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
def test_hash_empty
|
125
|
+
json = %{{}}
|
126
|
+
Oj::Doc.open(json) do |doc|
|
127
|
+
assert_equal(Hash, doc.type)
|
128
|
+
assert_equal({}, doc.fetch())
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
def test_hash
|
133
|
+
json = %{{"one":true,"two":false}}
|
134
|
+
Oj::Doc.open(json) do |doc|
|
135
|
+
assert_equal(Hash, doc.type)
|
136
|
+
assert_equal({'one' => true, 'two' => false}, doc.fetch())
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
# move() and where?()
|
141
|
+
def test_move_hash
|
142
|
+
json = %{{"one":{"two":false}}}
|
143
|
+
Oj::Doc.open(json) do |doc|
|
144
|
+
doc.move('/one')
|
145
|
+
assert_equal('/one', doc.where?)
|
146
|
+
doc.move('/one/two')
|
147
|
+
assert_equal('/one/two', doc.where?)
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
def test_move_array
|
152
|
+
json = %{[1,[2,true]]}
|
153
|
+
Oj::Doc.open(json) do |doc|
|
154
|
+
doc.move('/1')
|
155
|
+
assert_equal('/1', doc.where?)
|
156
|
+
doc.move('/2/1')
|
157
|
+
assert_equal('/2/1', doc.where?)
|
158
|
+
end
|
159
|
+
end
|
160
|
+
|
161
|
+
def test_move
|
162
|
+
Oj::Doc.open(@json1) do |doc|
|
163
|
+
[ '/',
|
164
|
+
'/array',
|
165
|
+
'/boolean',
|
166
|
+
'/array/1/hash/h2/a/3',
|
167
|
+
].each do |p|
|
168
|
+
doc.move(p)
|
169
|
+
assert_equal(p, doc.where?)
|
170
|
+
end
|
171
|
+
begin
|
172
|
+
doc.move('/array/x')
|
173
|
+
rescue Exception
|
174
|
+
assert_equal('/', doc.where?)
|
175
|
+
assert(true)
|
176
|
+
end
|
177
|
+
end
|
178
|
+
end
|
179
|
+
|
180
|
+
def test_move_slash
|
181
|
+
Oj::Doc.open(%|{"top":{"a/b":3}}|) do |doc|
|
182
|
+
doc.move('top/a\/b')
|
183
|
+
assert_equal('/top/a\/b', doc.where?)
|
184
|
+
end
|
185
|
+
end
|
186
|
+
|
187
|
+
def test_fetch_slash
|
188
|
+
Oj::Doc.open(%|{"a/b":3}|) do |doc|
|
189
|
+
x = doc.fetch('a\/b')
|
190
|
+
assert_equal(3, x)
|
191
|
+
end
|
192
|
+
end
|
193
|
+
|
194
|
+
def test_move_relative
|
195
|
+
Oj::Doc.open(@json1) do |doc|
|
196
|
+
[['/', 'array', '/array'],
|
197
|
+
['/array', '1/num', '/array/1/num'],
|
198
|
+
['/array/1/hash', 'h2/a', '/array/1/hash/h2/a'],
|
199
|
+
['/array/1', 'hash/h2/a/2', '/array/1/hash/h2/a/2'],
|
200
|
+
['/array/1/hash', '../string', '/array/1/string'],
|
201
|
+
['/array/1/hash', '..', '/array/1'],
|
202
|
+
].each do |start,path,where|
|
203
|
+
doc.move(start)
|
204
|
+
doc.move(path)
|
205
|
+
assert_equal(where, doc.where?)
|
206
|
+
end
|
207
|
+
end
|
208
|
+
end
|
209
|
+
|
210
|
+
def test_type
|
211
|
+
if '2.4.0' <= RUBY_VERSION
|
212
|
+
num_class = Integer
|
213
|
+
else
|
214
|
+
num_class = Fixnum
|
215
|
+
end
|
216
|
+
Oj::Doc.open(@json1) do |doc|
|
217
|
+
[['/', Hash],
|
218
|
+
['/array', Array],
|
219
|
+
['/array/1', Hash],
|
220
|
+
['/array/1/num', num_class],
|
221
|
+
['/array/1/string', String],
|
222
|
+
['/array/1/hash/h2/a', Array],
|
223
|
+
['/array/1/hash/../num', num_class],
|
224
|
+
['/array/1/hash/../..', Array],
|
225
|
+
].each do |path,type|
|
226
|
+
assert_equal(type, doc.type(path))
|
227
|
+
end
|
228
|
+
end
|
229
|
+
end
|
230
|
+
|
231
|
+
def test_local_key
|
232
|
+
Oj::Doc.open(@json1) do |doc|
|
233
|
+
[['/', nil],
|
234
|
+
['/array', 'array'],
|
235
|
+
['/array/1', 1],
|
236
|
+
['/array/1/num', 'num'],
|
237
|
+
['/array/1/string', 'string'],
|
238
|
+
['/array/1/hash/h2/a', 'a'],
|
239
|
+
['/array/1/hash/../num', 'num'],
|
240
|
+
['/array/1/hash/..', 1],
|
241
|
+
['/array/1/hash/../..', 'array'],
|
242
|
+
].each do |path,key|
|
243
|
+
doc.move(path)
|
244
|
+
if key.nil?
|
245
|
+
assert_nil(doc.local_key())
|
246
|
+
else
|
247
|
+
assert_equal(key, doc.local_key())
|
248
|
+
end
|
249
|
+
end
|
250
|
+
end
|
251
|
+
end
|
252
|
+
|
253
|
+
def test_fetch_move
|
254
|
+
Oj::Doc.open(@json1) do |doc|
|
255
|
+
[['/array/1/num', 3],
|
256
|
+
['/array/1/string', 'message'],
|
257
|
+
['/array/1/hash/h2/a', [1, 2, 3]],
|
258
|
+
['/array/1/hash/../num', 3],
|
259
|
+
['/array/1/hash/..', {'num' => 3, 'string' => 'message', 'hash' => {'h2' => {'a' => [1, 2, 3]}}}],
|
260
|
+
['/array/1/hash/../..', [{'num' => 3, 'string' => 'message', 'hash' => {'h2' => {'a' => [1, 2, 3]}}}]],
|
261
|
+
['/array/1', {'num' => 3, 'string' => 'message', 'hash' => {'h2' => {'a' => [1, 2, 3]}}}],
|
262
|
+
['/array', [{'num' => 3, 'string' => 'message', 'hash' => {'h2' => {'a' => [1, 2, 3]}}}]],
|
263
|
+
['/', {'array' => [{'num' => 3, 'string' => 'message', 'hash' => {'h2' => {'a' => [1, 2, 3]}}}], 'boolean' => true}],
|
264
|
+
].each do |path,val|
|
265
|
+
doc.move(path)
|
266
|
+
assert_equal(val, doc.fetch())
|
267
|
+
end
|
268
|
+
end
|
269
|
+
end
|
270
|
+
|
271
|
+
def test_fetch_path
|
272
|
+
Oj::Doc.open(@json1) do |doc|
|
273
|
+
[['/array/1/num', 3],
|
274
|
+
['/array/1/string', 'message'],
|
275
|
+
['/array/1/hash/h2/a', [1, 2, 3]],
|
276
|
+
['/array/1/hash/../num', 3],
|
277
|
+
['/array/1/hash/..', {'num' => 3, 'string' => 'message', 'hash' => {'h2' => {'a' => [1, 2, 3]}}}],
|
278
|
+
['/array/1/hash/../..', [{'num' => 3, 'string' => 'message', 'hash' => {'h2' => {'a' => [1, 2, 3]}}}]],
|
279
|
+
['/array/1', {'num' => 3, 'string' => 'message', 'hash' => {'h2' => {'a' => [1, 2, 3]}}}],
|
280
|
+
['/array', [{'num' => 3, 'string' => 'message', 'hash' => {'h2' => {'a' => [1, 2, 3]}}}]],
|
281
|
+
['/', {'array' => [{'num' => 3, 'string' => 'message', 'hash' => {'h2' => {'a' => [1, 2, 3]}}}], 'boolean' => true}],
|
282
|
+
].each do |path,val|
|
283
|
+
assert_equal(val, doc.fetch(path))
|
284
|
+
end
|
285
|
+
end
|
286
|
+
end
|
287
|
+
|
288
|
+
def test_move_fetch_path
|
289
|
+
Oj::Doc.open(@json1) do |doc|
|
290
|
+
[['/array/1', 'num', 3],
|
291
|
+
['/array/1', 'string', 'message'],
|
292
|
+
['/array/1/hash', 'h2/a', [1, 2, 3]],
|
293
|
+
].each do |path,fetch_path,val|
|
294
|
+
doc.move(path)
|
295
|
+
assert_equal(val, doc.fetch(fetch_path))
|
296
|
+
end
|
297
|
+
end
|
298
|
+
end
|
299
|
+
|
300
|
+
def test_home
|
301
|
+
Oj::Doc.open(@json1) do |doc|
|
302
|
+
doc.move('/array/1/num')
|
303
|
+
doc.home()
|
304
|
+
assert_equal('/', doc.where?)
|
305
|
+
end
|
306
|
+
end
|
307
|
+
|
308
|
+
def test_each_value_root
|
309
|
+
Oj::Doc.open(@json1) do |doc|
|
310
|
+
values = []
|
311
|
+
doc.each_value() { |v| values << v.to_s }
|
312
|
+
assert_equal(['1', '2', '3', '3', 'message', 'true'], values.sort)
|
313
|
+
end
|
314
|
+
end
|
315
|
+
|
316
|
+
def test_each_value_move
|
317
|
+
Oj::Doc.open(@json1) do |doc|
|
318
|
+
doc.move('/array/1/hash')
|
319
|
+
values = []
|
320
|
+
doc.each_value() { |v| values << v.to_s }
|
321
|
+
assert_equal(['1', '2', '3'], values.sort)
|
322
|
+
end
|
323
|
+
end
|
324
|
+
|
325
|
+
def test_each_value_path
|
326
|
+
Oj::Doc.open(@json1) do |doc|
|
327
|
+
values = []
|
328
|
+
doc.each_value('/array/1/hash') { |v| values << v.to_s }
|
329
|
+
assert_equal(['1', '2', '3'], values.sort)
|
330
|
+
end
|
331
|
+
end
|
332
|
+
|
333
|
+
def test_each_child_move
|
334
|
+
Oj::Doc.open(@json1) do |doc|
|
335
|
+
locations = []
|
336
|
+
doc.move('/array/1/hash/h2/a')
|
337
|
+
doc.each_child() { |d| locations << d.where? }
|
338
|
+
assert_equal(['/array/1/hash/h2/a/1', '/array/1/hash/h2/a/2', '/array/1/hash/h2/a/3'], locations)
|
339
|
+
locations = []
|
340
|
+
doc.move('/array/1')
|
341
|
+
doc.each_child() { |d| locations << d.where? }
|
342
|
+
assert_equal(['/array/1/num', '/array/1/string', '/array/1/hash'], locations)
|
343
|
+
end
|
344
|
+
end
|
345
|
+
|
346
|
+
def test_each_child_path
|
347
|
+
Oj::Doc.open(@json1) do |doc|
|
348
|
+
locations = []
|
349
|
+
doc.each_child('/array/1/hash/h2/a') { |d| locations << d.where? }
|
350
|
+
assert_equal(['/array/1/hash/h2/a/1', '/array/1/hash/h2/a/2', '/array/1/hash/h2/a/3'], locations)
|
351
|
+
locations = []
|
352
|
+
doc.each_child('/array/1') { |d| locations << d.where? }
|
353
|
+
assert_equal(['/array/1/num', '/array/1/string', '/array/1/hash'], locations)
|
354
|
+
end
|
355
|
+
end
|
356
|
+
|
357
|
+
def test_size
|
358
|
+
Oj::Doc.open('[1,2,3]') do |doc|
|
359
|
+
assert_equal(4, doc.size)
|
360
|
+
end
|
361
|
+
Oj::Doc.open('{"a":[1,2,3]}') do |doc|
|
362
|
+
assert_equal(5, doc.size)
|
363
|
+
end
|
364
|
+
end
|
365
|
+
|
366
|
+
def test_open_file
|
367
|
+
filename = File.join(File.dirname(__FILE__), 'open_file_test.json')
|
368
|
+
File.open(filename, 'w') { |f| f.write('{"a":[1,2,3]}') }
|
369
|
+
Oj::Doc.open_file(filename) do |doc|
|
370
|
+
assert_equal(5, doc.size)
|
371
|
+
end
|
372
|
+
end
|
373
|
+
|
374
|
+
def test_open_close
|
375
|
+
json = %{{"a":[1,2,3]}}
|
376
|
+
doc = Oj::Doc.open(json)
|
377
|
+
assert_equal(Oj::Doc, doc.class)
|
378
|
+
assert_equal(5, doc.size)
|
379
|
+
assert_equal('/', doc.where?)
|
380
|
+
doc.move('a/1')
|
381
|
+
doc.home()
|
382
|
+
assert_equal(2, doc.fetch('/a/2'))
|
383
|
+
assert_equal(2, doc.fetch('a/2'))
|
384
|
+
doc.close()
|
385
|
+
begin
|
386
|
+
doc.home()
|
387
|
+
rescue Exception
|
388
|
+
assert(true)
|
389
|
+
end
|
390
|
+
end
|
391
|
+
|
392
|
+
def test_file_open_close
|
393
|
+
filename = File.join(File.dirname(__FILE__), 'open_file_test.json')
|
394
|
+
File.open(filename, 'w') { |f| f.write('{"a":[1,2,3]}') }
|
395
|
+
doc = Oj::Doc.open_file(filename)
|
396
|
+
assert_equal(Oj::Doc, doc.class)
|
397
|
+
assert_equal(5, doc.size)
|
398
|
+
assert_equal('/', doc.where?)
|
399
|
+
doc.move('a/1')
|
400
|
+
doc.home()
|
401
|
+
assert_equal(2, doc.fetch('/a/2'))
|
402
|
+
assert_equal(2, doc.fetch('a/2'))
|
403
|
+
doc.close()
|
404
|
+
begin
|
405
|
+
doc.home()
|
406
|
+
rescue Exception
|
407
|
+
assert(true)
|
408
|
+
end
|
409
|
+
end
|
410
|
+
|
411
|
+
def test_open_no_close
|
412
|
+
json = %{{"a":[1,2,3]}}
|
413
|
+
doc = Oj::Doc.open(json)
|
414
|
+
assert_equal(Oj::Doc, doc.class)
|
415
|
+
assert_equal(5, doc.size)
|
416
|
+
assert_equal('/', doc.where?)
|
417
|
+
doc.move('a/1')
|
418
|
+
doc.home()
|
419
|
+
assert_equal(2, doc.fetch('/a/2'))
|
420
|
+
assert_equal(2, doc.fetch('a/2'))
|
421
|
+
doc = nil
|
422
|
+
GC.start
|
423
|
+
# a print statement confirms close is called
|
424
|
+
end
|
425
|
+
|
426
|
+
def test_dump
|
427
|
+
Oj::Doc.open('[1,[2,3]]') do |doc|
|
428
|
+
assert_equal('[1,[2,3]]', doc.dump())
|
429
|
+
end
|
430
|
+
Oj::Doc.open('[1,[2,3]]') do |doc|
|
431
|
+
assert_equal('[2,3]', doc.dump('/2'))
|
432
|
+
end
|
433
|
+
end
|
434
|
+
|
435
|
+
def test_each_leaf
|
436
|
+
results = Oj::Doc.open('[1,[2,3]]') do |doc|
|
437
|
+
h = {}
|
438
|
+
doc.each_leaf() { |d| h[d.where?] = d.fetch() }
|
439
|
+
h
|
440
|
+
end
|
441
|
+
assert_equal({'/1' => 1, '/2/1' => 2, '/2/2' => 3}, results)
|
442
|
+
end
|
443
|
+
|
444
|
+
def test_each_leaf_hash
|
445
|
+
results = Oj::Doc.open('{"a":{"x":2},"b":{"y":4}}') do |doc|
|
446
|
+
h = {}
|
447
|
+
doc.each_leaf() { |d| h[d.where?] = d.fetch() }
|
448
|
+
h
|
449
|
+
end
|
450
|
+
assert_equal({'/a/x' => 2, '/b/y' => 4}, results)
|
451
|
+
end
|
452
|
+
|
453
|
+
def test_comment
|
454
|
+
json = %{{
|
455
|
+
"x"/*one*/:/*two*/true,//three
|
456
|
+
"y":58/*four*/,
|
457
|
+
"z": [1,2/*five*/,
|
458
|
+
3 // six
|
459
|
+
]
|
460
|
+
}
|
461
|
+
}
|
462
|
+
results = Oj::Doc.open(json) do |doc|
|
463
|
+
h = {}
|
464
|
+
doc.each_leaf() { |d| h[d.where?] = d.fetch() }
|
465
|
+
h
|
466
|
+
end
|
467
|
+
assert_equal({'/x' => true, '/y' => 58, '/z/1' => 1, '/z/2' => 2, '/z/3' => 3}, results)
|
468
|
+
end
|
469
|
+
|
470
|
+
end # DocTest
|