-
Notifications
You must be signed in to change notification settings - Fork 339
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
Encode Time objects as ISO8601 for compatibility with JS's Date.parse() #721
Comments
Actually I'm not even sure if this issue makes sense. Before generating JSON, I have to convert Ruby objects to Hashes anyway, so that's where I could convert Let me know what you think. Thanks. |
So I've been thinking a little bit about this. I've come up with something like this: require 'json'
require 'time'
def sanitize(node)
case node
when Time
node = node.iso8601(3)
when Array
node = node.map do |obj|
sanitize(obj)
end
when Hash
node = node.transform_values do |value|
sanitize(value)
end
end
node
end
def dump(obj)
::JSON.dump sanitize(obj)
end It works, but is obviously not very performant. The other obvious solution would be: class Time
def to_json(*a)
iso8601(3).to_json(*a)
end
end But since it's monkey patching, it just doesn't feel right. Refinements can't be used because the limited scope. PR #718 seems to work on a solution for this problem. |
Yes, that's why I hate the We could change the default behavior of I leave this open for now and probably consider it solved once |
Thanks for the input. For now the the monkey patch will have to do. Closing this now and looking forward to the |
I've read about the recent performance gains in this gem. Congratulations!
It's nothing urgent but eventually I'd love to switch from Oj to this gem. Currently I can't do that because
Time
objects are not encoded as ISO8601, which is what Javascript'sDate.parse()
expects.Specifically, I've been using these Oj options:
Would it be possible to add options
time_format: :xmlschema
andsecond_precision: N
toJSON.generate
?The text was updated successfully, but these errors were encountered: