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_writer.rb
ADDED
@@ -0,0 +1,308 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# encoding: UTF-8
|
3
|
+
|
4
|
+
$: << File.dirname(__FILE__)
|
5
|
+
|
6
|
+
require 'helper'
|
7
|
+
|
8
|
+
class OjWriter < Minitest::Test
|
9
|
+
|
10
|
+
def setup
|
11
|
+
@default_options = Oj.default_options
|
12
|
+
end
|
13
|
+
|
14
|
+
def teardown
|
15
|
+
Oj.default_options = @default_options
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_string_writer_empty_array
|
19
|
+
w = Oj::StringWriter.new(:indent => 0)
|
20
|
+
w.push_array()
|
21
|
+
w.pop()
|
22
|
+
assert_equal("[]\n", w.to_s)
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_string_writer_nested_array
|
26
|
+
w = Oj::StringWriter.new(:indent => 0)
|
27
|
+
w.push_array()
|
28
|
+
w.push_array()
|
29
|
+
w.pop()
|
30
|
+
w.push_array()
|
31
|
+
w.push_array()
|
32
|
+
w.pop()
|
33
|
+
w.pop()
|
34
|
+
w.push_array()
|
35
|
+
w.pop()
|
36
|
+
w.pop()
|
37
|
+
assert_equal("[[],[[]],[]]\n", w.to_s)
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_string_writer_empty_object
|
41
|
+
w = Oj::StringWriter.new(:indent => 0)
|
42
|
+
w.push_object()
|
43
|
+
w.pop()
|
44
|
+
assert_equal("{}\n", w.to_s)
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_string_writer_push_null_value_with_key
|
48
|
+
w = Oj::StringWriter.new(:indent => 0, :mode => :compat)
|
49
|
+
w.push_object()
|
50
|
+
w.push_value(nil, 'nothing')
|
51
|
+
w.pop()
|
52
|
+
assert_equal(%|{"nothing":null}\n|, w.to_s)
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_string_writer_nested_object
|
56
|
+
w = Oj::StringWriter.new(:indent => 0)
|
57
|
+
w.push_object()
|
58
|
+
w.push_object("a1")
|
59
|
+
w.pop()
|
60
|
+
w.push_object("a2")
|
61
|
+
w.push_object("b")
|
62
|
+
w.pop()
|
63
|
+
w.pop()
|
64
|
+
w.push_object("a3")
|
65
|
+
w.pop()
|
66
|
+
w.pop()
|
67
|
+
assert_equal(%|{"a1":{},"a2":{"b":{}},"a3":{}}\n|, w.to_s)
|
68
|
+
end
|
69
|
+
|
70
|
+
def test_string_writer_nested_key
|
71
|
+
w = Oj::StringWriter.new(:indent => 0)
|
72
|
+
w.push_object()
|
73
|
+
w.push_key('a1')
|
74
|
+
w.push_object()
|
75
|
+
w.pop()
|
76
|
+
w.push_key('a2')
|
77
|
+
w.push_object('x')
|
78
|
+
w.push_object('b')
|
79
|
+
w.pop()
|
80
|
+
w.pop()
|
81
|
+
w.push_key('a3')
|
82
|
+
w.push_object()
|
83
|
+
w.push_key('a4')
|
84
|
+
w.push_value(37)
|
85
|
+
w.pop()
|
86
|
+
w.pop()
|
87
|
+
assert_equal(%|{"a1":{},"a2":{"b":{}},"a3":{"a4":37}}\n|, w.to_s)
|
88
|
+
end
|
89
|
+
|
90
|
+
def test_string_writer_value_array
|
91
|
+
w = Oj::StringWriter.new(:indent => 2)
|
92
|
+
w.push_array()
|
93
|
+
w.push_value(7)
|
94
|
+
w.push_value(7.3)
|
95
|
+
w.push_value(true)
|
96
|
+
w.push_value(nil)
|
97
|
+
w.push_value("a string")
|
98
|
+
w.push_value({'a' => 65})
|
99
|
+
w.push_value([1,2])
|
100
|
+
w.pop()
|
101
|
+
assert_equal(%|[
|
102
|
+
7,
|
103
|
+
7.3,
|
104
|
+
true,
|
105
|
+
null,
|
106
|
+
"a string",
|
107
|
+
{
|
108
|
+
"a":65
|
109
|
+
},
|
110
|
+
[
|
111
|
+
1,
|
112
|
+
2
|
113
|
+
]
|
114
|
+
]
|
115
|
+
|, w.to_s)
|
116
|
+
end
|
117
|
+
|
118
|
+
def test_string_writer_block
|
119
|
+
w = Oj::StringWriter.new(:indent => 0)
|
120
|
+
w.push_object() {
|
121
|
+
w.push_object("a1") {
|
122
|
+
w.push_value(7, 'a7')
|
123
|
+
}
|
124
|
+
w.push_array("a2") {
|
125
|
+
w.push_value('x')
|
126
|
+
w.push_value(3)
|
127
|
+
}
|
128
|
+
}
|
129
|
+
assert_equal(%|{"a1":{"a7":7},"a2":["x",3]}\n|, w.to_s)
|
130
|
+
end
|
131
|
+
|
132
|
+
def test_string_writer_json
|
133
|
+
w = Oj::StringWriter.new(:indent => 0)
|
134
|
+
w.push_array()
|
135
|
+
w.push_json('7')
|
136
|
+
w.push_json('true')
|
137
|
+
w.push_json(%|"a string"|)
|
138
|
+
w.push_object()
|
139
|
+
w.push_json('{"a":65}', 'x')
|
140
|
+
w.pop()
|
141
|
+
w.pop()
|
142
|
+
assert_equal(%|[7,true,"a string",{"x":{"a":65}}]\n|, w.to_s)
|
143
|
+
end
|
144
|
+
|
145
|
+
def test_string_writer_pop_excess
|
146
|
+
w = Oj::StringWriter.new(:indent => 0)
|
147
|
+
begin
|
148
|
+
w.push_object() {
|
149
|
+
w.push_key('key')
|
150
|
+
}
|
151
|
+
rescue Exception
|
152
|
+
assert(true)
|
153
|
+
return
|
154
|
+
end
|
155
|
+
assert(false, "*** expected an exception")
|
156
|
+
end
|
157
|
+
|
158
|
+
def test_string_writer_array_key
|
159
|
+
w = Oj::StringWriter.new(:indent => 0)
|
160
|
+
begin
|
161
|
+
w.push_array() {
|
162
|
+
w.push_key('key')
|
163
|
+
w.push_value(7)
|
164
|
+
}
|
165
|
+
rescue Exception
|
166
|
+
assert(true)
|
167
|
+
return
|
168
|
+
end
|
169
|
+
assert(false, "*** expected an exception")
|
170
|
+
end
|
171
|
+
|
172
|
+
def test_string_writer_pop_with_key
|
173
|
+
w = Oj::StringWriter.new(:indent => 0)
|
174
|
+
begin
|
175
|
+
w.pop()
|
176
|
+
rescue Exception
|
177
|
+
assert(true)
|
178
|
+
return
|
179
|
+
end
|
180
|
+
assert(false, "*** expected an exception")
|
181
|
+
end
|
182
|
+
|
183
|
+
def test_string_writer_obj_no_key
|
184
|
+
w = Oj::StringWriter.new(:indent => 0)
|
185
|
+
w.push_object()
|
186
|
+
begin
|
187
|
+
w.push_value(59)
|
188
|
+
rescue Exception
|
189
|
+
assert(true)
|
190
|
+
return
|
191
|
+
end
|
192
|
+
assert(false, "*** expected an exception")
|
193
|
+
end
|
194
|
+
|
195
|
+
def test_string_writer_deep
|
196
|
+
cnt = 1000
|
197
|
+
w = Oj::StringWriter.new(:indent => 0)
|
198
|
+
cnt.times do
|
199
|
+
w.push_array()
|
200
|
+
end
|
201
|
+
cnt.times do
|
202
|
+
w.pop()
|
203
|
+
end
|
204
|
+
# if no exception then passed
|
205
|
+
assert(true)
|
206
|
+
end
|
207
|
+
|
208
|
+
def test_string_writer_pop_all
|
209
|
+
w = Oj::StringWriter.new(:indent => 0)
|
210
|
+
w.push_object()
|
211
|
+
w.push_object("a1")
|
212
|
+
w.pop()
|
213
|
+
w.push_array("a2")
|
214
|
+
w.push_value(3)
|
215
|
+
w.push_array()
|
216
|
+
w.pop_all()
|
217
|
+
assert_equal(%|{"a1":{},"a2":[3,[]]}\n|, w.to_s)
|
218
|
+
end
|
219
|
+
|
220
|
+
def test_string_writer_reset
|
221
|
+
w = Oj::StringWriter.new(:indent => 0)
|
222
|
+
w.push_array()
|
223
|
+
w.pop()
|
224
|
+
w.reset()
|
225
|
+
assert_equal('', w.to_s)
|
226
|
+
end
|
227
|
+
|
228
|
+
# Stream Writer
|
229
|
+
|
230
|
+
def test_stream_writer_empty_array
|
231
|
+
output = StringIO.open("", "w+")
|
232
|
+
w = Oj::StreamWriter.new(output, :indent => 0)
|
233
|
+
w.push_array()
|
234
|
+
w.pop()
|
235
|
+
assert_equal("[]\n", output.string())
|
236
|
+
end
|
237
|
+
|
238
|
+
def test_stream_writer_mixed_stringio
|
239
|
+
output = StringIO.open("", "w+")
|
240
|
+
w = Oj::StreamWriter.new(output, :indent => 0)
|
241
|
+
w.push_object()
|
242
|
+
w.push_object("a1")
|
243
|
+
w.pop()
|
244
|
+
w.push_object("a2")
|
245
|
+
w.push_array("b")
|
246
|
+
w.push_value(7)
|
247
|
+
w.push_value(true)
|
248
|
+
w.push_value("string")
|
249
|
+
w.pop()
|
250
|
+
w.pop()
|
251
|
+
w.push_object("a3")
|
252
|
+
w.pop()
|
253
|
+
w.pop()
|
254
|
+
assert_equal(%|{"a1":{},"a2":{"b":[7,true,"string"]},"a3":{}}\n|, output.string())
|
255
|
+
end
|
256
|
+
|
257
|
+
def test_stream_writer_mixed_file
|
258
|
+
filename = File.join(File.dirname(__FILE__), 'open_file_test.json')
|
259
|
+
File.open(filename, "w") do |f|
|
260
|
+
w = Oj::StreamWriter.new(f, :indent => 0)
|
261
|
+
w.push_object()
|
262
|
+
w.push_object("a1")
|
263
|
+
w.pop()
|
264
|
+
w.push_object("a2")
|
265
|
+
w.push_array("b")
|
266
|
+
w.push_value(7)
|
267
|
+
w.push_value(true)
|
268
|
+
w.push_value("string")
|
269
|
+
w.pop()
|
270
|
+
w.pop()
|
271
|
+
w.push_object("a3")
|
272
|
+
w.pop()
|
273
|
+
w.pop()
|
274
|
+
end
|
275
|
+
content = File.read(filename)
|
276
|
+
assert_equal(%|{"a1":{},"a2":{"b":[7,true,"string"]},"a3":{}}\n|, content)
|
277
|
+
end
|
278
|
+
|
279
|
+
def test_stream_writer_nested_key_object
|
280
|
+
output = StringIO.open("", "w+")
|
281
|
+
w = Oj::StreamWriter.new(output, :indent => 0)
|
282
|
+
w.push_object()
|
283
|
+
w.push_key('a1')
|
284
|
+
w.push_object()
|
285
|
+
w.pop()
|
286
|
+
w.push_key('a2')
|
287
|
+
w.push_object('x')
|
288
|
+
w.push_object('b')
|
289
|
+
w.pop()
|
290
|
+
w.pop()
|
291
|
+
w.push_key('a3')
|
292
|
+
w.push_object()
|
293
|
+
w.push_key('a4')
|
294
|
+
w.push_value(37)
|
295
|
+
w.pop()
|
296
|
+
w.pop()
|
297
|
+
assert_equal(%|{"a1":{},"a2":{"b":{}},"a3":{"a4":37}}\n|, output.string())
|
298
|
+
end
|
299
|
+
|
300
|
+
def test_stream_writer_push_null_value_with_key
|
301
|
+
output = StringIO.open("", "w+")
|
302
|
+
w = Oj::StreamWriter.new(output, :indent => 0)
|
303
|
+
w.push_object()
|
304
|
+
w.push_value(nil, 'nothing')
|
305
|
+
w.pop()
|
306
|
+
assert_equal(%|{"nothing":null}\n|, output.string())
|
307
|
+
end
|
308
|
+
end # OjWriter
|