Skip to content

Commit 3f75e84

Browse files
committed
Make the type converters available to custom pre-processing phase visitors.
1 parent 6f8323e commit 3f75e84

File tree

2 files changed

+159
-2
lines changed

2 files changed

+159
-2
lines changed

YamlDotNet/Serialization/SerializerBuilder.cs

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,22 @@ public SerializerBuilder WithPreProcessingPhaseObjectGraphVisitor<TObjectGraphVi
368368
return WithPreProcessingPhaseObjectGraphVisitor(objectGraphVisitor, w => w.OnTop());
369369
}
370370

371+
/// <summary>
372+
/// Registers an additional <see cref="IObjectGraphVisitor{Nothing}" /> to be used by the serializer
373+
/// before emitting an object graph.
374+
/// </summary>
375+
/// <remarks>
376+
/// Registering a visitor in the pre-processing phase enables to traverse the object graph once
377+
/// before actually emitting it. This allows a visitor to collect information about the graph that
378+
/// can be used later by another visitor registered in the emission phase.
379+
/// </remarks>
380+
/// <param name="objectGraphVisitorFactory">A function that instantiates the type inspector.</param>
381+
public SerializerBuilder WithPreProcessingPhaseObjectGraphVisitor<TObjectGraphVisitor>(Func<IEnumerable<IYamlTypeConverter>, TObjectGraphVisitor> objectGraphVisitorFactory)
382+
where TObjectGraphVisitor : IObjectGraphVisitor<Nothing>
383+
{
384+
return WithPreProcessingPhaseObjectGraphVisitor(objectGraphVisitorFactory, w => w.OnTop());
385+
}
386+
371387
/// <summary>
372388
/// Registers an additional <see cref="IObjectGraphVisitor{Nothing}" /> to be used by the serializer
373389
/// before emitting an object graph.
@@ -399,6 +415,38 @@ Action<IRegistrationLocationSelectionSyntax<IObjectGraphVisitor<Nothing>>> where
399415
return this;
400416
}
401417

418+
419+
/// <summary>
420+
/// Registers an additional <see cref="IObjectGraphVisitor{Nothing}" /> to be used by the serializer
421+
/// before emitting an object graph.
422+
/// </summary>
423+
/// <remarks>
424+
/// Registering a visitor in the pre-processing phase enables to traverse the object graph once
425+
/// before actually emitting it. This allows a visitor to collect information about the graph that
426+
/// can be used later by another visitor registered in the emission phase.
427+
/// </remarks>
428+
/// <param name="objectGraphVisitorFactory">A function that instantiates the type inspector.</param>
429+
/// <param name="where">Configures the location where to insert the <see cref="IObjectGraphVisitor{Nothing}" /></param>
430+
public SerializerBuilder WithPreProcessingPhaseObjectGraphVisitor<TObjectGraphVisitor>(
431+
Func<IEnumerable<IYamlTypeConverter>, TObjectGraphVisitor> objectGraphVisitorFactory,
432+
Action<IRegistrationLocationSelectionSyntax<IObjectGraphVisitor<Nothing>>> where
433+
)
434+
where TObjectGraphVisitor : IObjectGraphVisitor<Nothing>
435+
{
436+
if (objectGraphVisitorFactory == null)
437+
{
438+
throw new ArgumentNullException(nameof(objectGraphVisitorFactory));
439+
}
440+
441+
if (where == null)
442+
{
443+
throw new ArgumentNullException(nameof(where));
444+
}
445+
446+
where(preProcessingPhaseObjectGraphVisitorFactories.CreateRegistrationLocationSelector(typeof(TObjectGraphVisitor), typeConverters => objectGraphVisitorFactory(typeConverters)));
447+
return this;
448+
}
449+
402450
/// <summary>
403451
/// Registers an additional <see cref="IObjectGraphVisitor{Nothing}" /> to be used by the serializer
404452
/// before emitting an object graph.
@@ -408,7 +456,7 @@ Action<IRegistrationLocationSelectionSyntax<IObjectGraphVisitor<Nothing>>> where
408456
/// before actually emitting it. This allows a visitor to collect information about the graph that
409457
/// can be used later by another visitor registered in the emission phase.
410458
/// </remarks>
411-
/// <param name="objectGraphVisitorFactory">A factory that creates the <see cref="IObjectGraphVisitor{Nothing}" /> based on a previously registered <see cref="IObjectGraphVisitor{Nothing}" />.</param>
459+
/// <param name="objectGraphVisitorFactory">A function that instantiates the type inspector based on a previously registered <see cref="IObjectGraphVisitor{Nothing}" />.</param>
412460
/// <param name="where">Configures the location where to insert the <see cref="IObjectGraphVisitor{Nothing}" /></param>
413461
public SerializerBuilder WithPreProcessingPhaseObjectGraphVisitor<TObjectGraphVisitor>(
414462
WrapperFactory<IObjectGraphVisitor<Nothing>, TObjectGraphVisitor> objectGraphVisitorFactory,
@@ -430,6 +478,37 @@ Action<ITrackingRegistrationLocationSelectionSyntax<IObjectGraphVisitor<Nothing>
430478
return this;
431479
}
432480

481+
/// <summary>
482+
/// Registers an additional <see cref="IObjectGraphVisitor{Nothing}" /> to be used by the serializer
483+
/// before emitting an object graph.
484+
/// </summary>
485+
/// <remarks>
486+
/// Registering a visitor in the pre-processing phase enables to traverse the object graph once
487+
/// before actually emitting it. This allows a visitor to collect information about the graph that
488+
/// can be used later by another visitor registered in the emission phase.
489+
/// </remarks>
490+
/// <param name="objectGraphVisitorFactory">A function that instantiates the type inspector based on a previously registered <see cref="IObjectGraphVisitor{Nothing}" />.</param>
491+
/// <param name="where">Configures the location where to insert the <see cref="IObjectGraphVisitor{Nothing}" /></param>
492+
public SerializerBuilder WithPreProcessingPhaseObjectGraphVisitor<TObjectGraphVisitor>(
493+
WrapperFactory<IEnumerable<IYamlTypeConverter>, IObjectGraphVisitor<Nothing>, TObjectGraphVisitor> objectGraphVisitorFactory,
494+
Action<ITrackingRegistrationLocationSelectionSyntax<IObjectGraphVisitor<Nothing>>> where
495+
)
496+
where TObjectGraphVisitor : IObjectGraphVisitor<Nothing>
497+
{
498+
if (objectGraphVisitorFactory == null)
499+
{
500+
throw new ArgumentNullException(nameof(objectGraphVisitorFactory));
501+
}
502+
503+
if (where == null)
504+
{
505+
throw new ArgumentNullException(nameof(where));
506+
}
507+
508+
where(preProcessingPhaseObjectGraphVisitorFactories.CreateTrackingRegistrationLocationSelector(typeof(TObjectGraphVisitor), (wrapped, typeConverters) => objectGraphVisitorFactory(wrapped, typeConverters)));
509+
return this;
510+
}
511+
433512
/// <summary>
434513
/// Unregisters an existing <see cref="IObjectGraphVisitor{Nothing}" /> of type <typeparam name="TObjectGraphVisitor" />.
435514
/// </summary>

YamlDotNet/Serialization/StaticSerializerBuilder.cs

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,22 @@ public StaticSerializerBuilder WithPreProcessingPhaseObjectGraphVisitor<TObjectG
374374
return WithPreProcessingPhaseObjectGraphVisitor(objectGraphVisitor, w => w.OnTop());
375375
}
376376

377+
/// <summary>
378+
/// Registers an additional <see cref="IObjectGraphVisitor{Nothing}" /> to be used by the serializer
379+
/// before emitting an object graph.
380+
/// </summary>
381+
/// <remarks>
382+
/// Registering a visitor in the pre-processing phase enables to traverse the object graph once
383+
/// before actually emitting it. This allows a visitor to collect information about the graph that
384+
/// can be used later by another visitor registered in the emission phase.
385+
/// </remarks>
386+
/// <param name="objectGraphVisitorFactory">A function that instantiates the type inspector.</param>
387+
public StaticSerializerBuilder WithPreProcessingPhaseObjectGraphVisitor<TObjectGraphVisitor>(Func<IEnumerable<IYamlTypeConverter>, TObjectGraphVisitor> objectGraphVisitorFactory)
388+
where TObjectGraphVisitor : IObjectGraphVisitor<Nothing>
389+
{
390+
return WithPreProcessingPhaseObjectGraphVisitor(objectGraphVisitorFactory, w => w.OnTop());
391+
}
392+
377393
/// <summary>
378394
/// Registers an additional <see cref="IObjectGraphVisitor{Nothing}" /> to be used by the serializer
379395
/// before emitting an object graph.
@@ -414,7 +430,38 @@ Action<IRegistrationLocationSelectionSyntax<IObjectGraphVisitor<Nothing>>> where
414430
/// before actually emitting it. This allows a visitor to collect information about the graph that
415431
/// can be used later by another visitor registered in the emission phase.
416432
/// </remarks>
417-
/// <param name="objectGraphVisitorFactory">A factory that creates the <see cref="IObjectGraphVisitor{Nothing}" /> based on a previously registered <see cref="IObjectGraphVisitor{Nothing}" />.</param>
433+
/// <param name="objectGraphVisitorFactory">A function that instantiates the type inspector.</param>
434+
/// <param name="where">Configures the location where to insert the <see cref="IObjectGraphVisitor{Nothing}" /></param>
435+
public StaticSerializerBuilder WithPreProcessingPhaseObjectGraphVisitor<TObjectGraphVisitor>(
436+
Func<IEnumerable<IYamlTypeConverter>, TObjectGraphVisitor> objectGraphVisitorFactory,
437+
Action<IRegistrationLocationSelectionSyntax<IObjectGraphVisitor<Nothing>>> where
438+
)
439+
where TObjectGraphVisitor : IObjectGraphVisitor<Nothing>
440+
{
441+
if (objectGraphVisitorFactory == null)
442+
{
443+
throw new ArgumentNullException(nameof(objectGraphVisitorFactory));
444+
}
445+
446+
if (where == null)
447+
{
448+
throw new ArgumentNullException(nameof(where));
449+
}
450+
451+
where(preProcessingPhaseObjectGraphVisitorFactories.CreateRegistrationLocationSelector(typeof(TObjectGraphVisitor), typeConverters => objectGraphVisitorFactory(typeConverters)));
452+
return this;
453+
}
454+
455+
/// <summary>
456+
/// Registers an additional <see cref="IObjectGraphVisitor{Nothing}" /> to be used by the serializer
457+
/// before emitting an object graph.
458+
/// </summary>
459+
/// <remarks>
460+
/// Registering a visitor in the pre-processing phase enables to traverse the object graph once
461+
/// before actually emitting it. This allows a visitor to collect information about the graph that
462+
/// can be used later by another visitor registered in the emission phase.
463+
/// </remarks>
464+
/// <param name="objectGraphVisitorFactory">A function that instantiates the type inspector based on a previously registered <see cref="IObjectGraphVisitor{Nothing}" />.</param>
418465
/// <param name="where">Configures the location where to insert the <see cref="IObjectGraphVisitor{Nothing}" /></param>
419466
public StaticSerializerBuilder WithPreProcessingPhaseObjectGraphVisitor<TObjectGraphVisitor>(
420467
WrapperFactory<IObjectGraphVisitor<Nothing>, TObjectGraphVisitor> objectGraphVisitorFactory,
@@ -436,6 +483,37 @@ Action<ITrackingRegistrationLocationSelectionSyntax<IObjectGraphVisitor<Nothing>
436483
return this;
437484
}
438485

486+
/// <summary>
487+
/// Registers an additional <see cref="IObjectGraphVisitor{Nothing}" /> to be used by the serializer
488+
/// before emitting an object graph.
489+
/// </summary>
490+
/// <remarks>
491+
/// Registering a visitor in the pre-processing phase enables to traverse the object graph once
492+
/// before actually emitting it. This allows a visitor to collect information about the graph that
493+
/// can be used later by another visitor registered in the emission phase.
494+
/// </remarks>
495+
/// <param name="objectGraphVisitorFactory">A function that instantiates the type inspector based on a previously registered <see cref="IObjectGraphVisitor{Nothing}" />.</param>
496+
/// <param name="where">Configures the location where to insert the <see cref="IObjectGraphVisitor{Nothing}" /></param>
497+
public StaticSerializerBuilder WithPreProcessingPhaseObjectGraphVisitor<TObjectGraphVisitor>(
498+
WrapperFactory<IEnumerable<IYamlTypeConverter>, IObjectGraphVisitor<Nothing>, TObjectGraphVisitor> objectGraphVisitorFactory,
499+
Action<ITrackingRegistrationLocationSelectionSyntax<IObjectGraphVisitor<Nothing>>> where
500+
)
501+
where TObjectGraphVisitor : IObjectGraphVisitor<Nothing>
502+
{
503+
if (objectGraphVisitorFactory == null)
504+
{
505+
throw new ArgumentNullException(nameof(objectGraphVisitorFactory));
506+
}
507+
508+
if (where == null)
509+
{
510+
throw new ArgumentNullException(nameof(where));
511+
}
512+
513+
where(preProcessingPhaseObjectGraphVisitorFactories.CreateTrackingRegistrationLocationSelector(typeof(TObjectGraphVisitor), (wrapped, typeConverters) => objectGraphVisitorFactory(wrapped, typeConverters)));
514+
return this;
515+
}
516+
439517
/// <summary>
440518
/// Unregisters an existing <see cref="IObjectGraphVisitor{Nothing}" /> of type <typeparam name="TObjectGraphVisitor" />.
441519
/// </summary>

0 commit comments

Comments
 (0)