oj: Oj::StreamWriter inconsistently handles Hashes with nil values

I am getting inconsistent results when running the following snippet of code within a irb console:

require 'oj'

io = StringIO.new
w = Oj::StreamWriter.new(io)
w.push_array
child = {"id"=>"1", "name"=>"foo", "nilElement"=>nil}
w.push_value(child)
puts io.string

specifically, the nilElement key/value pair is sometimes added and sometimes not.

# Output 1
"[{\"id\":\"1\",\"name\":\"foo\",\"nilElement\":null}"

# Output 2
"[{\"id\":\"1\",\"name\":\"foo\"}"

Within the same session, the behaviour stays consistent, i.e. nilElement is either always added or never added if w.push_value(child) is called multiple times.

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 23 (12 by maintainers)

Most upvoted comments

Ok, that not correct behaviour. Lets reopen this.

Okay, partly fixed. The fix is in the clear-error branch. It no longer crashes but it does seem to have a problem with the parsing that only shows up with the approach you are taking. I’ll dig some more tomorrow. Other commitments tonight.

It probably is a separate issue but I will look at it in any case.