question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TimeSpan Mismatch between Schema(string) vs Serialization(object graph)

See original GitHub issue

Can’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)

image

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:closed
  • Created 4 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
abbotwarecommented, Feb 4, 2020

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

0reactions
abbotwarecommented, Feb 5, 2020

@RicoSuter - makes sense - closing this - thanks

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found