我有以下模型:反序列化JSON來鍵入C#含參考另一反序列化對象
public interface IEntity
{
string Id { get; }
}
public class EntityParent : IEntity
{
public string Id { get; }
public EntityChild EntityChild { get; }
[JsonConstructor]
public EntityParent(string id, EntityChild entityChild)
{
Id = id;
EntityChild = entityChild;
}
}
public class EntityChild : IEntity
{
public string Id { get; }
public int Age { get; }
[JsonConstructor]
public EntityChild(string id, int age)
{
Id = id;
Age = age;
}
}
接着我有一些JSON,我需要反序列化到上述類型的集合:
{
"Children":
[
{
"Id" : "Billy",
"Age" : 42
}
],
"Parents" :
[
{
"Id" : "William",
"EntityChild" : "Billy"
}
]
}
最終我想要列出一個EntityChild
ren和一個列表EntityParent
s,它將(可選)包含對第一個列表中的對象的引用,或者至少包含對EntityChild
實例的引用。我試圖寫一個自定義的JsonConverter
(我用Newtonsoft.Json 9.0.1 NuGet包),其中在ReadJson()
方法,我具體Id
尋找一個孩子,像這樣:
public class ParentConverter<TEntity> : JsonConverter where TEntity : IEntity
{
private readonly IEnumerable<TEntity> _children;
public ParentConverter(IEnumerable<TEntity> children)
{
_children = children;
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue,
JsonSerializer serializer)
{
JObject jObject = JObject.Load(reader);
TEntity target = _children.FirstOrDefault(d => d.Id == jObject["Id"].ToString());
serializer.Populate(jObject.CreateReader(), target);
return target;
}
public override bool CanConvert(Type objectType)
{
return typeof(TEntity).IsAssignableFrom(objectType);
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
這裏的一個簡單的測試:
public class JsonTest
{
const string Json = @"
{
""Children"": [
{
""Id"" : ""Billy"",
""Age"" : 42
}
],
""Parents"" : [
{
""Id"" : ""William"",
""EntityChild"" : ""Billy""
}
]
}";
public static void Main()
{
JObject jObject = JObject.Parse(Json);
var children =
JsonConvert.DeserializeObject<List<EntityChild>>(jObject["Children"].ToString());
var parents =
JsonConvert.DeserializeObject<List<EntityParent>>(jObject["Parents"].ToString(),
new ParentConverter<EntityChild>(children));
}
}
children
正確反序列化,但parents
試圖調用JObject.Load(reader);
在ReadJson()
時拋出JsonReaderException
,稱「從JsonReader讀取JObject時出錯。當前的JsonReader項目不是一個對象:String。路徑'[0] .EntityChild'。「
有誰知道我應該如何去了解它在此先感謝
編輯:?更新了EntityChild
有額外的屬性強調的是,在EntityParent
的財產必須是EntityChild
類型,,而不是一個字符串
誰設計了JSON結構?這很奇怪 –
@SirRufo不幸的是,它超出了我的手:(:(這不是我);)。 – Caleb9