I want to deserialize this json data to Result class and put 3 book values(FirstBook, SecondBook, ThirdBook) into IEnumerable< Book >:
[
{
"Id": 1,
"FirstBook": "B1",
"SecondBook": "B2",
"ThirdBook": "B3",
...
},
{
"Id": 2,
"FirstBook": "BB1",
"SecondBook": "BB2",
"ThirdBook": "BB3",
...
}
]
Result Class:
public class Result
{
public int Id { get; set;}
[JsonConverter(typeof(BooksJsonConverter))]
public IEnumerable<Book> Books { get; set; }
}
Book class:
public class Book
{
public string Name { get; set; }
}
Deserialize it to class Result that contain IEnumerable< Book >
JsonConvert.DeserializeObject<IEnumerable<Result>>(json);
I think i should create JsonConverter:
public class BooksJsonConverter : JsonConverter<IEnumerable<Book>>
{
public override IEnumerable<Book> ReadJson(JsonReader reader, Type objectType, IEnumerable<Book> existingValue, bool hasExistingValue, JsonSerializer serializer)
{
//What should i write here?
return new List<Book>();
}
public override void WriteJson(JsonWriter writer, Book value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
In this situation ReadJson of BooksJsonConverter not called but when i add JsonProperty attrib it called but i don't know what should i write inside ReadJson:
public class Result
{
public int Id { get; set;}
[JsonConverter(typeof(BooksJsonConverter))]
[JsonProperty("FirstBook")]
public IEnumerable<Book> Books { get; set; }
}
Question is how should i convert FirstBook, SecondBook, ThirdBook from json to IEnumerable< Book > inside of result class?