oj 2.18.3 → 3.13.14

Sign up to get free protection for your applications and to get access to all the features.
Files changed (182) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +1324 -0
  3. data/README.md +51 -204
  4. data/RELEASE_NOTES.md +61 -0
  5. data/ext/oj/buf.h +49 -72
  6. data/ext/oj/cache.c +326 -0
  7. data/ext/oj/cache.h +21 -0
  8. data/ext/oj/cache8.c +61 -64
  9. data/ext/oj/cache8.h +12 -39
  10. data/ext/oj/circarray.c +37 -68
  11. data/ext/oj/circarray.h +16 -42
  12. data/ext/oj/code.c +221 -0
  13. data/ext/oj/code.h +40 -0
  14. data/ext/oj/compat.c +231 -107
  15. data/ext/oj/custom.c +1125 -0
  16. data/ext/oj/debug.c +132 -0
  17. data/ext/oj/dump.c +935 -2513
  18. data/ext/oj/dump.h +108 -0
  19. data/ext/oj/dump_compat.c +936 -0
  20. data/ext/oj/dump_leaf.c +164 -0
  21. data/ext/oj/dump_object.c +761 -0
  22. data/ext/oj/dump_strict.c +410 -0
  23. data/ext/oj/encode.h +7 -42
  24. data/ext/oj/encoder.c +43 -0
  25. data/ext/oj/err.c +40 -54
  26. data/ext/oj/err.h +52 -46
  27. data/ext/oj/extconf.rb +21 -30
  28. data/ext/oj/fast.c +1097 -1080
  29. data/ext/oj/intern.c +301 -0
  30. data/ext/oj/intern.h +26 -0
  31. data/ext/oj/mimic_json.c +893 -0
  32. data/ext/oj/object.c +549 -620
  33. data/ext/oj/odd.c +155 -167
  34. data/ext/oj/odd.h +37 -63
  35. data/ext/oj/oj.c +1661 -2063
  36. data/ext/oj/oj.h +341 -270
  37. data/ext/oj/parse.c +974 -737
  38. data/ext/oj/parse.h +105 -97
  39. data/ext/oj/parser.c +1526 -0
  40. data/ext/oj/parser.h +90 -0
  41. data/ext/oj/rails.c +1504 -0
  42. data/ext/oj/rails.h +18 -0
  43. data/ext/oj/reader.c +141 -163
  44. data/ext/oj/reader.h +75 -113
  45. data/ext/oj/resolve.c +45 -93
  46. data/ext/oj/resolve.h +7 -34
  47. data/ext/oj/rxclass.c +143 -0
  48. data/ext/oj/rxclass.h +26 -0
  49. data/ext/oj/saj.c +447 -511
  50. data/ext/oj/saj2.c +348 -0
  51. data/ext/oj/scp.c +91 -138
  52. data/ext/oj/sparse.c +793 -644
  53. data/ext/oj/stream_writer.c +331 -0
  54. data/ext/oj/strict.c +145 -109
  55. data/ext/oj/string_writer.c +493 -0
  56. data/ext/oj/trace.c +72 -0
  57. data/ext/oj/trace.h +28 -0
  58. data/ext/oj/usual.c +1254 -0
  59. data/ext/oj/util.c +136 -0
  60. data/ext/oj/util.h +20 -0
  61. data/ext/oj/val_stack.c +62 -70
  62. data/ext/oj/val_stack.h +95 -129
  63. data/ext/oj/validate.c +51 -0
  64. data/ext/oj/wab.c +622 -0
  65. data/lib/oj/bag.rb +1 -0
  66. data/lib/oj/easy_hash.rb +17 -8
  67. data/lib/oj/error.rb +10 -11
  68. data/lib/oj/json.rb +176 -0
  69. data/lib/oj/mimic.rb +158 -19
  70. data/lib/oj/state.rb +132 -0
  71. data/lib/oj/version.rb +2 -2
  72. data/lib/oj.rb +1 -31
  73. data/pages/Advanced.md +22 -0
  74. data/pages/Compatibility.md +25 -0
  75. data/pages/Custom.md +23 -0
  76. data/pages/Encoding.md +65 -0
  77. data/pages/JsonGem.md +94 -0
  78. data/pages/Modes.md +161 -0
  79. data/pages/Options.md +327 -0
  80. data/pages/Parser.md +309 -0
  81. data/pages/Rails.md +167 -0
  82. data/pages/Security.md +20 -0
  83. data/pages/WAB.md +13 -0
  84. data/test/activerecord/result_test.rb +32 -0
  85. data/test/activesupport4/decoding_test.rb +108 -0
  86. data/test/activesupport4/encoding_test.rb +531 -0
  87. data/test/activesupport4/test_helper.rb +41 -0
  88. data/test/activesupport5/abstract_unit.rb +45 -0
  89. data/test/activesupport5/decoding_test.rb +133 -0
  90. data/test/activesupport5/encoding_test.rb +500 -0
  91. data/test/activesupport5/encoding_test_cases.rb +98 -0
  92. data/test/activesupport5/test_helper.rb +72 -0
  93. data/test/activesupport5/time_zone_test_helpers.rb +39 -0
  94. data/test/activesupport6/abstract_unit.rb +44 -0
  95. data/test/activesupport6/decoding_test.rb +133 -0
  96. data/test/activesupport6/encoding_test.rb +507 -0
  97. data/test/activesupport6/encoding_test_cases.rb +98 -0
  98. data/test/activesupport6/test_common.rb +17 -0
  99. data/test/activesupport6/test_helper.rb +163 -0
  100. data/test/activesupport6/time_zone_test_helpers.rb +39 -0
  101. data/test/activesupport7/abstract_unit.rb +49 -0
  102. data/test/activesupport7/decoding_test.rb +125 -0
  103. data/test/activesupport7/encoding_test.rb +486 -0
  104. data/test/activesupport7/encoding_test_cases.rb +104 -0
  105. data/test/activesupport7/time_zone_test_helpers.rb +47 -0
  106. data/test/bar.rb +9 -0
  107. data/test/baz.rb +16 -0
  108. data/test/bug.rb +11 -46
  109. data/test/foo.rb +69 -16
  110. data/test/helper.rb +10 -1
  111. data/test/isolated/shared.rb +12 -8
  112. data/test/isolated/test_mimic_rails_after.rb +3 -3
  113. data/test/isolated/test_mimic_rails_before.rb +3 -3
  114. data/test/json_gem/json_addition_test.rb +216 -0
  115. data/test/json_gem/json_common_interface_test.rb +153 -0
  116. data/test/json_gem/json_encoding_test.rb +107 -0
  117. data/test/json_gem/json_ext_parser_test.rb +20 -0
  118. data/test/json_gem/json_fixtures_test.rb +35 -0
  119. data/test/json_gem/json_generator_test.rb +397 -0
  120. data/test/json_gem/json_generic_object_test.rb +90 -0
  121. data/test/json_gem/json_parser_test.rb +470 -0
  122. data/test/json_gem/json_string_matching_test.rb +42 -0
  123. data/test/json_gem/test_helper.rb +26 -0
  124. data/test/mem.rb +33 -0
  125. data/test/perf.rb +1 -1
  126. data/test/perf_compat.rb +30 -28
  127. data/test/perf_dump.rb +50 -0
  128. data/test/perf_object.rb +1 -1
  129. data/test/perf_once.rb +58 -0
  130. data/test/perf_parser.rb +189 -0
  131. data/test/perf_scp.rb +11 -10
  132. data/test/perf_strict.rb +30 -19
  133. data/test/perf_wab.rb +131 -0
  134. data/test/prec.rb +23 -0
  135. data/test/sample.rb +0 -1
  136. data/test/sample_json.rb +1 -1
  137. data/test/test_compat.rb +219 -102
  138. data/test/test_custom.rb +533 -0
  139. data/test/test_fast.rb +107 -35
  140. data/test/test_file.rb +19 -25
  141. data/test/test_generate.rb +21 -0
  142. data/test/test_hash.rb +11 -1
  143. data/test/test_integer_range.rb +72 -0
  144. data/test/test_null.rb +376 -0
  145. data/test/test_object.rb +357 -70
  146. data/test/test_parser.rb +27 -0
  147. data/test/test_parser_saj.rb +245 -0
  148. data/test/test_parser_usual.rb +217 -0
  149. data/test/test_rails.rb +35 -0
  150. data/test/test_saj.rb +1 -1
  151. data/test/test_scp.rb +39 -2
  152. data/test/test_strict.rb +186 -7
  153. data/test/test_various.rb +160 -774
  154. data/test/test_wab.rb +307 -0
  155. data/test/test_writer.rb +90 -2
  156. data/test/tests.rb +24 -0
  157. data/test/tests_mimic.rb +14 -0
  158. data/test/tests_mimic_addition.rb +7 -0
  159. data/test/zoo.rb +13 -0
  160. metadata +194 -56
  161. data/ext/oj/hash.c +0 -163
  162. data/ext/oj/hash.h +0 -46
  163. data/ext/oj/hash_test.c +0 -512
  164. data/test/activesupport_datetime_test.rb +0 -23
  165. data/test/bug2.rb +0 -10
  166. data/test/bug3.rb +0 -46
  167. data/test/bug_fast.rb +0 -32
  168. data/test/bug_load.rb +0 -24
  169. data/test/crash.rb +0 -111
  170. data/test/curl/curl_oj.rb +0 -46
  171. data/test/curl/get_oj.rb +0 -24
  172. data/test/curl/just_curl.rb +0 -31
  173. data/test/curl/just_oj.rb +0 -51
  174. data/test/example.rb +0 -11
  175. data/test/io.rb +0 -48
  176. data/test/isolated/test_mimic_rails_datetime.rb +0 -27
  177. data/test/mod.rb +0 -16
  178. data/test/rails.rb +0 -50
  179. data/test/russian.rb +0 -18
  180. data/test/struct.rb +0 -29
  181. data/test/test_serializer.rb +0 -59
  182. data/test/write_timebars.rb +0 -31
@@ -0,0 +1,245 @@
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 test_nil
59
+ handler = AllSaj.new()
60
+ json = %{null}
61
+ p = Oj::Parser.new(:saj)
62
+ p.handler = handler
63
+ p.parse(json)
64
+ assert_equal([[:add_value, nil, nil]], handler.calls)
65
+ end
66
+
67
+ def test_true
68
+ handler = AllSaj.new()
69
+ json = %{true}
70
+ p = Oj::Parser.new(:saj)
71
+ p.handler = handler
72
+ p.parse(json)
73
+ assert_equal([[:add_value, true, nil]], handler.calls)
74
+ end
75
+
76
+ def test_false
77
+ handler = AllSaj.new()
78
+ json = %{false}
79
+ p = Oj::Parser.new(:saj)
80
+ p.handler = handler
81
+ p.parse(json)
82
+ assert_equal([[:add_value, false, nil]], handler.calls)
83
+ end
84
+
85
+ def test_string
86
+ handler = AllSaj.new()
87
+ json = %{"a string"}
88
+ p = Oj::Parser.new(:saj)
89
+ p.handler = handler
90
+ p.parse(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
+ p = Oj::Parser.new(:saj)
98
+ p.handler = handler
99
+ p.parse(json)
100
+ assert_equal([[:add_value, 12345, nil]], handler.calls)
101
+ end
102
+
103
+ def test_float
104
+ handler = AllSaj.new()
105
+ json = %{12345.6789}
106
+ p = Oj::Parser.new(:saj)
107
+ p.handler = handler
108
+ p.parse(json)
109
+ assert_equal([[:add_value, 12345.6789, nil]], handler.calls)
110
+ end
111
+
112
+ def test_float_exp
113
+ handler = AllSaj.new()
114
+ json = %{12345.6789e7}
115
+ p = Oj::Parser.new(:saj)
116
+ p.handler = handler
117
+ p.parse(json)
118
+ assert_equal(1, handler.calls.size)
119
+ assert_equal(:add_value, handler.calls[0][0])
120
+ assert_equal((12345.6789e7 * 10000).to_i, (handler.calls[0][1] * 10000).to_i)
121
+ end
122
+
123
+ def test_array_empty
124
+ handler = AllSaj.new()
125
+ json = %{[]}
126
+ p = Oj::Parser.new(:saj)
127
+ p.handler = handler
128
+ p.parse(json)
129
+ assert_equal([[:array_start, nil],
130
+ [:array_end, nil]], handler.calls)
131
+ end
132
+
133
+ def test_array
134
+ handler = AllSaj.new()
135
+ json = %{[true,false]}
136
+ p = Oj::Parser.new(:saj)
137
+ p.handler = handler
138
+ p.parse(json)
139
+ assert_equal([[:array_start, nil],
140
+ [:add_value, true, nil],
141
+ [:add_value, false, nil],
142
+ [:array_end, nil]], handler.calls)
143
+ end
144
+
145
+ def test_hash_empty
146
+ handler = AllSaj.new()
147
+ json = %{{}}
148
+ p = Oj::Parser.new(:saj)
149
+ p.handler = handler
150
+ p.parse(json)
151
+ assert_equal([[:hash_start, nil],
152
+ [:hash_end, nil]], handler.calls)
153
+ end
154
+
155
+ def test_hash
156
+ handler = AllSaj.new()
157
+ json = %{{"one":true,"two":false}}
158
+ p = Oj::Parser.new(:saj)
159
+ p.handler = handler
160
+ p.parse(json)
161
+ assert_equal([[:hash_start, nil],
162
+ [:add_value, true, 'one'],
163
+ [:add_value, false, 'two'],
164
+ [:hash_end, nil]], handler.calls)
165
+ end
166
+
167
+ def test_full
168
+ handler = AllSaj.new()
169
+ Oj.saj_parse(handler, $json)
170
+ assert_equal([[:hash_start, nil],
171
+ [:array_start, 'array'],
172
+ [:hash_start, nil],
173
+ [:add_value, 3, 'num'],
174
+ [:add_value, 'message', 'string'],
175
+ [:hash_start, 'hash'],
176
+ [:hash_start, 'h2'],
177
+ [:array_start, 'a'],
178
+ [:add_value, 1, nil],
179
+ [:add_value, 2, nil],
180
+ [:add_value, 3, nil],
181
+ [:array_end, 'a'],
182
+ [:hash_end, 'h2'],
183
+ [:hash_end, 'hash'],
184
+ [:hash_end, nil],
185
+ [:array_end, 'array'],
186
+ [:add_value, true, 'boolean'],
187
+ [:hash_end, nil]], handler.calls)
188
+ end
189
+
190
+ def test_multiple
191
+ handler = AllSaj.new()
192
+ json = %|[true][false]|
193
+ p = Oj::Parser.new(:saj)
194
+ p.handler = handler
195
+ p.parse(json)
196
+ assert_equal([
197
+ [:array_start, nil],
198
+ [:add_value, true, nil],
199
+ [:array_end, nil],
200
+ [:array_start, nil],
201
+ [:add_value, false, nil],
202
+ [:array_end, nil],
203
+ ], handler.calls)
204
+ end
205
+
206
+ def test_io
207
+ handler = AllSaj.new()
208
+ json = %| [true,false] |
209
+ p = Oj::Parser.new(:saj)
210
+ p.handler = handler
211
+ p.load(StringIO.new(json))
212
+ assert_equal([
213
+ [:array_start, nil],
214
+ [:add_value, true, nil],
215
+ [:add_value, false, nil],
216
+ [:array_end, nil],
217
+ ], handler.calls)
218
+ end
219
+
220
+ def test_file
221
+ handler = AllSaj.new()
222
+ p = Oj::Parser.new(:saj)
223
+ p.handler = handler
224
+ p.file('saj_test.json')
225
+ assert_equal([
226
+ [:array_start, nil],
227
+ [:add_value, true, nil],
228
+ [:add_value, false, nil],
229
+ [:array_end, nil],
230
+ ], handler.calls)
231
+ end
232
+
233
+ def test_default
234
+ handler = AllSaj.new()
235
+ json = %|[true]|
236
+ Oj::Parser.saj.handler = handler
237
+ Oj::Parser.saj.parse(json)
238
+ assert_equal([
239
+ [:array_start, nil],
240
+ [:add_value, true, nil],
241
+ [:array_end, nil],
242
+ ], handler.calls)
243
+ end
244
+
245
+ end
@@ -0,0 +1,217 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: utf-8
3
+
4
+ $: << File.dirname(__FILE__)
5
+
6
+ require 'helper'
7
+
8
+ class UsualTest < Minitest::Test
9
+
10
+ def test_nil
11
+ p = Oj::Parser.new(:usual)
12
+ doc = p.parse('nil')
13
+ assert_nil(doc)
14
+ end
15
+
16
+ def test_primitive
17
+ p = Oj::Parser.new(:usual)
18
+ [
19
+ ['true', true],
20
+ ['false', false],
21
+ ['123', 123],
22
+ ['1.25', 1.25],
23
+ ['"abc"', 'abc'],
24
+ ].each { |x|
25
+ doc = p.parse(x[0])
26
+ assert_equal(x[1], doc)
27
+ }
28
+ end
29
+
30
+ def test_big
31
+ p = Oj::Parser.new(:usual)
32
+ doc = p.parse('12345678901234567890123456789')
33
+ assert_equal(BigDecimal, doc.class)
34
+ doc = p.parse('1234567890.1234567890123456789')
35
+ assert_equal(BigDecimal, doc.class)
36
+ end
37
+
38
+ def test_array
39
+ p = Oj::Parser.new(:usual)
40
+ [
41
+ ['[]', []],
42
+ ['[false]', [false]],
43
+ ['[true,false]', [true,false]],
44
+ ['[[]]', [[]]],
45
+ ['[true,[],false]', [true,[],false]],
46
+ ['[true,[true],false]', [true,[true],false]],
47
+ ].each { |x|
48
+ doc = p.parse(x[0])
49
+ assert_equal(x[1], doc)
50
+ }
51
+ end
52
+
53
+ def test_hash
54
+ p = Oj::Parser.new(:usual)
55
+ [
56
+ ['{}', {}],
57
+ ['{"a": null}', {'a' => nil}],
58
+ ['{"t": true, "f": false, "s": "abc"}', {'t' => true, 'f' => false, 's' => 'abc'}],
59
+ ['{"a": {}}', {'a' => {}}],
60
+ ['{"a": {"b": 2}}', {'a' => {'b' => 2}}],
61
+ ['{"a": [true]}', {'a' => [true]}],
62
+ ].each { |x|
63
+ doc = p.parse(x[0])
64
+ assert_equal(x[1], doc)
65
+ }
66
+ end
67
+
68
+ def test_symbol_keys
69
+ p = Oj::Parser.new(:usual)
70
+ assert_equal(false, p.symbol_keys)
71
+ p.symbol_keys = true
72
+ doc = p.parse('{"a": true, "b": false}')
73
+ assert_equal({a: true, b: false}, doc)
74
+ end
75
+
76
+ def test_strings
77
+ p = Oj::Parser.new(:usual)
78
+ doc = p.parse('{"ぴ": "", "ぴ ": "x", "c": "ぴーたー", "d": " ぴーたー "}')
79
+ assert_equal({'ぴ' => '', 'ぴ ' => 'x', 'c' => 'ぴーたー', 'd' => ' ぴーたー '}, doc)
80
+ end
81
+
82
+ def test_capacity
83
+ p = Oj::Parser.new(:usual, capacity: 1000)
84
+ assert_equal(4096, p.capacity)
85
+ p.capacity = 5000
86
+ assert_equal(5000, p.capacity)
87
+ end
88
+
89
+ def test_decimal
90
+ p = Oj::Parser.new(:usual)
91
+ assert_equal(:auto, p.decimal)
92
+ doc = p.parse('1.234567890123456789')
93
+ assert_equal(BigDecimal, doc.class)
94
+ assert_equal('0.1234567890123456789e1', doc.to_s)
95
+ doc = p.parse('1.25')
96
+ assert_equal(Float, doc.class)
97
+
98
+ p.decimal = :float
99
+ assert_equal(:float, p.decimal)
100
+ doc = p.parse('1.234567890123456789')
101
+ assert_equal(Float, doc.class)
102
+
103
+ p.decimal = :bigdecimal
104
+ assert_equal(:bigdecimal, p.decimal)
105
+ doc = p.parse('1.234567890123456789')
106
+ assert_equal(BigDecimal, doc.class)
107
+ doc = p.parse('1.25')
108
+ assert_equal(BigDecimal, doc.class)
109
+ assert_equal('0.125e1', doc.to_s)
110
+
111
+ p.decimal = :ruby
112
+ assert_equal(:ruby, p.decimal)
113
+ doc = p.parse('1.234567890123456789')
114
+ assert_equal(Float, doc.class)
115
+ end
116
+
117
+ def test_omit_null
118
+ p = Oj::Parser.new(:usual)
119
+ p.omit_null = true
120
+ doc = p.parse('{"a":true,"b":null}')
121
+ assert_equal({'a'=>true}, doc)
122
+
123
+ p.omit_null = false
124
+ doc = p.parse('{"a":true,"b":null}')
125
+ assert_equal({'a'=>true, 'b'=>nil}, doc)
126
+ end
127
+
128
+ class MyArray < Array
129
+ end
130
+
131
+ def test_array_class
132
+ p = Oj::Parser.new(:usual)
133
+ p.array_class = MyArray
134
+ assert_equal(MyArray, p.array_class)
135
+ doc = p.parse('[true]')
136
+ assert_equal(MyArray, doc.class)
137
+ end
138
+
139
+ class MyHash < Hash
140
+ end
141
+
142
+ def test_hash_class
143
+ p = Oj::Parser.new(:usual)
144
+ p.hash_class = MyHash
145
+ assert_equal(MyHash, p.hash_class)
146
+ doc = p.parse('{"a":true}')
147
+ assert_equal(MyHash, doc.class)
148
+ end
149
+
150
+ class MyClass
151
+ attr_accessor :a
152
+ attr_accessor :b
153
+
154
+ def to_s
155
+ "#{self.class}{a: #{@a} b: #{b}}"
156
+ end
157
+ end
158
+
159
+ class MyClass2 < MyClass
160
+ def self.json_create(arg)
161
+ obj = new
162
+ obj.a = arg['a']
163
+ obj.b = arg['b']
164
+ obj
165
+ end
166
+ end
167
+
168
+ def test_create_id
169
+ p = Oj::Parser.new(:usual)
170
+ p.create_id = '^'
171
+ doc = p.parse('{"a":true}')
172
+ assert_equal(Hash, doc.class)
173
+ doc = p.parse('{"a":true,"^":"UsualTest::MyClass","b":false}')
174
+ assert_equal('UsualTest::MyClass{a: true b: false}', doc.to_s)
175
+
176
+ doc = p.parse('{"a":true,"^":"UsualTest::MyClass2","b":false}')
177
+ assert_equal('UsualTest::MyClass2{a: true b: false}', doc.to_s)
178
+
179
+ p.hash_class = MyHash
180
+ assert_equal(MyHash, p.hash_class)
181
+ doc = p.parse('{"a":true}')
182
+ assert_equal(MyHash, doc.class)
183
+
184
+ doc = p.parse('{"a":true,"^":"UsualTest::MyClass","b":false}')
185
+ assert_equal('UsualTest::MyClass{a: true b: false}', doc.to_s)
186
+ end
187
+
188
+ def test_missing_class
189
+ p = Oj::Parser.new(:usual, create_id: '^')
190
+ json = '{"a":true,"^":"Auto","b":false}'
191
+ doc = p.parse(json)
192
+ assert_equal(Hash, doc.class)
193
+
194
+ p.missing_class = :auto
195
+ doc = p.parse(json)
196
+ # Auto should be defined after parsing
197
+ assert_equal(Auto, doc.class)
198
+ end
199
+
200
+ def test_class_cache
201
+ p = Oj::Parser.new(:usual)
202
+ p.create_id = '^'
203
+ p.class_cache = true
204
+ p.missing_class = :auto
205
+ json = '{"a":true,"^":"Auto2","b":false}'
206
+ doc = p.parse(json)
207
+ assert_equal(Auto2, doc.class)
208
+
209
+ doc = p.parse(json)
210
+ assert_equal(Auto2, doc.class)
211
+ end
212
+
213
+ def test_default_parser
214
+ doc = Oj::Parser.usual.parse('{"a":true,"b":null}')
215
+ assert_equal({'a'=>true, 'b'=>nil}, doc)
216
+ end
217
+ end
@@ -0,0 +1,35 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: utf-8
3
+
4
+ $: << File.dirname(__FILE__)
5
+
6
+ require 'helper'
7
+
8
+ class RailsJuice < Minitest::Test
9
+
10
+ def test_bigdecimal_dump
11
+ orig = Oj.default_options
12
+ Oj.default_options = { mode: :rails, bigdecimal_as_decimal: true }
13
+ bd = BigDecimal('123')
14
+ json = Oj.dump(bd)
15
+ Oj.default_options = orig
16
+
17
+ assert_equal('0.123e3', json.downcase)
18
+
19
+ json = Oj.dump(bd, mode: :rails, bigdecimal_as_decimal: false)
20
+ assert_equal('"0.123e3"', json.downcase)
21
+
22
+ json = Oj.dump(bd, mode: :rails, bigdecimal_as_decimal: true)
23
+ assert_equal('0.123e3', json.downcase)
24
+ end
25
+
26
+ def test_invalid_encoding
27
+ assert_raises(EncodingError) {
28
+ Oj.dump("\"\xf3j", mode: :rails)
29
+ }
30
+ assert_raises(EncodingError) {
31
+ Oj.dump("\xf3j", mode: :rails)
32
+ }
33
+ end
34
+
35
+ end
data/test/test_saj.rb CHANGED
@@ -180,7 +180,7 @@ class SajTest < Minitest::Test
180
180
  assert_equal([:add_value, 12345, nil], handler.calls.first)
181
181
  type, message, line, column = handler.calls.last
182
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)
183
+ assert_match(%r{invalid format, extra characters at line 1, column 6 \[(?:[A-Za-z]:\/)?(?:[a-z\.]+/)*saj\.c:\d+\]}, message)
184
184
  end
185
185
 
186
186
  end
data/test/test_scp.rb CHANGED
@@ -5,6 +5,7 @@ $: << File.dirname(__FILE__)
5
5
 
6
6
  require 'helper'
7
7
  require 'socket'
8
+ require 'stringio'
8
9
 
9
10
  $json = %{{
10
11
  "array": [
@@ -260,7 +261,27 @@ class ScpTest < Minitest::Test
260
261
  def test_double
261
262
  handler = AllHandler.new()
262
263
  json = %{{"one":true,"two":false}{"three":true,"four":false}}
263
- Oj.sc_parse(handler, json) { |j| j }
264
+ Oj.sc_parse(handler, json)
265
+ assert_equal([[:hash_start],
266
+ [:hash_key, 'one'],
267
+ [:hash_set, 'one', true],
268
+ [:hash_key, 'two'],
269
+ [:hash_set, 'too', false],
270
+ [:hash_end],
271
+ [:add_value, {}],
272
+ [:hash_start],
273
+ [:hash_key, 'three'],
274
+ [:hash_set, 'three', true],
275
+ [:hash_key, 'four'],
276
+ [:hash_set, 'four', false],
277
+ [:hash_end],
278
+ [:add_value, {}]], handler.calls)
279
+ end
280
+
281
+ def test_double_io
282
+ handler = AllHandler.new()
283
+ json = %{{"one":true,"two":false}{"three":true,"four":false}}
284
+ Oj.sc_parse(handler, StringIO.new(json))
264
285
  assert_equal([[:hash_start],
265
286
  [:hash_key, 'one'],
266
287
  [:hash_set, 'one', true],
@@ -299,12 +320,15 @@ class ScpTest < Minitest::Test
299
320
  end
300
321
 
301
322
  def test_pipe
323
+ # Windows does not support fork
324
+ return if RbConfig::CONFIG['host_os'] =~ /(mingw|mswin)/
325
+
302
326
  handler = AllHandler.new()
303
327
  json = %{{"one":true,"two":false}}
304
328
  IO.pipe do |read_io, write_io|
305
329
  if fork
306
330
  write_io.close
307
- Oj.sc_parse(handler, read_io) {|v| p v}
331
+ Oj.sc_parse(handler, read_io)
308
332
  read_io.close
309
333
  assert_equal([[:hash_start],
310
334
  [:hash_key, 'one'],
@@ -322,7 +346,20 @@ class ScpTest < Minitest::Test
322
346
  end
323
347
  end
324
348
 
349
+ def test_bad_bignum
350
+ if '2.4.0' < RUBY_VERSION
351
+ handler = AllHandler.new()
352
+ json = %|{"big":-e123456789}|
353
+ assert_raises Exception do # Can be either Oj::ParseError or ArgumentError depending on Ruby version
354
+ Oj.sc_parse(handler, json)
355
+ end
356
+ end
357
+ end
358
+
325
359
  def test_pipe_close
360
+ # Windows does not support fork
361
+ return if RbConfig::CONFIG['host_os'] =~ /(mingw|mswin)/
362
+
326
363
  json = %{{"one":true,"two":false}}
327
364
  IO.pipe do |read_io, write_io|
328
365
  if fork