TimeSpan Mismatch between Schema(string) vs Serialization(object graph)
See original GitHub issueCan’t seem to deserialize a class with TimeSpan from the swagger2csclient
generated clients.
The error i get on the client is
Newtonsoft.Json.JsonSerializationException: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.TimeSpan' because the type requires a JSON primitive value (e.g. string, number, boolean, null) to deserialize correctly.
To fix this error either change the JSON to a JSON primitive value (e.g. string, number, boolean, null) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
Path '[0].offlineTimeSpan.ticks', line 1, position 225.
Not sure if this is related, but I noticed the data returned during serialization doesn’t seem to match the schema (see image below)
I thought TimeSpan was supported… but I guess I have to add it?
how would one go about adding TimeSpan support to the RestAPI and Nswag the CSharp generated clients?
On the server / Rest side: Do i have to use this: https://github.com/RicoSuter/NJsonSchema/wiki/Type-Mappers Or do I use Netwonsoft settings?
services.AddSwaggerDocument(x =>
{
x.FlattenInheritanceHierarchy = true;
x.SchemaGenerator.Settings.TypeMappers.Add(???));
x.ActualSerializerSettings.Converters.Add(???);
});
On the client, I see there are options to expose the JsonSerializerSettings but the CreateSerializerSettings is not virtual.
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Serializing Timespan in Dictionary<string, object> with ...
So TimeSpan in an object can't deserialize to a timespan. I've tried with latest version of Json.Net too. But the result is same....
Read more >Data Contract Serializer
The data contract serializer supports a data contract model that helps you decouple the low-level details of the types you want to serialize...
Read more >DataSet and DataTable security guidance - ADO.NET
NET, DataSet and DataTable place the following restrictions on what types of objects may be present in the deserialized data.
Read more >C# 9.0 in a Nutshell Supplement
Serialization is the act of taking an in-memory object or object graph (set of objects that reference one another) and flattening it into...
Read more >Newtonsoft.Json.xml
Converts a binary value to and from a base 64 string value. Converts an object to and from JSON. Writes the JSON representation...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
OK - I know why I am confused - I am using NetCoreApp3.1 (upgraded)… and it seems that the json serialization is no longer based on Newtonsoft…
There is no converter for TimeSpan!
https://github.com/dotnet/runtime/tree/81bf79fd9aa75305e55abe2f7e9ef3f60624a3a1/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters
@RicoSuter - makes sense - closing this - thanks