An exciting project I am working on involves a web application with a backend REST web API developed in ASP.net Core 5 and a frontend Angular application written in TypeScript.
One of the APIs from the ASP.net backend returns an instance of a C# object defined as:
public class MyClass
{
DateTime expires {get; set;}
string ticket {get; set;}
}
When calling this API from my Angular app using Angular's HTTP Client, I deserialize the result as an instance of the following TypeScript class:
export class MyClass
{
expires: Date;
ticket: string;
}
Unfortunately, there seems to be an issue as upon inspecting the TypeScript object after it is returned, it shows that the 'expires' field holds a string instead of a Date object.
This discrepancy may be due to the fact that data travels between the backend and frontend in JSON format, which only recognizes strings and numbers and not types, leading to the conversion of the C# DateTime object to a string.
My main concern is how to address this challenge effectively. Ideally, I would like to maintain strong typing for my objects. Is there a way to configure the serialization process automatically on the ASP.net and/or Angular side to resolve this issue? If not, what alternate approach should I consider?