-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
JsonSerializer support for TimeSpan in 3.0? #29932
Comments
We currently do not have plans to support If we were to add it, we would also want to add |
Thanks - a custom converter would also be an easy enough way to make it work for our app for 3.0. Are those capabilities planned to ship with preview 7? |
Yes the custom converter support is now in master and thus will be in preview 7. However, since TimeSpan is a common BCL type we should still provide a default converter. |
We reviewed this today:
|
The standard encoding for time spans would be ISO8601's "duration". This is also what XML use to represent Now, arguably, |
This also changed default behavior of how AspNetCore handing returning |
The simplest solution is to create a custom converter:
|
Talking about customer evidence: for our software-development support for TimeSpan is highly necessary. |
Ran into this today porting an app to .NET Core 3.0 as well. Because this is closed does that mean Microsoft has no plans to add native support? @khellang's comment seems a pretty convincing argument to me that it should be on a roadmap somewhere... |
Re-opening for 5.0 based on additional asks. The ISO8601 duration is likely the best representation although compat with Newtonsoft should also be considered. |
Just ran into this issue today. The default behavior is worse than previous behavior and was entirely unexpected. We should fix it, either by using ISO8601 or being compatible with Newtonsoft. |
@mfeingol What behavior? That it simply fails?
It's really easy to just add the workaround @rezabayesteh mentioned. |
@khellang: what I observed with a relatively vanilla ASP.NET Core project is that it serializes a
I did, but that shouldn't be necessary for such a commonly used type. |
I've just found this issue today (reported by my customer) and have to switch back all of my aspnet webapi and apps to use Newtonsoft.Json serializer instead using configuration in Startup.cs: services.AddControllers() In my cases I use a few nullable TimeSpan (TimeSpan?) and System.Text.Json has serialized it as: { This causes a little problem for javascript objects in web browsers, as well as for various cross-platform (means various programming languages) deserializers consuming my apis. I would expect same serialization result as Newtonsoft.Json serializer: { |
@bashocz Why won't the workaround mentioned in https://github.com/dotnet/corefx/issues/38641#issuecomment-540200476 work for you? |
@khellang I just want to highlight TimeSpan serialization is an issue for another developers, and give it an attention. I'd very like to switch to System.Text.Json, however there are some obstacles. I'd investigated new System.Text.Json a few weeks ago and found it is not feature complete. I've raised an issue dotnet/corefx#41747 and was pointed to other dotnet/corefx#39031, dotnet/corefx#41325, dotnet/corefx#38650 related. Because of that all of our internal microservices still use Newtonsoft.Json. For unknown reason I forgot to manage devs to fix it on public APIs and webapps as well. BTW: I try to avoid workarounds as much as possible in production code.. it's hard to maintain and remove it in future. |
@khellang , it's not that a workaround won't work. It's just such a basic thing shouldn't need developers to add a workaround. As a big feature introduced for .NET core 3, it shouldn't lack such basic implementations. |
@arisewanggithub There are global settings available to controllers. You can configure it via |
Is this closed cause we are having workaround?! |
The issue is still open, pending an API/behavior proposal, review and implementation. In the meantime, it's pretty easy to work around by using the converter mentioned in https://github.com/dotnet/corefx/issues/38641#issuecomment-540200476. |
For anyone blocked by this, here's a NuGet package with a converter (JsonTimeSpanConverter) we can use ahead of the 5.0 drop: Macross.Json.Extensions Supports TimeSpan & Nullable<TimeSpan> types. |
@eiriktsarpalis, as I wrote in the PR, I feel burned out and resigning therefore. It would be better to not prevent others from taking this. |
Take care @YohDeadfall, and thank you for working on the feature. |
@eiriktsarpalis @layomia I'm happy to pick this up if that would be helpful. |
Thanks @CodeBlanch, I've assigned the issue to you. @YohDeadfall's PR #51492 would be a good starting point, in particular the conversation around the serialization format. |
@eiriktsarpalis I just read through the thread, it sounds like we want to go with standard TimeSpan format instead of ISO8601 can you confirm that for me, and then I'll get started? |
The issue with using the ISO860 period format is that it can represent values that don't fit cleanly into a This is why in Noda Time there are separate FWIW, my advice is indeed to align to |
Yes, I think we go for the standard TimeSpan format and aim for roundtrip compatibility with Json.NET. cc @JamesNK who might have opinions on the matter. |
Following #53539 this feature will be adding one new API to facilitate source generation: namespace System.Text.Json.Serialization.Metadata
{
public static class JsonMetadataServices
{
public static System.Text.Json.Serialization.JsonConverter<System.TimeSpan> TimeSpanConverter { get; }
}
} |
This will be added to the netstandard2.0 builds too, right? (Since it can be, as TimeSpan is not a new type.) Thus the additional discussions on #53539 with respect to that won’t apply here? |
namespace System.Text.Json.Serialization.Metadata
{
public static class JsonMetadataServices
{
public static JsonConverter<TimeSpan> TimeSpanConverter { get; }
}
} |
EDIT @eiriktsarpalis: See #29932 (comment) for the API proposal.
Apologies if this is already answered somewhere else, but if it was my search abilities failed me.
I'm trying out the preview 6 of 3.0 with some existing production application code, and one of the issues that's come out of testing is that some of our existing objects we use in our API contracts use properties which are
TimeSpan
values, which are then represented as strings.Is support for
TimeSpan
properties planned for 3.0 for the new System.Text.Json APIs?If it's not going to be that gives us notice to do some refactoring ahead of September to change them to strings so we can use the new serializer, where as if it's planned but just not implemented yet then we just need to wait for a later preview to get this working.
Below is a minimal repro unit test that demonstrates the failure for
TimeSpan
to be handled compared to our existing JSON .NET code.The text was updated successfully, but these errors were encountered: