Exemplo n.º 1
0
 public static async void SerializeAuthor()
 {
     using (FileStream fs = new FileStream("author.json", FileMode.Create))
     {
         AuthorFileModel author = new AuthorFileModel("Alexander", "Pushkin", "1799-01-26");
         await JsonSerializer.SerializeAsync(fs, author);
     }
 }
 public BookFileModel(string name, string pages, string publisher, string dateOfPublishing, string dateOfWriting, AuthorFileModel author)
 {
     Name             = name;
     Pages            = pages;
     Publisher        = publisher;
     DateOfPublishing = dateOfPublishing;
     DateOfWriting    = dateOfWriting;
     Author           = author;
 }
        public AuthorFileModel GetAuthorFromFile(string path)
        {
            string jsonString = null;

            using (StreamReader fs = new StreamReader(path))
            {
                jsonString = fs.ReadLine();
            }
            AuthorFileModel author = null;

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