From bde72508b3134408cd1488bd8babc6c5051d609a Mon Sep 17 00:00:00 2001 From: dbolin Date: Mon, 21 Mar 2022 18:08:34 -0700 Subject: [PATCH] Additional fixes for custom serialization --- Apex.Serialization/Apex.Serialization.csproj | 2 +- Apex.Serialization/Internal/DynamicCode.cs | 45 +------------ .../AbstractSerializerTestBase.cs | 2 +- .../CustomSerializationTests.cs | 66 +++++++------------ 4 files changed, 29 insertions(+), 86 deletions(-) diff --git a/Apex.Serialization/Apex.Serialization.csproj b/Apex.Serialization/Apex.Serialization.csproj index 85bcf16..f2e73b0 100644 --- a/Apex.Serialization/Apex.Serialization.csproj +++ b/Apex.Serialization/Apex.Serialization.csproj @@ -2,7 +2,7 @@ net6 - 4.0.1 + 4.0.2 Dominic Bolin A high performance contract-less binary serializer https://github.com/dbolin/Apex.Serialization diff --git a/Apex.Serialization/Internal/DynamicCode.cs b/Apex.Serialization/Internal/DynamicCode.cs index 06505a8..cabbb83 100644 --- a/Apex.Serialization/Internal/DynamicCode.cs +++ b/Apex.Serialization/Internal/DynamicCode.cs @@ -365,13 +365,6 @@ private static Expression WriteValue( return nullableExpression; } - var customExpression = HandleCustomWrite(output, stream, declaredType, valueAccessExpression, settings); - if (customExpression != null) - { - inlineWrite = true; - return customExpression; - } - var writeStruct = WriteStructExpression(declaredType, valueAccessExpression, stream, TypeFields.GetOrderedFields(declaredType, settings)); if (writeStruct != null) { @@ -466,7 +459,6 @@ private static Expression WriteValue( return null; } - var statements = new List(); var customWriteStatements = new List(); foreach (var entry in settings.CustomActionSerializers) @@ -501,26 +493,7 @@ private static Expression WriteValue( { return null; } - - var afterWriteLabel = Expression.Label("afterWrite"); - if (!declaredType.IsValueType) - { - statements.Add(ReserveConstantSize(stream, 1)); - statements.Add(Expression.IfThenElse( - Expression.ReferenceEqual(valueAccessExpression, Expression.Constant(null)), - Expression.Block( - Expression.Call(stream, BinaryStreamMethods.GenericMethods.WriteValueMethodInfo, Expression.Constant((byte)0)), - Expression.Continue(afterWriteLabel) - ), - Expression.Block( - Expression.Call(stream, BinaryStreamMethods.GenericMethods.WriteValueMethodInfo, Expression.Constant((byte)1)) - ) - )); - statements.AddRange(customWriteStatements); - statements.Add(Expression.Label(afterWriteLabel)); - } - - return statements.Count > 0 ? Expression.Block(statements) : Expression.Block(customWriteStatements); + return Expression.Block(customWriteStatements); } private static Expression? HandlePrimitiveWrite(ParameterExpression stream, ParameterExpression output, Type declaredType, @@ -985,7 +958,6 @@ private static Expression AfterDeserializeCallExpression(Type type, MethodInfo m return null; } - var statements = new List(); var customReadStatements = new List(); foreach (var entry in settings.CustomActionDeserializers) @@ -1021,21 +993,8 @@ private static Expression AfterDeserializeCallExpression(Type type, MethodInfo m return null; } - if (!type.IsValueType && readMetadata) - { - statements.Add(ReserveConstantSize(stream, 1)); - statements.Add(Expression.IfThenElse( - Expression.Equal(Expression.Constant((byte)0), Expression.Call(stream, BinaryStreamMethods.GenericMethods.ReadValueMethodInfo)), - Expression.Block( - Expression.Constant(null) - ), - Expression.Block( - customReadStatements - ) - )); - } - return statements.Count > 0 ? Expression.Block(statements) : Expression.Block(customReadStatements); + return Expression.Block(customReadStatements); } private static Expression? ReadStructExpression(Type type, ParameterExpression stream, diff --git a/Tests/Apex.Serialization.Tests/AbstractSerializerTestBase.cs b/Tests/Apex.Serialization.Tests/AbstractSerializerTestBase.cs index 3bed753..a91d223 100644 --- a/Tests/Apex.Serialization.Tests/AbstractSerializerTestBase.cs +++ b/Tests/Apex.Serialization.Tests/AbstractSerializerTestBase.cs @@ -25,7 +25,7 @@ protected AbstractSerializerTestBase() private Action[] _settingsConfigurations = new Action[] { s => s.SerializationMode = Mode.Graph, - s => s.UseSerializedVersionId = true, + s => s.UseSerializedVersionId = false, s => s.InliningMaxDepth = 0, s => s.ForceReflectionToSetReadonlyFields = true, s => s.FlattenClassHierarchy = false diff --git a/Tests/Apex.Serialization.Tests/CustomSerializationTests.cs b/Tests/Apex.Serialization.Tests/CustomSerializationTests.cs index f6728a7..04d023c 100644 --- a/Tests/Apex.Serialization.Tests/CustomSerializationTests.cs +++ b/Tests/Apex.Serialization.Tests/CustomSerializationTests.cs @@ -8,7 +8,7 @@ namespace Apex.Serialization.Tests { - public class CustomSerializationTests + public class CustomSerializationTests : AbstractSerializerTestBase { public class Test { @@ -64,33 +64,12 @@ public class TestWithHashSet public readonly HashSet values = new HashSet(); } - [Fact] - public void SimpleTest() + public CustomSerializationTests() { - var settings = new Settings { SupportSerializationHooks = true } - .RegisterCustomSerializer(Test.Serialize, Test.Deserialize) - .MarkSerializable(typeof(Test)); - - var binary = Binary.Create(settings); - var m = new MemoryStream(); - - var x = new Test {Value = 10, Nested = new Test { Value = 9 } }; - - binary.Write(x, m); - - m.Seek(0, SeekOrigin.Begin); - - var y = binary.Read(m); - - y.Value.Should().Be(x.Value - 1); - y.Nested.Value.Should().Be(x.Nested.Value - 1); - } - - [Fact] - public void HashSetTest() - { - var settings = new Settings { SupportSerializationHooks = true } - .RegisterCustomSerializer>((o, s) => + _modifySettings = s => + { + s.RegisterCustomSerializer(Test.Serialize, Test.Deserialize); + s.RegisterCustomSerializer>((o, s) => { s.Write(o.Count); foreach (var i in o) @@ -105,24 +84,29 @@ public void HashSetTest() { o.Add(s.ReadObject()); } - }) - .MarkSerializable(typeof(TestWithHashSet)) - .MarkSerializable(typeof(HashSet)) - .MarkSerializable(typeof(Test)); + }); + }; + } - var binary = Binary.Create(settings); - var m = new MemoryStream(); + [Fact] + public void SimpleTest() + { + var x = new Test {Value = 10 }; - var x = new TestWithHashSet(); - x.values.Add(new Test { Value = 123 }); + RoundTrip(x, (a, b) => a.Value == b.Value + 1); - binary.Write(x, m); + x = new Test { Value = 10, Nested = new Test { Value = 9 } }; - m.Seek(0, SeekOrigin.Begin); + RoundTrip(x, (a, b) => a.Value == b.Value + 1 && a.Nested.Value == b.Nested.Value + 1); + } - var y = binary.Read(m); + [Fact] + public void HashSetTest() + { + var x = new TestWithHashSet(); + x.values.Add(new Test { Value = 123 }); - y.values.First().Value.Should().Be(123); + RoundTrip(x, (a, b) => a.values.First().Value == b.values.First().Value + 1); } [Fact] @@ -152,7 +136,7 @@ public void CustomContextTest() [Fact] public void Precompile() { - var settings = new Settings { SupportSerializationHooks = true } + var settings = new Settings { SupportSerializationHooks = true, AllowFunctionSerialization = true } .MarkSerializable(typeof(CustomSerializationTests)); var binary = Binary.Create(settings); @@ -165,7 +149,7 @@ private class OpenGeneric { } [Fact] public void PrecompileOpenGeneric() { - var settings = new Settings { SupportSerializationHooks = true } + var settings = new Settings { SupportSerializationHooks = true, AllowFunctionSerialization = true } .MarkSerializable(typeof(OpenGeneric<>)); var binary = Binary.Create(settings);