Oj: Explicit type check on parse parameter

Created on 17 May 2017  路  8Comments  路  Source: ohler55/oj

Currently, when running in mimic_json or rails mode, Oj appears to do a hard-check on the argument passed to JSON.parse, and raises an ArgumentError if it is not a String or IO object.

This is a deviation from the standard lib JSON.parse, which allows for any string-like or IO-like object. Many gems and libraries have used (abused?) this feature.


Test case:

require 'oj'

Oj.mimic_JSON
Oj.add_to_json

class StringPlus

  def initialize(str)
    @str = str
  end
  def to_str
    @str
  end
  alias to_s to_str
end

test_str = StringPlus.new('{"testing": 123}')
JSON.parse(test_str)

Most helpful comment

Might take a day or two.

All 8 comments

Ouch, ok, I'll test the json gem and update Oj in mimic mode to handle that with a call to to_s and check for what ever IO methods are needed before that.

Great! Thanks for the fast response!

Might take a day or two.

or maybe less. :-) please try the latest master.

3.0.9 released

Checking it now, preliminary test seem to indicate success

Can this be closed?

Yep, sorry, it works perfectly!

Was this page helpful?
0 / 5 - 0 ratings