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

How to get type converters when using WithPreProcessingPhaseObjectGraphVisitor #677

Closed
WEGFan opened this issue Feb 15, 2022 · 1 comment · Fixed by #895
Closed

How to get type converters when using WithPreProcessingPhaseObjectGraphVisitor #677

WEGFan opened this issue Feb 15, 2022 · 1 comment · Fixed by #895

Comments

@WEGFan
Copy link

WEGFan commented Feb 15, 2022

I'm currently trying to solve #660 in our project, by duplicating AnchorAssigner and replacing assignments dictionary definition to a compare by reference one. However, when I'm building the serializer, I found that WithPreProcessingPhaseObjectGraphVisitor() doesn't take a Func type as the parameter, so I need to instantiate my own ReferenceEqualAnchorAssigner but I don't know how can I get type converters.

So I would like to know is there a way to get a list of type converters?

My code is

new SerializerBuilder()
    .ConfigureDefaultValuesHandling(DefaultValuesHandling.Preserve)
    // expecting something `like typeConverters => new ReferenceEqualAnchorAssigner(typeConverters)` for the first argument
    .WithPreProcessingPhaseObjectGraphVisitor(new ReferenceEqualAnchorAssigner( ??? ), w => w.InsteadOf<AnchorAssigner>())
    .WithEmissionPhaseObjectGraphVisitor(args => new AnchorAssigningObjectGraphVisitor(args.InnerVisitor, args.EventEmitter, args.GetPreProcessingPhaseObjectGraphVisitor<ReferenceEqualAnchorAssigner>()),
        w => w.InsteadOf<AnchorAssigningObjectGraphVisitor>())
    .Build();

and ReferenceEqualAnchorAssigner is the same as AnchorAssigner except assignments:

public sealed class ReferenceEqualAnchorAssigner : PreProcessingPhaseObjectGraphVisitorSkeleton, IAliasProvider {
    private readonly IDictionary<object, AnchorAssignment> assignments = new Dictionary<object, AnchorAssignment>(ReferenceEqualityComparer.Instance);
    ...
}
@EdwardCooke
Copy link
Collaborator

There are 3 pre-built type converters, the DateTimeConverter, GuidConverter and SystemTypeConverter. They are in the YamlDotNet.Serialization.Converters namespace. In the serializer/deserializer builder base class, only GuidConverter and SystemTypeConverter are registered. I do see a way to remove type converters, but not add. You can see the code where it adds the type converters here:

typeConverterFactories = new LazyComponentRegistrationList<Nothing, IYamlTypeConverter>
{
{ typeof(GuidConverter), _ => new GuidConverter(false) },
{ typeof(SystemTypeConverter), _ => new SystemTypeConverter() }
};

I hope this helps.

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

Successfully merging a pull request may close this issue.

2 participants