I am trying to perform custom serialisation, all the happy path code works but the null value path is not behaving as I'd like.
I have set the serializer settings to NullValueHandling.Ignore
and other parts of my object graph that are null (and don't use my custom serialization) have the null values removed. It looks like the Newtonsoft serializer writes to a string builder so we should be able to 'rewind' any written json tokens but I don't see how to not write anything.
Doing nothing and just returning causes the serializer to throw an exception as the json would be invalid.
Any clues?
public class SpecialConvertor : JsonConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
if (value == null || (int)value == 0)
{
if (serializer.NullValueHandling == NullValueHandling.Ignore)
{
//how to make this work?
}
else
{
writer.WriteNull();
}
return;
}
// the rest of WriteJson
}
// the rest of SpecialConvertor
}