Last active
April 6, 2023 00:03
-
-
Save DilanLivera/e95594aed9210daef55f7f10575d51f6 to your computer and use it in GitHub Desktop.
System.Text.Json
- Create the converter
public class DateTimeConverter : JsonConverter<DateTime>
{
public override DateTime Read(
ref Utf8JsonReader reader,
Type typeToConvert,
JsonSerializerOptions options)
{
return DateTime.ParseExact(
reader.GetString(),
"yyyy-MM-dd HH:mm:ss.fff",
CultureInfo.InvariantCulture);
}
public override void Write(
Utf8JsonWriter writer,
DateTime value,
JsonSerializerOptions options)
{
writer.WriteStringValue(
value.ToString("yyyy-MM-dd HH:mm:ss.fff", CultureInfo.InvariantCulture));
}
}
- Add the converter to the
JsonSerializerOptions
's converters list
var options = new JsonSerializerOptions();
options.Converters.Add(new DateTimeConverter());
var result = await response.Content.ReadFromJsonAsync<SomeDto>(options);
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment