I want to deserialize JSON to list of object, The JSON has structure like this.
{
"metadata":{ I don't care about metadata },
"results": [
{ object that I really want },
{ object that I really want },
{ object that I really want }
...
]
}
I want to get only list of object inside results
node and because there are some properties that I want to deserialze it myself, so I implement JsonConverter
using implementation from Alain's answer in "How to implement custom JsonConverter in JSON.NET to deserialize a List of base class objects?", he created JsonConverter
derived generic class called JsonCreationConverter<T>
that have protect abstract Create
method, which actually deserialize JSON, which in turn get called by JsonConverter
's ReadJson
.
My derived class's signature and its Create
signature is like
public class BoringTypeConverter: JsonCreationConverter<List<BoringType>>
{
protected override List<BoringType> Create(Type objectType, JObject jObject)
{
List<BoringType> boringTypes = new List<BoringType>();
JArray results = (JArray)jObject["results"];
// deserialize logic
...
return boringTypes;
}
}
And I used it like this
JsonConvert.DeserializeObject<List<BoringType>>(jsonString, new BoringTypeConverter());
While I was debuging test, I found that Create
method successfully deserialize JSON to List<BoringType>
but as soon as I hit serializer.Populate(jObjectReader, target)
I got error Cannot populate JSON object onto type 'System.Collections.Generic.List1[BoringType]'. Path 'metadata', line 2, position 15.
So I want to know that is the problem here?
Create
method didn't do anything about metadata
field then why it complain about metadata
?