I have an object: Link : Entity
that sometimes should be serialized to, or from, just a plain string of text.
Other times its should just serialize like any other entity, using the attributes and class/property data under Link and Entity.
Below is example psudo-code for what I'd want to do in a JsonConverter class for the write function, but I can't figure out how... is this possible?
I need to do this for read and write btw.
protected override void Write(Utf8JsonWriter writer, Type type, Link value)
{
if (value.Types.SequenceEqual(DefaultTypeNames)
&& value.MediaType == DefaultMediaType
&& value.Rel == null
&& value.Name == null
&& value.HrefLang == null
&& value.Context == null
&& value.Id == null
&& value.Height == null
&& value.Width == null
&& value.Preview == null)
{
writer.WriteStringValue(value.Href);
}
else
{
JsonSerializer.SerializeDefault(writer, value, type);
}
}
SerializeDefault would be the logic I'm trying to find, afaik this function doesn't exist.
I have also tried using source generation, but I need to use init only setters, and source generation doesn't support those as of yet.