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

ActiveSupport::TimeWithZone not serialized like JSON #408

Closed
Elyahou opened this issue Jul 24, 2017 · 9 comments
Closed

ActiveSupport::TimeWithZone not serialized like JSON #408

Elyahou opened this issue Jul 24, 2017 · 9 comments

Comments

@Elyahou
Copy link

Elyahou commented Jul 24, 2017

now = Time.zone.now
puts "JSON: " + JSON.generate([now])
puts "Oj: " + Oj.dump([now], mode: :compat)

output:

JSON: ["2017-07-24 17:29:38 UTC"]
Oj: ["2017-07-24T17:29:38.071Z"]

Updated:
Using Rails 4.1.16
Ruby 2.2.7
json: 1.8.6
Oj: 3.3.2

@ohler55
Copy link
Owner

ohler55 commented Jul 24, 2017

I'll need more detail. The title implies you are using Rails or ActiveSupport yet you show code for the json gem only. What other initialization was done? There is a lot of interplay between ActiveSupport, the json gem, and Oj.

@Elyahou
Copy link
Author

Elyahou commented Jul 24, 2017

@ohler55 Thanks I updated with rails, ruby and json version. something else ?

@Elyahou
Copy link
Author

Elyahou commented Jul 24, 2017

I just reproduced with a clean Rails app with the specified versions

@ohler55
Copy link
Owner

ohler55 commented Jul 24, 2017

You are experiencing the Rails vs json gem conflicts. You will see that now.to_json matches Oj as does ActiveSupport::JSON.encode(now). Oj.generate(now) matches the JSON.generate(now) output however. You will also notice that without Rails Oj.dump(now, mode: :compat) matches the json gem if `Time.now``` is used.

Unfortunately the complexity and performance penalty for putting in conditional checks for the various combinations of ActiveSupport and json gem versions along with whether ActiveSupport is present to not is prohibitive. The current tests are for two general cases:

  1. Straight json gem v2.x mimicing with no ActiveSupport.
  2. ActiveSupport 5.x which pulls in the json gem but changes the to_json and as_json methods.

In case 2 no attempt is made to bypass the ActiveSupport changes in favor of the json gem version except for the Oj.generate method which should match JSON.generate.

So, finally after trying to explain the mess, if you want to use Rails and then call the json gem calls directly then the Oj.generate should be used instead of Oj.dump.

@Elyahou
Copy link
Author

Elyahou commented Jul 25, 2017

thanks @ohler55 For the explanation :)
I tried Oj.generate, but it seems that it do not solve the issue:

Oj.generate([now])
 => "[\"2017-07-25T02:33:49.602Z\"]"

@ohler55
Copy link
Owner

ohler55 commented Jul 25, 2017

That is odd. It worked for me but that was with ActiveRecord 5 and a newer json gem. The core of the issue is with the to_json or as_json method. Are you able to change either of those?

@ohler55
Copy link
Owner

ohler55 commented Aug 7, 2017

What is the status of this issue? Still open? If so did you try the suggested change?

@ohler55
Copy link
Owner

ohler55 commented Nov 20, 2017

Assuming it is closed.

@ohler55 ohler55 closed this as completed Nov 20, 2017
@j1wilmot
Copy link

j1wilmot commented Mar 1, 2023

I believe the behavior described here was addressed by #654

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