-
-
Notifications
You must be signed in to change notification settings - Fork 486
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
How to serialize the !include Directive in YamlDotNet #880
Comments
On the YamlScalarNode try setting the scalar type to plain. That should do it. |
@EdwardCooke thanks for the answer. The NodeType property is get only.
|
The property is actually style. Sorry about that.
|
@EdwardCooke the result is same.
|
I hope this is only temporary solution :)
|
What if you set the tag property to !include or include? Then set the value to your file. |
Just got to spend more time on this. This is much cleaner, using a yaml type convert and an include object. using YamlDotNet.Core;
using YamlDotNet.Core.Events;
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;
var serializer = new SerializerBuilder()
.WithTagMapping("!include", typeof(IncludedObject))
.WithTypeConverter(new TestTypeConverter())
.WithNamingConvention(CamelCaseNamingConvention.Instance)
.Build();
var o = new object[] {
new IncludedObject{ Filename = "test.yaml" },
new Book
{
Title = "a title",
Path = "a path",
Badges = "some badges"
}
};
var serialized = serializer.Serialize(o);
Console.WriteLine(serialized);
class Outer
{
public object[] Items { get; set; }
}
class Book
{
public string Title { get; set; }
public string Path { get; set; }
public string Badges { get; set; }
}
// This is the magic that makes it easily re-usable
class TestTypeConverter : IYamlTypeConverter
{
public bool Accepts(Type type) => type == typeof(IncludedObject);
public object? ReadYaml(IParser parser, Type type)
{
throw new NotImplementedException();
}
public void WriteYaml(IEmitter emitter, object? value, Type type)
{
if (type != typeof(IncludedObject))
{
return;
}
if (value is IncludedObject o)
{
emitter.Emit(new Scalar(null, "!include", o.Filename, ScalarStyle.Plain, false, false));
}
}
}
class IncludedObject
{
public string Filename { get; set; } = string.Empty;
} Results in
|
If you want to see how to read in an include directive, checkout my answer here. |
@EdwardCooke thanks! |
Description:
I have a YAML configuration that includes external files using the !include directive, like this:
I'm using YamlDotNet for YAML serialization in my C# project, and I'm wondering how to properly serialize this YAML structure while ensuring that the !include directive is correctly processed to include external files.
I'm tried
also
The result is both cases the same:
Is there something like
RawYamlNode
?The text was updated successfully, but these errors were encountered: