Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'am able to deserialize the yaml file, consult and change the values. Due to an requirement, I have to use the minus(-) symbol in front of the variables NAME, VERSION, ...

The file 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

With the test program developed with the help of Johan Skeet (Error deserializing yaml with yamldotnet - Property not found)

I'm able to process the data, but the serialization does not remain the minus symbol. The code presently:

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();
        }
    }
}

I think that the symbol is related to List in yaml files. So I have tested the change of the object to:

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; }
    }
}

No success :(

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

What I'm doing wrong?

I did not find any discussion that could have the correct help. It could be nice to keep the original formating, including comments, but I saw in several dicussions that yamldotnet is not able to do that yet. This symbol is a must.

Thank you in advance and sorry for this basic questions.

question from:https://stackoverflow.com/questions/66065242/c-sharp-usage-of-list-in-yaml-error-in-serialization

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
161 views
Welcome To Ask or Share your Answers For Others

1 Answer

Your problem is here:

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

Software-01 contains a sequence, where each item contains a mapping with a single key-value pair. To deserialize to your original structure, change this to

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

If you want to deserialize the YAML as-is, use this structure:

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

As you can see, since there is a YAML sequence inside of Software-01, you cannot directly map it to a custom class. Generally this would be possible, but the documentation on that is sadly lacking – you can of course take the generated UserSoft object and convert it to a nicer structure by hand.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...