octokit/webhooks.net

Allow serialization of `DateTimeOffset`

JamieMagee opened this issue · 0 comments

Describe the feature

We have custom DateTimeOffset converters for nullable and non-nullable types. These are required because GitHub webhooks can contain timestamps in ISO8601 or UNIX milliseconds formats, so the built-in JSON converters aren't suitable.

Currently we have only implemented the deserialization part, and the serializers throw a NotImplementedException.

public override void Write(Utf8JsonWriter writer, DateTimeOffset? value, JsonSerializerOptions options) =>
throw new NotImplementedException($"The {nameof(NullableDateTimeOffsetConverter)} does not support serializing to JSON");

public override void Write(Utf8JsonWriter writer, DateTimeOffset value, JsonSerializerOptions options) =>
throw new NotImplementedException($"The {nameof(DateTimeOffsetConverter)} does not support serializing to JSON");

We avoided writing the serializers because we can't be sure exactly what the original object's format was. I think throwing an exception is more painful than getting the wrong format, and we should just pick either ISO8601 or UNIX milliseconds and stick with it.

See #22 for more information.