Add DateOnly and TimeOnly support to AspNetCore and additional functionality for working with System.Text.Json
You can also install via the .NET CLI with the following command:
dotnet add package AspNetCoreDateAndTimeOnly.Json
If you're using Visual Studio you can also install via the built in NuGet package manager.
string stringjson = objectdata.ToJSON();
MyObject objectdata = stringjson.GetJSON<MyObject>();
MyObject newobjectdata = objectdata.Clone<MyObject>();
In net 6 you have to add converters with the AddDateAndTimeJsonConverters
extension in AddJsonOptions
of the MvcBuilder.
builder.Services.AddControllers()
.AddJsonOptions(options =>
{
options.JsonSerializerOptions.Converters.AddDateAndTimeJsonConverters();
});
You can use the DynamicContractResolver
class to serialize or deserialize JSON conversions in a custom way.
var settings = new JsonSerializerOptions
{
TypeInfoResolver = new DynamicContractResolver(properties, ignoreProperties, ignoreNull),
};
Where.
properties
is an array with the properties of the object you want to serialize.
ignoreProperties
is an array with the properties of the object you want to ignore.
ignoreNull
is a boolean value to indicate whether to ignore null properties (also ignores empty IEnumerable).