5

From this answer I have class JsonCreationConverter<T> and some implementations for concrete types. But this abstract class misses the implementation of WriteJson method.

Over the internet I find the code:

public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
    //use the default serialization - it works fine
    serializer.Serialize(writer, value);
}

But this code ends up with StackOverflowException as it calls itself all the time (of course). Other solutions were for concrete objects implementations with serializing all the values one by one. I really want to avoid it, just want to use default serialization, which is OK for me. Just to avoid calling my JsonConverter for serialization. I need it only for deserialization. Is it possible? How?

Community
  • 1
  • 1
Zoka
  • 2,312
  • 4
  • 23
  • 33

1 Answers1

9

Try overriding the CanWrite property getter in the converter so that it returns false. This will prevent the converter from being used during serialization.

    public override bool CanWrite
    {
        get { return false; }
    }
Brian Rogers
  • 125,747
  • 31
  • 299
  • 300