JsonSerializer serializer = JsonSerializer.Create(new JsonSerializerSettings
{
TypeNameHandling = Newtonsoft.Json.TypeNameHandling.All,
Converters = new List<JsonConverter>(new JsonConverter[]
{
new InterfaceProxyConverter(TypeMetadataCache.ImplementationBuilder),
})
});
string json = "{\"$type\":\"Command.TestCommand, TestDeserializationWithDummyClasses\",\"Id\":1,\"Name\":\"bob\"}";
using (StringReader reader = new StringReader(json))
using (JsonTextReader jsonReader = new JsonTextReader(reader))
{
ITestCommand cmd = serializer.Deserialize<ITestCommand>(jsonReader);
}
This will fail on the exception:
"Type specified in JSON 'Command.TestCommand, TestDeserializationWithDummyClasses, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' is not compatible with 'DynamicInternal08d251996b944976b8ca3a79ccc30000.Command.ITestCommand, CommandDynamicInternal08d251996b944976b8ca3a79ccc30000, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'. Path '$type', line 1, position 67."
The problem seems to be in InterfaceProxyConverter. In this contrived example I can just comment out the InterfaceProxyConverter and it will deserialize just fine.
The MT2 InterfaceProxyConverter will also work fine. It will also work if I change ITestCommand to TestCommand in the Deserialize statement with both the MT2 and MT3 InterfaceProxyConverter.
It looks like the change in the InterfaceProxyConverter.ReadJson method is causing the issue.
MT3
Type proxyType = _builder.GetImplementationType(objectType);
return serializer.Deserialize(reader, proxyType);
If I change that to be similar to how MT2 does it then it works fine. (Not suggesting that is the fix, just pointing it out.)
Type proxyType = _builder.GetImplementationType(objectType);
object obj = Activator.CreateInstance(proxyType);
serializer.Populate(reader, obj);
return obj;
Thanks,
Brian