oj 2.0.0 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (133) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +17 -23
  3. data/README.md +74 -425
  4. data/ext/oj/buf.h +103 -0
  5. data/ext/oj/cache8.c +4 -0
  6. data/ext/oj/circarray.c +68 -0
  7. data/ext/oj/circarray.h +23 -0
  8. data/ext/oj/code.c +227 -0
  9. data/ext/oj/code.h +40 -0
  10. data/ext/oj/compat.c +243 -0
  11. data/ext/oj/custom.c +1097 -0
  12. data/ext/oj/dump.c +766 -1534
  13. data/ext/oj/dump.h +92 -0
  14. data/ext/oj/dump_compat.c +937 -0
  15. data/ext/oj/dump_leaf.c +254 -0
  16. data/ext/oj/dump_object.c +810 -0
  17. data/ext/oj/dump_rails.c +329 -0
  18. data/ext/oj/dump_strict.c +416 -0
  19. data/ext/oj/encode.h +51 -0
  20. data/ext/oj/err.c +57 -0
  21. data/ext/oj/err.h +70 -0
  22. data/ext/oj/extconf.rb +17 -7
  23. data/ext/oj/fast.c +213 -180
  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 +817 -0
  28. data/ext/oj/mimic_rails.c +806 -0
  29. data/ext/oj/mimic_rails.h +17 -0
  30. data/ext/oj/object.c +752 -0
  31. data/ext/oj/odd.c +230 -0
  32. data/ext/oj/odd.h +44 -0
  33. data/ext/oj/oj.c +1288 -929
  34. data/ext/oj/oj.h +240 -69
  35. data/ext/oj/parse.c +1014 -0
  36. data/ext/oj/parse.h +92 -0
  37. data/ext/oj/reader.c +223 -0
  38. data/ext/oj/reader.h +151 -0
  39. data/ext/oj/resolve.c +127 -0
  40. data/ext/oj/{cache.h → resolve.h} +6 -13
  41. data/ext/oj/rxclass.c +133 -0
  42. data/ext/oj/rxclass.h +27 -0
  43. data/ext/oj/saj.c +77 -175
  44. data/ext/oj/scp.c +224 -0
  45. data/ext/oj/sparse.c +911 -0
  46. data/ext/oj/stream_writer.c +301 -0
  47. data/ext/oj/strict.c +162 -0
  48. data/ext/oj/string_writer.c +480 -0
  49. data/ext/oj/val_stack.c +98 -0
  50. data/ext/oj/val_stack.h +188 -0
  51. data/lib/oj/active_support_helper.rb +41 -0
  52. data/lib/oj/bag.rb +6 -10
  53. data/lib/oj/easy_hash.rb +52 -0
  54. data/lib/oj/json.rb +172 -0
  55. data/lib/oj/mimic.rb +260 -5
  56. data/lib/oj/saj.rb +13 -10
  57. data/lib/oj/schandler.rb +142 -0
  58. data/lib/oj/state.rb +131 -0
  59. data/lib/oj/version.rb +1 -1
  60. data/lib/oj.rb +11 -23
  61. data/pages/Advanced.md +22 -0
  62. data/pages/Compatibility.md +25 -0
  63. data/pages/Custom.md +23 -0
  64. data/pages/Encoding.md +65 -0
  65. data/pages/JsonGem.md +79 -0
  66. data/pages/Modes.md +140 -0
  67. data/pages/Options.md +250 -0
  68. data/pages/Rails.md +60 -0
  69. data/pages/Security.md +20 -0
  70. data/test/_test_active.rb +76 -0
  71. data/test/_test_active_mimic.rb +96 -0
  72. data/test/_test_mimic_rails.rb +126 -0
  73. data/test/activesupport4/decoding_test.rb +105 -0
  74. data/test/activesupport4/encoding_test.rb +531 -0
  75. data/test/activesupport4/test_helper.rb +41 -0
  76. data/test/activesupport5/decoding_test.rb +125 -0
  77. data/test/activesupport5/encoding_test.rb +483 -0
  78. data/test/activesupport5/encoding_test_cases.rb +90 -0
  79. data/test/activesupport5/test_helper.rb +50 -0
  80. data/test/activesupport5/time_zone_test_helpers.rb +24 -0
  81. data/test/helper.rb +27 -0
  82. data/test/isolated/shared.rb +310 -0
  83. data/test/isolated/test_mimic_after.rb +13 -0
  84. data/test/isolated/test_mimic_alone.rb +12 -0
  85. data/test/isolated/test_mimic_as_json.rb +45 -0
  86. data/test/isolated/test_mimic_before.rb +13 -0
  87. data/test/isolated/test_mimic_define.rb +28 -0
  88. data/test/isolated/test_mimic_rails_after.rb +22 -0
  89. data/test/isolated/test_mimic_rails_before.rb +21 -0
  90. data/test/isolated/test_mimic_redefine.rb +15 -0
  91. data/test/json_gem/json_addition_test.rb +216 -0
  92. data/test/json_gem/json_common_interface_test.rb +143 -0
  93. data/test/json_gem/json_encoding_test.rb +109 -0
  94. data/test/json_gem/json_ext_parser_test.rb +20 -0
  95. data/test/json_gem/json_fixtures_test.rb +35 -0
  96. data/test/json_gem/json_generator_test.rb +383 -0
  97. data/test/json_gem/json_generic_object_test.rb +90 -0
  98. data/test/json_gem/json_parser_test.rb +470 -0
  99. data/test/json_gem/json_string_matching_test.rb +42 -0
  100. data/test/json_gem/test_helper.rb +18 -0
  101. data/test/perf_compat.rb +130 -0
  102. data/test/perf_fast.rb +9 -9
  103. data/test/perf_file.rb +64 -0
  104. data/test/{perf_obj.rb → perf_object.rb} +24 -10
  105. data/test/perf_scp.rb +151 -0
  106. data/test/perf_strict.rb +32 -113
  107. data/test/sample.rb +2 -3
  108. data/test/test_compat.rb +474 -0
  109. data/test/test_custom.rb +355 -0
  110. data/test/test_debian.rb +53 -0
  111. data/test/test_fast.rb +66 -16
  112. data/test/test_file.rb +237 -0
  113. data/test/test_gc.rb +49 -0
  114. data/test/test_hash.rb +29 -0
  115. data/test/test_null.rb +376 -0
  116. data/test/test_object.rb +1010 -0
  117. data/test/test_saj.rb +16 -16
  118. data/test/test_scp.rb +417 -0
  119. data/test/test_strict.rb +410 -0
  120. data/test/test_various.rb +815 -0
  121. data/test/test_writer.rb +308 -0
  122. data/test/tests.rb +9 -902
  123. data/test/tests_mimic.rb +14 -0
  124. data/test/tests_mimic_addition.rb +7 -0
  125. metadata +253 -38
  126. data/ext/oj/cache.c +0 -148
  127. data/ext/oj/foo.rb +0 -6
  128. data/ext/oj/load.c +0 -1049
  129. data/test/a.rb +0 -38
  130. data/test/perf1.rb +0 -64
  131. data/test/perf2.rb +0 -76
  132. data/test/perf_obj_old.rb +0 -213
  133. data/test/test_mimic.rb +0 -208
@@ -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