Rewrite
I was researching how to deserialize a TimeSpan
using Newtonsoft’s JSON.net and found code in my current project that used System.Text.Json.JsonSerializer
instead. This code appeared to not fail on the operation of deserializing the TimeSpan
property, as per the unit tests I was running.
To confirm, I ran a test case in the latest version of Linqpad 6 (which uses .NET Core):
public class Project { public TimeSpan AverageScanTime { get; set; } }
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");
Unfortunately, the deserialization failed (see image below).
My question is: can the TimeSpan
be serialized/deserialized using either library? If so, how? Or is my test case flawed in some respect?