Is there any way in Json.Net to tell it to serialize most types as normal, but to use a custom serializer I'll provide whenever it spots an object of type x?
Essentially the idea here is that AFAIK, if you want to use custom serialization for a type in Json.Net, you have to specify how that's done inside the type, eg. by implementing ISerializable or by using attributes. However if I don't have access to the type, I can't modify serialization for the type.
Instead I want to know if I can inject that information into the jsonConverter, so that it will know to use custom serialization for the type.
Hypothetically I want to do something like this:
class A
{
B B {get; set;}
}
class B
{
}
BSerializer : ISerilalizer<B>
{
public string Serialize(B obj) => "This is a B"
}
...
JsonConverter.SerializeObject(new A{B = new B()}, new BSerializer())
And the result of this serialization would be
{
"B" : "This is a B"
}
Now obviously this interface doesn't exist, but can I do something similiar using the existing APIs Json.Net provides?