oj
oj copied to clipboard
Issue encoding classes that define a to_json method
Oj ignores to_json methods defined on a class and encodes as it normally would without any to_json method being defined.
require 'json'
require 'oj'
class Klass
def self.to_json(*)
'correct_result'
end
end
puts JSON.dump(Klass)
puts Klass.to_json
puts Oj.dump(Klass)
puts Oj::Rails.encode(Klass)
This will output: correct_result correct_result {"^c":"Klass"} "Klass"
The was not something I anticipated anyone needing. I'll look at adding that this week.
Thank you so much! This isn't urgent as I was able to implement a workaround using the Singleton module, but I thought I'd bring it to your attention just so you're aware.