概述: 不幸的是,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 classBlogPost
{
publicstring Title { get; set; } = null!;
publicstring Description { get; set; } = null!;
publicOptions Options { get; set; } = null!;
publicAuthor Author { get; set; } = null!;
publicstring Content { get; set; } = null!;
}
public classOptions
{
publicbool IsDraft { get; set; }
publicDateTimeOffset Date { get; set; }
}
public classAuthor
{
publicstring Name { get; set; } = null!;
publicstring Twitter { get; set; } = null!;
publicstring LinkedIn { get; set; } = null!;
publicJob Job { get; set; } = null!;
}
public classJob
{
publicstring Company { get; set; } = null!;
publicstring Description { get; set; } = null!;
publicstring Website { get; set; } = null!;
}
截至目前,YamlDotNet 不支持任何記錄;反序列化程式需要一個類,該類在內容中具有空建構函式和相應的 setter。
反序列化非常簡單:
IDeserializer deserializer = newDeserializerBuilder()
.WithNamingConvention(CamelCaseNamingConvention.Instance)
.Build();
// yamlBlogPost = string with yaml content
BlogPost blogPost = deserializer.Deserialize<BlogPost>(yamlBlogPost);
然後,該物件將 YAML 檔中的相應值作為例項包含在內,可以照常存取。blogPost
序列化為 YAML
序列化也同樣簡單,只是相反:
ISerializer serializer = newSerializerBuilder()
.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: YamlDotNet
usingYamlDotNet.Serialization;
usingYamlDotNet.Serialization.NamingConventions;
// --------------------------------------------------------------------
// sample Yaml-File in style of a blog post header
string 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 model
IDeserializer deserializer = newDeserializerBuilder()
.WithNamingConvention(CamelCaseNamingConvention.Instance)
.Build();
BlogPost blogPost = deserializer.Deserialize<BlogPost>(yamlBlogPost);
// Print blog post
Console.WriteLine(newstring('-', 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 options
ISerializer serializer = newSerializerBuilder()
.WithNamingConvention(CamelCaseNamingConvention.Instance)
.Build();
string blogPostYaml = serializer.Serialize(blogPost);
Console.WriteLine(newstring('-', 30));
Console.WriteLine($"## Print Blog Post Text");
Console.WriteLine(blogPostYaml);
// --------------------------------------------------------------------
// sample models as classes, records are not supported today
public classBlogPost
{
publicstring Title { get; set; } = null!;
publicstring Description { get; set; } = null!;
publicOptions Options { get; set; } = null!;
publicAuthor Author { get; set; } = null!;
publicstring Content { get; set; } = null!;
}
public classOptions
{
publicbool IsDraft { get; set; }
publicDateTimeOffset Date { get; set; }
}
public classAuthor
{
publicstring Name { get; set; } = null!;
publicstring Twitter { get; set; } = null!;
publicstring LinkedIn { get; set; } = null!;
publicJob Job { get; set; } = null!;
}
public classJob
{
publicstring Company { get; set; } = null!;
publicstring Description { get; set; } = null!;
publicstring Website { get; set; } = null!;
}
PS:請不要使用Yaml,如果你沒有必要的話。每個人都討厭yaml。
如果你喜歡我的文章,請給我一個贊!謝謝