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

Problem upgrading to v3 with Rails & JSON #673

Closed
cameronmd-telus opened this issue Jul 23, 2021 · 1 comment
Closed

Problem upgrading to v3 with Rails & JSON #673

cameronmd-telus opened this issue Jul 23, 2021 · 1 comment

Comments

@cameronmd-telus
Copy link

I apologize if this is not the correct place to post this, I think this may be more of a configuration problem or something that's just not supported, more than an "issue", per-se.

I am attempting to upgrade to Oj 3.x.x in a Rails application that is sorely out-of-date with regards to it's dependencies :(, and I'm running into an issue related to Oj.

This may be similar to: #390

The application is using JSON.dump/load to serialize/deserialize data in the model (i.e. serialize :property, MySerializerUsingJSON) as well as JSON.load/parse in a number of places. So far, there is at least one type that is being serialized differently than in 2.17.5: ActiveSupport::TimeWithZone.

With Oj 2.17.5 & the oj_mimic_json gem installed, JSON.dump would use Oj and the value would be formatted in ISO8601. However, with Oj 3.12.1 & 'Oj.optimize_rails`, the format is quite different. I'm guessing this is likely a case where Oj was not compatible with JSON, so it ended up being changed in 3.x.

Is there any way to configure Oj 3.x to ensure that use of JSON will serialize/deserialize ActiveSupport::TimeWithZone(and other types) as shown in 2.17.5, below?. I've not been able to determine how, if there is.

I could change the serialization to use Oj directly instead of JSON, but I'm not sure what other differences there may be.

Below are some examples of behaviour with version 2.17.5 and 3.12.1

Oj 2.17.5 without mimic_JSON called:

0> now = Time.zone.now
=> Fri, 01 Jan 2021 09:00:00 PST -08:00

0> now.class
=> ActiveSupport::TimeWithZone

0> JSON.dump(now)
=> "\"2021-01-01 09:00:00 -0800\""

0> Oj.dump(now)
=> "\"2021-01-01T09:00:00-08:00\""

0> ActiveSupport::JSON::Encoding.json_encoder.new.encode(now)
=> "\"2021-01-01T09:00:00-08:00\""

Oj 2.17.5 with mimic_JSON called:

0> now = Time.zone.now
=> Fri, 01 Jan 2021 09:00:00 PST -08:00

0> JSON.dump(now)
=> "\"2021-01-01T09:00:00-08:00\""

0> Oj.dump(now)
=> "\"2021-01-01T09:00:00-08:00\""

0> ActiveSupport::JSON::Encoding.json_encoder.new.encode(now)
=> "\"2021-01-01T09:00:00-08:00\""

Oj 2.17.5 Before and after mimic_JSON

0> now = Time.zone.now
=> Fri, 01 Jan 2021 09:00:00 PST -08:00

0> now.class
=> ActiveSupport::TimeWithZone

0> JSON.dump(now)
=> "\"2021-01-01 09:00:00 -0800\""

0> Oj.dump(now)
=> "\"2021-01-01T09:00:00-08:00\""

0> ActiveSupport::JSON::Encoding.json_encoder.new.encode(now)
=> "\"2021-01-01T09:00:00-08:00\""

0> Oj.mimic_JSON
=> JSON

0> JSON.dump(now)
=> "\"2021-01-01T09:00:00-08:00\""

0> Oj.dump(now)
=> "\"2021-01-01T09:00:00-08:00\""

0> ActiveSupport::JSON::Encoding.json_encoder.new.encode(now)
=> "\"2021-01-01T09:00:00-08:00\""

Oj 3.12.1 with optimize_rails

0> now = Time.zone.now
=> Fri, 01 Jan 2021 09:00:00 PST -08:00

0> now.class
=> ActiveSupport::TimeWithZone

0> JSON.dump(now)
=> "\"2021-01-01 09:00:00 -0800\""

0> Oj.dump(now)
=> "\"2021-01-01T09:00:00-08:00\""

0> ActiveSupport::JSON::Encoding.json_encoder.new.encode(now)
=> "\"2021-01-01T09:00:00-08:00\""

Environment:

  • Rails: 4.2.11.1
  • Ruby: 2.5.9
  • Oj: Attempting to upgrade from 2.17.5 to 3.12.1
@j1wilmot
Copy link

j1wilmot commented Mar 1, 2023

This behavior changed in #654 to make Oj consistent with JSON when using mimic_JSON. Using to_json instead of JSON.generate / JSON.dump may be one way to work around this for ActiveSupport::TimeWIthZone.

@ohler55 ohler55 closed this as completed Dec 4, 2023
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

3 participants