0

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.

Mário Cera
  • 25
  • 1
  • 5
  • Why you write in the title "error in serialization" if the error message clearly says the **opposite**: "Exception during deserialization"? With a minus sign, your `Software-01` and `Software-02` becomes sequences instead of mapping, so you should read them correspondingly. – Tsyvarev Feb 05 '21 at 18:44
  • Thank you Tsyvarev. You are correct. – Mário Cera Feb 08 '21 at 11:00

1 Answers1

0

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.

flyx
  • 35,506
  • 7
  • 89
  • 126
  • Hi Flyx. I was using Software-01 on the example, but the requirement from my case is without numering. Strange, they are lacking that conversion, but after using your suggestion, I got the - on serialized file. Thank you very much. – Mário Cera Feb 08 '21 at 15:15
  • @MárioCera I don't quite follow, if you don't do the numbering, you can't have `Software-01` and `Software-02` on the same level. If there's something unclear, you can another question. – flyx Feb 08 '21 at 15:33