Skip to content

Commit

Permalink
Merge pull request #116 from bdovaz/feature/reuse-serializer-instance
Browse files Browse the repository at this point in the history
Allow reuse of Serializer instance
  • Loading branch information
xoofx authored Aug 11, 2022
2 parents a8fcaa5 + 3fc9219 commit 1fa7fa2
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 3 deletions.
49 changes: 47 additions & 2 deletions src/SharpYaml.Tests/Serialization/SerializationTests2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,8 @@
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using NUnit.Framework;
using SharpYaml.Serialization;
using SharpYaml.Serialization.Serializers;
Expand Down Expand Up @@ -160,6 +158,53 @@ public void TestSimpleStructWithDefaultValues()
Assert.AreEqual(value.Test.Height, newValue.Test.Height);
}

private static readonly object[] s_serializerSettingsReuseFail =
{
new object[] { new SerializerSettings { } },
new object[] { new SerializerSettings { EmitAlias = true, ResetAlias = false } }
};

[TestCaseSource(nameof(s_serializerSettingsReuseFail))]
public void TestSerializerReuseFail(SerializerSettings serializerSettings)
{
var serializer = new Serializer(serializerSettings);

object data = new
{
Value = "testValue"
};

var text = serializer.Serialize(data);
serializer.Deserialize(text);

text = serializer.Serialize(data);
Assert.Throws<AnchorNotFoundException>(() => serializer.Deserialize(text));
}

private static readonly object[] s_serializerSettingsReuseSuccess =
{
new object[] { new SerializerSettings { EmitAlias = true, ResetAlias = true } },
new object[] { new SerializerSettings { EmitAlias = false, ResetAlias = true } },
new object[] { new SerializerSettings { EmitAlias = false, ResetAlias = false } }
};

[TestCaseSource(nameof(s_serializerSettingsReuseSuccess))]
public void TestSerializerReuseSuccess(SerializerSettings serializerSettings)
{
var serializer = new Serializer(serializerSettings);

object data = new
{
Value = "testValue"
};

var text = serializer.Serialize(data);
serializer.Deserialize(text);

text = serializer.Serialize(data);
Assert.DoesNotThrow(() => serializer.Deserialize(text));
}

[Test]
public void TestSimpleStructMemberOrdering()
{
Expand Down
10 changes: 10 additions & 0 deletions src/SharpYaml/Serialization/Serializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,11 @@ public void Serialize(IEmitter emitter, object? graph, Type? type, SerializerCon
context.WriteYaml(graph, type);
context.Writer.DocumentEnd();
context.Writer.StreamEnd();

if (Settings.ResetAlias && ObjectSerializer is AnchorSerializer anchorSerializer)
{
anchorSerializer.Reset();
}
}

/// <summary>
Expand Down Expand Up @@ -535,6 +540,11 @@ public void Serialize(IEmitter emitter, object? graph, Type? type, SerializerCon
reader.Expect<StreamEnd>();
}

if (Settings.ResetAlias && ObjectSerializer is AnchorSerializer anchorSerializer)
{
anchorSerializer.Reset();
}

return result;
}

Expand Down
7 changes: 7 additions & 0 deletions src/SharpYaml/Serialization/SerializerSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public SerializerSettings(IYamlSchema? schema)
PreferredIndent = 2;
IndentLess = false;
EmitAlias = true;
ResetAlias = false;
SortKeyForMapping = true;
EmitJsonCompatible = false;
EmitCapacityForList = false;
Expand Down Expand Up @@ -129,6 +130,12 @@ public int PreferredIndent
/// <value><c>true</c> to emit anchor alias; otherwise, <c>false</c>.</value>
public bool EmitAlias { get; set; }

/// <summary>
/// Gets or sets a value indicating whether to reset anchor alias in order to reuse <see cref="Serializer" /> instance. Default is false.
/// </summary>
/// <value><c>true</c> to reset anchor alias; otherwise, <c>false</c>.</value>
public bool ResetAlias { get; set; }

/// <summary>
/// Gets or sets a value indicating whether to emit tags when serializing. Default is false.
/// </summary>
Expand Down
7 changes: 6 additions & 1 deletion src/SharpYaml/Serialization/Serializers/AnchorSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Reflection;
using SharpYaml.Events;

Expand Down Expand Up @@ -143,5 +142,11 @@ public override void WriteYaml(ref ObjectContext objectContext)

base.WriteYaml(ref objectContext);
}

public void Reset()
{
aliasToObject.Clear();
objectToAlias.Clear();
}
}
}

0 comments on commit 1fa7fa2

Please sign in to comment.