oj icon indicating copy to clipboard operation
oj copied to clipboard

Issue encoding classes that define a to_json method

Open jack-rabe opened this issue 3 years ago • 2 comments

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"

jack-rabe avatar Feb 04 '22 14:02 jack-rabe

The was not something I anticipated anyone needing. I'll look at adding that this week.

ohler55 avatar Feb 08 '22 01:02 ohler55

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.

jack-rabe avatar Feb 08 '22 13:02 jack-rabe