-
-
Notifications
You must be signed in to change notification settings - Fork 488
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 do I specify the tag for each item in the List #765
Comments
That result is actually not valid YAML. Here's what the reference yaml parser returns for that (notice all the errors) I'm assuming you mean this (colons on the end): - person1:
age: 10
name: 1
- person2:
age: 20
name: 2
- person3:
age: 30
name: 3
- person4:
age: 40
name: 4 In which case, this works: using System.Collections.Generic;
using YamlDotNet.Core;
using YamlDotNet.Serialization;
namespace ConsoleApp1
{
public static class TypeConverterExample1
{
public static void Test()
{
var personList = new List<Person?>
{
new Person() { Id = "person1", Age = 10, Name = "1" },
new Person() { Id = "person2", Age = 20, Name = "2" },
new Person() { Id = "person3", Age = 30, Name = "3" },
new Person() { Id = "person4", Age = 40, Name = "4" },
null
};
var serializer = new SerializerBuilder()
.WithTypeConverter(new PersonTypeConverter())
.Build();
var yaml = serializer.Serialize(personList);
Console.WriteLine(yaml);
}
private class PersonTypeConverter : IYamlTypeConverter
{
public bool Accepts(Type type) => type == typeof(Person);
public object? ReadYaml(IParser parser, Type type)
{
throw new NotImplementedException();
}
public void WriteYaml(IEmitter emitter, object? value, Type type)
{
var person = (Person?)value;
if (person == null)
{
emitter.Emit(new YamlDotNet.Core.Events.Scalar("null"));
return;
}
emitter.Emit(new YamlDotNet.Core.Events.MappingStart(new AnchorName(), new TagName(), false, YamlDotNet.Core.Events.MappingStyle.Any));
emitter.Emit(new YamlDotNet.Core.Events.Scalar(person.Id));
emitter.Emit(new YamlDotNet.Core.Events.MappingStart(new AnchorName(), new TagName(), false, YamlDotNet.Core.Events.MappingStyle.Any));
emitter.Emit(new YamlDotNet.Core.Events.Scalar("age"));
emitter.Emit(new YamlDotNet.Core.Events.Scalar($"{person.Age}"));
emitter.Emit(new YamlDotNet.Core.Events.Scalar("name"));
emitter.Emit(new YamlDotNet.Core.Events.Scalar($"{person.Name}"));
emitter.Emit(new YamlDotNet.Core.Events.MappingEnd());
emitter.Emit(new YamlDotNet.Core.Events.MappingEnd());
}
}
private class Person
{
public string Id { get; set; }
public int Age { get; set; }
public string Name { get; set; }
}
}
} |
Thank you very much. That's exactly what I wanted. |
How do I deserialize them with tag?
|
I try to match the property name in the ReadYaml method against Parser.Current, but it is difficult to determine the hierarchy if my Person object contains a List type property.
|
My sample code:
Console.WriteLine result:
I want each item to have a different tag
just like this:
or tag uses an attribute of the Person class
What should I do to achieve this?Thank you!
The text was updated successfully, but these errors were encountered: