Uebungsaufgaben/Semester3/DotNET/Aufgabenblatt3/Aufgabe8/Program.cs
2024-12-19 23:55:07 +01:00

47 lines
1.4 KiB
C#

using Aufgabe8;
using System.Text.Json;
Console.WriteLine("Willkommen zu Aufgabe8");
Employee Mitarbeiter1 = new Employee("Herbert", "Müller", Gender.Male);
Employee.Serialize(Mitarbeiter1);
Console.WriteLine($"Original: " +
$"Vorname: {Mitarbeiter1.FirstName}, " +
$"Nachname: {Mitarbeiter1.LastName}, " +
$"Gender: {Mitarbeiter1.Gender}");
Employee Clon = Employee.Deserialize();
Console.WriteLine($"Clon: " +
$"Vorname: {Clon.FirstName}, " +
$"Nachname: {Clon.LastName}, " +
$"Gender: {Clon.Gender}");
[Serializable]
public class Employee
{
public string FirstName { get; set; }
public string LastName { get; set; }
public Gender Gender { get; set; }
public Employee() {}
public Employee(string firstName, string lastName, Gender gender)
{
this.FirstName = firstName;
this.LastName = lastName;
this.Gender = gender;
}
public static void Serialize(Employee employee)
{
string jsonString = JsonSerializer.Serialize(employee, new JsonSerializerOptions { WriteIndented = true });
File.WriteAllText(@"..\Personen.json", jsonString);
}
public static Employee Deserialize()
{
string jsonString = File.ReadAllText(@"..\Personen.json");
return JsonSerializer.Deserialize<Employee>(jsonString);
}
}