Skip to content

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

@seandilda

Description

@seandilda

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions