From d03eecd41dd1b667fdba06f044b7685241a10b31 Mon Sep 17 00:00:00 2001 From: bdovaz Date: Fri, 5 Aug 2022 11:37:11 +0200 Subject: [PATCH 1/2] Allow reuse of Serializer instance - Add ResetAlias setting - Add tests --- .../Serialization/SerializationTests2.cs | 49 ++++++++++++++++++- src/SharpYaml/Serialization/Serializer.cs | 10 ++++ .../Serialization/SerializerSettings.cs | 7 +++ .../Serializers/AnchorSerializer.cs | 7 ++- 4 files changed, 70 insertions(+), 3 deletions(-) diff --git a/src/SharpYaml.Tests/Serialization/SerializationTests2.cs b/src/SharpYaml.Tests/Serialization/SerializationTests2.cs index 185d5cd..aaf3712 100644 --- a/src/SharpYaml.Tests/Serialization/SerializationTests2.cs +++ b/src/SharpYaml.Tests/Serialization/SerializationTests2.cs @@ -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; @@ -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(() => 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() { diff --git a/src/SharpYaml/Serialization/Serializer.cs b/src/SharpYaml/Serialization/Serializer.cs index 1a75abc..e3dfe10 100644 --- a/src/SharpYaml/Serialization/Serializer.cs +++ b/src/SharpYaml/Serialization/Serializer.cs @@ -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(); + } } /// @@ -535,6 +540,11 @@ public void Serialize(IEmitter emitter, object? graph, Type? type, SerializerCon reader.Expect(); } + if (Settings.ResetAlias && ObjectSerializer is AnchorSerializer anchorSerializer) + { + anchorSerializer.Reset(); + } + return result; } diff --git a/src/SharpYaml/Serialization/SerializerSettings.cs b/src/SharpYaml/Serialization/SerializerSettings.cs index d6b6c2c..0e511e5 100644 --- a/src/SharpYaml/Serialization/SerializerSettings.cs +++ b/src/SharpYaml/Serialization/SerializerSettings.cs @@ -83,6 +83,7 @@ public SerializerSettings(IYamlSchema? schema) PreferredIndent = 2; IndentLess = false; EmitAlias = true; + ResetAlias = false; SortKeyForMapping = true; EmitJsonCompatible = false; EmitCapacityForList = false; @@ -129,6 +130,12 @@ public int PreferredIndent /// true to emit anchor alias; otherwise, false. public bool EmitAlias { get; set; } + /// + /// Gets or sets a value indicating whether to reset anchor alias in order to reuse instance. Default is true. + /// + /// true to reset anchor alias; otherwise, false. + public bool ResetAlias { get; set; } + /// /// Gets or sets a value indicating whether to emit tags when serializing. Default is false. /// diff --git a/src/SharpYaml/Serialization/Serializers/AnchorSerializer.cs b/src/SharpYaml/Serialization/Serializers/AnchorSerializer.cs index e8642e5..3663b6f 100644 --- a/src/SharpYaml/Serialization/Serializers/AnchorSerializer.cs +++ b/src/SharpYaml/Serialization/Serializers/AnchorSerializer.cs @@ -46,7 +46,6 @@ using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; -using System.Globalization; using System.Reflection; using SharpYaml.Events; @@ -143,5 +142,11 @@ public override void WriteYaml(ref ObjectContext objectContext) base.WriteYaml(ref objectContext); } + + public void Reset() + { + aliasToObject.Clear(); + objectToAlias.Clear(); + } } } From 3fc9219d425be1bdddf0c8f2b8c7b1d21242653a Mon Sep 17 00:00:00 2001 From: bdovaz Date: Fri, 5 Aug 2022 11:40:49 +0200 Subject: [PATCH 2/2] Minor doc fix --- src/SharpYaml/Serialization/SerializerSettings.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SharpYaml/Serialization/SerializerSettings.cs b/src/SharpYaml/Serialization/SerializerSettings.cs index 0e511e5..fe4eb31 100644 --- a/src/SharpYaml/Serialization/SerializerSettings.cs +++ b/src/SharpYaml/Serialization/SerializerSettings.cs @@ -131,7 +131,7 @@ public int PreferredIndent public bool EmitAlias { get; set; } /// - /// Gets or sets a value indicating whether to reset anchor alias in order to reuse instance. Default is true. + /// Gets or sets a value indicating whether to reset anchor alias in order to reuse instance. Default is false. /// /// true to reset anchor alias; otherwise, false. public bool ResetAlias { get; set; }