在.NET中解析操作Yaml文件

程序員有二十年 2024-05-11 19:35:30

概述:不幸的是,YAML 文件是當今所有開發人員日常生活的一部分;盡管它們非常容易出錯,並且幾乎不可能在沒有 IDE 和架構信息的情況下進行編輯而不會經常遇到錯誤——向所有認爲這是一個好主意的 CI 系統致以問候:事實並非如此——但我們必須接受我們必須處理它們的事實。在 .NET 中,YamlDotNet 庫是處理 YAML 文件的常用選擇。該庫非常易于使用,並爲大多數 YAML 文件提供了非常好的支持。處理 YAML 文件時,提供兩種不同的選項:通過字典訪問鍵值或反序列化爲類。這兩種方法都有其優點和缺點,但在大多數情況下,反序列化爲類是更方便、更好的選擇。YamlDotNet從 YAML 反序列化

不幸的是,YAML 文件是當今所有開發人員日常生活的一部分;盡管它們非常容易出錯,並且幾乎不可能在沒有 IDE 和架構信息的情況下進行編輯而不會經常遇到錯誤——向所有認爲這是一個好主意的 CI 系統致以問候:事實並非如此——但我們必須接受我們必須處理它們的事實。

在 .NET 中,YamlDotNet 庫是處理 YAML 文件的常用選擇。該庫非常易于使用,並爲大多數 YAML 文件提供了非常好的支持。

處理 YAML 文件時,提供兩種不同的選項:通過字典訪問鍵值或反序列化爲類。這兩種方法都有其優點和缺點,但在大多數情況下,反序列化爲類是更方便、更好的選擇。YamlDotNet

從 YAML 反序列化

對于類的反序列化,相應的類也是必需的;舉個例子,我采用博客文章潛在標題的結構:

title: Handle Yaml Files with .NET description: This blog post shows a simple sample how to serialize and deserialize yaml files with .NET options: isDraft: true date: 2024-04-23T15:30:00Z author: name: BEN ABT twitter: https://twitter.com/Abt_Benjamin linkedIn: https://www.linkedin.com/in/benjaminabt/ job: company: Medialesson GmbH description: Chief PullRequest Officer website: https://media-lesson.com/

因此,類結構可以 1:1 實現:

public BlogPost { public string Title { get; set; } = !; public string Description { get; set; } = !; public Options Options { get; set; } = !; public Author Author { get; set; } = !; public string Content { get; set; } = !; } public Options { public bool IsDraft { get; set; } public DateTimeOffset Date { get; set; } } public Author { public string Name { get; set; } = !; public string Twitter { get; set; } = !; public string LinkedIn { get; set; } = !; public Job Job { get; set; } = !; } public Job { public string Company { get; set; } = !; public string Description { get; set; } = !; public string Website { get; set; } = !; }

截至目前,YamlDotNet 不支持任何記錄;反序列化程序需要一個類,該類在屬性中具有空構造函數和相應的 setter。

反序列化非常簡單:

IDeserializer deserializer = new DeserializerBuilder() .WithNamingConvention(CamelCaseNamingConvention.Instance) .Build(); // yamlBlogPost = string with yaml content BlogPost blogPost = deserializer.Deserialize<BlogPost>(yamlBlogPost);

然後,該對象將 YAML 文件中的相應值作爲實例包含在內,可以照常訪問。blogPost

序列化爲 YAML

序列化也同樣簡單,只是相反:

ISerializer serializer = new SerializerBuilder() .WithNamingConvention(CamelCaseNamingConvention.Instance) .Build(); string blogPostYaml = serializer.Serialize(blogPost);完整示例// --------------------------------------------------------------------// This sample shows the handling of YAML files in .NET.// The YamlDotNet library (https://github.com/aaubry/YamlDotNet) is used to serialize and deserialize YAML files.// 2024-04-23 - https://schwabencode.com// Runtime: .NET 8// Sample Project: Console App// Dependency: YamlDotNetusing YamlDotNet.Serialization;using YamlDotNet.Serialization.NamingConventions;// --------------------------------------------------------------------// sample Yaml-File in style of a blog post headerstring yamlBlogPost =""" title: Handle Yaml Files with .NET description: This blog post shows a simple sample how to serialize and deserialize yaml files with .NET options: isDraft: true date: 2024-04-23T15:30:00Z author: name: BEN ABT twitter: https://twitter.com/Abt_Benjamin linkedIn: https://www.linkedin.com/in/benjaminabt/ job: company: Medialesson GmbH description: Chief PullRequest Officer website: https://media-lesson.com/""";// --------------------------------------------------------------------// deserialize string as modelIDeserializer deserializer = new DeserializerBuilder() .WithNamingConvention(CamelCaseNamingConvention.Instance) .Build();BlogPost blogPost = deserializer.Deserialize<BlogPost>(yamlBlogPost);// Print blog postConsole.WriteLine(new string('-', 30));Console.WriteLine($"## Print Blog Post Object");Console.WriteLine($"Title: {blogPost.Title}");Console.WriteLine($"\tDescription: {blogPost.Description}");Console.WriteLine($"Options");Console.WriteLine($"\tIs Draft: {blogPost.Options.IsDraft}");Console.WriteLine($"\tDate: {blogPost.Options.Date:o}");Console.WriteLine($"Author");Console.WriteLine($"\tName: {blogPost.Author.Name}");Console.WriteLine($"\tTwitter: {blogPost.Author.Twitter}");Console.WriteLine($"\tLinkedIn: {blogPost.Author.LinkedIn}");Console.WriteLine($"\tJob");Console.WriteLine($"\t\tCompany: {blogPost.Author.Job.Company}");Console.WriteLine($"\t\tDescription: {blogPost.Author.Job.Description}");Console.WriteLine($"\t\tWebsite: {blogPost.Author.Job.Website}");// --------------------------------------------------------------------// create yaml serializer with optionsISerializer serializer = new SerializerBuilder() .WithNamingConvention(CamelCaseNamingConvention.Instance) .Build();string blogPostYaml = serializer.Serialize(blogPost);Console.WriteLine(new string('-', 30));Console.WriteLine($"## Print Blog Post Text");Console.WriteLine(blogPostYaml);// --------------------------------------------------------------------// sample models ases, records are not supported todaypublic BlogPost{public string Title { get; set; } = !;public string Description { get; set; } = !;public Options Options { get; set; } = !;public Author Author { get; set; } = !;public string Content { get; set; } = !;}public Options{public bool IsDraft { get; set; }public DateTimeOffset Date { get; set; }}public Author{public string Name { get; set; } = !;public string Twitter { get; set; } = !;public string LinkedIn { get; set; } = !;public Job Job { get; set; } = !;}public Job{public string Company { get; set; } = !;public string Description { get; set; } = !;public string Website { get; set; } = !;}

PS:請不要使用Yaml,如果你沒有必要的話。每個人都討厭yaml。

如果你喜歡我的文章,請給我一個贊!謝謝

0 阅读:0

程序員有二十年

簡介:感謝大家的關注