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

WithTagMapping for Serializer #229

Merged
merged 1 commit into from
Dec 19, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions YamlDotNet.Test/Serialization/SerializationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,18 @@ public void DeserializeCustomTags()
.ShouldBeEquivalentTo(new { X = 10, Y = 20 }, o => o.ExcludingMissingMembers());
}

[Fact]
public void SerializeCustomTags()
{
var expectedResult = Yaml.StreamFrom("tags.yaml").ReadToEnd();
SerializerBuilder.WithTagMapping("tag:yaml.org,2002:point", typeof(Point));

var point = new Point(10, 20);
var result = Serializer.Serialize(point);

result.Should().Be(expectedResult);
}

[Fact]
public void DeserializeExplicitType()
{
Expand Down
4 changes: 2 additions & 2 deletions YamlDotNet.Test/files/tags.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
!!point
X: 10
Y: 20
X: 10
Y: 20
26 changes: 26 additions & 0 deletions YamlDotNet/Serialization/EventEmitters/CustomTagEventEmitter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using YamlDotNet.Core;

namespace YamlDotNet.Serialization.EventEmitters
{
internal class CustomTagEventEmitter: ChainedEventEmitter
{
private IDictionary<Type, string> tagMappings;

public CustomTagEventEmitter(IEventEmitter inner, IDictionary<Type, string> tagMappings)
:base(inner)
{
this.tagMappings = tagMappings;
}

public override void Emit(MappingStartEventInfo eventInfo, IEmitter emitter)
{
if(tagMappings.ContainsKey(eventInfo.Source.Type))
{
eventInfo.Tag = tagMappings[eventInfo.Source.Type];
}
base.Emit(eventInfo, emitter);
}
}
}
21 changes: 21 additions & 0 deletions YamlDotNet/Serialization/SerializerBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public sealed class SerializerBuilder : BuilderSkeleton<SerializerBuilder>
private readonly LazyComponentRegistrationList<IEnumerable<IYamlTypeConverter>, IObjectGraphVisitor<Nothing>> preProcessingPhaseObjectGraphVisitorFactories;
private readonly LazyComponentRegistrationList<EmissionPhaseObjectGraphVisitorArgs, IObjectGraphVisitor<IEmitter>> emissionPhaseObjectGraphVisitorFactories;
private readonly LazyComponentRegistrationList<IEventEmitter, IEventEmitter> eventEmitterFactories;
private readonly IDictionary<Type, string> tagMappings = new Dictionary<Type, string>();

public SerializerBuilder()
{
Expand Down Expand Up @@ -71,6 +72,7 @@ public SerializerBuilder()
objectGraphTraversalStrategyFactory = (typeInspector, typeResolver, typeConverters) => new FullObjectGraphTraversalStrategy(typeInspector, typeResolver, 50, namingConvention ?? new NullNamingConvention());

WithTypeResolver(new DynamicTypeResolver());
WithEventEmitter(inner => new CustomTagEventEmitter(inner, tagMappings));
}

protected override SerializerBuilder Self { get { return this; } }
Expand Down Expand Up @@ -110,6 +112,25 @@ Action<IRegistrationLocationSelectionSyntax<IEventEmitter>> where
return Self;
}

internal SerializerBuilder WithTagMapping(string tag, Type type)
{
if (tag == null)
{
throw new ArgumentNullException("tag");
}

if (type == null)
{
throw new ArgumentNullException("type");
}
if(tagMappings.ContainsKey(type))
{
throw new ArgumentException(String.Format("Type already has a registered tag: {0}", tag), "tag");
}
tagMappings.Add(type, tag);
return this;
}

/// <summary>
/// Ensures that it will be possible to deserialize the serialized objects.
/// This option will force the emission of tags and emit only properties with setters.
Expand Down
1 change: 1 addition & 0 deletions YamlDotNet/YamlDotNet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@
<Compile Include="Serialization\BuilderSkeleton.cs" />
<Compile Include="RepresentationModel\YamlVisitorBase.cs" />
<Compile Include="Serialization\Converters\GuidConverter.cs" />
<Compile Include="Serialization\EventEmitters\CustomTagEventEmitter.cs" />
<Compile Include="Serialization\Deserializer.cs" />
<Compile Include="Serialization\DeserializerBuilder.cs" />
<Compile Include="Serialization\EmissionPhaseObjectGraphVisitorArgs.cs" />
Expand Down