I had a problem with an API that had time type variables, I connect to a database with a Time field and of course the JSON returns something like "HoraTimeSpan": { "ticks": 0, "days": 0, "hours ": 0, "milliseconds": 0, "minutes": 0, "seconds": 0, "totalDays": 0, "totalHours": 0, "totalMilliseconds": 0, "totalMinutes": 0, "totalSeconds": 0 },
When I want to assign it to a TimeSpan, or use it, it displays the serialization error, at least it happened to me in 3.x and 5 Core, so how can I correct the problem?
The answer is simple and works for core 3.x and higher and can also be used if you need to serialize other types of data such as geolocation that are also time-type structures. I will do this example with time
In the api we make a class that allows us to manipulate the time and make it a string so we would have a class similar to this:
When the json is accessed to send the data, the Json converter detects that it is a TimeSpan type and manipulates it both for reading and writing so that it works in post and get.
simple now it will only be enough to make the call from the dependency injection, we do that at the start in service
There we tell you that when you do the serialization use the class above, how? thus
and that's it
I publish it because it gave me some headaches and I solved it like this, greetings