Не удается десериализовать класс контейнер
Имеется небольшая библиотека классов. Сериализовать в json удается без проблем. А вот при десериализации возникает исключение Newtonsoft.Json.JsonSerializationException: "Cannot create and populate list type BooksLib.Bookstore`1[BooksLib.Product]. Path '', line 1, position 1.".
public class Product : IComparable<Product>
{
public double Price
{
...
}
private string title;
/// <summary>
/// Название
/// </summary>
public string Title
{
...
}
public override string ToString()
{
return $"Price = {Price:f2}";
}
public static explicit operator double(Product product)
{
return product.Price;
}
public int CompareTo(Product other) => Price.CompareTo(other.Price);
public Product(double price, string title)
{
Price = price;
Title = title;
}
}
}
public class Book : Product
{
private short numberOfPages;
private short year;
private double rating;
public Book(double price, string title, short numberOfPages, short year, double rating):base(price,title)
{
NumberOfPages = numberOfPages;
Year = year;
Rating = rating;
}
public short NumberOfPages
{
get => numberOfPages;
set
{
...
}
}
public short Year
{
get => year;
set
{
...
year = value;
}
}
public double Rating
{
...
}
}
public string stringGetShortInfo() => $"{NumberOfPages}.{Year}.{unique(Title)}.{Math.Round(Rating,2) * 100}";
private int unique(string str)
{
...
}
public override string ToString()
{
...
}
}
}
public class Bookstore<T> : IEnumerable<T> where T : Product
{
private List<T> items = new List<T>();
public void Add(T item)
{
items.Add(item);
}
public IEnumerator<T> GetEnumerator()
{
for (int index = 0; index < items.Count; index++)
{
yield return items[index];
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return (IEnumerator)GetEnumerator();
}
}
}
class Program
{
static IEnumerable<Product> books = new Bookstore<Product>();
static string path = "../../../Подготовка к экр 1 Вариант/books.json";
static void Main(string[] args)
{
do
{
Deserialize();
foreach(var i in books)
{
Console.WriteLine(i);
}
} while (Console.ReadKey().Key != ConsoleKey.Escape);
}
static void Deserialize()
{
if(File.Exists(path))
{
using (StreamReader reader = new StreamReader(path))
{
JsonSerializer deserializer = new JsonSerializer();
books = (Bookstore<Product>)deserializer.Deserialize(reader, typeof(Bookstore<Product>));
}
}
}
}
}