Replies: 2 comments 2 replies
-
What is the main difference between |
Beta Was this translation helpful? Give feedback.
-
Hey, everyone, this write-up mostly relates to a caveat when using TL/DR: In Extended details below: My understanding is that with Here's a demonstration with these Oj settings: # Oj version 3.10.0
#JSON.dump
JSON.dump(Time.now.utc) => "2022-01-11 12:17:36 UTC"
# ActiveSupport::JSON.encode
ActiveSupport::JSON.encode(Time.now.utc) => "2022-01-11T12:17:36.614Z"
# Oj.dump
#0: rails.c:1468:Oj:}: dump Time
#0: rails.c:517:Oj:>: as_json Time
#0: rails.c:527:Oj:<: as_json Time
#0: rails.c:1468:Oj:}: dump String
#0: rails.c:1479:Oj:{: dump String
#0: rails.c:1479:Oj:{: dump Time
Oj.dump(Time.now.utc) => "2022-01-11T12:17:36.614Z" Notice above the Now, if we run the same code but add # Oj version 3.10.0
#JSON.dump
#0:dump_compat.c:940:Oj:}: dump Time
#0:dump_compat.c:965:Oj:{: dump Time
JSON.dump(Time.now.utc) => "2022-01-11 12:27:17 UTC"
# ActiveSupport::JSON.encode
#0: rails.c:1468:Oj:}: dump Time
#0: rails.c:1479:Oj:{: dump Time
ActiveSupport::JSON.encode(Time.now.utc) => "2022-01-11T12:27:17.051Z"
# Oj.dump
#0: rails.c:1468:Oj:}: dump Time
#0: rails.c:1479:Oj:{: dump Time
Oj.dump(Time.now.utc) => "2022-01-11T12:27:17.051Z" Both The bug in # Oj version 3.10.0
# JSON.dump
#0:dump_compat.c:940:Oj:}: dump Hash
#0:dump_compat.c:940:Oj:}: dump Time
#0:dump_compat.c:123:Oj:>: to_json Time
#0: rails.c:1468:Oj:}: dump Time
#0: rails.c:1479:Oj:{: dump Time
#0:dump_compat.c:131:Oj:<: to_json Time
#0:dump_compat.c:965:Oj:{: dump Time
#0:dump_compat.c:965:Oj:{: dump Hash
JSON.dump({created_at: Time.now.utc}) => {"created_at":"2022-01-11T12:27:16.966Z"} As you can see in the trace, This was fixed in # Oj version 3.10.1
# JSON.dump
#0:dump_compat.c:941:Oj:}: dump Hash
#0:dump_compat.c:941:Oj:}: dump Time
#0:dump_compat.c:123:Oj:>: to_json Time
#0:dump_compat.c:131:Oj:<: to_json Time
#0:dump_compat.c:966:Oj:{: dump Time
#0:dump_compat.c:966:Oj:{: dump Hash
JSON.dump({created_at: Time.now.utc}) => {"created_at":"2022-01-11 12:34:36 UTC"}
Because Oj has to mimic the JSON standard library gem time format and, separately, mimic the ActiveSupport time format (both of which Oj has no control over, it just needs to adapt) we might write a test that catches whenever one the formats change. |
Beta Was this translation helpful? Give feedback.
-
Please place notes about dealing with Rails and ActiveSupport in this discussion topic.
Beta Was this translation helpful? Give feedback.
All reactions