using System.Text.Json; using BewerbungsSeite.Models.SiteContent; namespace BewerbungsSeite.Services; /// /// Wandelt den Seiteninhalt zwischen Objektmodell und UTF-8-JSON um, so wie er /// im Feld PageContent.Content abgelegt wird. /// public static class PageContentSerializer { public static readonly JsonSerializerOptions Options = new() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase, PropertyNameCaseInsensitive = true, WriteIndented = false }; public static byte[] Serialize(PageContent content) => JsonSerializer.SerializeToUtf8Bytes(content, Options); /// /// Liest den Inhalt zurueck. Fehlende oder unbekannte Felder fuehren nicht zu /// einem Fehler: unbekannte werden ignoriert, fehlende erhalten ihren Initialwert. /// public static PageContent Deserialize(byte[]? content) { if (content is null || content.Length == 0) { return new PageContent(); } var result = JsonSerializer.Deserialize(content, Options) ?? new PageContent(); return Normalize(result); } /// /// Stellt sicher, dass kein Bereich null ist, auch wenn ein aelterer /// Datensatz ihn noch nicht kennt oder ihn ausdruecklich als null enthaelt. /// private static PageContent Normalize(PageContent content) { content.Hero ??= new HeroSection(); content.Hero.TitleLines ??= new List(); content.Hero.PrimaryButton ??= new LinkItem(); content.Hero.SecondaryButton ??= new LinkItem(); content.Hero.SocialLinks ??= new List(); content.About ??= new AboutSection(); content.About.Facts ??= new List(); content.Skills ??= new SkillsSection(); content.Skills.Items ??= new List(); content.Projects ??= new ProjectsSection(); content.Projects.Items ??= new List(); foreach (var project in content.Projects.Items) { project.Details ??= new List(); project.Links ??= new List(); } content.Goals ??= new GoalsSection(); content.Goals.Items ??= new List(); content.Experience ??= new ExperienceSection(); content.Experience.Items ??= new List(); content.Documents ??= new DocumentsSection(); content.Documents.Items ??= new List(); content.Contact ??= new ContactSection(); content.Contact.InfoBoxes ??= new List(); return content; } }