oj 3.14.3 → 3.15.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (66) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +4 -0
  3. data/ext/oj/custom.c +5 -15
  4. data/ext/oj/dump.c +27 -2
  5. data/ext/oj/mimic_json.c +21 -0
  6. data/ext/oj/object.c +7 -21
  7. data/ext/oj/oj.c +20 -0
  8. data/ext/oj/oj.h +3 -0
  9. data/ext/oj/strict.c +9 -27
  10. data/ext/oj/wab.c +9 -27
  11. data/lib/oj/version.rb +1 -1
  12. data/lib/oj.rb +3 -0
  13. data/pages/Options.md +4 -0
  14. data/test/_test_active.rb +8 -8
  15. data/test/_test_active_mimic.rb +7 -7
  16. data/test/_test_mimic_rails.rb +17 -19
  17. data/test/files.rb +14 -14
  18. data/test/foo.rb +5 -5
  19. data/test/helper.rb +4 -4
  20. data/test/mem.rb +8 -7
  21. data/test/perf.rb +21 -26
  22. data/test/perf_compat.rb +30 -32
  23. data/test/perf_dump.rb +25 -25
  24. data/test/perf_fast.rb +80 -82
  25. data/test/perf_file.rb +27 -29
  26. data/test/perf_object.rb +65 -68
  27. data/test/perf_once.rb +8 -7
  28. data/test/perf_parser.rb +40 -46
  29. data/test/perf_saj.rb +46 -53
  30. data/test/perf_scp.rb +57 -69
  31. data/test/perf_simple.rb +40 -38
  32. data/test/perf_strict.rb +68 -70
  33. data/test/perf_wab.rb +67 -69
  34. data/test/prec.rb +3 -3
  35. data/test/sample.rb +16 -15
  36. data/test/sample_json.rb +8 -7
  37. data/test/test_compat.rb +44 -46
  38. data/test/test_custom.rb +56 -42
  39. data/test/test_debian.rb +6 -9
  40. data/test/test_fast.rb +78 -72
  41. data/test/test_file.rb +16 -21
  42. data/test/test_gc.rb +5 -5
  43. data/test/test_generate.rb +5 -5
  44. data/test/test_hash.rb +4 -4
  45. data/test/test_integer_range.rb +9 -9
  46. data/test/test_null.rb +18 -20
  47. data/test/test_object.rb +76 -86
  48. data/test/test_parser.rb +4 -4
  49. data/test/test_parser_debug.rb +4 -4
  50. data/test/test_parser_saj.rb +31 -31
  51. data/test/test_parser_usual.rb +3 -3
  52. data/test/test_rails.rb +2 -2
  53. data/test/test_saj.rb +8 -8
  54. data/test/test_scp.rb +29 -29
  55. data/test/test_strict.rb +25 -31
  56. data/test/test_various.rb +121 -75
  57. data/test/test_wab.rb +43 -42
  58. data/test/test_writer.rb +46 -46
  59. data/test/tests.rb +7 -7
  60. data/test/tests_mimic.rb +6 -6
  61. data/test/tests_mimic_addition.rb +6 -6
  62. metadata +3 -6
  63. data/test/bar.rb +0 -11
  64. data/test/baz.rb +0 -16
  65. data/test/bug.rb +0 -16
  66. data/test/zoo.rb +0 -13
@@ -1,10 +1,10 @@
1
1
  #!/usr/bin/env ruby
2
- # encoding: UTF-8
2
+ # frozen_string_literal: true
3
3
 
4
- $: << File.dirname(__FILE__)
4
+ $LOAD_PATH << __dir__
5
5
 
6
6
  require 'helper'
7
- #Oj.mimic_JSON
7
+ # Oj.mimic_JSON
8
8
  require 'rails/all'
9
9
 
10
10
  require 'active_model'
@@ -36,14 +36,12 @@ end
36
36
  class MimicRails < Minitest::Test
37
37
 
38
38
  def test_mimic_exception
39
- begin
40
- ActiveSupport::JSON.decode("{")
41
- puts "Failed"
42
- rescue ActiveSupport::JSON.parse_error
43
- assert(true)
44
- rescue Exception
45
- assert(false, 'Expected a JSON::ParserError')
46
- end
39
+ ActiveSupport::JSON.decode('{')
40
+ puts 'Failed'
41
+ rescue ActiveSupport::JSON.parse_error
42
+ assert(true)
43
+ rescue Exception
44
+ assert(false, 'Expected a JSON::ParserError')
47
45
  end
48
46
 
49
47
  def test_dump_string
@@ -84,11 +82,11 @@ class MimicRails < Minitest::Test
84
82
  category = Category.new(1, 'test')
85
83
  serializer = CategorySerializer.new(category)
86
84
 
87
- json = serializer.to_json()
85
+ serializer.to_json()
88
86
  puts "*** serializer.to_json() #{serializer.to_json()}"
89
- json = serializer.as_json()
87
+ serializer.as_json()
90
88
  puts "*** serializer.as_json() #{serializer.as_json()}"
91
- json = JSON.dump(serializer)
89
+ JSON.dump(serializer)
92
90
  puts "*** JSON.dump(serializer) #{JSON.dump(serializer)}"
93
91
 
94
92
  puts "*** category.to_json() #{category.to_json()}"
@@ -103,7 +101,7 @@ class MimicRails < Minitest::Test
103
101
  cat2 = Category.new(2, 'test')
104
102
  a = Array.wrap([cat1, cat2])
105
103
 
106
- #serializer = CategorySerializer.new(a)
104
+ # serializer = CategorySerializer.new(a)
107
105
 
108
106
  puts "*** a.to_json() #{a.to_json()}"
109
107
  puts "*** a.as_json() #{a.as_json()}"
@@ -113,13 +111,13 @@ class MimicRails < Minitest::Test
113
111
 
114
112
  def test_dump_time
115
113
  Oj.default_options= {:indent => 2}
116
- now = ActiveSupport::TimeZone['America/Chicago'].parse("2014-11-01 13:20:47")
114
+ now = ActiveSupport::TimeZone['America/Chicago'].parse('2014-11-01 13:20:47')
117
115
  json = Oj.dump(now, mode: :object, time_format: :xmlschema)
118
- #puts "*** json: #{json}"
116
+ # puts "*** json: #{json}"
119
117
 
120
118
  oj_dump = Oj.load(json, mode: :object, time_format: :xmlschema)
121
- #puts "Now: #{now}\n Oj: #{oj_dump}"
122
- assert_equal("2014-11-01T13:20:47-05:00", oj_dump.xmlschema)
119
+ # puts "Now: #{now}\n Oj: #{oj_dump}"
120
+ assert_equal('2014-11-01T13:20:47-05:00', oj_dump.xmlschema)
123
121
  end
124
122
 
125
123
  end # MimicRails
data/test/files.rb CHANGED
@@ -1,29 +1,29 @@
1
1
  #!/usr/bin/env ruby -wW2
2
+ # frozen_string_literal: true
2
3
 
3
- if $0 == __FILE__
4
- $: << '.'
5
- $: << '..'
6
- $: << '../lib'
7
- $: << '../ext'
4
+ if $PROGRAM_NAME == __FILE__
5
+ $LOAD_PATH << '.'
6
+ $LOAD_PATH << '..'
7
+ $LOAD_PATH << '../lib'
8
+ $LOAD_PATH << '../ext'
8
9
  end
9
10
 
10
11
  require 'sample/file'
11
12
  require 'sample/dir'
12
13
 
13
14
  def files(dir)
14
- d = ::Sample::Dir.new(dir)
15
+ d = Sample::Dir.new(dir)
15
16
  Dir.new(dir).each do |fn|
16
17
  next if fn.start_with?('.')
17
18
 
18
19
  filename = File.join(dir, fn)
19
- #filename = '.' == dir ? fn : File.join(dir, fn)
20
- if File.directory?(filename)
21
- d << files(filename)
22
- else
23
- d << ::Sample::File.new(filename)
24
- end
20
+ # filename = '.' == dir ? fn : File.join(dir, fn)
21
+ d << if File.directory?(filename)
22
+ files(filename)
23
+ else
24
+ Sample::File.new(filename)
25
+ end
25
26
  end
26
- #pp d
27
+ # pp d
27
28
  d
28
29
  end
29
-
data/test/foo.rb CHANGED
@@ -1,14 +1,14 @@
1
1
  #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
2
3
 
3
- $: << '.'
4
- $: << File.join(File.dirname(__FILE__), "../lib")
5
- $: << File.join(File.dirname(__FILE__), "../ext")
4
+ $LOAD_PATH << '.'
5
+ $LOAD_PATH << File.join(__dir__, '../lib')
6
+ $LOAD_PATH << File.join(__dir__, '../ext')
6
7
 
7
- require "oj"
8
+ require 'oj'
8
9
 
9
10
  GC.stress = true
10
11
 
11
- require "oj"
12
12
  Oj.mimic_JSON
13
13
 
14
14
  Oj.add_to_json(Hash)
data/test/helper.rb CHANGED
@@ -20,8 +20,8 @@ require 'oj'
20
20
  def verify_gc_compaction
21
21
  # This method was added in Ruby 3.0.0. Calling it this way asks the GC to
22
22
  # move objects around, helping to find object movement bugs.
23
- if defined?(GC.verify_compaction_references) == 'method' && !(RbConfig::CONFIG['host_os'] =~ /(mingw|mswin)/)
24
- if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("3.2.0")
23
+ if defined?(GC.verify_compaction_references) == 'method' && RbConfig::CONFIG['host_os'] !~ /(mingw|mswin)/
24
+ if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('3.2.0')
25
25
  GC.verify_compaction_references(expand_heap: true, toward: :empty)
26
26
  else
27
27
  GC.verify_compaction_references(double_heap: true, toward: :empty)
@@ -33,7 +33,7 @@ $ruby = RUBY_DESCRIPTION.split(' ')[0]
33
33
  $ruby = 'ree' if 'ruby' == $ruby && RUBY_DESCRIPTION.include?('Ruby Enterprise Edition')
34
34
 
35
35
  class Range
36
- def to_hash()
37
- { 'begin' => self.begin, 'end' => self.end, 'exclude_end' => self.exclude_end? }
36
+ def to_hash
37
+ { 'begin' => self.begin, 'end' => self.end, 'exclude_end' => exclude_end? }
38
38
  end
39
39
  end
data/test/mem.rb CHANGED
@@ -1,16 +1,17 @@
1
1
  #!/usr/bin/env ruby
2
- # encoding: UTF-8
2
+ # frozen_string_literal: true
3
3
 
4
- $: << '.'
5
- $: << File.join(File.dirname(__FILE__), "../lib")
6
- $: << File.join(File.dirname(__FILE__), "../ext")
4
+ require 'English'
5
+ $LOAD_PATH << '.'
6
+ $LOAD_PATH << File.join(__dir__, '../lib')
7
+ $LOAD_PATH << File.join(__dir__, '../ext')
7
8
 
8
9
  require 'oj'
9
10
 
10
11
  Oj.default_options = { mode: :rails, cache_keys: false, cache_str: -1 }
11
12
 
12
13
  def mem
13
- `ps -o rss= -p #{$$}`.to_i
14
+ `ps -o rss= -p #{$PROCESS_ID}`.to_i
14
15
  end
15
16
 
16
17
  ('a'..'z').each { |a|
@@ -20,8 +21,8 @@ end
20
21
  ('a'..'z').each { |e|
21
22
  ('a'..'z').each { |f|
22
23
  key = "#{a}#{b}#{c}#{d}#{e}#{f}"
23
- x = Oj.load(%|{ "#{key}": 101}|)
24
- #Oj.dump(x)
24
+ Oj.load(%|{ "#{key}": 101}|)
25
+ # Oj.dump(x)
25
26
  }
26
27
  }
27
28
  }
data/test/perf.rb CHANGED
@@ -1,6 +1,8 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Perf
2
4
 
3
- def initialize()
5
+ def initialize
4
6
  @items = []
5
7
  end
6
8
 
@@ -18,7 +20,7 @@ class Perf
18
20
  end
19
21
 
20
22
  def run(iter)
21
- base = Item.new(nil, nil) { }
23
+ base = Item.new(nil, nil) {}
22
24
  base.run(iter, 0.0)
23
25
  @items.each do |i|
24
26
  i.run(iter, base.duration)
@@ -31,9 +33,7 @@ class Perf
31
33
  summary()
32
34
  end
33
35
 
34
- def summary()
35
- fastest = nil
36
- slowest = nil
36
+ def summary
37
37
  width = 6
38
38
  @items.each do |i|
39
39
  next if i.duration.nil?
@@ -44,23 +44,20 @@ class Perf
44
44
  iva.delete_if { |i| i.duration.nil? }
45
45
  iva = iva.sort_by { |i| i.duration }
46
46
  puts
47
- puts "Summary:"
48
- puts "%*s time (secs) rate (ops/sec)" % [width, 'System']
47
+ puts 'Summary:'
48
+ puts '%*s time (secs) rate (ops/sec)' % [width, 'System']
49
49
  puts "#{'-' * width} ----------- --------------"
50
50
  iva.each do |i|
51
- if i.duration.nil?
52
- else
53
- puts "%*s %11.3f %14.3f" % [width, i.title, i.duration, i.rate ]
54
- end
51
+ puts '%*s %11.3f %14.3f' % [width, i.title, i.duration, i.rate ] unless i.duration.nil?
55
52
  end
56
53
  puts
57
54
  puts "Comparison Matrix\n(performance factor, 2.0 means row is twice as fast as column)"
58
- puts ([' ' * width] + iva.map { |i| "%*s" % [width, i.title] }).join(' ')
59
- puts (['-' * width] + iva.map { |i| '-' * width }).join(' ')
55
+ puts ([' ' * width] + iva.map { |i| '%*s' % [width, i.title] }).join(' ')
56
+ puts (['-' * width] + iva.map { |_i| '-' * width }).join(' ')
60
57
  iva.each do |i|
61
- line = ["%*s" % [width, i.title]]
58
+ line = ['%*s' % [width, i.title]]
62
59
  iva.each do |o|
63
- line << ("%*.2f" % [width, o.duration / i.duration])
60
+ line << ('%*.2f' % [width, o.duration / i.duration])
64
61
  end
65
62
  puts line.join(' ')
66
63
  end
@@ -90,17 +87,15 @@ class Perf
90
87
  end
91
88
 
92
89
  def run(iter, base)
93
- begin
94
- GC.start
95
- @before.call unless @before.nil?
96
- start = Time.now
97
- iter.times { @blk.call }
98
- @duration = Time.now - start - base
99
- @duration = 0.0 if @duration < 0.0
100
- @rate = iter / @duration
101
- rescue Exception => e
102
- @error = "#{e.class}: #{e.message}"
103
- end
90
+ GC.start
91
+ @before.call unless @before.nil?
92
+ start = Time.now
93
+ iter.times { @blk.call }
94
+ @duration = Time.now - start - base
95
+ @duration = 0.0 if @duration < 0.0
96
+ @rate = iter / @duration
97
+ rescue Exception => e
98
+ @error = "#{e.class}: #{e.message}"
104
99
  end
105
100
 
106
101
  end # Item
data/test/perf_compat.rb CHANGED
@@ -1,9 +1,9 @@
1
1
  #!/usr/bin/env ruby
2
- # encoding: UTF-8
2
+ # frozen_string_literal: true
3
3
 
4
- $: << '.'
5
- $: << File.join(File.dirname(__FILE__), "../lib")
6
- $: << File.join(File.dirname(__FILE__), "../ext")
4
+ $LOAD_PATH << '.'
5
+ $LOAD_PATH << File.join(__dir__, '../lib')
6
+ $LOAD_PATH << File.join(__dir__, '../ext')
7
7
 
8
8
  require 'optparse'
9
9
  require 'perf'
@@ -11,25 +11,23 @@ require 'oj'
11
11
 
12
12
  $verbose = false
13
13
  $indent = 0
14
- $iter = 20000
14
+ $iter = 20_000
15
15
  $size = 0
16
16
 
17
17
  opts = OptionParser.new
18
- opts.on("-v", "verbose") { $verbose = true }
19
- opts.on("-c", "--count [Int]", Integer, "iterations") { |i| $iter = i }
20
- opts.on("-i", "--indent [Int]", Integer, "indentation") { |i| $indent = i }
21
- opts.on("-s", "--size [Int]", Integer, "size (~Kbytes)") { |i| $size = i }
22
- opts.on("-h", "--help", "Show this display") { puts opts; Process.exit!(0) }
23
- files = opts.parse(ARGV)
18
+ opts.on('-v', 'verbose') { $verbose = true }
19
+ opts.on('-c', '--count [Int]', Integer, 'iterations') { |i| $iter = i }
20
+ opts.on('-i', '--indent [Int]', Integer, 'indentation') { |i| $indent = i }
21
+ opts.on('-s', '--size [Int]', Integer, 'size (~Kbytes)') { |i| $size = i }
22
+ opts.on('-h', '--help', 'Show this display') { puts opts; Process.exit!(0) }
23
+ opts.parse(ARGV)
24
24
 
25
25
  def capture_error(tag, orig, load_key, dump_key, &blk)
26
- begin
27
- obj = blk.call(orig)
28
- puts obj unless orig == obj
29
- raise "#{tag} #{dump_key} and #{load_key} did not return the same object as the original." unless orig == obj
30
- rescue Exception => e
31
- $failed[tag] = "#{e.class}: #{e.message}"
32
- end
26
+ obj = blk.call(orig)
27
+ puts obj unless orig == obj
28
+ raise "#{tag} #{dump_key} and #{load_key} did not return the same object as the original." unless orig == obj
29
+ rescue Exception => e
30
+ $failed[tag] = "#{e.class}: #{e.message}"
33
31
  end
34
32
 
35
33
  # Verify that all packages dump and load correctly and return the same Object as the original.
@@ -39,7 +37,7 @@ capture_error('JSON::Ext', $obj, 'generate', 'parse') { |o|
39
37
  require 'json/ext'
40
38
  JSON.generator = JSON::Ext::Generator
41
39
  JSON.parser = JSON::Ext::Parser
42
- JSON.load(JSON.generate(o))
40
+ JSON.parse(JSON.generate(o))
43
41
  }
44
42
 
45
43
  module One
@@ -47,7 +45,7 @@ module One
47
45
  module Three
48
46
  class Empty
49
47
 
50
- def initialize()
48
+ def initialize
51
49
  @a = 1
52
50
  @b = 2
53
51
  @c = 3
@@ -58,16 +56,16 @@ module One
58
56
  end
59
57
  alias == eql?
60
58
 
61
- def as_json(*a)
59
+ def as_json(*_a)
62
60
  {JSON.create_id => self.class.name, 'a' => @a, 'b' => @b, 'c' => @c }
63
61
  end
64
-
65
- def to_json(*a)
62
+
63
+ def to_json(*_a)
66
64
  JSON.generate(as_json())
67
65
  end
68
66
 
69
- def self.json_create(h)
70
- self.new()
67
+ def self.json_create(_h)
68
+ new()
71
69
  end
72
70
  end # Empty
73
71
  end # Three
@@ -77,8 +75,8 @@ end # One
77
75
  $obj = {
78
76
  'a' => 'Alpha', # string
79
77
  'b' => true, # boolean
80
- 'c' => 12345, # number
81
- 'd' => [ true, [false, [-123456789, nil], 3.9676, ['Something else.', false], nil]], # mix it up array
78
+ 'c' => 12_345, # number
79
+ 'd' => [ true, [false, [-123_456_789, nil], 3.9676, ['Something else.', false], nil]], # mix it up array
82
80
  'e' => { 'zero' => nil, 'one' => 1, 'two' => 2, 'three' => [3], 'four' => [0, 1, 2, 3, 4] }, # hash
83
81
  'f' => nil, # nil
84
82
  'g' => One::Two::Three::Empty.new(),
@@ -109,13 +107,13 @@ if $verbose
109
107
  end
110
108
 
111
109
  puts '-' * 80
112
- puts "Compat Parse Performance"
110
+ puts 'Compat Parse Performance'
113
111
  perf = Perf.new()
114
- unless $failed.has_key?('JSON::Ext')
115
- perf.add('JSON::Ext', 'parse') { JSON.load($json) }
112
+ unless $failed.key?('JSON::Ext')
113
+ perf.add('JSON::Ext', 'parse') { JSON.parse($json) }
116
114
  perf.before('JSON::Ext') { JSON.parser = JSON::Ext::Parser }
117
115
  end
118
- unless $failed.has_key?('Oj:compat')
116
+ unless $failed.key?('Oj:compat')
119
117
  perf.add('Oj:compat', 'compat_load') { Oj.compat_load($json) }
120
118
  end
121
119
  perf.run($iter)
@@ -125,6 +123,6 @@ puts '-' * 80
125
123
  puts
126
124
 
127
125
  unless $failed.empty?
128
- puts "The following packages were not included for the reason listed"
126
+ puts 'The following packages were not included for the reason listed'
129
127
  $failed.each { |tag, msg| puts "***** #{tag}: #{msg}" }
130
128
  end
data/test/perf_dump.rb CHANGED
@@ -1,27 +1,27 @@
1
1
  #!/usr/bin/env ruby
2
- # encoding: UTF-8
2
+ # frozen_string_literal: true
3
3
 
4
- $: << '.'
5
- $: << File.join(File.dirname(__FILE__), "../lib")
6
- $: << File.join(File.dirname(__FILE__), "../ext")
4
+ $LOAD_PATH << '.'
5
+ $LOAD_PATH << File.join(__dir__, '../lib')
6
+ $LOAD_PATH << File.join(__dir__, '../ext')
7
7
 
8
8
  require 'optparse'
9
9
  require 'oj'
10
10
 
11
- $verbose = false
12
- $indent = 2
13
- $iter = 100_000
14
- $size = 2
11
+ @verbose = false
12
+ @indent = 2
13
+ @iter = 100_000
14
+ @size = 2
15
15
 
16
16
  opts = OptionParser.new
17
- opts.on("-v", "verbose") { $verbose = true }
18
- opts.on("-c", "--count [Int]", Integer, "iterations") { |i| $iter = i }
19
- opts.on("-i", "--indent [Int]", Integer, "indentation") { |i| $indent = i }
20
- opts.on("-s", "--size [Int]", Integer, "size (~Kbytes)") { |i| $size = i }
21
- opts.on("-h", "--help", "Show this display") { puts opts; Process.exit!(0) }
22
- files = opts.parse(ARGV)
23
-
24
- $obj = {
17
+ opts.on('-v', 'verbose') { @verbose = true }
18
+ opts.on('-c', '--count [Int]', Integer, 'iterations') { |i| @iter = i }
19
+ opts.on('-i', '--indent [Int]', Integer, 'indentation') { |i| @indent = i }
20
+ opts.on('-s', '--size [Int]', Integer, 'size (~Kbytes)') { |i| @size = i }
21
+ opts.on('-h', '--help', 'Show this display') { puts opts; Process.exit!(0) }
22
+ opts.parse(ARGV)
23
+
24
+ @obj = {
25
25
  'a' => 'Alpha', # string
26
26
  'b' => true, # boolean
27
27
  'c' => 12345, # number
@@ -32,19 +32,19 @@ $obj = {
32
32
  'i' => [[[[[[[nil]]]]]]] # deep array, again, not that deep
33
33
  }
34
34
 
35
- Oj.default_options = { :indent => $indent, :mode => :strict }
35
+ Oj.default_options = { :indent => @indent, :mode => :strict }
36
36
 
37
- if 0 < $size
38
- o = $obj
39
- $obj = []
40
- (4 * $size).times do
41
- $obj << o
37
+ if 0 < @size
38
+ o = @obj
39
+ @obj = []
40
+ (4 * @size).times do
41
+ @obj << o
42
42
  end
43
43
  end
44
44
 
45
- $json = Oj.dump($obj)
45
+ @json = Oj.dump(@obj)
46
46
  GC.start
47
47
  start = Time.now
48
- $iter.times { Oj.dump($obj) }
48
+ @iter.times { Oj.dump(@obj) }
49
49
  duration = Time.now - start
50
- puts "Dumped #{$json.length} byte JSON #{$iter} times in %0.3f seconds or %0.3f iteration/sec." % [duration, $iter / duration]
50
+ puts "Dumped #{@json.length} byte JSON #{@iter} times in %0.3f seconds or %0.3f iteration/sec." % [duration, @iter / duration]