oj 2.18.3 → 3.13.14
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/CHANGELOG.md +1324 -0
- data/README.md +51 -204
- data/RELEASE_NOTES.md +61 -0
- data/ext/oj/buf.h +49 -72
- data/ext/oj/cache.c +326 -0
- data/ext/oj/cache.h +21 -0
- data/ext/oj/cache8.c +61 -64
- data/ext/oj/cache8.h +12 -39
- data/ext/oj/circarray.c +37 -68
- data/ext/oj/circarray.h +16 -42
- data/ext/oj/code.c +221 -0
- data/ext/oj/code.h +40 -0
- data/ext/oj/compat.c +231 -107
- data/ext/oj/custom.c +1125 -0
- data/ext/oj/debug.c +132 -0
- data/ext/oj/dump.c +935 -2513
- data/ext/oj/dump.h +108 -0
- data/ext/oj/dump_compat.c +936 -0
- data/ext/oj/dump_leaf.c +164 -0
- data/ext/oj/dump_object.c +761 -0
- data/ext/oj/dump_strict.c +410 -0
- data/ext/oj/encode.h +7 -42
- data/ext/oj/encoder.c +43 -0
- data/ext/oj/err.c +40 -54
- data/ext/oj/err.h +52 -46
- data/ext/oj/extconf.rb +21 -30
- data/ext/oj/fast.c +1097 -1080
- data/ext/oj/intern.c +301 -0
- data/ext/oj/intern.h +26 -0
- data/ext/oj/mimic_json.c +893 -0
- data/ext/oj/object.c +549 -620
- data/ext/oj/odd.c +155 -167
- data/ext/oj/odd.h +37 -63
- data/ext/oj/oj.c +1661 -2063
- data/ext/oj/oj.h +341 -270
- data/ext/oj/parse.c +974 -737
- data/ext/oj/parse.h +105 -97
- data/ext/oj/parser.c +1526 -0
- data/ext/oj/parser.h +90 -0
- data/ext/oj/rails.c +1504 -0
- data/ext/oj/rails.h +18 -0
- data/ext/oj/reader.c +141 -163
- data/ext/oj/reader.h +75 -113
- data/ext/oj/resolve.c +45 -93
- data/ext/oj/resolve.h +7 -34
- data/ext/oj/rxclass.c +143 -0
- data/ext/oj/rxclass.h +26 -0
- data/ext/oj/saj.c +447 -511
- data/ext/oj/saj2.c +348 -0
- data/ext/oj/scp.c +91 -138
- data/ext/oj/sparse.c +793 -644
- data/ext/oj/stream_writer.c +331 -0
- data/ext/oj/strict.c +145 -109
- data/ext/oj/string_writer.c +493 -0
- data/ext/oj/trace.c +72 -0
- data/ext/oj/trace.h +28 -0
- data/ext/oj/usual.c +1254 -0
- data/ext/oj/util.c +136 -0
- data/ext/oj/util.h +20 -0
- data/ext/oj/val_stack.c +62 -70
- data/ext/oj/val_stack.h +95 -129
- data/ext/oj/validate.c +51 -0
- data/ext/oj/wab.c +622 -0
- data/lib/oj/bag.rb +1 -0
- data/lib/oj/easy_hash.rb +17 -8
- data/lib/oj/error.rb +10 -11
- data/lib/oj/json.rb +176 -0
- data/lib/oj/mimic.rb +158 -19
- data/lib/oj/state.rb +132 -0
- data/lib/oj/version.rb +2 -2
- data/lib/oj.rb +1 -31
- 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 +94 -0
- data/pages/Modes.md +161 -0
- data/pages/Options.md +327 -0
- data/pages/Parser.md +309 -0
- data/pages/Rails.md +167 -0
- data/pages/Security.md +20 -0
- data/pages/WAB.md +13 -0
- data/test/activerecord/result_test.rb +32 -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/abstract_unit.rb +45 -0
- data/test/activesupport5/decoding_test.rb +133 -0
- data/test/activesupport5/encoding_test.rb +500 -0
- data/test/activesupport5/encoding_test_cases.rb +98 -0
- data/test/activesupport5/test_helper.rb +72 -0
- data/test/activesupport5/time_zone_test_helpers.rb +39 -0
- data/test/activesupport6/abstract_unit.rb +44 -0
- data/test/activesupport6/decoding_test.rb +133 -0
- data/test/activesupport6/encoding_test.rb +507 -0
- data/test/activesupport6/encoding_test_cases.rb +98 -0
- data/test/activesupport6/test_common.rb +17 -0
- data/test/activesupport6/test_helper.rb +163 -0
- data/test/activesupport6/time_zone_test_helpers.rb +39 -0
- data/test/activesupport7/abstract_unit.rb +49 -0
- data/test/activesupport7/decoding_test.rb +125 -0
- data/test/activesupport7/encoding_test.rb +486 -0
- data/test/activesupport7/encoding_test_cases.rb +104 -0
- data/test/activesupport7/time_zone_test_helpers.rb +47 -0
- data/test/bar.rb +9 -0
- data/test/baz.rb +16 -0
- data/test/bug.rb +11 -46
- data/test/foo.rb +69 -16
- data/test/helper.rb +10 -1
- data/test/isolated/shared.rb +12 -8
- data/test/isolated/test_mimic_rails_after.rb +3 -3
- data/test/isolated/test_mimic_rails_before.rb +3 -3
- data/test/json_gem/json_addition_test.rb +216 -0
- data/test/json_gem/json_common_interface_test.rb +153 -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 +397 -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 +26 -0
- data/test/mem.rb +33 -0
- data/test/perf.rb +1 -1
- data/test/perf_compat.rb +30 -28
- data/test/perf_dump.rb +50 -0
- data/test/perf_object.rb +1 -1
- data/test/perf_once.rb +58 -0
- data/test/perf_parser.rb +189 -0
- data/test/perf_scp.rb +11 -10
- data/test/perf_strict.rb +30 -19
- data/test/perf_wab.rb +131 -0
- data/test/prec.rb +23 -0
- data/test/sample.rb +0 -1
- data/test/sample_json.rb +1 -1
- data/test/test_compat.rb +219 -102
- data/test/test_custom.rb +533 -0
- data/test/test_fast.rb +107 -35
- data/test/test_file.rb +19 -25
- data/test/test_generate.rb +21 -0
- data/test/test_hash.rb +11 -1
- data/test/test_integer_range.rb +72 -0
- data/test/test_null.rb +376 -0
- data/test/test_object.rb +357 -70
- data/test/test_parser.rb +27 -0
- data/test/test_parser_saj.rb +245 -0
- data/test/test_parser_usual.rb +217 -0
- data/test/test_rails.rb +35 -0
- data/test/test_saj.rb +1 -1
- data/test/test_scp.rb +39 -2
- data/test/test_strict.rb +186 -7
- data/test/test_various.rb +160 -774
- data/test/test_wab.rb +307 -0
- data/test/test_writer.rb +90 -2
- 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 +194 -56
- data/ext/oj/hash.c +0 -163
- data/ext/oj/hash.h +0 -46
- data/ext/oj/hash_test.c +0 -512
- data/test/activesupport_datetime_test.rb +0 -23
- data/test/bug2.rb +0 -10
- data/test/bug3.rb +0 -46
- data/test/bug_fast.rb +0 -32
- data/test/bug_load.rb +0 -24
- data/test/crash.rb +0 -111
- data/test/curl/curl_oj.rb +0 -46
- data/test/curl/get_oj.rb +0 -24
- data/test/curl/just_curl.rb +0 -31
- data/test/curl/just_oj.rb +0 -51
- data/test/example.rb +0 -11
- data/test/io.rb +0 -48
- data/test/isolated/test_mimic_rails_datetime.rb +0 -27
- data/test/mod.rb +0 -16
- data/test/rails.rb +0 -50
- data/test/russian.rb +0 -18
- data/test/struct.rb +0 -29
- data/test/test_serializer.rb +0 -59
- data/test/write_timebars.rb +0 -31
data/test/test_custom.rb
ADDED
@@ -0,0 +1,533 @@
|
|
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, :_z
|
24
|
+
|
25
|
+
def initialize(x, y)
|
26
|
+
@x = x
|
27
|
+
@y = y
|
28
|
+
@_z = x.to_s
|
29
|
+
end
|
30
|
+
def ==(o)
|
31
|
+
self.class == o.class && @x == o.x && @y = o.y
|
32
|
+
end
|
33
|
+
def to_json(*args)
|
34
|
+
%|{"xx":#{@x},"yy":#{y}}|
|
35
|
+
end
|
36
|
+
def raw_json(depth, indent)
|
37
|
+
%|{"xxx":#{@x},"yyy":#{y}}|
|
38
|
+
end
|
39
|
+
def as_json(*args)
|
40
|
+
{'a' => @x, :b => @y }
|
41
|
+
end
|
42
|
+
def to_hash()
|
43
|
+
{'b' => @x, 'n' => @y }
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
class AsJson
|
48
|
+
attr_accessor :x, :y
|
49
|
+
|
50
|
+
def initialize(x, y)
|
51
|
+
@x = x
|
52
|
+
@y = y
|
53
|
+
end
|
54
|
+
def ==(o)
|
55
|
+
self.class == o.class && @x == o.x && @y = o.y
|
56
|
+
end
|
57
|
+
def as_json(*args)
|
58
|
+
{'a' => @x, :b => @y }
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
class AsRails
|
63
|
+
attr_accessor :x, :y
|
64
|
+
|
65
|
+
def initialize(x, y)
|
66
|
+
@x = x
|
67
|
+
@y = y
|
68
|
+
end
|
69
|
+
def ==(o)
|
70
|
+
self.class == o.class && @x == o.x && @y = o.y
|
71
|
+
end
|
72
|
+
def as_json(*args)
|
73
|
+
a = @x
|
74
|
+
a = a.as_json if a.respond_to?('as_json')
|
75
|
+
b = @y
|
76
|
+
b = b.as_json if b.respond_to?('as_json')
|
77
|
+
{'a' => a, :b => b }
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
def setup
|
82
|
+
@default_options = Oj.default_options
|
83
|
+
Oj.default_options = { :mode => :custom }
|
84
|
+
end
|
85
|
+
|
86
|
+
def teardown
|
87
|
+
Oj.default_options = @default_options
|
88
|
+
end
|
89
|
+
|
90
|
+
def test_nil
|
91
|
+
dump_and_load(nil, false)
|
92
|
+
end
|
93
|
+
|
94
|
+
def test_true
|
95
|
+
dump_and_load(true, false)
|
96
|
+
end
|
97
|
+
|
98
|
+
def test_false
|
99
|
+
dump_and_load(false, false)
|
100
|
+
end
|
101
|
+
|
102
|
+
def test_fixnum
|
103
|
+
dump_and_load(0, false)
|
104
|
+
dump_and_load(12345, false)
|
105
|
+
dump_and_load(-54321, false)
|
106
|
+
dump_and_load(1, false)
|
107
|
+
end
|
108
|
+
|
109
|
+
def test_float
|
110
|
+
dump_and_load(0.0, false)
|
111
|
+
dump_and_load(12345.6789, false)
|
112
|
+
dump_and_load(70.35, false)
|
113
|
+
dump_and_load(-54321.012, false)
|
114
|
+
dump_and_load(1.7775, false)
|
115
|
+
dump_and_load(2.5024, false)
|
116
|
+
dump_and_load(2.48e16, false)
|
117
|
+
dump_and_load(2.48e100 * 1.0e10, false)
|
118
|
+
dump_and_load(-2.48e100 * 1.0e10, false)
|
119
|
+
end
|
120
|
+
|
121
|
+
def test_float_parse
|
122
|
+
f = Oj.load("12.123456789012345678", mode: :custom, bigdecimal_load: :float);
|
123
|
+
assert_equal(Float, f.class)
|
124
|
+
end
|
125
|
+
|
126
|
+
def test_float_parse_fast
|
127
|
+
f = Oj.load("12.123456789012345678", mode: :custom, bigdecimal_load: :fast);
|
128
|
+
assert_equal(Float, f.class)
|
129
|
+
assert(12.12345678901234 <= f && f < 12.12345678901236)
|
130
|
+
end
|
131
|
+
|
132
|
+
def test_nan_dump
|
133
|
+
assert_equal('null', Oj.dump(0/0.0, :nan => :null))
|
134
|
+
assert_equal('3.3e14159265358979323846', Oj.dump(0/0.0, :nan => :huge))
|
135
|
+
assert_equal('NaN', Oj.dump(0/0.0, :nan => :word))
|
136
|
+
begin
|
137
|
+
Oj.dump(0/0.0, :nan => :raise)
|
138
|
+
rescue Exception
|
139
|
+
assert(true)
|
140
|
+
return
|
141
|
+
end
|
142
|
+
assert(false, "*** expected an exception")
|
143
|
+
end
|
144
|
+
|
145
|
+
def test_infinity_dump
|
146
|
+
assert_equal('null', Oj.dump(1/0.0, :nan => :null))
|
147
|
+
assert_equal('3.0e14159265358979323846', Oj.dump(1/0.0, :nan => :huge))
|
148
|
+
assert_equal('Infinity', Oj.dump(1/0.0, :nan => :word))
|
149
|
+
begin
|
150
|
+
Oj.dump(1/0.0, :nan => :raise)
|
151
|
+
rescue Exception
|
152
|
+
assert(true)
|
153
|
+
return
|
154
|
+
end
|
155
|
+
assert(false, "*** expected an exception")
|
156
|
+
end
|
157
|
+
|
158
|
+
def test_neg_infinity_dump
|
159
|
+
assert_equal('null', Oj.dump(-1/0.0, :nan => :null))
|
160
|
+
assert_equal('-3.0e14159265358979323846', Oj.dump(-1/0.0, :nan => :huge))
|
161
|
+
assert_equal('-Infinity', Oj.dump(-1/0.0, :nan => :word))
|
162
|
+
begin
|
163
|
+
Oj.dump(-1/0.0, :nan => :raise)
|
164
|
+
rescue Exception
|
165
|
+
assert(true)
|
166
|
+
return
|
167
|
+
end
|
168
|
+
assert(false, "*** expected an exception")
|
169
|
+
end
|
170
|
+
|
171
|
+
def test_string
|
172
|
+
dump_and_load('', false)
|
173
|
+
dump_and_load('abc', false)
|
174
|
+
dump_and_load("abc\ndef", false)
|
175
|
+
dump_and_load("a\u0041", false)
|
176
|
+
end
|
177
|
+
|
178
|
+
def test_string_ascii
|
179
|
+
json = Oj.dump("ぴーたー", :escape_mode => :ascii)
|
180
|
+
assert_equal(%{"\\u3074\\u30fc\\u305f\\u30fc"}, json)
|
181
|
+
dump_and_load("ぴーたー", false, :escape_mode => :ascii)
|
182
|
+
end
|
183
|
+
|
184
|
+
def test_string_json
|
185
|
+
json = Oj.dump("ぴーたー", :escape_mode => :json)
|
186
|
+
assert_equal(%{"ぴーたー"}, json)
|
187
|
+
dump_and_load("ぴーたー", false, :escape_mode => :json)
|
188
|
+
end
|
189
|
+
|
190
|
+
def test_array
|
191
|
+
dump_and_load([], false)
|
192
|
+
dump_and_load([true, false], false)
|
193
|
+
dump_and_load(['a', 1, nil], false)
|
194
|
+
dump_and_load([[nil]], false)
|
195
|
+
dump_and_load([[nil], 58], false)
|
196
|
+
end
|
197
|
+
|
198
|
+
def test_array_deep
|
199
|
+
dump_and_load([1,[2,[3,[4,[5,[6,[7,[8,[9,[10,[11,[12,[13,[14,[15,[16,[17,[18,[19,[20]]]]]]]]]]]]]]]]]]]], false)
|
200
|
+
end
|
201
|
+
|
202
|
+
def test_deep_nest
|
203
|
+
begin
|
204
|
+
n = 10000
|
205
|
+
Oj.strict_load('[' * n + ']' * n)
|
206
|
+
rescue Exception => e
|
207
|
+
assert(false, e.message)
|
208
|
+
end
|
209
|
+
end
|
210
|
+
|
211
|
+
def test_hash
|
212
|
+
dump_and_load({}, false)
|
213
|
+
dump_and_load({ 'true' => true, 'false' => false}, false)
|
214
|
+
dump_and_load({ 'true' => true, 'array' => [], 'hash' => { }}, false)
|
215
|
+
end
|
216
|
+
|
217
|
+
def test_hash_deep
|
218
|
+
dump_and_load({'1' => {
|
219
|
+
'2' => {
|
220
|
+
'3' => {
|
221
|
+
'4' => {
|
222
|
+
'5' => {
|
223
|
+
'6' => {
|
224
|
+
'7' => {
|
225
|
+
'8' => {
|
226
|
+
'9' => {
|
227
|
+
'10' => {
|
228
|
+
'11' => {
|
229
|
+
'12' => {
|
230
|
+
'13' => {
|
231
|
+
'14' => {
|
232
|
+
'15' => {
|
233
|
+
'16' => {
|
234
|
+
'17' => {
|
235
|
+
'18' => {
|
236
|
+
'19' => {
|
237
|
+
'20' => {}}}}}}}}}}}}}}}}}}}}}, false)
|
238
|
+
end
|
239
|
+
|
240
|
+
def test_hash_escaped_key
|
241
|
+
json = %{{"a\nb":true,"c\td":false}}
|
242
|
+
obj = Oj.load(json)
|
243
|
+
assert_equal({"a\nb" => true, "c\td" => false}, obj)
|
244
|
+
end
|
245
|
+
|
246
|
+
def test_hash_non_string_key
|
247
|
+
assert_equal(%|{"1":true}|, Oj.dump({ 1 => true }, :indent => 0))
|
248
|
+
end
|
249
|
+
|
250
|
+
def test_bignum_object
|
251
|
+
dump_and_load(7 ** 55, false)
|
252
|
+
end
|
253
|
+
|
254
|
+
def test_bigdecimal
|
255
|
+
assert_equal('0.314159265358979323846e1', Oj.dump(BigDecimal('3.14159265358979323846'), bigdecimal_as_decimal: true).downcase())
|
256
|
+
assert_equal('"0.314159265358979323846e1"', Oj.dump(BigDecimal('3.14159265358979323846'), bigdecimal_as_decimal: false).downcase())
|
257
|
+
dump_and_load(BigDecimal('3.14159265358979323846'), false, :bigdecimal_load => true)
|
258
|
+
end
|
259
|
+
|
260
|
+
def test_object
|
261
|
+
obj = Jeez.new(true, 58)
|
262
|
+
json = Oj.dump(obj, create_id: "^o", use_to_json: false, use_as_json: false, use_to_hash: false)
|
263
|
+
assert_equal(%|{"x":true,"y":58,"_z":"true"}|, json)
|
264
|
+
json = Oj.dump(obj, create_id: "^o", use_to_json: false, use_as_json: false, use_to_hash: false, ignore_under: true)
|
265
|
+
assert_equal(%|{"x":true,"y":58}|, json)
|
266
|
+
dump_and_load(obj, false, :create_id => "^o", :create_additions => true)
|
267
|
+
end
|
268
|
+
|
269
|
+
def test_object_to_json
|
270
|
+
obj = Jeez.new(true, 58)
|
271
|
+
json = Oj.dump(obj, :use_to_json => true, :use_as_json => false, :use_to_hash => false)
|
272
|
+
assert_equal(%|{"xx":true,"yy":58}|, json)
|
273
|
+
end
|
274
|
+
|
275
|
+
def test_object_as_json
|
276
|
+
obj = Jeez.new(true, 58)
|
277
|
+
json = Oj.dump(obj, :use_to_json => false, :use_as_json => true, :use_to_hash => false)
|
278
|
+
assert_equal(%|{"a":true,"b":58}|, json)
|
279
|
+
end
|
280
|
+
|
281
|
+
def test_object_to_hash
|
282
|
+
obj = Jeez.new(true, 58)
|
283
|
+
json = Oj.dump(obj, :use_to_json => false, :use_as_json => false, :use_to_hash => true)
|
284
|
+
assert_equal(%|{"b":true,"n":58}|, json)
|
285
|
+
end
|
286
|
+
|
287
|
+
def test_object_raw_json
|
288
|
+
obj = Jeez.new(true, 58)
|
289
|
+
json = Oj.dump(obj, :use_to_json => true, :use_as_json => false, :use_raw_json => true, :use_to_hash => false)
|
290
|
+
assert_equal(%|{"xxx":true,"yyy":58}|, json)
|
291
|
+
end
|
292
|
+
|
293
|
+
def test_raw_json_stringwriter
|
294
|
+
obj = Oj::StringWriter.new(:indent => 0)
|
295
|
+
obj.push_array()
|
296
|
+
obj.pop()
|
297
|
+
json = Oj.dump(obj, :use_raw_json => true)
|
298
|
+
assert_equal(%|[]|, json)
|
299
|
+
end
|
300
|
+
|
301
|
+
def test_as_raw_json_stringwriter
|
302
|
+
obj = Oj::StringWriter.new(:indent => 0)
|
303
|
+
obj.push_array()
|
304
|
+
obj.push_value(3)
|
305
|
+
obj.pop()
|
306
|
+
j = AsJson.new(1, obj)
|
307
|
+
|
308
|
+
json = Oj.dump(j, use_raw_json: true, use_as_json: true, indent: 2)
|
309
|
+
assert_equal(%|{
|
310
|
+
"a":1,
|
311
|
+
"b":[3]
|
312
|
+
}
|
313
|
+
|, json)
|
314
|
+
|
315
|
+
json = Oj.dump(j, use_raw_json: false, use_as_json: true, indent: 2)
|
316
|
+
assert_equal(%|{
|
317
|
+
"a":1,
|
318
|
+
"b":{}
|
319
|
+
}
|
320
|
+
|, json)
|
321
|
+
end
|
322
|
+
|
323
|
+
def test_rails_as_raw_json_stringwriter
|
324
|
+
obj = Oj::StringWriter.new(:indent => 0)
|
325
|
+
obj.push_array()
|
326
|
+
obj.push_value(3)
|
327
|
+
obj.pop()
|
328
|
+
j = AsRails.new(1, obj)
|
329
|
+
json = Oj.dump(j, mode: :rails, use_raw_json: true, indent: 2)
|
330
|
+
assert_equal(%|{
|
331
|
+
"a":1,
|
332
|
+
"b":{}
|
333
|
+
}
|
334
|
+
|, json)
|
335
|
+
|
336
|
+
Oj::Rails.optimize
|
337
|
+
json = Oj.dump(j, mode: :rails, use_raw_json: true, indent: 2)
|
338
|
+
Oj::Rails.deoptimize
|
339
|
+
assert_equal(%|{
|
340
|
+
"a":1,
|
341
|
+
"b":[3]
|
342
|
+
}
|
343
|
+
|, json)
|
344
|
+
|
345
|
+
end
|
346
|
+
|
347
|
+
def test_symbol
|
348
|
+
json = Oj.dump(:abc)
|
349
|
+
assert_equal('"abc"', json)
|
350
|
+
end
|
351
|
+
|
352
|
+
def test_class
|
353
|
+
assert_equal(%|"CustomJuice"|, Oj.dump(CustomJuice))
|
354
|
+
end
|
355
|
+
|
356
|
+
def test_module
|
357
|
+
assert_equal(%|"CustomJuice::TestModule"|, Oj.dump(TestModule))
|
358
|
+
end
|
359
|
+
|
360
|
+
def test_symbol_keys
|
361
|
+
json = %|{
|
362
|
+
"x":true,
|
363
|
+
"y":58,
|
364
|
+
"z": [1,2,3]
|
365
|
+
}
|
366
|
+
|
|
367
|
+
obj = Oj.load(json, :symbol_keys => true)
|
368
|
+
assert_equal({ :x => true, :y => 58, :z => [1, 2, 3]}, obj)
|
369
|
+
end
|
370
|
+
|
371
|
+
def test_double
|
372
|
+
json = %{{ "x": 1}{ "y": 2}}
|
373
|
+
results = []
|
374
|
+
Oj.load(json, :mode => :strict) { |x| results << x }
|
375
|
+
|
376
|
+
assert_equal([{ 'x' => 1 }, { 'y' => 2 }], results)
|
377
|
+
end
|
378
|
+
|
379
|
+
def test_circular_hash
|
380
|
+
h = { 'a' => 7 }
|
381
|
+
h['b'] = h
|
382
|
+
json = Oj.dump(h, :indent => 2, :circular => true)
|
383
|
+
assert_equal(%|{
|
384
|
+
"a":7,
|
385
|
+
"b":null
|
386
|
+
}
|
387
|
+
|, json)
|
388
|
+
end
|
389
|
+
|
390
|
+
def test_omit_nil
|
391
|
+
json = Oj.dump({'x' => {'a' => 1, 'b' => nil }, 'y' => nil}, :omit_nil => true)
|
392
|
+
assert_equal(%|{"x":{"a":1}}|, json)
|
393
|
+
end
|
394
|
+
|
395
|
+
def test_complex
|
396
|
+
obj = Complex(2, 9)
|
397
|
+
dump_and_load(obj, false, :create_id => "^o", :create_additions => true)
|
398
|
+
end
|
399
|
+
|
400
|
+
def test_rational
|
401
|
+
obj = Rational(2, 9)
|
402
|
+
dump_and_load(obj, false, :create_id => "^o", :create_additions => true)
|
403
|
+
end
|
404
|
+
|
405
|
+
def test_range
|
406
|
+
obj = 3..8
|
407
|
+
dump_and_load(obj, false, :create_id => "^o", :create_additions => true)
|
408
|
+
end
|
409
|
+
|
410
|
+
def test_date
|
411
|
+
obj = Date.new(2017, 1, 5)
|
412
|
+
dump_and_load(obj, false, :create_id => "^o", :create_additions => true)
|
413
|
+
end
|
414
|
+
|
415
|
+
def test_date_unix
|
416
|
+
obj = Date.new(2017, 1, 5)
|
417
|
+
json = Oj.dump(obj, :indent => 2, time_format: :unix)
|
418
|
+
assert_equal('1483574400.000000000', json)
|
419
|
+
end
|
420
|
+
|
421
|
+
def test_date_unix_zone
|
422
|
+
obj = Date.new(2017, 1, 5)
|
423
|
+
json = Oj.dump(obj, :indent => 2, time_format: :unix_zone)
|
424
|
+
assert_equal('1483574400.000000000', json)
|
425
|
+
end
|
426
|
+
|
427
|
+
def test_date_ruby
|
428
|
+
obj = Date.new(2017, 1, 5)
|
429
|
+
json = Oj.dump(obj, :indent => 2, time_format: :ruby)
|
430
|
+
assert_equal('"2017-01-05"', json)
|
431
|
+
end
|
432
|
+
|
433
|
+
def test_date_xmlschema
|
434
|
+
obj = Date.new(2017, 1, 5)
|
435
|
+
json = Oj.dump(obj, :indent => 2, time_format: :xmlschema)
|
436
|
+
assert_equal('"2017-01-05"', json)
|
437
|
+
end
|
438
|
+
|
439
|
+
def test_datetime
|
440
|
+
obj = DateTime.new(2017, 1, 5, 10, 20, 30)
|
441
|
+
dump_and_load(obj, false, :create_id => "^o", :create_additions => true)
|
442
|
+
end
|
443
|
+
|
444
|
+
def test_datetime_unix
|
445
|
+
obj = DateTime.new(2017, 1, 5, 10, 20, 30, '-0500')
|
446
|
+
json = Oj.dump(obj, :indent => 2, time_format: :unix)
|
447
|
+
assert_equal('1483629630.000000000', json)
|
448
|
+
end
|
449
|
+
|
450
|
+
def test_datetime_unix_zone
|
451
|
+
# older versions seems to have issues getting the utc offset.
|
452
|
+
if '2.4' <= RUBY_VERSION
|
453
|
+
obj = DateTime.new(2017, 1, 5, 10, 20, 30, '-0500')
|
454
|
+
json = Oj.dump(obj, :indent => 2, time_format: :unix_zone)
|
455
|
+
assert_equal('1483629630.000000000e-18000', json)
|
456
|
+
end
|
457
|
+
end
|
458
|
+
|
459
|
+
def test_datetime_ruby
|
460
|
+
obj = DateTime.new(2017, 1, 5, 10, 20, 30, '-0500')
|
461
|
+
json = Oj.dump(obj, :indent => 2, time_format: :ruby)
|
462
|
+
assert_equal('"2017-01-05T10:20:30-05:00"', json)
|
463
|
+
end
|
464
|
+
|
465
|
+
def test_datetime_xmlschema
|
466
|
+
obj = DateTime.new(2017, 1, 5, 10, 20, 30, '-0500')
|
467
|
+
json = Oj.dump(obj, :indent => 2, time_format: :xmlschema)
|
468
|
+
assert_equal('"2017-01-05T10:20:30-05:00"', json)
|
469
|
+
end
|
470
|
+
|
471
|
+
def test_regexp
|
472
|
+
# this notation must be used to get an == match later
|
473
|
+
obj = /(?ix-m:^yes$)/
|
474
|
+
dump_and_load(obj, false, :create_id => "^o", :create_additions => true)
|
475
|
+
end
|
476
|
+
|
477
|
+
def test_openstruct
|
478
|
+
obj = OpenStruct.new(:a => 1, 'b' => 2)
|
479
|
+
dump_and_load(obj, false, :create_id => "^o", :create_additions => true)
|
480
|
+
end
|
481
|
+
|
482
|
+
def test_time
|
483
|
+
obj = Time.now()
|
484
|
+
dump_load_dump(obj, false, :time_format => :unix, :create_id => "^o", :create_additions => true)
|
485
|
+
dump_load_dump(obj, false, :time_format => :unix_zone, :create_id => "^o", :create_additions => true)
|
486
|
+
dump_load_dump(obj, false, :time_format => :xmlschema, :create_id => "^o", :create_additions => true)
|
487
|
+
dump_load_dump(obj, false, :time_format => :ruby, :create_id => "^o", :create_additions => true)
|
488
|
+
end
|
489
|
+
|
490
|
+
def dump_and_load(obj, trace=false, options={})
|
491
|
+
options = options.merge(:indent => 2, :mode => :custom)
|
492
|
+
json = Oj.dump(obj, options)
|
493
|
+
puts json if trace
|
494
|
+
|
495
|
+
loaded = Oj.load(json, options);
|
496
|
+
if obj.nil?
|
497
|
+
assert_nil(loaded)
|
498
|
+
else
|
499
|
+
assert_equal(obj, loaded)
|
500
|
+
end
|
501
|
+
loaded
|
502
|
+
end
|
503
|
+
|
504
|
+
def dump_and_load_inspect(obj, trace=false, options={})
|
505
|
+
options = options.merge(:indent => 2, :mode => :custom)
|
506
|
+
json = Oj.dump(obj, options)
|
507
|
+
puts json if trace
|
508
|
+
|
509
|
+
loaded = Oj.load(json, options);
|
510
|
+
if obj.nil?
|
511
|
+
assert_nil(loaded)
|
512
|
+
else
|
513
|
+
assert_equal(obj.inspect, loaded.inspect)
|
514
|
+
end
|
515
|
+
loaded
|
516
|
+
end
|
517
|
+
|
518
|
+
def dump_load_dump(obj, trace=false, options={})
|
519
|
+
options = options.merge(:indent => 2, :mode => :custom)
|
520
|
+
json = Oj.dump(obj, options)
|
521
|
+
puts json if trace
|
522
|
+
|
523
|
+
loaded = Oj.load(json, options);
|
524
|
+
if obj.nil?
|
525
|
+
assert_nil(loaded)
|
526
|
+
else
|
527
|
+
json2 = Oj.dump(loaded, options)
|
528
|
+
assert_equal(json, json2)
|
529
|
+
end
|
530
|
+
loaded
|
531
|
+
end
|
532
|
+
|
533
|
+
end
|