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

Serialize tags #1035

Closed
heeko opened this issue Jan 22, 2025 · 2 comments
Closed

Serialize tags #1035

heeko opened this issue Jan 22, 2025 · 2 comments

Comments

@heeko
Copy link

heeko commented Jan 22, 2025

I'm trying to serialize a type with a tag, but I'm probably missing something, or there is a bug with the serializer. I'm using YamlDotNet 16.3.0. Here is a sample snippet that look like what I'm trying to achieve:

var serializer = new SerializerBuilder()
    .WithTypeConverter(new StringTestTypeConverter())
    .WithTagMapping("!myTag", typeof(string))
    .Build();

var yaml = serializer.Serialize("testTag");
Console.WriteLine(yaml);

public class StringTestTypeConverter : IYamlTypeConverter
{
    public bool Accepts(Type type)
    {
        return type == typeof(string);
    }

    public object? ReadYaml(IParser parser, Type type, ObjectDeserializer rootDeserializer)
    {
        throw new NotImplementedException();
    }

    public void WriteYaml(IEmitter emitter, object? value, Type type, ObjectSerializer serializer)
    {
        var scalar = new Scalar(AnchorName.Empty, "!myTag", (string)value);
        emitter.Emit(scalar);
    }
}

This exemple is outputing

"testTag"

What I would like, and what I would expect from the serializer is to output:

!myTag "testTag"

Am I missing something here?

@heeko
Copy link
Author

heeko commented Jan 22, 2025

I figure out what's going wrong. I had to play with the isPlainImplicit and isQuotedImplicit values.

@heeko heeko closed this as completed Jan 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants
@heeko and others