static void Main(string[] args) { do { Bookstore <Product> books = new Bookstore <Product>(); int n = Read(); for (int i = 0; i < n; i++) { try { books.Add(new Book() { Price = rnd.NextDouble() * 20, NumberOfPages = (short)rnd.Next(0, 701), Year = (short)rnd.Next(1980, 2030), Title = Check1(), Rating = rnd.NextDouble() * ((7 + 2) - 2) }); } catch (Exception) { --i; } } books.Add(new Product() { Price = 1, Title = "Товар1" }); foreach (var item in books) { Console.WriteLine(item); } try { DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(Bookstore <Product>), new Type[] { typeof(Book), typeof(Product) }); using (FileStream fs = new FileStream(@"../../../books.json", FileMode.OpenOrCreate)) { jsonSerializer.WriteObject(fs, books); } } catch (Exception ex) { Console.WriteLine($"JSON не ок {ex.Message}"); } Console.WriteLine("Нажмите escape для выхода"); } while (Console.ReadKey(true).Key != ConsoleKey.Escape); }
static void SerializeBookstoreJson(string path, Bookstore <Product> products) { DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(Bookstore <Product>)); try { using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write)) { jsonSerializer.WriteObject(fs, products); } Console.WriteLine("JSON ок"); } catch (Exception e) { Console.WriteLine($"JSON не ок {e.Message}"); } }