Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue encoding classes that define a to_json method #747

Open
jack-rabe opened this issue Feb 4, 2022 · 2 comments
Open

Issue encoding classes that define a to_json method #747

jack-rabe opened this issue Feb 4, 2022 · 2 comments

Comments

@jack-rabe
Copy link

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"

@ohler55
Copy link
Owner

ohler55 commented Feb 8, 2022

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

@jack-rabe
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants