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

Oj.optimize_rails breaks JSON.load - creates BigDecimal instead of Float #914

Open
seandilda opened this issue Feb 5, 2024 · 3 comments

Comments

@seandilda
Copy link

I ran into an issue in my application after adding the oj gem. I tracked it down to oj turning some numeric values into BigDecimal objects when parsing them. Without oj, they are read as a Float object.

Loading development environment (Rails 6.1.7.6)
irb> JSON.load('{"key":175620.54484250804}')['key'].class
=> Float
irb> Oj.optimize_rails
=> nil
irb> JSON.load('{"key":175620.54484250804}')['key'].class
=> BigDecimal
irb>

Rails version: 6.1.7.6
Ruby version: 3.0

The particular issue I'm seeing happens when using JSON as the encoder for ActiveRecord::Store: https://api.rubyonrails.org/v6.1/classes/ActiveRecord/Store.html

@ohler55
Copy link
Owner

ohler55 commented Feb 5, 2024

It sounds like the :bigdecimal_load option is not set to :float. It gets a bit tricky with a mixture of rails and json. The calls that start with JSON. are from the json gem yet rails changes some of the defaults and setting of the json gem. Can you try setting :bigdecimal_load before calling JSON.load and see if that makes a difference?

@seandilda
Copy link
Author

Thank you. Running Oj.default_options = { bigdecimal_load: :float } did fix the issue.

Is this something that should be done automatically by Oj.optimize_rails ?
If there's a concern of breaking compatibility with existing deployments, could the rails page recommend setting this option?

@ohler55
Copy link
Owner

ohler55 commented Feb 5, 2024

There might be. I'll dig into it a bit more tonight. I'm not sure what the default is for rails and json. Does your environment change any defaults that might effect the processing and if so that would they be called?

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