oj 2.18.3 → 2.18.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -6,6 +6,12 @@ $: << File.dirname(__FILE__)
6
6
  require 'helper'
7
7
 
8
8
  class Juice < Minitest::Test
9
+ def gen_whitespaced_string(length = Random.new.rand(100))
10
+ whitespace_chars = [" ", "\t", "\f", "\n", "\r"]
11
+ result = ""
12
+ length.times { result << whitespace_chars.sample }
13
+ result
14
+ end
9
15
 
10
16
  module TestModule
11
17
  end
@@ -128,6 +134,7 @@ class Juice < Minitest::Test
128
134
  :use_to_json=>false,
129
135
  :use_as_json=>false,
130
136
  :nilnil=>true,
137
+ :empty_string=>true,
131
138
  :allow_gc=>false,
132
139
  :quirks_mode=>false,
133
140
  :allow_invalid_unicode=>true,
@@ -1299,6 +1306,21 @@ class Juice < Minitest::Test
1299
1306
  assert_equal(nil, obj)
1300
1307
  end
1301
1308
 
1309
+ def test_empty_string_true
1310
+ 10.times do
1311
+ result = Oj.load(gen_whitespaced_string, :empty_string => true)
1312
+ assert_nil(result)
1313
+ end
1314
+ end
1315
+
1316
+ def test_empty_string_false
1317
+ 10.times do
1318
+ assert_raises(Oj::ParseError) do
1319
+ Oj.load(gen_whitespaced_string, :empty_string => false)
1320
+ end
1321
+ end
1322
+ end
1323
+
1302
1324
  def test_quirks_null_mode
1303
1325
  assert_raises(Oj::ParseError) { Oj.load("null", :quirks_mode => false) }
1304
1326
  assert_equal(nil, Oj.load("null", :quirks_mode => true))
@@ -44,6 +44,14 @@ class OjWriter < Minitest::Test
44
44
  assert_equal("{}\n", w.to_s)
45
45
  end
46
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
+
47
55
  def test_string_writer_nested_object
48
56
  w = Oj::StringWriter.new(:indent => 0)
49
57
  w.push_object()
@@ -289,4 +297,12 @@ class OjWriter < Minitest::Test
289
297
  assert_equal(%|{"a1":{},"a2":{"b":{}},"a3":{"a4":37}}\n|, output.string())
290
298
  end
291
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
292
308
  end # OjWriter
@@ -0,0 +1,185 @@
1
+
2
+ $: << File.expand_path("../../oj/ext", __FILE__)
3
+ $: << File.expand_path("../../oj/lib", __FILE__)
4
+
5
+ require 'rubygems'
6
+ require 'sqlite3'
7
+ require 'terminal-table'
8
+ require 'oj'
9
+ require 'rails/all'
10
+ require 'benchmark'
11
+ begin
12
+ require 'benchmark/memory'
13
+ rescue LoadError => e
14
+ end
15
+
16
+ OJ_1 = { mode: :object, use_as_json: false, float_precision: 16, bigdecimal_as_decimal: false, nan: :null }
17
+ OJ_2 = { mode: :compat, use_as_json: false, float_precision: 16, bigdecimal_as_decimal: false, nan: :null, time_format: :xmlschema, second_precision: 3 }
18
+ OJ_3 = { mode: :compat, use_as_json: true, float_precision: 16, bigdecimal_as_decimal: false, nan: :null, time_format: :xmlschema, second_precision: 3 }
19
+
20
+ # test data
21
+ class Colors
22
+ include Enumerable
23
+ def each
24
+ yield 'red'
25
+ yield 'green'
26
+ yield 'blue'
27
+ end
28
+ end
29
+
30
+ Struct.new('Customer', :name, :address)
31
+
32
+ fork { exit 99 }
33
+ Process.wait
34
+
35
+ ActiveRecord::Base.logger = Logger.new(STDERR)
36
+ ActiveRecord::Base.logger.level = 1
37
+
38
+ ActiveRecord::Base.establish_connection(
39
+ adapter: 'sqlite3',
40
+ database:':memory:'
41
+ )
42
+
43
+ ActiveRecord::Migration.verbose = false
44
+ ActiveRecord::Schema.define do
45
+ create_table :users do |table|
46
+ table.column :name, :string
47
+ end
48
+ end
49
+
50
+ class User < ActiveRecord::Base
51
+ validates :name, presence: true
52
+ end
53
+
54
+ user = User.new
55
+ user.valid?
56
+
57
+ TEST_DATA = {
58
+ Regexp: /test/,
59
+ FalseClass: false,
60
+ NilClass: nil,
61
+ Object: Object.new,
62
+ TrueClass: true,
63
+ String: 'abc',
64
+ StringChinese: '二胡',
65
+ Numeric: 1,
66
+ Symbol: :sym,
67
+ Time: Time.new,
68
+ Array: [],
69
+ Hash: {},
70
+ HashNotEmpty: {a: 1},
71
+ Date: Date.new,
72
+ DateTime: DateTime.new,
73
+ Enumerable: Colors.new,
74
+ BigDecimal: '1'.to_d/3,
75
+ BigDecimalInfinity: '0.5'.to_d/0,
76
+ Struct: Struct::Customer.new('Dave', '123 Main'),
77
+ Float: 1.0/3,
78
+ FloatInfinity: 0.5/0,
79
+ Range: (1..10),
80
+ 'Process::Status': $?,
81
+ 'ActiveSupport::TimeWithZone': Time.utc(2005,2,1,15,15,10).in_time_zone('Hawaii'),
82
+ 'ActiveModel::Errors': user.errors,
83
+ 'ActiveSupport::Duration': 1.month.ago,
84
+ 'ActiveSupport::Multibyte::Chars': 'über'.mb_chars,
85
+ 'ActiveRecord::Relation': User.where(name: 'aaa'),
86
+
87
+ # 'ActionDispatch::Journey::GTG::TransitionTable': TODO,
88
+ }
89
+
90
+ # helper
91
+ def compare(expected, result)
92
+ if result.is_a? Exception
93
+ '💀'
94
+ else
95
+ if expected != result
96
+ puts "*** expected: #{expected}"
97
+ puts "*** actual: #{result}"
98
+ end
99
+ expected == result ? '👌' : '❌'
100
+ end
101
+ end
102
+
103
+ # def compare(expected, result)
104
+ # if result.is_a? Exception
105
+ # 'Error'
106
+ # else
107
+ # expected == result ? 'Ok' : 'Fail'
108
+ # end
109
+ # end
110
+
111
+ # actual tests
112
+ test_result = TEST_DATA.map do |key, val|
113
+ to_json_result = val.to_json
114
+
115
+ json_generate_result = begin
116
+ JSON.generate(val)
117
+ rescue JSON::GeneratorError => e
118
+ e
119
+ end
120
+
121
+ Oj.default_options = OJ_1
122
+ oj_dump_result_1 = begin
123
+ if key == :DateTime
124
+ Exception.new('Unknown error')
125
+ else
126
+ Oj.dump(val)
127
+ end
128
+ rescue NoMemoryError => e
129
+ e
130
+ end
131
+
132
+ Oj.default_options = OJ_2
133
+ oj_dump_result_2 = begin
134
+ Oj.dump(val)
135
+ rescue NoMemoryError => e
136
+ e
137
+ end
138
+
139
+ Oj.default_options = OJ_3
140
+ oj_dump_result_3 = begin
141
+ Oj.dump(val)
142
+ rescue NoMemoryError => e
143
+ e
144
+ end
145
+
146
+ [key, {
147
+ to_json_result: to_json_result,
148
+ #json_generate: compare(to_json_result, json_generate_result),
149
+ #oj_dump_1: compare(to_json_result, oj_dump_result_1),
150
+ oj_dump_2: compare(to_json_result, oj_dump_result_2),
151
+ #oj_dump_3: compare(to_json_result, oj_dump_result_3),
152
+ }]
153
+ end.to_h
154
+
155
+ # format output
156
+ rows = test_result.map do |key, val|
157
+ [key, val[:json_generate], val[:oj_dump_1], val[:oj_dump_2], val[:oj_dump_3]]
158
+ end
159
+
160
+ puts "\nComparing Rails to_json with other JSON implementations\n"
161
+ puts Terminal::Table.new headings: ['class', 'JSON.generate', 'Oj.dump (object)', 'Oj.dump (compat)', 'Oj.dump (compat, as_json)'], rows: rows
162
+
163
+ Oj.default_options = OJ_3
164
+
165
+ obj = TEST_DATA
166
+
167
+ puts "\n"
168
+
169
+ =begin
170
+ if Benchmark.respond_to?(:memory)
171
+ Benchmark.memory do |x|
172
+ x.report('to_json:'){ 10_000.times { obj.to_json } }
173
+ x.report('Oj:') { 10_000.times { Oj.dump(obj) } }
174
+ x.compare!
175
+ end
176
+ puts "---------------------------------------------\n\n"
177
+ end
178
+
179
+ Benchmark.benchmark(Benchmark::CAPTION, 14, Benchmark::FORMAT) do |x|
180
+ x.report('to_json:'){ 10_000.times { obj.to_json } }
181
+ x.report('Oj:') { 10_000.times { Oj.dump(obj) } }
182
+ end
183
+ =end
184
+ puts "\n"
185
+
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: oj
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.18.3
4
+ version: 2.18.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Ohler
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-03-14 00:00:00.000000000 Z
11
+ date: 2017-03-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake-compiler
@@ -39,19 +39,19 @@ dependencies:
39
39
  - !ruby/object:Gem::Version
40
40
  version: '5'
41
41
  - !ruby/object:Gem::Dependency
42
- name: rails
42
+ name: wwtd
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - "~>"
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
- version: '4'
47
+ version: '0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - "~>"
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
- version: '4'
54
+ version: '0'
55
55
  description: 'The fastest JSON parser and object serializer. '
56
56
  email: peter@ohler.com
57
57
  executables: []
@@ -69,6 +69,7 @@ files:
69
69
  - ext/oj/circarray.h
70
70
  - ext/oj/compat.c
71
71
  - ext/oj/dump.c
72
+ - ext/oj/dump_custom.c
72
73
  - ext/oj/encode.h
73
74
  - ext/oj/err.c
74
75
  - ext/oj/err.h
@@ -113,10 +114,6 @@ files:
113
114
  - test/bug_fast.rb
114
115
  - test/bug_load.rb
115
116
  - test/crash.rb
116
- - test/curl/curl_oj.rb
117
- - test/curl/get_oj.rb
118
- - test/curl/just_curl.rb
119
- - test/curl/just_oj.rb
120
117
  - test/example.rb
121
118
  - test/files.rb
122
119
  - test/foo.rb
@@ -132,7 +129,9 @@ files:
132
129
  - test/isolated/test_mimic_rails_before.rb
133
130
  - test/isolated/test_mimic_rails_datetime.rb
134
131
  - test/isolated/test_mimic_redefine.rb
132
+ - test/mem.rb
135
133
  - test/mod.rb
134
+ - test/omit.rb
136
135
  - test/perf.rb
137
136
  - test/perf_compat.rb
138
137
  - test/perf_fast.rb
@@ -143,6 +142,7 @@ files:
143
142
  - test/perf_simple.rb
144
143
  - test/perf_strict.rb
145
144
  - test/rails.rb
145
+ - test/rails_datetime_test.rb
146
146
  - test/russian.rb
147
147
  - test/sample.rb
148
148
  - test/sample/change.rb
@@ -160,6 +160,7 @@ files:
160
160
  - test/sample_json.rb
161
161
  - test/struct.rb
162
162
  - test/test_compat.rb
163
+ - test/test_custom.rb
163
164
  - test/test_debian.rb
164
165
  - test/test_fast.rb
165
166
  - test/test_file.rb
@@ -173,6 +174,7 @@ files:
173
174
  - test/test_various.rb
174
175
  - test/test_writer.rb
175
176
  - test/write_timebars.rb
177
+ - test/x_test.rb
176
178
  homepage: http://www.ohler.com/oj
177
179
  licenses:
178
180
  - MIT
@@ -200,73 +202,74 @@ signing_key:
200
202
  specification_version: 4
201
203
  summary: A fast JSON parser and serializer.
202
204
  test_files:
203
- - test/_test_active.rb
204
- - test/_test_active_mimic.rb
205
- - test/_test_mimic_rails.rb
206
- - test/activesupport_datetime_test.rb
207
- - test/bug.rb
208
- - test/bug2.rb
209
- - test/bug3.rb
210
- - test/bug_fast.rb
211
- - test/bug_load.rb
212
- - test/crash.rb
213
- - test/curl/curl_oj.rb
214
- - test/curl/get_oj.rb
215
- - test/curl/just_curl.rb
216
- - test/curl/just_oj.rb
217
- - test/example.rb
218
- - test/files.rb
219
- - test/foo.rb
220
- - test/helper.rb
221
- - test/io.rb
222
- - test/isolated/shared.rb
223
- - test/isolated/test_mimic_after.rb
224
- - test/isolated/test_mimic_alone.rb
225
- - test/isolated/test_mimic_as_json.rb
226
- - test/isolated/test_mimic_before.rb
227
- - test/isolated/test_mimic_define.rb
228
- - test/isolated/test_mimic_rails_after.rb
229
- - test/isolated/test_mimic_rails_before.rb
230
- - test/isolated/test_mimic_rails_datetime.rb
231
- - test/isolated/test_mimic_redefine.rb
232
- - test/mod.rb
233
- - test/perf.rb
234
- - test/perf_compat.rb
235
- - test/perf_fast.rb
236
- - test/perf_file.rb
237
205
  - test/perf_object.rb
238
- - test/perf_saj.rb
239
- - test/perf_scp.rb
240
- - test/perf_simple.rb
241
- - test/perf_strict.rb
242
- - test/rails.rb
206
+ - test/test_hash.rb
207
+ - test/test_custom.rb
208
+ - test/bug3.rb
209
+ - test/write_timebars.rb
210
+ - test/test_writer.rb
243
211
  - test/russian.rb
244
- - test/sample/change.rb
245
- - test/sample/dir.rb
246
- - test/sample/doc.rb
212
+ - test/rails_datetime_test.rb
213
+ - test/omit.rb
214
+ - test/test_gc.rb
215
+ - test/_test_active_mimic.rb
216
+ - test/test_serializer.rb
247
217
  - test/sample/file.rb
248
218
  - test/sample/group.rb
219
+ - test/sample/oval.rb
220
+ - test/sample/rect.rb
249
221
  - test/sample/hasprops.rb
250
222
  - test/sample/layer.rb
223
+ - test/sample/text.rb
224
+ - test/sample/doc.rb
251
225
  - test/sample/line.rb
252
- - test/sample/oval.rb
253
- - test/sample/rect.rb
226
+ - test/sample/dir.rb
254
227
  - test/sample/shape.rb
255
- - test/sample/text.rb
256
- - test/sample.rb
257
- - test/sample_json.rb
258
- - test/struct.rb
259
- - test/test_compat.rb
260
- - test/test_debian.rb
261
- - test/test_fast.rb
262
- - test/test_file.rb
263
- - test/test_gc.rb
264
- - test/test_hash.rb
228
+ - test/sample/change.rb
229
+ - test/crash.rb
230
+ - test/io.rb
265
231
  - test/test_object.rb
232
+ - test/struct.rb
233
+ - test/mod.rb
234
+ - test/sample.rb
235
+ - test/perf_scp.rb
236
+ - test/bug2.rb
266
237
  - test/test_saj.rb
267
- - test/test_scp.rb
268
- - test/test_serializer.rb
269
238
  - test/test_strict.rb
239
+ - test/perf_saj.rb
240
+ - test/perf_simple.rb
241
+ - test/perf_fast.rb
242
+ - test/example.rb
243
+ - test/_test_active.rb
244
+ - test/perf_compat.rb
270
245
  - test/test_various.rb
271
- - test/test_writer.rb
272
- - test/write_timebars.rb
246
+ - test/activesupport_datetime_test.rb
247
+ - test/test_scp.rb
248
+ - test/bug_load.rb
249
+ - test/files.rb
250
+ - test/x_test.rb
251
+ - test/foo.rb
252
+ - test/isolated/test_mimic_rails_after.rb
253
+ - test/isolated/test_mimic_rails_datetime.rb
254
+ - test/isolated/test_mimic_redefine.rb
255
+ - test/isolated/test_mimic_as_json.rb
256
+ - test/isolated/shared.rb
257
+ - test/isolated/test_mimic_alone.rb
258
+ - test/isolated/test_mimic_after.rb
259
+ - test/isolated/test_mimic_before.rb
260
+ - test/isolated/test_mimic_rails_before.rb
261
+ - test/isolated/test_mimic_define.rb
262
+ - test/_test_mimic_rails.rb
263
+ - test/test_file.rb
264
+ - test/bug.rb
265
+ - test/mem.rb
266
+ - test/sample_json.rb
267
+ - test/test_debian.rb
268
+ - test/test_fast.rb
269
+ - test/rails.rb
270
+ - test/perf_file.rb
271
+ - test/test_compat.rb
272
+ - test/helper.rb
273
+ - test/perf.rb
274
+ - test/bug_fast.rb
275
+ - test/perf_strict.rb