Example #1
0
 public static async void SerializeBook()
 {
     using (FileStream fs = new FileStream("book.json", FileMode.Create))
     {
         BookFileModel book1 = new BookFileModel(
             "Crime and punishment",
             "321",
             "Exmo",
             "2015-02-14",
             "1866-01-23",
             new AuthorFileModel(
                 "Fedor",
                 "Dostoevskiy",
                 "1821-11-11"
                 )
             );
         await JsonSerializer.SerializeAsync(fs, book1);
     }
 }
        public BookFileModel GetBookFromFile(string path)
        {
            string jsonString = null;

            using (StreamReader fs = new StreamReader(path))
            {
                jsonString = fs.ReadLine();
            }
            BookFileModel book = null;

            try
            {
                book = JsonSerializer.Deserialize <BookFileModel>(jsonString);
            }
            catch
            {
                throw new FileNotFoundException("JSON file is missing!");
            }
            return(book);
        }