因此,我有两个类,如下面的类。它们都在相同的命名空间和相同的共享项目中。Newtonsoft.json序列化和反序列化base/inheirited类来自共享项目
public class Person{
public string Name{get;set;}
}
public class EmployedPerson : Person{
public string JobTitle{get;set;}
}
当我serilize这些物品放入的RabbitMQ我序列化的基类,像这样:反序列化时
JsonSerializerSettings settings = new JsonSerializerSettings
{
TypeNameAssemblyFormatHandling = TypeNameAssemblyFormatHandling.Simple,
TypeNameHandling = TypeNameHandling.Objects
};
JsonConvert.SerializeObject(input, settings)
但是我遇到的问题。我希望能够做到像下面显示的那样将反序列化作为基类,然后检查它是否是不合意的类型。
类型检查:
Person person = Deserialize<Person>(e.Body, Encoding.Unicode);
if (person is EmployedPerson)
{
logger.LogInformation("This person has a job!");
}
反序列化设置:
JsonSerializerSettings settings = new JsonSerializerSettings
{
TypeNameAssemblyFormatHandling = TypeNameAssemblyFormatHandling.Simple,
TypeNameHandling = TypeNameHandling.Auto
};
反序列化逻辑:
private static T Deserialize<T>(byte[] data, Encoding encoding) where T : class
{
try
{
using (MemoryStream stream = new MemoryStream(data))
using (StreamReader reader = new StreamReader(stream, encoding))
return JsonSerializer.Create(settings).Deserialize(reader, typeof(T)) as T;
}
catch (Exception e)
{
Type typeParameter = typeof(T);
logger.LogError(LogEvent.SERIALIZATION_ERROR, e, "Deserializing type {@TypeName} failed", typeParameter.Name);
logger.LogInformation(Encoding.UTF8.GetString(data));
return default(T);
}
}
结果: 上面的代码失败,因为$类型属性包含大会名称在rabbitmq的每一端,程序集名称是不同的导致类在共享项目中。
实例错误:
Newtonsoft.Json.JsonSerializationException: Error resolving type specified in JSON 'Shared.Objects.EmployedPerson, Person.Dispatcher'. Path '$type', line 1, position 75. ---> System.IO.FileNotFoundException: Could not load file or assembly 'Person.Dispatcher, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.
我认为这有可能会是一个回答您的问题: https://stackoverflow.com/questions/12381636/why-is-json-net-is-not-working-with-inheritance-while - 反序列化 –
你可以1)写一个[自定义'SerializationBinder'](https://www.newtonsoft.com/json/help/html/SerializeSerializationBinder.htm)。编写你自己的活页夹清理反序列化的类型也是一个好主意,出于安全原因,这里解释[https://stackoverflow.com/q/39565954/3744182]。 2)发出你自己的自定义类型属性,并用一个自定义的'JsonConverter'解析它,如[Json.Net带多形子对象类型的序列化](https://stackoverflow.com/q/29528648/3744182) 。 – dbc
纵观SerializationBinder。 – Theyouthis