Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
921 B
0
Indexable
Never
// Generated by ChatGPT
public class PatchDateTimeOffsetJsonConverter : JsonConverter
{
	public override bool CanConvert(Type objectType) => objectType.Name.EndsWith("Patch");
	
	public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) => throw new NotSupportedException();
	
	public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
	{
		writer.WriteStartObject();
		foreach (var property in value.GetType().GetProperties())
		{
			var propertyValue = property.GetValue(value);
			if (propertyValue is null) { continue; }

			writer.WritePropertyName(property.Name);
			switch (propertyValue)
			{
				case DateTimeOffset date: serializer.Serialize(writer, date.DateTime); break; // sans timezone
				default: serializer.Serialize(writer, propertyValue); break;
			}
		}
		writer.WriteEndObject();
	}
}