Skip to content

Instantly share code, notes, and snippets.

Created June 10, 2022 17:03
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
public class DateOnlyConverter : JsonConverter<DateOnly>
private readonly string serializationFormat;
public DateOnlyConverter() : this(null) { }
public DateOnlyConverter(string? serializationFormat)
this.serializationFormat = serializationFormat ?? "yyyy-MM-dd";
public override DateOnly Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
var value = reader.GetString();
return DateOnly.Parse(value!);
public override void Write(Utf8JsonWriter writer, DateOnly value, JsonSerializerOptions options)
=> writer.WriteStringValue(value.ToString(serializationFormat));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment