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?

0

1 Answer 1

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; }
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I feel quite ashamed I didn't find it ourself :-).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.