【问题标题】:c# usage of list in yaml - error in serializationc# yaml中list的用法-序列化错误
【发布时间】:2021-02-05 14:41:59
【问题描述】:

我能够反序列化 yaml 文件、查阅和更改值。 由于要求,我必须在变量 NAME、VERSION、... 前面使用减号(-)符号

文件 test.yaml

PlatForm: windows
Version: 10
    # Software Info
SOFTWARE:
    Software-01:
        - NAME    : MFS2020
        - VERSION : 1.12.2015
        - Customized  : true
    Software-02:
        - NAME    : DCS
        - VERSION : 6
        - Customized  : false

在 Johan Skeet (Error deserializing yaml with yamldotnet - Property not found) 的帮助下开发的测试程序

我能够处理数据,但序列化不会保留减号。 目前的代码:

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using YamlDotNet.RepresentationModel;
using YamlDotNet.Serialization;

public class UserSoft
{
    public string PlatForm { get; set; }
    public string Version { get; set; }
    public Dictionary<string, Software> SOFTWARE { get; set; }

    public class Software
    {
        public string NAME { get; set; }
        public string VERSION { get; set; }
        public string Customized { get; set; }

    }
}

class Program
{
    static void Main(string[] args)
    {
        string text = File.ReadAllText("test.yaml");
        var deserializer = new DeserializerBuilder().Build();
        UserSoft deserialized = deserializer.Deserialize<UserSoft>(text);
        
        deserialized.PlatForm = "Linux";
       

       Console.WriteLine("***Dumping Object Using Yaml Serializer***");
        var stringBuilder = new StringBuilder();
        var serializer = new Serializer();
        stringBuilder.AppendLine(serializer.Serialize(deserialized));
        


        //file construction
        var stream = new FileStream("Output.yml", FileMode.OpenOrCreate);
        using (StreamWriter writer = new StreamWriter(stream, Encoding.UTF8))
        {
            writer.Write(stringBuilder);
            writer.Close();
        }
    }
}

我认为该符号与 yaml 文件中的 List 有关。所以我测试了对象的变化为:

public class UserSoft
{
    public string PlatForm { get; set; }
    public string Version { get; set; }
    public Dictionary<string, Software> SOFTWARE { get; set; }

    public class Software
    {
        public List<string> NAME { get; set; }
        public List<string> VERSION { get; set; }
        public List<string> Customized { get; set; }
    }
}

没有成功:(

Exception thrown: 'YamlDotNet.Core.YamlException' in YamlDotNet.dll
An unhandled exception of type 'YamlDotNet.Core.YamlException' occurred in YamlDotNet.dll
(Line: 5, Col: 15, Idx: 75) - (Line: 5, Col: 22, Idx: 82): Exception during deserialization

我做错了什么?

我没有找到任何可以提供正确帮助的讨论。 保留原始格式(包括 cmets)可能会很好,但我在几个讨论中看到 yamldotnet 还不能做到这一点。这个符号是必须的。

提前感谢您对这些基本问题感到抱歉。

【问题讨论】:

  • 如果错误信息清楚地表明相反:“反序列化期间的异常”,为什么要在标题中写“序列化错误”?带减号,你的Software-01Software-02 变成序列而不是映射,所以你应该相应地阅读它们。
  • 谢谢 Tsyvarev。你是对的。

标签: c# list serialization yaml yamldotnet


【解决方案1】:

你的问题在这里:

    Software-01:
        - NAME    : MFS2020
        - VERSION : 1.12.2015
        - Customized  : true

Software-01 包含一个序列,其中每个项目都包含一个具有单个键值对的映射。要反序列化为您的原始结构,请将其更改为

    Software-01:
        NAME    : MFS2020
        VERSION : 1.12.2015
        Customized  : true

如果您想按原样反序列化 YAML,请使用以下结构:

public class UserSoft
{
    public string PlatForm { get; set; }
    public string Version { get; set; }
    public Dictionary<string, List<Dictionary<string, string>>> SOFTWARE { get; set; }
}

如您所见,由于Software-01 内部有一个 YAML 序列,您不能直接将其映射到自定义类。通常这是可能的,但是关于 is sadly lacking 的文档 - 您当然可以获取生成的 UserSoft 对象并手动将其转换为更好的结构。

【讨论】:

  • 嗨,弗莱克斯。我在示例中使用了 Software-01,但我的案例要求没有编号。奇怪,他们缺乏这种转换,但在使用你的建议后,我得到了 - on 序列化文件。非常感谢。
  • @MárioCera 我不太明白,如果你不做编号,你就不能在同一级别上拥有Software-01Software-02。如果有不清楚的地方,你可以再提问。
猜你喜欢
  • 1970-01-01
  • 2010-09-19
  • 2017-08-22
  • 1970-01-01
  • 1970-01-01
  • 2011-06-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多