Skip to content

Commit

Permalink
Make the type converters available to custom pre-processing phase vis…
Browse files Browse the repository at this point in the history
…itors.
  • Loading branch information
stijnherreman committed Jan 22, 2024
1 parent 6f8323e commit 3f75e84
Show file tree
Hide file tree
Showing 2 changed files with 159 additions and 2 deletions.
81 changes: 80 additions & 1 deletion YamlDotNet/Serialization/SerializerBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,22 @@ public SerializerBuilder WithPreProcessingPhaseObjectGraphVisitor<TObjectGraphVi
return WithPreProcessingPhaseObjectGraphVisitor(objectGraphVisitor, w => w.OnTop());
}

/// <summary>
/// Registers an additional <see cref="IObjectGraphVisitor{Nothing}" /> to be used by the serializer
/// before emitting an object graph.
/// </summary>
/// <remarks>
/// 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.
/// </remarks>
/// <param name="objectGraphVisitorFactory">A function that instantiates the type inspector.</param>
public SerializerBuilder WithPreProcessingPhaseObjectGraphVisitor<TObjectGraphVisitor>(Func<IEnumerable<IYamlTypeConverter>, TObjectGraphVisitor> objectGraphVisitorFactory)
where TObjectGraphVisitor : IObjectGraphVisitor<Nothing>
{
return WithPreProcessingPhaseObjectGraphVisitor(objectGraphVisitorFactory, w => w.OnTop());
}

/// <summary>
/// Registers an additional <see cref="IObjectGraphVisitor{Nothing}" /> to be used by the serializer
/// before emitting an object graph.
Expand Down Expand Up @@ -399,6 +415,38 @@ Action<IRegistrationLocationSelectionSyntax<IObjectGraphVisitor<Nothing>>> where
return this;
}


/// <summary>
/// Registers an additional <see cref="IObjectGraphVisitor{Nothing}" /> to be used by the serializer
/// before emitting an object graph.
/// </summary>
/// <remarks>
/// 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.
/// </remarks>
/// <param name="objectGraphVisitorFactory">A function that instantiates the type inspector.</param>
/// <param name="where">Configures the location where to insert the <see cref="IObjectGraphVisitor{Nothing}" /></param>
public SerializerBuilder WithPreProcessingPhaseObjectGraphVisitor<TObjectGraphVisitor>(
Func<IEnumerable<IYamlTypeConverter>, TObjectGraphVisitor> objectGraphVisitorFactory,
Action<IRegistrationLocationSelectionSyntax<IObjectGraphVisitor<Nothing>>> where
)
where TObjectGraphVisitor : IObjectGraphVisitor<Nothing>
{
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;
}

/// <summary>
/// Registers an additional <see cref="IObjectGraphVisitor{Nothing}" /> to be used by the serializer
/// before emitting an object graph.
Expand All @@ -408,7 +456,7 @@ Action<IRegistrationLocationSelectionSyntax<IObjectGraphVisitor<Nothing>>> 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.
/// </remarks>
/// <param name="objectGraphVisitorFactory">A factory that creates the <see cref="IObjectGraphVisitor{Nothing}" /> based on a previously registered <see cref="IObjectGraphVisitor{Nothing}" />.</param>
/// <param name="objectGraphVisitorFactory">A function that instantiates the type inspector based on a previously registered <see cref="IObjectGraphVisitor{Nothing}" />.</param>
/// <param name="where">Configures the location where to insert the <see cref="IObjectGraphVisitor{Nothing}" /></param>
public SerializerBuilder WithPreProcessingPhaseObjectGraphVisitor<TObjectGraphVisitor>(
WrapperFactory<IObjectGraphVisitor<Nothing>, TObjectGraphVisitor> objectGraphVisitorFactory,
Expand All @@ -430,6 +478,37 @@ Action<ITrackingRegistrationLocationSelectionSyntax<IObjectGraphVisitor<Nothing>
return this;
}

/// <summary>
/// Registers an additional <see cref="IObjectGraphVisitor{Nothing}" /> to be used by the serializer
/// before emitting an object graph.
/// </summary>
/// <remarks>
/// 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.
/// </remarks>
/// <param name="objectGraphVisitorFactory">A function that instantiates the type inspector based on a previously registered <see cref="IObjectGraphVisitor{Nothing}" />.</param>
/// <param name="where">Configures the location where to insert the <see cref="IObjectGraphVisitor{Nothing}" /></param>
public SerializerBuilder WithPreProcessingPhaseObjectGraphVisitor<TObjectGraphVisitor>(
WrapperFactory<IEnumerable<IYamlTypeConverter>, IObjectGraphVisitor<Nothing>, TObjectGraphVisitor> objectGraphVisitorFactory,
Action<ITrackingRegistrationLocationSelectionSyntax<IObjectGraphVisitor<Nothing>>> where
)
where TObjectGraphVisitor : IObjectGraphVisitor<Nothing>
{
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;
}

/// <summary>
/// Unregisters an existing <see cref="IObjectGraphVisitor{Nothing}" /> of type <typeparam name="TObjectGraphVisitor" />.
/// </summary>
Expand Down
80 changes: 79 additions & 1 deletion YamlDotNet/Serialization/StaticSerializerBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,22 @@ public StaticSerializerBuilder WithPreProcessingPhaseObjectGraphVisitor<TObjectG
return WithPreProcessingPhaseObjectGraphVisitor(objectGraphVisitor, w => w.OnTop());
}

/// <summary>
/// Registers an additional <see cref="IObjectGraphVisitor{Nothing}" /> to be used by the serializer
/// before emitting an object graph.
/// </summary>
/// <remarks>
/// 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.
/// </remarks>
/// <param name="objectGraphVisitorFactory">A function that instantiates the type inspector.</param>
public StaticSerializerBuilder WithPreProcessingPhaseObjectGraphVisitor<TObjectGraphVisitor>(Func<IEnumerable<IYamlTypeConverter>, TObjectGraphVisitor> objectGraphVisitorFactory)
where TObjectGraphVisitor : IObjectGraphVisitor<Nothing>
{
return WithPreProcessingPhaseObjectGraphVisitor(objectGraphVisitorFactory, w => w.OnTop());
}

/// <summary>
/// Registers an additional <see cref="IObjectGraphVisitor{Nothing}" /> to be used by the serializer
/// before emitting an object graph.
Expand Down Expand Up @@ -414,7 +430,38 @@ Action<IRegistrationLocationSelectionSyntax<IObjectGraphVisitor<Nothing>>> 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.
/// </remarks>
/// <param name="objectGraphVisitorFactory">A factory that creates the <see cref="IObjectGraphVisitor{Nothing}" /> based on a previously registered <see cref="IObjectGraphVisitor{Nothing}" />.</param>
/// <param name="objectGraphVisitorFactory">A function that instantiates the type inspector.</param>
/// <param name="where">Configures the location where to insert the <see cref="IObjectGraphVisitor{Nothing}" /></param>
public StaticSerializerBuilder WithPreProcessingPhaseObjectGraphVisitor<TObjectGraphVisitor>(
Func<IEnumerable<IYamlTypeConverter>, TObjectGraphVisitor> objectGraphVisitorFactory,
Action<IRegistrationLocationSelectionSyntax<IObjectGraphVisitor<Nothing>>> where
)
where TObjectGraphVisitor : IObjectGraphVisitor<Nothing>
{
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;
}

/// <summary>
/// Registers an additional <see cref="IObjectGraphVisitor{Nothing}" /> to be used by the serializer
/// before emitting an object graph.
/// </summary>
/// <remarks>
/// 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.
/// </remarks>
/// <param name="objectGraphVisitorFactory">A function that instantiates the type inspector based on a previously registered <see cref="IObjectGraphVisitor{Nothing}" />.</param>
/// <param name="where">Configures the location where to insert the <see cref="IObjectGraphVisitor{Nothing}" /></param>
public StaticSerializerBuilder WithPreProcessingPhaseObjectGraphVisitor<TObjectGraphVisitor>(
WrapperFactory<IObjectGraphVisitor<Nothing>, TObjectGraphVisitor> objectGraphVisitorFactory,
Expand All @@ -436,6 +483,37 @@ Action<ITrackingRegistrationLocationSelectionSyntax<IObjectGraphVisitor<Nothing>
return this;
}

/// <summary>
/// Registers an additional <see cref="IObjectGraphVisitor{Nothing}" /> to be used by the serializer
/// before emitting an object graph.
/// </summary>
/// <remarks>
/// 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.
/// </remarks>
/// <param name="objectGraphVisitorFactory">A function that instantiates the type inspector based on a previously registered <see cref="IObjectGraphVisitor{Nothing}" />.</param>
/// <param name="where">Configures the location where to insert the <see cref="IObjectGraphVisitor{Nothing}" /></param>
public StaticSerializerBuilder WithPreProcessingPhaseObjectGraphVisitor<TObjectGraphVisitor>(
WrapperFactory<IEnumerable<IYamlTypeConverter>, IObjectGraphVisitor<Nothing>, TObjectGraphVisitor> objectGraphVisitorFactory,
Action<ITrackingRegistrationLocationSelectionSyntax<IObjectGraphVisitor<Nothing>>> where
)
where TObjectGraphVisitor : IObjectGraphVisitor<Nothing>
{
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;
}

/// <summary>
/// Unregisters an existing <see cref="IObjectGraphVisitor{Nothing}" /> of type <typeparam name="TObjectGraphVisitor" />.
/// </summary>
Expand Down

0 comments on commit 3f75e84

Please sign in to comment.