Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System Text JsonSerializer Deserialization of TimeSpan

In researching how to deserialize a TimeSpan using Newtonsoft's JSON.net I came across code in my current project that did not use Json.net. It used System.Text.Json.JsonSerializer and appeared to not fail on the operation of deserializing the TimeSpan property, as per the unit tests I was running.

Great I thought, .Net Core 3.1 has surpassed the historical issue of deserializing a TimeSpan and all is good. So fired up a test case in the latest version of Linqpad 6 (which uses .NET Core) to verify and to my chagrin it failed.


So the question is, can the TimeSpan be serialized/deserialized using either library (and if so how)… or is my test case below flawed in some respect?


Code

public class Project { public TimeSpan AverageScanTime { get; set; } }

Linqpad C# Code

var newP = new Project() { AverageScanTime = TimeSpan.FromHours(1) };

newP.Dump("New one");

var json = System.Text.Json.JsonSerializer.Serialize(newP);

json.Dump("JSON serialized");

System.Text.Json.JsonSerializer.Deserialize<Project>(json)
                               .Dump("JSON Deserialize");

Deserialize Failure

enter image description here

like image 485
ΩmegaMan Avatar asked Jan 02 '20 00:01

ΩmegaMan


People also ask

What is JsonSerializer deserialize?

Overloads. Deserialize(Stream, Type, JsonSerializerOptions) Reads the UTF-8 encoded text representing a single JSON value into a returnType . The Stream will be read to completion.

Which is better Newtonsoft JSON or System text JSON?

Json does case-insensitive property name matching by default. The System. Text. Json default is case-sensitive, which gives better performance since it's doing an exact match.

What does JsonSerializer serialize do?

JsonSerializer Class (System.Text.Json) Provides functionality to serialize objects or value types to JSON and to deserialize JSON into objects or value types.

Is Newtonsoft JSON obsolete?

Obsolete. The value types allowed by the JsonSchema. JSON Schema validation has been moved to its own package. See https://www.newtonsoft.com/jsonschema for more details.


3 Answers

JsonSerializer for TimeSpan seem will added in Future (removed from .NET 6 milestone). You can trace this issue in Future milestone or this issue.

At this time, you can implement JsonTimeSpanConverter on your own. Or you can install Macross.Json.Extensions nuget package and follow the instruction to de/serializer.

like image 197
Poy Chang Avatar answered Oct 21 '22 15:10

Poy Chang


An addition to the answer from Poy Chang

Swagger (Swashbuckle) also requires a configuration

services.AddSwaggerGen(options =>
{
    options.MapType(typeof(TimeSpan), () => new OpenApiSchema
    {
        Type = "string",
        Example = new OpenApiString("00:00:00")
    });
});
like image 39
live2 Avatar answered Oct 21 '22 17:10

live2


TimeSpanConverter is available in .NET 6.0. So TimeSpan serialization/deserialization will work without custom converters out of the box.

Issue: https://github.com/dotnet/runtime/issues/29932

Implementation: https://github.com/dotnet/runtime/pull/54186

like image 2
Igor Lyadov Avatar answered Oct 21 '22 17:10

Igor Lyadov