Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Yaml format after .Save() #339

Open
YvesR opened this issue Jul 25, 2018 · 7 comments
Open

Yaml format after .Save() #339

YvesR opened this issue Jul 25, 2018 · 7 comments
Labels

Comments

@YvesR
Copy link

YvesR commented Jul 25, 2018

Hello,
I am new to YamlDotNet, as everyone uses it, I downloaded and used it from nuget.org.

I use the component for loading, modifying and save key values from yml configuration files.

Example:

---
name: latest
alias: ''
url: http://loki
path: c:\informer\company\latest
files: c:\informer\data\latest
auth: 1
environment: production

When I load, modify and save the new yml file looks like this:

name: latest
alias: ''
url: http://loki
path: c:\informer\company\latest
files: c:\informer\data\latest
auth: &1344666770 1
environment: production
...

So it removed the --- from start, added ... at the end and added a &number at auth: key.

This is my code for load and modify data:

    /// <summary>
    /// Update company settings in krcompprops table from the current company
    /// </summary>
    /// <param name="key"></param>
    /// <param name="value"></param>
    public void UpdateCompanySettings(string key, string value)
    {
      var reader = new StreamReader(GetCompanyRoot() + companyYml);
      var yaml = new YamlStream();
      yaml.Load(reader);
      reader.Close();
      var mapping = (YamlMappingNode)yaml.Documents[0].RootNode;
      ((YamlScalarNode)mapping.Children[key]).Value = value;
      using (TextWriter writer = File.CreateText(GetCompanyRoot() + companyYml)) { 
        yaml.Save(writer);
      }
    }

    /// <summary>
    /// Returns company settings in krcompprops table from the current company
    /// </summary>
    /// <param name="key"></param>
    public dynamic GetCompanySettings(string key)
    {
      using ( var reader = new StreamReader(GetCompanyRoot() + @"\ror\config\company.yml")) { 
        var yaml = new YamlStream();
        yaml.Load(reader);
        var mapping = (YamlMappingNode)yaml.Documents[0].RootNode;
        return ((YamlScalarNode) mapping.Children[key]).Value;
      }
    }

So this new resulting yml file seems still be correct, but it confuses me, is everything correct in this case?

@seanfinniss
Copy link

seanfinniss commented Oct 15, 2018

// in:
using (TextWriter writer = File.CreateText(GetCompanyRoot() + companyYml)) {
yaml.Save(writer);
}

// change this to:
using (TextWriter writer = File.CreateText(GetCompanyRoot() + companyYml)) {
yaml.Save(writer, false); // will now suppress the creation of anchors in your nodes.
}

@aaubry
Copy link
Owner

aaubry commented Sep 25, 2019

I'm sorry I was unable to answer this question in a timely fashion. As you have certainly moved on to other things, I will close this issue, but feel free to reopen it if this is still an issue.

@aaubry aaubry closed this as completed Sep 25, 2019
@YvesR
Copy link
Author

YvesR commented Sep 26, 2019

Oh somehow I missed the previous answer. I will try it today and if it works I be happy, else I will be back here :)

@YvesR
Copy link
Author

YvesR commented Sep 26, 2019

Hello,

@seanfinniss answer helped that &prefix before the number is gone, but I still have --- removed and ... added at the end of the yml file.

@aaubry any more advise here?

Thanks, Yves

@aaubry aaubry reopened this Sep 28, 2019
@aaubry
Copy link
Owner

aaubry commented Sep 28, 2019

I need to look into this. Maybe there is some information that is not being preserved.

@aaubry aaubry added the bug label Sep 28, 2019
@cveld
Copy link

cveld commented Dec 29, 2020

Any progress the past year? In my case the yaml stream emits an undesirable explicit document marker as well.

Code:

var mappingNode = new YamlMappingNode();
var yaml = new YamlDocument(mappingNode);            
mappingNode.Add("one", "other");
var yamlStream = new YamlStream(yaml);
var buffer = new StringBuilder();
using (var writer = new StringWriter(buffer))
{
	yamlStream.Save(writer, assignAnchors: false);
	output.WriteLine(writer.ToString());
}

Current output:

one: other
...

Desired output:

one: other

@queil
Copy link

queil commented Dec 8, 2021

@cveld Try doing serialize instead. The below is F# code but it is similar to C#:

  let serializer = SerializerBuilder().Build();

  let SaveFile (path:string) (yaml:YamlStream) =
    use file = File.Open(path, FileMode.Create)
    use writer = new StreamWriter(file, System.Text.Encoding.UTF8)
    serializer.Serialize(writer, yaml.Documents.[0].RootNode)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants