繁体   English   中英

在 yaml 中序列化 c# object

[英]Serializing a c# object in yaml

我正在尝试序列化一个看起来像这样的文本:

m_list 2 1035 6 16 204 206 501
pdtse 1 6
m_list 3 1 1027 10 1035 1 2038 8 -1
pdtse 1 16
m_list 3 1 1027 8 1035 1 2042 6 -1
pdtse 1 204
m_list 3 1 1027 15 1035 1 2042 10 -1
pdtse 1 206
m_list 3 1 1027 8 1035 1 2046 7 -1
pdtse 1 501
m_list 3 1 500 1 1027 14 1035 1 -1
pdtclose

但是,经过几次研究和调试后,我仍然得到相同的结果,并且无法弄清楚错误来自哪里。

这是我得到的结果:

结果

但是缺少物品和随机物品,例如 *o0 & &o0,我无法弄清楚为什么

这是我的代码:

        string[] lines = File.ReadAllLines(Path.Combine(_configuration.BasePath, _configuration.RecipeFile));
        List<Recipe> recipes = new();
        Recipe tempRecipe = new();
        long vnumToInsert = 0;

        foreach (string line in lines)
        {
            string[] parts = line.Split(' ');

            if (line.StartsWith("m_list 2"))
            {
                foreach (string part in parts.Skip(3))
                {
                    if (tempRecipe.Recipes == null)
                    {
                        tempRecipe.Recipes = new List<RecipeAttributes>()
                        {
                            new RecipeAttributes
                            {
                                ItemVnum = Convert.ToInt64(part),
                                ProducerItemVnum = Convert.ToInt64(parts[2]),
                                Amount = 1
                            }
                        };
                    }
                    else
                    {
                        tempRecipe.Recipes.Add(new RecipeAttributes
                        {
                            ItemVnum = Convert.ToInt64(part),
                            ProducerItemVnum = Convert.ToInt64(parts[2]),
                            Amount = 1
                        });
                    }
                }
            }
            
            if (line.StartsWith("pdtse"))
            {
                vnumToInsert = Convert.ToInt64(parts[2]);
            }

            if (line.StartsWith("m_list 3"))
            {
                for (int i = 3; i < parts.Length; i += 2)
                {
                    if (Convert.ToInt64(parts[i]) < 0) continue;

                    if (tempRecipe.Recipes == null) continue;

                    var findRecipeRelatedToVnum = tempRecipe.Recipes.FirstOrDefault(s => s.ItemVnum == vnumToInsert);

                    if (findRecipeRelatedToVnum == null) break;

                    if (findRecipeRelatedToVnum.Items == null)
                    {
                        findRecipeRelatedToVnum.Items = new List<ItemAttributes>()
                        {
                            new ItemAttributes()
                            {
                                ItemVNum = Convert.ToInt64(parts[i]),
                                Amount = Convert.ToInt64(parts[i + 1])
                            }
                        };
                    }
                    else
                    {
                        if (findRecipeRelatedToVnum.Items.FirstOrDefault(s => s.ItemVNum == Convert.ToInt64(parts[i])) != null) continue;

                        findRecipeRelatedToVnum.Items.Add(new ItemAttributes()
                        {
                            ItemVNum = Convert.ToInt64(parts[i]),
                            Amount = Convert.ToInt64(parts[i + 1])
                        });
                    }
                }
            }

            if (line.StartsWith("pdtclose"))
            {
                recipes.Add(tempRecipe);
                tempRecipe = new();
            }
        }


        Recipe toSerialize = new();
        List<long> alreadyDone = new();
        foreach (Recipe recipe in recipes)
        {
            toSerialize = new();

            if (recipe.Recipes == null) continue;

            if (alreadyDone.Contains(recipe.Recipes[0].ProducerItemVnum)) continue;

            foreach (Recipe r in recipes.Where(s => s.Recipes?.First().ProducerItemVnum == recipe.Recipes?.First().ProducerItemVnum))
            {
                toSerialize = r;
            }

            alreadyDone.Add(recipe.Recipes[0].ProducerItemVnum);
            var serializer = new SerializerBuilder().Build();
            var yaml = serializer.Serialize(toSerialize);
            Console.WriteLine(yaml);
        }

还有我的 yaml class:

using YamlDotNet.Serialization;

namespace YamlParser.Entities
{
    public record Recipe
    {
        [YamlMember(Alias = "recipes")]
        public List<RecipeAttributes> Recipes { get; set; }
    }

    public record RecipeAttributes
    {
        [YamlMember(Alias = "item_vnum")]
        public long ItemVnum { get; set; }

        [YamlMember(Alias = "quantity")]
        public long Amount { get; set; }

        [YamlMember(Alias = "producer_item_vnum")]
        public long ProducerItemVnum { get; set; }

        [YamlMember(Alias = "items")]
        public List<ItemAttributes> Items { get; set; }
    }

    public record ItemAttributes
    {
        [YamlMember(Alias = "item_vnum")]
        public long ItemVNum { get; set; }

        [YamlMember(Alias = "quantity")]
        public long Amount { get; set; }
    }
}

提前感谢您的帮助,祝您有美好的一天!

你可以使用如下它需要这个 Nuget YamlDotNet.Serialization

private string? Serialize(Dictionary<string, object> dictionary)
        {
            if (dictionary.Count == 0) return null;
            var serializer = new SerializerBuilder().Build();
            var yaml = serializer.Serialize(dictionary);
            return string.IsNullOrEmpty(yaml)
                    ? yaml
                    : $"---\n{yaml}";
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM