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

Set anchor tag name during serialization using YamlDotNet? #807

Closed
Nidhi1819 opened this issue May 19, 2023 · 4 comments
Closed

Set anchor tag name during serialization using YamlDotNet? #807

Nidhi1819 opened this issue May 19, 2023 · 4 comments
Labels

Comments

@Nidhi1819
Copy link

          This sounds interesting. Is there also an easy way to control the anchor naming during serialization? I would like to use YamlDotNet to create a stable file format, that changes as little as possible during multiple deserialize, change, serialize cycles, so different versions of the files can be compared and merged easily.

Originally posted by @rohahn in #227 (comment)

@Nidhi1819
Copy link
Author

I am looking for answer to this thread. I am using XML file and generating YAML out of it using YamlDotNet. The YAML that gets generated has anchor tags but the name of the tags is not meaningfully. I want to control the name given to anchor tag and give a more meaningful name to it. Is there any easy way to do it or will have to write a custom emitter for it.

@Nidhi1819 Nidhi1819 changed the title This sounds interesting. Is there also an easy way to control the anchor naming during serialization? I would like to use YamlDotNet to create a stable file format, that changes as little as possible during multiple deserialize, change, serialize cycles, so different versions of the files can be compared and merged easily. Set anchor tag name during serialization using YamlDotNet? May 19, 2023
@EdwardCooke
Copy link
Collaborator

I think you can replace this implementation with your own and be able to create more meaningful names. However, you may not be able to get meaningful context to generate a name that makes sense.

public sealed class AnchorAssigner : PreProcessingPhaseObjectGraphVisitorSkeleton, IAliasProvider

@EdwardCooke
Copy link
Collaborator

Since there was a solution provided with no feedback I’m closing this issue. If you disagree go ahead and reopen it and we can discuss more.

@BBoldenow-Alt
Copy link

BBoldenow-Alt commented Jul 11, 2023

Since there was a solution provided with no feedback I’m closing this issue. If you disagree go ahead and reopen it and we can discuss more.

@EdwardCooke - I think this is reasonable, but am having trouble getting the IAliasProvider implementation to trigger. I mostly copied the AnchorAssigner, but customized the name portion. I then provided this to my serializer like this:

var anchorNaming = new CustomAnchorAssigner(new List<IYamlTypeConverter>()
{
    customConverter1,
    customConverter2,
    customConverter3,
});

var serializerBuilder = new SerializerBuilder()
    .ConfigureDefaultValuesHandling(DefaultValuesHandling.OmitEmptyCollections)
    .ConfigureDefaultValuesHandling(DefaultValuesHandling.OmitNull)
    .WithTypeConverter(customConverter1)
    .WithTypeConverter(customConverter2)
    .WithTypeConverter(customConverter3)
    .WithPreProcessingPhaseObjectGraphVisitor(anchorNaming)

This triggers all of the VisitObject events, but the GetAlias method is never called. Is there something I'm missing to use this Alias Provider?

Edit:

I was able to get this working by adding the following line to the SerializerBuilder

.WithEmissionPhaseObjectGraphVisitor(args => new AnchorAssigningObjectGraphVisitor(args.InnerVisitor, args.EventEmitter, anchorNaming),
w => w.InsteadOf<AnchorAssigningObjectGraphVisitor>())

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

3 participants