Skip to content

Serialization of timedeltas should be consistent #4665

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

Closed
nip3o opened this issue Nov 9, 2016 · 4 comments
Closed

Serialization of timedeltas should be consistent #4665

nip3o opened this issue Nov 9, 2016 · 4 comments

Comments

@nip3o
Copy link

nip3o commented Nov 9, 2016

When serializing a datetime.timedelta()as part of a JSON-object, it is serialized as the total number of seconds. Support for this was introduced in #584. When using a serializers.DurationField, it is serialized as a HH:MM:DD-like string, as introduced in #2989.

This can be quite troublesome on the client, since different API:s will have different behaviour depending on their implementation. In my specific use-case we transform the data from the API into moment.js duration objects, and depending on API implementation this currently has to be done in different ways.

I realize this would be a breaking change, but I think it is worth to discuss.

@xordoquy
Copy link
Collaborator

xordoquy commented Nov 9, 2016

You can still either override the JSONRenderer or create a custom duration field to make the output / input more consistent

@nip3o
Copy link
Author

nip3o commented Nov 9, 2016

Yup, that is what I'm currently doing, I just fail to see the point of serializing the same data type in different ways by default.

@tomchristie
Copy link
Member

tomchristie commented Nov 9, 2016

We could change the default implementation of JSONEncoder to handle timedelta differently, but I'm not particularly convinced at the moment. It'd be a little more consistent, but I don't think there's any absolute requirement that the default representation for DurationField should also happen to be the same as the default represention of datetime.timedelta (yes, it'd absolutely be nicer, but I wouldn't consider it a bug - these are two different cases, which happen to have the same internal type)

@tomchristie
Copy link
Member

the point of

It's historical, rather than by design.

If we do consider a solution then I think we need to make it easy to switch between the different usage styles (Eg. introduce a setting for it)

@tomchristie tomchristie closed this as not planned Won't fix, can't repro, duplicate, stale Oct 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants