I have a custom converter for so I can ignore empty strings but the WriteJson method throws a stackoverflow exception. What's the proper way to write out the value here?
internal class StringConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return objectType == typeof(string);
}
public override object ReadJson(JsonReader reader, Type t, object existingValue, JsonSerializer serializer)
{
var theValue = reader.Value?.ToString();
return !string.IsNullOrWhiteSpace(theValue) ? theValue : null;
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
if (!string.IsNullOrWhiteSpace(value.ToString()))
{
serializer.Serialize(writer, value.ToString()); //stackoverflow error here
return;
}
writer.WriteNull(); //how do I get it to skip the entry?
}
public static readonly StringConverter Singleton = new StringConverter();
}