From 3f75e8424b0afc54f596c40fe7f39d75b31a0e59 Mon Sep 17 00:00:00 2001 From: Stijn Herreman Date: Sun, 21 Jan 2024 23:38:10 +0100 Subject: [PATCH] Make the type converters available to custom pre-processing phase visitors. --- YamlDotNet/Serialization/SerializerBuilder.cs | 81 ++++++++++++++++++- .../Serialization/StaticSerializerBuilder.cs | 80 +++++++++++++++++- 2 files changed, 159 insertions(+), 2 deletions(-) diff --git a/YamlDotNet/Serialization/SerializerBuilder.cs b/YamlDotNet/Serialization/SerializerBuilder.cs index 8b817f7c..be6cad98 100755 --- a/YamlDotNet/Serialization/SerializerBuilder.cs +++ b/YamlDotNet/Serialization/SerializerBuilder.cs @@ -368,6 +368,22 @@ public SerializerBuilder WithPreProcessingPhaseObjectGraphVisitor w.OnTop()); } + /// + /// Registers an additional to be used by the serializer + /// before emitting an object graph. + /// + /// + /// Registering a visitor in the pre-processing phase enables to traverse the object graph once + /// before actually emitting it. This allows a visitor to collect information about the graph that + /// can be used later by another visitor registered in the emission phase. + /// + /// A function that instantiates the type inspector. + public SerializerBuilder WithPreProcessingPhaseObjectGraphVisitor(Func, TObjectGraphVisitor> objectGraphVisitorFactory) + where TObjectGraphVisitor : IObjectGraphVisitor + { + return WithPreProcessingPhaseObjectGraphVisitor(objectGraphVisitorFactory, w => w.OnTop()); + } + /// /// Registers an additional to be used by the serializer /// before emitting an object graph. @@ -399,6 +415,38 @@ Action>> where return this; } + + /// + /// Registers an additional to be used by the serializer + /// before emitting an object graph. + /// + /// + /// Registering a visitor in the pre-processing phase enables to traverse the object graph once + /// before actually emitting it. This allows a visitor to collect information about the graph that + /// can be used later by another visitor registered in the emission phase. + /// + /// A function that instantiates the type inspector. + /// Configures the location where to insert the + public SerializerBuilder WithPreProcessingPhaseObjectGraphVisitor( + Func, TObjectGraphVisitor> objectGraphVisitorFactory, + Action>> where + ) + where TObjectGraphVisitor : IObjectGraphVisitor + { + if (objectGraphVisitorFactory == null) + { + throw new ArgumentNullException(nameof(objectGraphVisitorFactory)); + } + + if (where == null) + { + throw new ArgumentNullException(nameof(where)); + } + + where(preProcessingPhaseObjectGraphVisitorFactories.CreateRegistrationLocationSelector(typeof(TObjectGraphVisitor), typeConverters => objectGraphVisitorFactory(typeConverters))); + return this; + } + /// /// Registers an additional to be used by the serializer /// before emitting an object graph. @@ -408,7 +456,7 @@ Action>> where /// before actually emitting it. This allows a visitor to collect information about the graph that /// can be used later by another visitor registered in the emission phase. /// - /// A factory that creates the based on a previously registered . + /// A function that instantiates the type inspector based on a previously registered . /// Configures the location where to insert the public SerializerBuilder WithPreProcessingPhaseObjectGraphVisitor( WrapperFactory, TObjectGraphVisitor> objectGraphVisitorFactory, @@ -430,6 +478,37 @@ Action return this; } + /// + /// Registers an additional to be used by the serializer + /// before emitting an object graph. + /// + /// + /// Registering a visitor in the pre-processing phase enables to traverse the object graph once + /// before actually emitting it. This allows a visitor to collect information about the graph that + /// can be used later by another visitor registered in the emission phase. + /// + /// A function that instantiates the type inspector based on a previously registered . + /// Configures the location where to insert the + public SerializerBuilder WithPreProcessingPhaseObjectGraphVisitor( + WrapperFactory, IObjectGraphVisitor, TObjectGraphVisitor> objectGraphVisitorFactory, + Action>> where + ) + where TObjectGraphVisitor : IObjectGraphVisitor + { + if (objectGraphVisitorFactory == null) + { + throw new ArgumentNullException(nameof(objectGraphVisitorFactory)); + } + + if (where == null) + { + throw new ArgumentNullException(nameof(where)); + } + + where(preProcessingPhaseObjectGraphVisitorFactories.CreateTrackingRegistrationLocationSelector(typeof(TObjectGraphVisitor), (wrapped, typeConverters) => objectGraphVisitorFactory(wrapped, typeConverters))); + return this; + } + /// /// Unregisters an existing of type . /// diff --git a/YamlDotNet/Serialization/StaticSerializerBuilder.cs b/YamlDotNet/Serialization/StaticSerializerBuilder.cs index 99b332fa..2adde6a5 100644 --- a/YamlDotNet/Serialization/StaticSerializerBuilder.cs +++ b/YamlDotNet/Serialization/StaticSerializerBuilder.cs @@ -374,6 +374,22 @@ public StaticSerializerBuilder WithPreProcessingPhaseObjectGraphVisitor w.OnTop()); } + /// + /// Registers an additional to be used by the serializer + /// before emitting an object graph. + /// + /// + /// Registering a visitor in the pre-processing phase enables to traverse the object graph once + /// before actually emitting it. This allows a visitor to collect information about the graph that + /// can be used later by another visitor registered in the emission phase. + /// + /// A function that instantiates the type inspector. + public StaticSerializerBuilder WithPreProcessingPhaseObjectGraphVisitor(Func, TObjectGraphVisitor> objectGraphVisitorFactory) + where TObjectGraphVisitor : IObjectGraphVisitor + { + return WithPreProcessingPhaseObjectGraphVisitor(objectGraphVisitorFactory, w => w.OnTop()); + } + /// /// Registers an additional to be used by the serializer /// before emitting an object graph. @@ -414,7 +430,38 @@ Action>> where /// before actually emitting it. This allows a visitor to collect information about the graph that /// can be used later by another visitor registered in the emission phase. /// - /// A factory that creates the based on a previously registered . + /// A function that instantiates the type inspector. + /// Configures the location where to insert the + public StaticSerializerBuilder WithPreProcessingPhaseObjectGraphVisitor( + Func, TObjectGraphVisitor> objectGraphVisitorFactory, + Action>> where + ) + where TObjectGraphVisitor : IObjectGraphVisitor + { + if (objectGraphVisitorFactory == null) + { + throw new ArgumentNullException(nameof(objectGraphVisitorFactory)); + } + + if (where == null) + { + throw new ArgumentNullException(nameof(where)); + } + + where(preProcessingPhaseObjectGraphVisitorFactories.CreateRegistrationLocationSelector(typeof(TObjectGraphVisitor), typeConverters => objectGraphVisitorFactory(typeConverters))); + return this; + } + + /// + /// Registers an additional to be used by the serializer + /// before emitting an object graph. + /// + /// + /// Registering a visitor in the pre-processing phase enables to traverse the object graph once + /// before actually emitting it. This allows a visitor to collect information about the graph that + /// can be used later by another visitor registered in the emission phase. + /// + /// A function that instantiates the type inspector based on a previously registered . /// Configures the location where to insert the public StaticSerializerBuilder WithPreProcessingPhaseObjectGraphVisitor( WrapperFactory, TObjectGraphVisitor> objectGraphVisitorFactory, @@ -436,6 +483,37 @@ Action return this; } + /// + /// Registers an additional to be used by the serializer + /// before emitting an object graph. + /// + /// + /// Registering a visitor in the pre-processing phase enables to traverse the object graph once + /// before actually emitting it. This allows a visitor to collect information about the graph that + /// can be used later by another visitor registered in the emission phase. + /// + /// A function that instantiates the type inspector based on a previously registered . + /// Configures the location where to insert the + public StaticSerializerBuilder WithPreProcessingPhaseObjectGraphVisitor( + WrapperFactory, IObjectGraphVisitor, TObjectGraphVisitor> objectGraphVisitorFactory, + Action>> where + ) + where TObjectGraphVisitor : IObjectGraphVisitor + { + if (objectGraphVisitorFactory == null) + { + throw new ArgumentNullException(nameof(objectGraphVisitorFactory)); + } + + if (where == null) + { + throw new ArgumentNullException(nameof(where)); + } + + where(preProcessingPhaseObjectGraphVisitorFactories.CreateTrackingRegistrationLocationSelector(typeof(TObjectGraphVisitor), (wrapped, typeConverters) => objectGraphVisitorFactory(wrapped, typeConverters))); + return this; + } + /// /// Unregisters an existing of type . ///