Предположим, что у меня есть следующая иерархия классов:
public abstract class Organization
{
/* properties related to all organizations */
}
public sealed class Company : Organization
{
/* properties related to companies */
}
public sealed class NonProfitOrganization : Organization
{
/* properties related to non profit organizations */
}
Возможно ли, что json.net использует свойство (скажем, "тип" или "дискриминатор" ), чтобы определить, какой тип объекта, когда он десериализует организацию? Например, следующее должно десериализовать экземпляр компании.
{
"type": "company"
/* other properties related to companies */
}
И следующее должно десериализовать экземпляр NonProfitOrganization.
{
"type": "non-profit"
/* other properties related to non profit */
}
Когда я вызываю следующее:
Organization organization = JsonConvert.DeserializeObject<Organization>(payload);
где полезная нагрузка - это приведенные выше фрагменты JSON. Я взглянул на установку "TypeNameHandling на свойства или классы, но он сериализует весь тип .NET, который не является" переносимым" между клиентом и сервер, когда классы определены в разных пространствах имен и сборках.
Я бы предпочел определить тип - нейтральный способ, который клиенты, написанные на любом языке, могут использовать для определения фактического типа сериализации типа объекта.