Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

:compat differs from JSON & YAJL for Time instances #51

Closed
awd-switzerland opened this Issue Dec 5, 2012 · 4 comments

Comments

Projects
None yet
3 participants

In Rails: puts JSON.dump([Time.now]) # -> ["2012-12-05T16:52:54+01:00"]
In Plain Ruby: puts JSON.dump([Time.now]) # -> ["2012-12-05 16:54:23 +0100"]
Oj: puts Oj.dump([Time.now], mode: :compat) # -> [1354722798.022583000]

The rails version makes more sense than the plain ruby one, since it can be used by JS' Date constructor (new Date("2012-12-05T16:52:54+01:00") is valid and correct).

I'm not sure about the proper solution, though.

Owner

ohler55 commented Dec 5, 2012

The solution Oj uses is an option called :time_format that can be set as an option on dump or in the default_options. Set the time_format to :xmlschema or :ruby according to your preference.

@ohler55 ohler55 closed this Dec 5, 2012

I see, thanks for the information. Don't know why I missed that in the docs yesterday :)

rwz commented Dec 13, 2012

Won't it make sense to use :time_format => :ruby as a default for :compat mode? Otherwise it is not fully compatible :)

Owner

ohler55 commented Dec 13, 2012

Sadly, the various JSON parsers use different time formats. That is why there is both a mode and a time_format option. Most people would find it confusing to have compat mode also change the time_format option especially when it has been set by them earlier. It might have made sense if I had made specific compatible mode for each different JSON parser but that is not the way it started out and now it does not seem like a good idea to change the APIs since there are quite a few users.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment