oj 2.18.5 → 3.16.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (166) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +1452 -0
  3. data/README.md +53 -221
  4. data/RELEASE_NOTES.md +61 -0
  5. data/ext/oj/buf.h +54 -72
  6. data/ext/oj/cache.c +329 -0
  7. data/ext/oj/cache.h +22 -0
  8. data/ext/oj/cache8.c +61 -63
  9. data/ext/oj/cache8.h +12 -39
  10. data/ext/oj/circarray.c +38 -67
  11. data/ext/oj/circarray.h +16 -42
  12. data/ext/oj/code.c +214 -0
  13. data/ext/oj/code.h +40 -0
  14. data/ext/oj/compat.c +194 -110
  15. data/ext/oj/custom.c +1074 -0
  16. data/ext/oj/debug.c +126 -0
  17. data/ext/oj/dump.c +1276 -2494
  18. data/ext/oj/dump.h +110 -0
  19. data/ext/oj/dump_compat.c +897 -0
  20. data/ext/oj/dump_leaf.c +162 -0
  21. data/ext/oj/dump_object.c +710 -0
  22. data/ext/oj/dump_strict.c +399 -0
  23. data/ext/oj/encode.h +7 -42
  24. data/ext/oj/encoder.c +43 -0
  25. data/ext/oj/err.c +28 -53
  26. data/ext/oj/err.h +49 -46
  27. data/ext/oj/extconf.rb +33 -32
  28. data/ext/oj/fast.c +1082 -1098
  29. data/ext/oj/intern.c +313 -0
  30. data/ext/oj/intern.h +22 -0
  31. data/ext/oj/mem.c +318 -0
  32. data/ext/oj/mem.h +53 -0
  33. data/ext/oj/mimic_json.c +919 -0
  34. data/ext/oj/object.c +545 -625
  35. data/ext/oj/odd.c +158 -168
  36. data/ext/oj/odd.h +32 -58
  37. data/ext/oj/oj.c +1727 -2080
  38. data/ext/oj/oj.h +334 -259
  39. data/ext/oj/parse.c +974 -753
  40. data/ext/oj/parse.h +97 -90
  41. data/ext/oj/parser.c +1600 -0
  42. data/ext/oj/parser.h +103 -0
  43. data/ext/oj/rails.c +1478 -0
  44. data/ext/oj/rails.h +18 -0
  45. data/ext/oj/reader.c +136 -163
  46. data/ext/oj/reader.h +76 -112
  47. data/ext/oj/resolve.c +45 -94
  48. data/ext/oj/resolve.h +7 -34
  49. data/ext/oj/rxclass.c +144 -0
  50. data/ext/oj/rxclass.h +26 -0
  51. data/ext/oj/saj.c +445 -511
  52. data/ext/oj/saj2.c +584 -0
  53. data/ext/oj/saj2.h +23 -0
  54. data/ext/oj/scp.c +82 -143
  55. data/ext/oj/simd.h +10 -0
  56. data/ext/oj/sparse.c +749 -644
  57. data/ext/oj/stream_writer.c +329 -0
  58. data/ext/oj/strict.c +114 -112
  59. data/ext/oj/string_writer.c +517 -0
  60. data/ext/oj/trace.c +72 -0
  61. data/ext/oj/trace.h +55 -0
  62. data/ext/oj/usual.c +1218 -0
  63. data/ext/oj/usual.h +69 -0
  64. data/ext/oj/util.c +136 -0
  65. data/ext/oj/util.h +20 -0
  66. data/ext/oj/val_stack.c +75 -72
  67. data/ext/oj/val_stack.h +94 -127
  68. data/ext/oj/validate.c +46 -0
  69. data/ext/oj/wab.c +586 -0
  70. data/lib/oj/active_support_helper.rb +1 -3
  71. data/lib/oj/bag.rb +8 -1
  72. data/lib/oj/easy_hash.rb +21 -13
  73. data/lib/oj/error.rb +10 -12
  74. data/lib/oj/json.rb +188 -0
  75. data/lib/oj/mimic.rb +165 -26
  76. data/lib/oj/saj.rb +20 -6
  77. data/lib/oj/schandler.rb +5 -4
  78. data/lib/oj/state.rb +135 -0
  79. data/lib/oj/version.rb +2 -3
  80. data/lib/oj.rb +3 -31
  81. data/pages/Advanced.md +22 -0
  82. data/pages/Compatibility.md +25 -0
  83. data/pages/Custom.md +23 -0
  84. data/pages/Encoding.md +65 -0
  85. data/pages/InstallOptions.md +20 -0
  86. data/pages/JsonGem.md +94 -0
  87. data/pages/Modes.md +161 -0
  88. data/pages/Options.md +337 -0
  89. data/pages/Parser.md +309 -0
  90. data/pages/Rails.md +167 -0
  91. data/pages/Security.md +20 -0
  92. data/pages/WAB.md +13 -0
  93. metadata +126 -163
  94. data/ext/oj/hash.c +0 -163
  95. data/ext/oj/hash.h +0 -46
  96. data/ext/oj/hash_test.c +0 -512
  97. data/test/_test_active.rb +0 -76
  98. data/test/_test_active_mimic.rb +0 -96
  99. data/test/_test_mimic_rails.rb +0 -126
  100. data/test/activesupport_datetime_test.rb +0 -23
  101. data/test/bug.rb +0 -51
  102. data/test/bug2.rb +0 -10
  103. data/test/bug3.rb +0 -46
  104. data/test/bug_fast.rb +0 -32
  105. data/test/bug_load.rb +0 -24
  106. data/test/crash.rb +0 -111
  107. data/test/curl/curl_oj.rb +0 -46
  108. data/test/curl/get_oj.rb +0 -24
  109. data/test/curl/just_curl.rb +0 -31
  110. data/test/curl/just_oj.rb +0 -51
  111. data/test/example.rb +0 -11
  112. data/test/files.rb +0 -29
  113. data/test/foo.rb +0 -24
  114. data/test/helper.rb +0 -27
  115. data/test/io.rb +0 -48
  116. data/test/isolated/shared.rb +0 -310
  117. data/test/isolated/test_mimic_after.rb +0 -13
  118. data/test/isolated/test_mimic_alone.rb +0 -12
  119. data/test/isolated/test_mimic_as_json.rb +0 -45
  120. data/test/isolated/test_mimic_before.rb +0 -13
  121. data/test/isolated/test_mimic_define.rb +0 -28
  122. data/test/isolated/test_mimic_rails_after.rb +0 -22
  123. data/test/isolated/test_mimic_rails_before.rb +0 -21
  124. data/test/isolated/test_mimic_rails_datetime.rb +0 -27
  125. data/test/isolated/test_mimic_redefine.rb +0 -15
  126. data/test/mod.rb +0 -16
  127. data/test/perf.rb +0 -107
  128. data/test/perf_compat.rb +0 -128
  129. data/test/perf_fast.rb +0 -164
  130. data/test/perf_file.rb +0 -64
  131. data/test/perf_object.rb +0 -138
  132. data/test/perf_saj.rb +0 -109
  133. data/test/perf_scp.rb +0 -151
  134. data/test/perf_simple.rb +0 -287
  135. data/test/perf_strict.rb +0 -128
  136. data/test/rails.rb +0 -50
  137. data/test/russian.rb +0 -18
  138. data/test/sample/change.rb +0 -14
  139. data/test/sample/dir.rb +0 -19
  140. data/test/sample/doc.rb +0 -36
  141. data/test/sample/file.rb +0 -48
  142. data/test/sample/group.rb +0 -16
  143. data/test/sample/hasprops.rb +0 -16
  144. data/test/sample/layer.rb +0 -12
  145. data/test/sample/line.rb +0 -20
  146. data/test/sample/oval.rb +0 -10
  147. data/test/sample/rect.rb +0 -10
  148. data/test/sample/shape.rb +0 -35
  149. data/test/sample/text.rb +0 -20
  150. data/test/sample.rb +0 -55
  151. data/test/sample_json.rb +0 -37
  152. data/test/struct.rb +0 -29
  153. data/test/test_compat.rb +0 -398
  154. data/test/test_debian.rb +0 -53
  155. data/test/test_fast.rb +0 -458
  156. data/test/test_file.rb +0 -245
  157. data/test/test_gc.rb +0 -49
  158. data/test/test_hash.rb +0 -29
  159. data/test/test_object.rb +0 -745
  160. data/test/test_saj.rb +0 -186
  161. data/test/test_scp.rb +0 -396
  162. data/test/test_serializer.rb +0 -59
  163. data/test/test_strict.rb +0 -254
  164. data/test/test_various.rb +0 -1383
  165. data/test/test_writer.rb +0 -308
  166. data/test/write_timebars.rb +0 -31
data/test/test_saj.rb DELETED
@@ -1,186 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # encoding: UTF-8
3
-
4
- $: << File.dirname(__FILE__)
5
-
6
- require 'helper'
7
-
8
- $json = %{{
9
- "array": [
10
- {
11
- "num" : 3,
12
- "string": "message",
13
- "hash" : {
14
- "h2" : {
15
- "a" : [ 1, 2, 3 ]
16
- }
17
- }
18
- }
19
- ],
20
- "boolean" : true
21
- }}
22
-
23
- class AllSaj < Oj::Saj
24
- attr_accessor :calls
25
-
26
- def initialize()
27
- @calls = []
28
- end
29
-
30
- def hash_start(key)
31
- @calls << [:hash_start, key]
32
- end
33
-
34
- def hash_end(key)
35
- @calls << [:hash_end, key]
36
- end
37
-
38
- def array_start(key)
39
- @calls << [:array_start, key]
40
- end
41
-
42
- def array_end(key)
43
- @calls << [:array_end, key]
44
- end
45
-
46
- def add_value(value, key)
47
- @calls << [:add_value, value, key]
48
- end
49
-
50
- def error(message, line, column)
51
- @calls << [:error, message, line, column]
52
- end
53
-
54
- end # AllSaj
55
-
56
- class SajTest < Minitest::Test
57
-
58
- def setup
59
- @default_options = Oj.default_options
60
- end
61
-
62
- def teardown
63
- Oj.default_options = @default_options
64
- end
65
-
66
- def test_nil
67
- handler = AllSaj.new()
68
- json = %{null}
69
- Oj.saj_parse(handler, json)
70
- assert_equal([[:add_value, nil, nil]], handler.calls)
71
- end
72
-
73
- def test_true
74
- handler = AllSaj.new()
75
- json = %{true}
76
- Oj.saj_parse(handler, json)
77
- assert_equal([[:add_value, true, nil]], handler.calls)
78
- end
79
-
80
- def test_false
81
- handler = AllSaj.new()
82
- json = %{false}
83
- Oj.saj_parse(handler, json)
84
- assert_equal([[:add_value, false, nil]], handler.calls)
85
- end
86
-
87
- def test_string
88
- handler = AllSaj.new()
89
- json = %{"a string"}
90
- Oj.saj_parse(handler, json)
91
- assert_equal([[:add_value, 'a string', nil]], handler.calls)
92
- end
93
-
94
- def test_fixnum
95
- handler = AllSaj.new()
96
- json = %{12345}
97
- Oj.saj_parse(handler, json)
98
- assert_equal([[:add_value, 12345, nil]], handler.calls)
99
- end
100
-
101
- def test_float
102
- handler = AllSaj.new()
103
- json = %{12345.6789}
104
- Oj.saj_parse(handler, json)
105
- assert_equal([[:add_value, 12345.6789, nil]], handler.calls)
106
- end
107
-
108
- def test_float_exp
109
- handler = AllSaj.new()
110
- json = %{12345.6789e7}
111
- Oj.saj_parse(handler, json)
112
- assert_equal(1, handler.calls.size)
113
- assert_equal(:add_value, handler.calls[0][0])
114
- assert_equal((12345.6789e7 * 10000).to_i, (handler.calls[0][1] * 10000).to_i)
115
- end
116
-
117
- def test_array_empty
118
- handler = AllSaj.new()
119
- json = %{[]}
120
- Oj.saj_parse(handler, json)
121
- assert_equal([[:array_start, nil],
122
- [:array_end, nil]], handler.calls)
123
- end
124
-
125
- def test_array
126
- handler = AllSaj.new()
127
- json = %{[true,false]}
128
- Oj.saj_parse(handler, json)
129
- assert_equal([[:array_start, nil],
130
- [:add_value, true, nil],
131
- [:add_value, false, nil],
132
- [:array_end, nil]], handler.calls)
133
- end
134
-
135
- def test_hash_empty
136
- handler = AllSaj.new()
137
- json = %{{}}
138
- Oj.saj_parse(handler, json)
139
- assert_equal([[:hash_start, nil],
140
- [:hash_end, nil]], handler.calls)
141
- end
142
-
143
- def test_hash
144
- handler = AllSaj.new()
145
- json = %{{"one":true,"two":false}}
146
- Oj.saj_parse(handler, json)
147
- assert_equal([[:hash_start, nil],
148
- [:add_value, true, 'one'],
149
- [:add_value, false, 'two'],
150
- [:hash_end, nil]], handler.calls)
151
- end
152
-
153
- def test_full
154
- handler = AllSaj.new()
155
- Oj.saj_parse(handler, $json)
156
- assert_equal([[:hash_start, nil],
157
- [:array_start, 'array'],
158
- [:hash_start, nil],
159
- [:add_value, 3, 'num'],
160
- [:add_value, 'message', 'string'],
161
- [:hash_start, 'hash'],
162
- [:hash_start, 'h2'],
163
- [:array_start, 'a'],
164
- [:add_value, 1, nil],
165
- [:add_value, 2, nil],
166
- [:add_value, 3, nil],
167
- [:array_end, 'a'],
168
- [:hash_end, 'h2'],
169
- [:hash_end, 'hash'],
170
- [:hash_end, nil],
171
- [:array_end, 'array'],
172
- [:add_value, true, 'boolean'],
173
- [:hash_end, nil]], handler.calls)
174
- end
175
-
176
- def test_fixnum_bad
177
- handler = AllSaj.new()
178
- json = %{12345xyz}
179
- Oj.saj_parse(handler, json)
180
- assert_equal([:add_value, 12345, nil], handler.calls.first)
181
- type, message, line, column = handler.calls.last
182
- assert_equal([:error, 1, 6], [type, line, column])
183
- assert_match(%r{invalid format, extra characters at line 1, column 6 \[(?:[a-z\.]+/)*saj\.c:\d+\]}, message)
184
- end
185
-
186
- end
data/test/test_scp.rb DELETED
@@ -1,396 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # encoding: UTF-8
3
-
4
- $: << File.dirname(__FILE__)
5
-
6
- require 'helper'
7
- require 'socket'
8
-
9
- $json = %{{
10
- "array": [
11
- {
12
- "num" : 3,
13
- "string": "message",
14
- "hash" : {
15
- "h2" : {
16
- "a" : [ 1, 2, 3 ]
17
- }
18
- }
19
- }
20
- ],
21
- "boolean" : true
22
- }}
23
-
24
- class NoHandler < Oj::ScHandler
25
- def initialize()
26
- end
27
- end
28
-
29
- class AllHandler < Oj::ScHandler
30
- attr_accessor :calls
31
-
32
- def initialize()
33
- @calls = []
34
- end
35
-
36
- def hash_start()
37
- @calls << [:hash_start]
38
- {}
39
- end
40
-
41
- def hash_end()
42
- @calls << [:hash_end]
43
- end
44
-
45
- def hash_key(key)
46
- @calls << [:hash_key, key]
47
- return 'too' if 'two' == key
48
- return :symbol if 'symbol' == key
49
- key
50
- end
51
-
52
- def array_start()
53
- @calls << [:array_start]
54
- []
55
- end
56
-
57
- def array_end()
58
- @calls << [:array_end]
59
- end
60
-
61
- def add_value(value)
62
- @calls << [:add_value, value]
63
- end
64
-
65
- def hash_set(h, key, value)
66
- @calls << [:hash_set, key, value]
67
- end
68
-
69
- def array_append(a, value)
70
- @calls << [:array_append, value]
71
- end
72
-
73
- end # AllHandler
74
-
75
- class Closer < AllHandler
76
- attr_accessor :io
77
- def initialize(io)
78
- super()
79
- @io = io
80
- end
81
-
82
- def hash_start()
83
- @calls << [:hash_start]
84
- @io.close
85
- {}
86
- end
87
-
88
- def hash_set(h, key, value)
89
- @calls << [:hash_set, key, value]
90
- @io.close
91
- end
92
-
93
- end # Closer
94
-
95
- class ScpTest < Minitest::Test
96
-
97
- def setup
98
- @default_options = Oj.default_options
99
- end
100
-
101
- def teardown
102
- Oj.default_options = @default_options
103
- end
104
-
105
- def test_nil
106
- handler = AllHandler.new()
107
- json = %{null}
108
- Oj.sc_parse(handler, json)
109
- assert_equal([[:add_value, nil]], handler.calls)
110
- end
111
-
112
- def test_true
113
- handler = AllHandler.new()
114
- json = %{true}
115
- Oj.sc_parse(handler, json)
116
- assert_equal([[:add_value, true]], handler.calls)
117
- end
118
-
119
- def test_false
120
- handler = AllHandler.new()
121
- json = %{false}
122
- Oj.sc_parse(handler, json)
123
- assert_equal([[:add_value, false]], handler.calls)
124
- end
125
-
126
- def test_string
127
- handler = AllHandler.new()
128
- json = %{"a string"}
129
- Oj.sc_parse(handler, json)
130
- assert_equal([[:add_value, 'a string']], handler.calls)
131
- end
132
-
133
- def test_fixnum
134
- handler = AllHandler.new()
135
- json = %{12345}
136
- Oj.sc_parse(handler, json)
137
- assert_equal([[:add_value, 12345]], handler.calls)
138
- end
139
-
140
- def test_float
141
- handler = AllHandler.new()
142
- json = %{12345.6789}
143
- Oj.sc_parse(handler, json)
144
- assert_equal([[:add_value, 12345.6789]], handler.calls)
145
- end
146
-
147
- def test_float_exp
148
- handler = AllHandler.new()
149
- json = %{12345.6789e7}
150
- Oj.sc_parse(handler, json)
151
- assert_equal(1, handler.calls.size)
152
- assert_equal(:add_value, handler.calls[0][0])
153
- assert_equal((12345.6789e7 * 10000).to_i, (handler.calls[0][1] * 10000).to_i)
154
- end
155
-
156
- def test_array_empty
157
- handler = AllHandler.new()
158
- json = %{[]}
159
- Oj.sc_parse(handler, json)
160
- assert_equal([[:array_start],
161
- [:array_end],
162
- [:add_value, []]], handler.calls)
163
- end
164
-
165
- def test_array
166
- handler = AllHandler.new()
167
- json = %{[true,false]}
168
- Oj.sc_parse(handler, json)
169
- assert_equal([[:array_start],
170
- [:array_append, true],
171
- [:array_append, false],
172
- [:array_end],
173
- [:add_value, []]], handler.calls)
174
- end
175
-
176
- def test_hash_empty
177
- handler = AllHandler.new()
178
- json = %{{}}
179
- Oj.sc_parse(handler, json)
180
- assert_equal([[:hash_start],
181
- [:hash_end],
182
- [:add_value, {}]], handler.calls)
183
- end
184
-
185
- def test_hash
186
- handler = AllHandler.new()
187
- json = %{{"one":true,"two":false}}
188
- Oj.sc_parse(handler, json)
189
- assert_equal([[:hash_start],
190
- [:hash_key, 'one'],
191
- [:hash_set, 'one', true],
192
- [:hash_key, 'two'],
193
- [:hash_set, 'too', false],
194
- [:hash_end],
195
- [:add_value, {}]], handler.calls)
196
- end
197
-
198
- def test_hash_sym
199
- handler = AllHandler.new()
200
- json = %{{"one":true,"two":false}}
201
- Oj.sc_parse(handler, json, :symbol_keys => true)
202
- assert_equal([[:hash_start],
203
- [:hash_key, 'one'],
204
- [:hash_set, 'one', true],
205
- [:hash_key, 'two'],
206
- [:hash_set, 'too', false],
207
- [:hash_end],
208
- [:add_value, {}]], handler.calls)
209
- end
210
-
211
- def test_symbol_hash_key_without_symbol_keys
212
- handler = AllHandler.new()
213
- json = %{{"one":true,"symbol":false}}
214
- Oj.sc_parse(handler, json)
215
- assert_equal([[:hash_start],
216
- [:hash_key, 'one'],
217
- [:hash_set, 'one', true],
218
- [:hash_key, 'symbol'],
219
- [:hash_set, :symbol, false],
220
- [:hash_end],
221
- [:add_value, {}]], handler.calls)
222
- end
223
-
224
- def test_full
225
- handler = AllHandler.new()
226
- Oj.sc_parse(handler, $json)
227
- assert_equal([[:hash_start],
228
- [:hash_key, 'array'],
229
- [:array_start],
230
- [:hash_start],
231
- [:hash_key, 'num'],
232
- [:hash_set, "num", 3],
233
- [:hash_key, 'string'],
234
- [:hash_set, "string", "message"],
235
- [:hash_key, 'hash'],
236
- [:hash_start],
237
- [:hash_key, 'h2'],
238
- [:hash_start],
239
- [:hash_key, 'a'],
240
- [:array_start],
241
- [:array_append, 1],
242
- [:array_append, 2],
243
- [:array_append, 3],
244
- [:array_end],
245
- [:hash_set, "a", []],
246
- [:hash_end],
247
- [:hash_set, "h2", {}],
248
- [:hash_end],
249
- [:hash_set, "hash", {}],
250
- [:hash_end],
251
- [:array_append, {}],
252
- [:array_end],
253
- [:hash_set, "array", []],
254
- [:hash_key, 'boolean'],
255
- [:hash_set, "boolean", true],
256
- [:hash_end],
257
- [:add_value, {}]], handler.calls)
258
- end
259
-
260
- def test_double
261
- handler = AllHandler.new()
262
- json = %{{"one":true,"two":false}{"three":true,"four":false}}
263
- Oj.sc_parse(handler, json) { |j| j }
264
- assert_equal([[:hash_start],
265
- [:hash_key, 'one'],
266
- [:hash_set, 'one', true],
267
- [:hash_key, 'two'],
268
- [:hash_set, 'too', false],
269
- [:hash_end],
270
- [:add_value, {}],
271
- [:hash_start],
272
- [:hash_key, 'three'],
273
- [:hash_set, 'three', true],
274
- [:hash_key, 'four'],
275
- [:hash_set, 'four', false],
276
- [:hash_end],
277
- [:add_value, {}]], handler.calls)
278
- end
279
-
280
- def test_none
281
- handler = NoHandler.new()
282
- Oj.sc_parse(handler, $json)
283
- end
284
-
285
- def test_fixnum_bad
286
- handler = AllHandler.new()
287
- json = %{12345xyz}
288
- assert_raises Oj::ParseError do
289
- Oj.sc_parse(handler, json)
290
- end
291
- end
292
-
293
- def test_null_string
294
- handler = AllHandler.new()
295
- json = %{"\0"}
296
- assert_raises Oj::ParseError do
297
- Oj.sc_parse(handler, json)
298
- end
299
- end
300
-
301
- def test_pipe
302
- handler = AllHandler.new()
303
- json = %{{"one":true,"two":false}}
304
- IO.pipe do |read_io, write_io|
305
- if fork
306
- write_io.close
307
- Oj.sc_parse(handler, read_io) {|v| p v}
308
- read_io.close
309
- assert_equal([[:hash_start],
310
- [:hash_key, 'one'],
311
- [:hash_set, 'one', true],
312
- [:hash_key, 'two'],
313
- [:hash_set, 'too', false],
314
- [:hash_end],
315
- [:add_value, {}]], handler.calls)
316
- else
317
- read_io.close
318
- write_io.write json
319
- write_io.close
320
- Process.exit(0)
321
- end
322
- end
323
- end
324
-
325
- def test_pipe_close
326
- json = %{{"one":true,"two":false}}
327
- IO.pipe do |read_io, write_io|
328
- if fork
329
- write_io.close
330
- handler = Closer.new(read_io)
331
- err = nil
332
- begin
333
- Oj.sc_parse(handler, read_io)
334
- read_io.close
335
- rescue Exception => e
336
- err = e.class.to_s
337
- end
338
- assert_equal("IOError", err)
339
- assert_equal([[:hash_start],
340
- [:hash_key, 'one'],
341
- [:hash_set, 'one', true]], handler.calls)
342
- else
343
- read_io.close
344
- write_io.write json[0..11]
345
- sleep(0.1)
346
- begin
347
- write_io.write json[12..-1]
348
- rescue Exception => e
349
- # ignore, should fail to write
350
- end
351
- write_io.close
352
- Process.exit(0)
353
- end
354
- end
355
- end
356
-
357
- def test_socket_close
358
- json = %{{"one":true,"two":false}}
359
- begin
360
- server = TCPServer.new(8080)
361
- rescue
362
- # Not able to open a socket to run the test. Might be Travis.
363
- return
364
- end
365
- Thread.start(json) do |j|
366
- c = server.accept()
367
- c.puts json[0..11]
368
- 10.times {
369
- break if c.closed?
370
- sleep(0.1)
371
- }
372
- unless c.closed?
373
- c.puts json[12..-1]
374
- c.close
375
- end
376
- end
377
- begin
378
- sock = TCPSocket.new('localhost', 8080)
379
- rescue
380
- # Not able to open a socket to run the test. Might be Travis.
381
- return
382
- end
383
- handler = Closer.new(sock)
384
- err = nil
385
- begin
386
- Oj.sc_parse(handler, sock)
387
- rescue Exception => e
388
- err = e.class.to_s
389
- end
390
- assert_equal("IOError", err)
391
- assert_equal([[:hash_start],
392
- [:hash_key, 'one'],
393
- [:hash_set, 'one', true]], handler.calls)
394
- end
395
-
396
- end
@@ -1,59 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # encoding: UTF-8
3
-
4
- $: << File.dirname(__FILE__)
5
-
6
- %w(lib ext test).each do |dir|
7
- $LOAD_PATH.unshift File.expand_path("../../#{dir}", __FILE__)
8
- end
9
-
10
- require 'minitest'
11
- require 'minitest/autorun'
12
- require 'oj'
13
-
14
- Oj.mimic_JSON
15
-
16
- require 'rails/all'
17
- require 'active_model'
18
- require 'active_model_serializers'
19
- require 'active_support/json'
20
-
21
- #Oj.mimic_JSON
22
-
23
- class Category
24
- include ActiveModel::Model
25
- include ActiveModel::SerializerSupport
26
-
27
- attr_accessor :id, :name
28
-
29
- def initialize(id, name)
30
- @id = id
31
- @name = name
32
- end
33
- end
34
-
35
- class CategorySerializer < ActiveModel::Serializer
36
- attributes :id, :name
37
- end
38
-
39
- class MimicRails < Minitest::Test
40
-
41
- def test_dump_object
42
- Oj.default_options= {:indent => 0}
43
- category = Category.new(1, 'test')
44
- serializer = CategorySerializer.new(category)
45
-
46
- json = serializer.to_json()
47
- puts "*** serializer.to_json() #{serializer.to_json()}"
48
- assert_equal(%|{"category":{"id":1,"name":"test"}}|, json)
49
-
50
- json = serializer.as_json()
51
- puts "*** serializer.as_json() #{serializer.as_json()}"
52
- assert_equal({"category" => {:id => 1, :name => "test"}}, json)
53
-
54
- json = JSON.dump(serializer)
55
- puts "*** JSON.dump(serializer) #{JSON.dump(serializer)}"
56
- assert_equal(%|{"category":{"id":1,"name":"test"}}|, json)
57
- end
58
-
59
- end # MimicRails