Sha256: 8f3720e900725fa8adea8b1ee0f8c2d5ea6fd07500dc18e22c092501134577e5

Contents?: true

Size: 1.46 KB

Versions: 6

Compression:

Stored size: 1.46 KB

Contents

#!/usr/bin/env ruby

$: << '.'
$: << '../lib'
$: << '../ext'

require 'oj'

Oj.default_options = {mode: :strict}
json = Oj.dump({ aggregations: { hashtags: { buckets: Array.new(100) { { key: 'foo', count: 100 } } } } }, mode: :strict)

def expect(a,b)
  puts "*** #{a} != #{b}" unless a == b
end

def dig(obj, *path)
  return obj if path.empty?
  
  if obj.is_a?(Array)
    obj.each { |v|
      r = dig(v, *path)
      return r unless r.nil?
    }
  end
  return nil unless obj.is_a?(Hash)
  
  dig(obj[path[0]], *path[1..-1])
end

1000.times do |i|
  Oj::Doc.open(json) do |doc|
    # this always passes
    doc.each_child('/aggregations/hashtags/buckets') do |bucket|
      expect(bucket.fetch('key'), 'foo')
      expect(bucket.fetch('count'), 100)
    end

    # load any other json using Oj.load
    # this will pass
    other = Oj.load(json)
    dig(other, 'aggregations', 'hashtags', 'buckets').each do |bucket|
      expect(bucket.fetch('key'), 'foo')
      expect(bucket.fetch('count'), 100)
    end
    GC.start
    doc.each_child('/aggregations/hashtags/buckets') do |bucket|
      # This is where it fails!!!! It will be some other object (even an rspec matcher in some cases)
      expect(bucket.fetch('key'), 'foo')
      expect(bucket.fetch('count'), 100)
    end

    # this always passes if it gets this far
    dig(other, 'aggregations', 'hashtags', 'buckets').each do |bucket|
      expect(bucket.fetch('key'), 'foo')
      expect(bucket.fetch('count'), 100)
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
oj-3.6.11 test/foo.rb
oj-3.6.10 test/foo.rb
oj-3.6.9 test/foo.rb
oj-3.6.8 test/foo.rb
oj-3.6.7 test/foo.rb
oj-3.6.5 test/foo.rb