首先,
SourceType错过了您的json中的菜单项“ Submenus Test”。
其次,您不应该仅仅
ToObject因为该
Submenus属性而使用它,而应该以递归方式对其进行处理。
以下
ReadJson将起作用:
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer){ var jObject = JObject.Load(reader); var sourceType = jObject["SourceType"].Value<string>(); object target = null; switch (sourceType) { case SourceType.File: target = new FileMenu(); break; case SourceType.Folder: target = new FolderMenu(); break; case SourceType.Json: target = new JsonMenu(); break; case SourceType.RestGet: target = new RestMenu(); break; case SourceType.Rss: target = new RssMenu(); break; case SourceType.Text: target = new TextMenu(); break; case SourceType.Url: target = new UrlMenu(); break; default: throw new ArgumentException("Invalid source type"); } serializer.Populate(jObject.CreateReader(), target); return target;}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)