Tar*_*yen 1 c# serialization json json.net asp.net-web-api
Json.Net没有将其接收到的对象反序列化为我的 Control 类的正确派生类。(请参阅以下对该问题的解释。另请注意,我认为这是解释该问题所需的最少代码量。提前感谢您审阅此问题。)
我正在尝试将以下类序列化为 JSON 或从 JSON 反序列化。
public class Page {
public Guid Id { get; set; }
public Guid CustomerId { get; set; }
public IList<Control> Controls { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这是 Control 类:
public class Control : ControlBase
{
public override Enums.CsControlType CsControlType { get { return Enums.CsControlType.Base; } }
}
Run Code Online (Sandbox Code Playgroud)
这是 ControlBase 抽象类:
public abstract class ControlBase
{
public Guid Id { get; set; }
public virtual Enums.CsControlType CsControlType { get; }
public Enums.ControlType Type { get; set; }
public string PropertyName { get; set; }
public IList<int> Width { get; set; }
public string FriendlyName { get; set; }
public string Description { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这是从 Control 派生的 OptionsControl:
public class OptionsControl : Control
{
public override Enums.CsControlType CsControlType { get { return Enums.CsControlType.OptionsControl; } }
public IDictionary<string, string> Options;
}
Run Code Online (Sandbox Code Playgroud)
当 OptionsControl 被序列化时,JSON 如下所示。请注意,Json.Net 添加了该$type属性,以便(据说)它可以将控件反序列化回 OptionsControl。
{
"options":{
"TN":"TN"
},
"csControlType":4,
"id":"00000000-0000-0000-0000-000000000000",
"type":4,
"propertyName":"addresses[0].state",
"width":[
2,
2,
6
],
"friendlyName":"State",
"description":null,
"$type":"MyApp.Infrastructure.Core.Models.UI.Controls.OptionsControl, MyApp.Infrastructure.Core"
}
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试反序列化页面(其中包含基本控件和选项控件)时,所有控件都将反序列化为基本控件,并且属性options(包括上面的状态列表)将被忽略。
这是我尝试反序列化(和更新)通过 WebAPI 服务收到的 Page 对象的方法:
[HttpPut]
[ActionName("UpdatePage")]
public async Task<CommandResult> UpdatePageAsync([FromBody]Object page)
{
try
{
// Deserialize the page into the correct object
// When I Debug.WriteLine the page.ToString(), the page still has the $type property at this point (before it is deserialized).
var dsPage = return JsonConvert.DeserializeObject<Page>(page.ToString(), new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.All
});
// The dsPage passed in here does not have any OptionsControls.
// At this point, they have all been deserialized into the base Control, unfortunately.
return await _uiCommandFacade.UpdatePageAsync(dsPage, msg);
}
catch (Exception ex)
{
return new CommandResult()
{
ErrorType = ErrorType.ControllerError,
DeveloperMessage = $"Unable to update page",
Exception = ex
};
}
}
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,我正在使用 TypeNameHandling.All (如文档中所述并在 SO 上多次提到)进行反序列化(就像我在序列化期间一样,这就是$type在 JSON 中生成属性的原因)。但是,当我反序列化 Page 对象时,OptionsControlPage 对象中的任何对象都会被反序列化为常规基类Control,因此该Options属性将被忽略(因此我的状态列表不会更新/丢弃)。
如何让 Json.Net 正确反序列化我的控件及其衍生物?
看起来问题可能是由于$type元数据属性不是控件的 JSON 中的第一个属性所致。通常,Json.Net 需要将此属性放在第一位才能识别它。尝试添加
MetadataPropertyHandling = MetadataPropertyHandling.ReadAhead
Run Code Online (Sandbox Code Playgroud)
反序列化时的设置。$type这应该允许 Json.Net稍后在 JSON 中找到该属性。