我想将两个 YAML 文件合并为一个,这样可以避免重复并合并属性。例如,有两个 yaml 文件,如下所示:
yaml1:
first_name: "John"
last_name: "Smith"
enabled: false
roles:
- user
yaml2:
enabled: true
roles:
- user
- admin
我希望得到以下结果:
first_name: "John"
last_name: "Smith"
enabled: true
roles:
- user
- admin
到目前为止,我能够将 YAML 转换为 JSON 并使用 this example ,但是,我想知道一种使用 C# YAML 库(如 yamldotnet 和 SharpYaml )的方法。
最佳答案
我使用 Yamldotnet 然后使用以下算法实现了它:
2.1 如果是新字段添加
2.2 如果该字段存在且不是集合,则覆盖它
2.3 如果字段存在并且是集合,则合并集合
2.3.1 如果新值不是集合,则将其添加到集合中
2.3.2 如果新值是一个集合,则将每个非重复元素添加到集合中。出于这个原因,我使用
HashSet
,不允许重复项的集合。 编码:
static void Main(string[] args)
{
var deserializer = new DeserializerBuilder()
.WithNamingConvention(new CamelCaseNamingConvention())
.Build();
var object1 = deserializer.Deserialize<Dictionary<string,object>>(@"---
first_name: ""John""
last_name: ""Smith""
enabled: false
roles:
- user
...");
var object2 = deserializer.Deserialize<Dictionary<string, object>>(@"---
enabled: true
roles:
- user
- admin
...");
foreach (var tuple in object2)
{
if (!object1.ContainsKey(tuple.Key))
{
object1.Add(tuple.Key, tuple.Value);
continue;
}
var oldValue = object1[tuple.Key];
if (!(oldValue is ICollection))
{
object1[tuple.Key] = tuple.Value;
continue;
}
//Merge collection
var mergeCollection = new HashSet<object>(oldValue as IEnumerable<object>);
if (!(tuple.Value is ICollection))
mergeCollection.Add(tuple.Value);
else
{
foreach (var item in tuple.Value as IEnumerable)
{
mergeCollection.Add(item);
}
}
object1[tuple.Key] = mergeCollection;
}
var result = new SerializerBuilder().Build().Serialize(object1);
}
我希望这可以帮助你:)
关于c# - 在 C# 中合并 YAML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53412509/