oj 3.7.12

Sign up to get free protection for your applications and to get access to all the features.
Files changed (156) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +21 -0
  3. data/README.md +96 -0
  4. data/ext/oj/buf.h +103 -0
  5. data/ext/oj/cache8.c +107 -0
  6. data/ext/oj/cache8.h +48 -0
  7. data/ext/oj/circarray.c +68 -0
  8. data/ext/oj/circarray.h +23 -0
  9. data/ext/oj/code.c +235 -0
  10. data/ext/oj/code.h +42 -0
  11. data/ext/oj/compat.c +299 -0
  12. data/ext/oj/custom.c +1188 -0
  13. data/ext/oj/dump.c +1232 -0
  14. data/ext/oj/dump.h +94 -0
  15. data/ext/oj/dump_compat.c +973 -0
  16. data/ext/oj/dump_leaf.c +252 -0
  17. data/ext/oj/dump_object.c +837 -0
  18. data/ext/oj/dump_strict.c +433 -0
  19. data/ext/oj/encode.h +45 -0
  20. data/ext/oj/err.c +57 -0
  21. data/ext/oj/err.h +70 -0
  22. data/ext/oj/extconf.rb +47 -0
  23. data/ext/oj/fast.c +1771 -0
  24. data/ext/oj/hash.c +163 -0
  25. data/ext/oj/hash.h +46 -0
  26. data/ext/oj/hash_test.c +512 -0
  27. data/ext/oj/mimic_json.c +873 -0
  28. data/ext/oj/object.c +771 -0
  29. data/ext/oj/odd.c +231 -0
  30. data/ext/oj/odd.h +44 -0
  31. data/ext/oj/oj.c +1694 -0
  32. data/ext/oj/oj.h +381 -0
  33. data/ext/oj/parse.c +1085 -0
  34. data/ext/oj/parse.h +111 -0
  35. data/ext/oj/rails.c +1485 -0
  36. data/ext/oj/rails.h +21 -0
  37. data/ext/oj/reader.c +231 -0
  38. data/ext/oj/reader.h +151 -0
  39. data/ext/oj/resolve.c +102 -0
  40. data/ext/oj/resolve.h +14 -0
  41. data/ext/oj/rxclass.c +147 -0
  42. data/ext/oj/rxclass.h +27 -0
  43. data/ext/oj/saj.c +714 -0
  44. data/ext/oj/scp.c +224 -0
  45. data/ext/oj/sparse.c +910 -0
  46. data/ext/oj/stream_writer.c +363 -0
  47. data/ext/oj/strict.c +212 -0
  48. data/ext/oj/string_writer.c +512 -0
  49. data/ext/oj/trace.c +79 -0
  50. data/ext/oj/trace.h +28 -0
  51. data/ext/oj/util.c +136 -0
  52. data/ext/oj/util.h +19 -0
  53. data/ext/oj/val_stack.c +118 -0
  54. data/ext/oj/val_stack.h +185 -0
  55. data/ext/oj/wab.c +631 -0
  56. data/lib/oj.rb +21 -0
  57. data/lib/oj/active_support_helper.rb +41 -0
  58. data/lib/oj/bag.rb +88 -0
  59. data/lib/oj/easy_hash.rb +52 -0
  60. data/lib/oj/error.rb +22 -0
  61. data/lib/oj/json.rb +176 -0
  62. data/lib/oj/mimic.rb +267 -0
  63. data/lib/oj/saj.rb +66 -0
  64. data/lib/oj/schandler.rb +142 -0
  65. data/lib/oj/state.rb +131 -0
  66. data/lib/oj/version.rb +5 -0
  67. data/pages/Advanced.md +22 -0
  68. data/pages/Compatibility.md +25 -0
  69. data/pages/Custom.md +23 -0
  70. data/pages/Encoding.md +65 -0
  71. data/pages/JsonGem.md +79 -0
  72. data/pages/Modes.md +154 -0
  73. data/pages/Options.md +266 -0
  74. data/pages/Rails.md +116 -0
  75. data/pages/Security.md +20 -0
  76. data/pages/WAB.md +13 -0
  77. data/test/_test_active.rb +76 -0
  78. data/test/_test_active_mimic.rb +96 -0
  79. data/test/_test_mimic_rails.rb +126 -0
  80. data/test/activerecord/result_test.rb +27 -0
  81. data/test/activesupport4/decoding_test.rb +108 -0
  82. data/test/activesupport4/encoding_test.rb +531 -0
  83. data/test/activesupport4/test_helper.rb +41 -0
  84. data/test/activesupport5/decoding_test.rb +125 -0
  85. data/test/activesupport5/encoding_test.rb +485 -0
  86. data/test/activesupport5/encoding_test_cases.rb +90 -0
  87. data/test/activesupport5/test_helper.rb +50 -0
  88. data/test/activesupport5/time_zone_test_helpers.rb +24 -0
  89. data/test/big.rb +15 -0
  90. data/test/files.rb +29 -0
  91. data/test/foo.rb +33 -0
  92. data/test/helper.rb +26 -0
  93. data/test/isolated/shared.rb +308 -0
  94. data/test/isolated/test_mimic_after.rb +13 -0
  95. data/test/isolated/test_mimic_alone.rb +12 -0
  96. data/test/isolated/test_mimic_as_json.rb +45 -0
  97. data/test/isolated/test_mimic_before.rb +13 -0
  98. data/test/isolated/test_mimic_define.rb +28 -0
  99. data/test/isolated/test_mimic_rails_after.rb +22 -0
  100. data/test/isolated/test_mimic_rails_before.rb +21 -0
  101. data/test/isolated/test_mimic_redefine.rb +15 -0
  102. data/test/json_gem/json_addition_test.rb +216 -0
  103. data/test/json_gem/json_common_interface_test.rb +148 -0
  104. data/test/json_gem/json_encoding_test.rb +107 -0
  105. data/test/json_gem/json_ext_parser_test.rb +20 -0
  106. data/test/json_gem/json_fixtures_test.rb +35 -0
  107. data/test/json_gem/json_generator_test.rb +383 -0
  108. data/test/json_gem/json_generic_object_test.rb +90 -0
  109. data/test/json_gem/json_parser_test.rb +470 -0
  110. data/test/json_gem/json_string_matching_test.rb +42 -0
  111. data/test/json_gem/test_helper.rb +18 -0
  112. data/test/mem.rb +35 -0
  113. data/test/perf.rb +107 -0
  114. data/test/perf_compat.rb +130 -0
  115. data/test/perf_fast.rb +164 -0
  116. data/test/perf_file.rb +64 -0
  117. data/test/perf_object.rb +138 -0
  118. data/test/perf_saj.rb +109 -0
  119. data/test/perf_scp.rb +151 -0
  120. data/test/perf_simple.rb +287 -0
  121. data/test/perf_strict.rb +145 -0
  122. data/test/perf_wab.rb +131 -0
  123. data/test/sample.rb +54 -0
  124. data/test/sample/change.rb +14 -0
  125. data/test/sample/dir.rb +19 -0
  126. data/test/sample/doc.rb +36 -0
  127. data/test/sample/file.rb +48 -0
  128. data/test/sample/group.rb +16 -0
  129. data/test/sample/hasprops.rb +16 -0
  130. data/test/sample/layer.rb +12 -0
  131. data/test/sample/line.rb +20 -0
  132. data/test/sample/oval.rb +10 -0
  133. data/test/sample/rect.rb +10 -0
  134. data/test/sample/shape.rb +35 -0
  135. data/test/sample/text.rb +20 -0
  136. data/test/sample_json.rb +37 -0
  137. data/test/test_compat.rb +509 -0
  138. data/test/test_custom.rb +406 -0
  139. data/test/test_debian.rb +53 -0
  140. data/test/test_fast.rb +470 -0
  141. data/test/test_file.rb +239 -0
  142. data/test/test_gc.rb +49 -0
  143. data/test/test_hash.rb +29 -0
  144. data/test/test_integer_range.rb +73 -0
  145. data/test/test_null.rb +376 -0
  146. data/test/test_object.rb +1018 -0
  147. data/test/test_saj.rb +186 -0
  148. data/test/test_scp.rb +433 -0
  149. data/test/test_strict.rb +410 -0
  150. data/test/test_various.rb +739 -0
  151. data/test/test_wab.rb +307 -0
  152. data/test/test_writer.rb +380 -0
  153. data/test/tests.rb +24 -0
  154. data/test/tests_mimic.rb +14 -0
  155. data/test/tests_mimic_addition.rb +7 -0
  156. metadata +359 -0
@@ -0,0 +1,307 @@
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 'uri'
16
+ require 'oj'
17
+
18
+ # Simple version of WAB::UUID for testing.
19
+ module WAB
20
+ class UUID
21
+ attr_reader :id
22
+ def initialize(id)
23
+ @id = id.downcase
24
+ raise Exception.new("Invalid UUID format.") if /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/.match(@id).nil?
25
+ end
26
+ def to_s
27
+ @id
28
+ end
29
+ def ==(other)
30
+ other.is_a?(self.class) && @id == other.id
31
+ end
32
+ end # UUID
33
+ end # WAB
34
+
35
+
36
+ class WabJuice < Minitest::Test
37
+
38
+ module TestModule
39
+ end
40
+
41
+ def test_nil
42
+ dump_and_load(nil, false)
43
+ end
44
+
45
+ def test_true
46
+ dump_and_load(true, false)
47
+ end
48
+
49
+ def test_false
50
+ dump_and_load(false, false)
51
+ end
52
+
53
+ def test_fixnum
54
+ dump_and_load(0, false)
55
+ dump_and_load(12345, false)
56
+ dump_and_load(-54321, false)
57
+ dump_and_load(1, false)
58
+ end
59
+
60
+ def test_float
61
+ dump_and_load(0.0, false)
62
+ dump_and_load(12345.6789, false)
63
+ dump_and_load(70.35, false)
64
+ dump_and_load(-54321.012, false)
65
+ dump_and_load(1.7775, false)
66
+ dump_and_load(2.5024, false)
67
+ dump_and_load(2.48e16, false)
68
+ dump_and_load(2.48e100 * 1.0e10, false)
69
+ dump_and_load(-2.48e100 * 1.0e10, false)
70
+ end
71
+
72
+ def test_nan_dump
73
+ assert_raises() { Oj.dump(0/0.0, mode: :wab) }
74
+ end
75
+
76
+ def test_infinity_dump
77
+ assert_raises() { Oj.dump(1/0.0, mode: :wab) }
78
+ end
79
+
80
+ def test_neg_infinity_dump
81
+ assert_raises() { Oj.dump(-1/0.0, mode: :wab) }
82
+ end
83
+
84
+ def test_string
85
+ dump_and_load('', false)
86
+ dump_and_load('abc', false)
87
+ dump_and_load("abc\ndef", false)
88
+ dump_and_load("a\u0041", false)
89
+ end
90
+
91
+ def test_encode
92
+ dump_and_load("ぴーたー", false)
93
+ end
94
+
95
+ def test_array
96
+ dump_and_load([], false)
97
+ dump_and_load([true, false], false)
98
+ dump_and_load(['a', 1, nil], false)
99
+ dump_and_load([[nil]], false)
100
+ dump_and_load([[nil], 58], false)
101
+ end
102
+
103
+ def test_array_deep
104
+ dump_and_load([1,[2,[3,[4,[5,[6,[7,[8,[9,[10,[11,[12,[13,[14,[15,[16,[17,[18,[19,[20]]]]]]]]]]]]]]]]]]]], false)
105
+ end
106
+
107
+ def test_deep_nest
108
+ begin
109
+ n = 10000
110
+ Oj.wab_load('[' * n + ']' * n)
111
+ rescue Exception => e
112
+ assert(false, e.message)
113
+ end
114
+ end
115
+
116
+ # Hash
117
+ def test_hash
118
+ dump_and_load({}, false)
119
+ dump_and_load({ true: true, false: false}, false)
120
+ dump_and_load({ true: true, array: [], hash: { }}, false)
121
+ end
122
+
123
+ def test_hash_non_sym_keys
124
+ assert_raises() { Oj.dump({ 'true' => true}, mode: :wab) }
125
+ end
126
+
127
+ def test_hash_deep
128
+ dump_and_load({x1: {
129
+ x2: {
130
+ x3: {
131
+ x4: {
132
+ x5: {
133
+ x6: {
134
+ x7: {
135
+ x8: {
136
+ x9: {
137
+ x10: {
138
+ x11: {
139
+ x12: {
140
+ x13: {
141
+ x14: {
142
+ x15: {
143
+ x16: {
144
+ x17: {
145
+ x18: {
146
+ x19: {
147
+ x20: {}}}}}}}}}}}}}}}}}}}}}, false)
148
+ end
149
+
150
+ def test_non_str_hash
151
+ assert_raises() { Oj.dump({ 1 => true, 0 => false }, mode: :wab) }
152
+ end
153
+
154
+ def test_bignum_object
155
+ dump_and_load(7 ** 55, false)
156
+ end
157
+
158
+ # BigDecimal
159
+ def test_bigdecimal_wab
160
+ dump_and_load(BigDecimal('3.14159265358979323846'), false)
161
+ end
162
+
163
+ def test_bigdecimal_load
164
+ orig = BigDecimal('80.51')
165
+ json = Oj.dump(orig, mode: :wab)
166
+ bg = Oj.load(json, :mode => :wab, :bigdecimal_load => true)
167
+ assert_equal(BigDecimal, bg.class)
168
+ assert_equal(orig, bg)
169
+ end
170
+
171
+ def test_range
172
+ assert_raises() { Oj.dump(1..7, mode: :wab) }
173
+ end
174
+
175
+ # Stream IO
176
+ def test_io_string
177
+ json = %{{
178
+ "x":true,
179
+ "y":58,
180
+ "z": [1,2,3]
181
+ }
182
+ }
183
+ input = StringIO.new(json)
184
+ obj = Oj.wab_load(input)
185
+ assert_equal({ x: true, y: 58, z: [1, 2, 3]}, obj)
186
+ end
187
+
188
+ def test_io_file
189
+ filename = File.join(File.dirname(__FILE__), 'open_file_test.json')
190
+ File.open(filename, 'w') { |f| f.write(%{{
191
+ "x":true,
192
+ "y":58,
193
+ "z": [1,2,3]
194
+ }
195
+ }) }
196
+ f = File.new(filename)
197
+ obj = Oj.wab_load(f)
198
+ f.close()
199
+ assert_equal({ x: true, y: 58, z: [1, 2, 3]}, obj)
200
+ end
201
+
202
+ def test_symbol
203
+ json = Oj.dump(:abc, mode: :wab)
204
+ assert_equal('"abc"', json)
205
+ end
206
+
207
+ def test_time
208
+ t = Time.gm(2017, 1, 5, 23, 58, 7, 123456.789)
209
+ json = Oj.dump(t, mode: :wab)
210
+ assert_equal('"2017-01-05T23:58:07.123456789Z"', json)
211
+ # must load and convert to json as the Time#== does not match identical
212
+ # times due to the way it tracks fractional seconds.
213
+ loaded = Oj.wab_load(json);
214
+ assert_equal(json, Oj.dump(loaded, mode: :wab), "json mismatch after load")
215
+ end
216
+
217
+ def test_uuid
218
+ u = ::WAB::UUID.new('123e4567-e89b-12d3-a456-426655440000')
219
+ json = Oj.dump(u, mode: :wab)
220
+ assert_equal('"123e4567-e89b-12d3-a456-426655440000"', json)
221
+ dump_and_load(u, false)
222
+ end
223
+
224
+ def test_uri
225
+ u = URI('http://opo.technology/sample')
226
+ json = Oj.dump(u, mode: :wab)
227
+ assert_equal('"http://opo.technology/sample"', json)
228
+ dump_and_load(u, false)
229
+ end
230
+
231
+ def test_class
232
+ assert_raises() { Oj.dump(WabJuice, mode: :wab) }
233
+ end
234
+
235
+ def test_module
236
+ assert_raises() { Oj.dump(TestModule, mode: :wab) }
237
+ end
238
+
239
+ # symbol_keys option
240
+ def test_symbol_keys
241
+ json = %{{
242
+ "x":true,
243
+ "y":58,
244
+ "z": [1,2,3]
245
+ }
246
+ }
247
+ obj = Oj.wab_load(json, :symbol_keys => true)
248
+ assert_equal({ x: true, y: 58, z: [1, 2, 3]}, obj)
249
+ end
250
+
251
+ # comments
252
+ def test_comment_slash
253
+ json = %{{
254
+ "x":true,//three
255
+ "y":58,
256
+ "z": [1,2,
257
+ 3 // six
258
+ ]}
259
+ }
260
+ obj = Oj.wab_load(json)
261
+ assert_equal({ x: true, y: 58, z: [1, 2, 3]}, obj)
262
+ end
263
+
264
+ def test_comment_c
265
+ json = %{{
266
+ "x"/*one*/:/*two*/true,
267
+ "y":58,
268
+ "z": [1,2,3]}
269
+ }
270
+ obj = Oj.wab_load(json)
271
+ assert_equal({ x: true, y: 58, z: [1, 2, 3]}, obj)
272
+ end
273
+
274
+ def test_comment
275
+ json = %{{
276
+ "x"/*one*/:/*two*/true,//three
277
+ "y":58/*four*/,
278
+ "z": [1,2/*five*/,
279
+ 3 // six
280
+ ]
281
+ }
282
+ }
283
+ obj = Oj.wab_load(json)
284
+ assert_equal({ x: true, y: 58, z: [1, 2, 3]}, obj)
285
+ end
286
+
287
+ def test_double
288
+ json = %{{ "x": 1}{ "y": 2}}
289
+ results = []
290
+ Oj.load(json, :mode => :wab) { |x| results << x }
291
+
292
+ assert_equal([{ x: 1 }, { y: 2 }], results)
293
+ end
294
+
295
+ def dump_and_load(obj, trace=false)
296
+ json = Oj.dump(obj, mode: :wab, indent: 2)
297
+ puts json if trace
298
+ loaded = Oj.wab_load(json);
299
+ if obj.nil?
300
+ assert_nil(loaded)
301
+ else
302
+ assert_equal(obj, loaded)
303
+ end
304
+ loaded
305
+ end
306
+
307
+ end
@@ -0,0 +1,380 @@
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
+ class SString < ::String
231
+ alias :write :concat
232
+ end
233
+
234
+ def test_stream_writer_encoding
235
+ output = SString.new.force_encoding(::Encoding::UTF_8)
236
+ w = Oj::StreamWriter.new(output, :indent => 0)
237
+ # Oddly enough, when pushing ASCII characters with UTF-8 encoding or even
238
+ # ASCII-8BIT does not change the output encoding. Pushing any non-ASCII no
239
+ # matter what the encoding changes the output encoding to ASCII-8BIT.
240
+ x = "香港" # Hong Kong
241
+ x = x.force_encoding('ASCII-8BIT')
242
+ w.push_value(x)
243
+ assert_equal(::Encoding::UTF_8, output.encoding)
244
+ end
245
+
246
+ def test_stream_writer_empty_array
247
+ output = StringIO.open("", "w+")
248
+ w = Oj::StreamWriter.new(output, :indent => 0)
249
+ w.push_array()
250
+ w.pop()
251
+ assert_equal("[]\n", output.string())
252
+ end
253
+
254
+ def test_stream_writer_mixed_stringio
255
+ output = StringIO.open("", "w+")
256
+ w = Oj::StreamWriter.new(output, :indent => 0)
257
+ w.push_object()
258
+ w.push_object("a1")
259
+ w.pop()
260
+ w.push_object("a2")
261
+ w.push_array("b")
262
+ w.push_value(7)
263
+ w.push_value(true)
264
+ w.push_value("string")
265
+ w.pop()
266
+ w.pop()
267
+ w.push_object("a3")
268
+ w.pop()
269
+ w.pop()
270
+ result = output.string()
271
+ assert_equal(%|{"a1":{},"a2":{"b":[7,true,"string"]},"a3":{}}\n|, result)
272
+ end
273
+
274
+ def test_stream_writer_mixed_file
275
+ filename = File.join(File.dirname(__FILE__), 'open_file_test.json')
276
+ File.open(filename, "w") do |f|
277
+ w = Oj::StreamWriter.new(f, :indent => 0)
278
+ w.push_object()
279
+ w.push_object("a1")
280
+ w.pop()
281
+ w.push_object("a2")
282
+ w.push_array("b")
283
+ w.push_value(7)
284
+ w.push_value(true)
285
+ w.push_value("string")
286
+ w.pop()
287
+ w.pop()
288
+ w.push_object("a3")
289
+ w.pop()
290
+ w.pop()
291
+ end
292
+ content = File.read(filename)
293
+ assert_equal(%|{"a1":{},"a2":{"b":[7,true,"string"]},"a3":{}}\n|, content)
294
+ end
295
+
296
+ def test_stream_writer_nested_key_object
297
+ output = StringIO.open("", "w+")
298
+ w = Oj::StreamWriter.new(output, :indent => 0)
299
+ w.push_object()
300
+ w.push_key('a1')
301
+ w.push_object()
302
+ w.pop()
303
+ w.push_key('a2')
304
+ w.push_object('x')
305
+ w.push_object('b')
306
+ w.pop()
307
+ w.pop()
308
+ w.push_key('a3')
309
+ w.push_object()
310
+ w.push_key('a4')
311
+ w.push_value(37)
312
+ w.pop()
313
+ w.pop()
314
+ assert_equal(%|{"a1":{},"a2":{"b":{}},"a3":{"a4":37}}\n|, output.string())
315
+ end
316
+
317
+ def push_stuff(w, pop_all=true)
318
+ w.push_object()
319
+ w.push_object("a1")
320
+ w.pop()
321
+ w.push_object("a2")
322
+ w.push_array("b")
323
+ w.push_value(7)
324
+ w.push_value(true)
325
+ w.push_value("string")
326
+ w.pop()
327
+ w.pop()
328
+ w.push_object("a3")
329
+ if pop_all
330
+ w.pop_all()
331
+ else
332
+ w.pop()
333
+ w.pop()
334
+ end
335
+ end
336
+
337
+ def test_stream_writer_buf_small
338
+ output = StringIO.open("", "w+")
339
+ w = Oj::StreamWriter.new(output, :indent => 0, :buffer_size => 20)
340
+ push_stuff(w)
341
+ assert_equal(%|{"a1":{},"a2":{"b":[7,true,"string"]},"a3":{}}\n|, output.string())
342
+ end
343
+
344
+ def test_stream_writer_buf_large
345
+ output = StringIO.open("", "w+")
346
+ w = Oj::StreamWriter.new(output, :indent => 0, :buffer_size => 16000)
347
+ push_stuff(w)
348
+ assert_equal(%|{"a1":{},"a2":{"b":[7,true,"string"]},"a3":{}}\n|, output.string())
349
+ end
350
+
351
+ def test_stream_writer_buf_flush
352
+ output = StringIO.open("", "w+")
353
+ w = Oj::StreamWriter.new(output, :indent => 0, :buffer_size => 4096)
354
+ push_stuff(w, false)
355
+ # no flush so nothing should be in the output yet
356
+ assert_equal("", output.string())
357
+ w.flush()
358
+ assert_equal(%|{"a1":{},"a2":{"b":[7,true,"string"]},"a3":{}}\n|, output.string())
359
+ end
360
+
361
+ def test_stream_writer_buf_flush_small
362
+ output = StringIO.open("", "w+")
363
+ w = Oj::StreamWriter.new(output, :indent => 0, :buffer_size => 30)
364
+ push_stuff(w, false)
365
+ # flush should be called at 30 bytes but since the writes are chunky flush
366
+ # is called after adding "string".
367
+ assert_equal(%|{"a1":{},"a2":{"b":[7,true,"string"|, output.string())
368
+ w.flush()
369
+ assert_equal(%|{"a1":{},"a2":{"b":[7,true,"string"]},"a3":{}}\n|, output.string())
370
+ end
371
+
372
+ def test_stream_writer_push_null_value_with_key
373
+ output = StringIO.open("", "w+")
374
+ w = Oj::StreamWriter.new(output, :indent => 0)
375
+ w.push_object()
376
+ w.push_value(nil, 'nothing')
377
+ w.pop()
378
+ assert_equal(%|{"nothing":null}\n|, output.string())
379
+ end
380
+ end # OjWriter