Skip to content

Commit

Permalink
Additional fixes for custom serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
dbolin committed Mar 22, 2022
1 parent aa64a83 commit bde7250
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 86 deletions.
2 changes: 1 addition & 1 deletion Apex.Serialization/Apex.Serialization.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFrameworks>net6</TargetFrameworks>
<Version>4.0.1</Version>
<Version>4.0.2</Version>
<Authors>Dominic Bolin</Authors>
<Description>A high performance contract-less binary serializer</Description>
<PackageProjectUrl>https://github.com/dbolin/Apex.Serialization</PackageProjectUrl>
Expand Down
45 changes: 2 additions & 43 deletions Apex.Serialization/Internal/DynamicCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -466,7 +459,6 @@ private static Expression WriteValue(
return null;
}

var statements = new List<Expression>();
var customWriteStatements = new List<Expression>();

foreach (var entry in settings.CustomActionSerializers)
Expand Down Expand Up @@ -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<BufferedStream>.GenericMethods<byte>.WriteValueMethodInfo, Expression.Constant((byte)0)),
Expression.Continue(afterWriteLabel)
),
Expression.Block(
Expression.Call(stream, BinaryStreamMethods<BufferedStream>.GenericMethods<byte>.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,
Expand Down Expand Up @@ -985,7 +958,6 @@ private static Expression AfterDeserializeCallExpression(Type type, MethodInfo m
return null;
}

var statements = new List<Expression>();
var customReadStatements = new List<Expression>();

foreach (var entry in settings.CustomActionDeserializers)
Expand Down Expand Up @@ -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<BufferedStream>.GenericMethods<byte>.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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ protected AbstractSerializerTestBase()
private Action<Settings>[] _settingsConfigurations = new Action<Settings>[]
{
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
Expand Down
66 changes: 25 additions & 41 deletions Tests/Apex.Serialization.Tests/CustomSerializationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace Apex.Serialization.Tests
{
public class CustomSerializationTests
public class CustomSerializationTests : AbstractSerializerTestBase
{
public class Test
{
Expand Down Expand Up @@ -64,33 +64,12 @@ public class TestWithHashSet
public readonly HashSet<Test> values = new HashSet<Test>();
}

[Fact]
public void SimpleTest()
public CustomSerializationTests()
{
var settings = new Settings { SupportSerializationHooks = true }
.RegisterCustomSerializer<Test>(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<Test>(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<HashSet<Test>>((o, s) =>
_modifySettings = s =>
{
s.RegisterCustomSerializer<Test>(Test.Serialize, Test.Deserialize);
s.RegisterCustomSerializer<HashSet<Test>>((o, s) =>
{
s.Write(o.Count);
foreach (var i in o)
Expand All @@ -105,24 +84,29 @@ public void HashSetTest()
{
o.Add(s.ReadObject<Test>());
}
})
.MarkSerializable(typeof(TestWithHashSet))
.MarkSerializable(typeof(HashSet<Test>))
.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<TestWithHashSet>(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]
Expand Down Expand Up @@ -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);

Expand All @@ -165,7 +149,7 @@ private class OpenGeneric<T> { }
[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);

Expand Down

0 comments on commit bde7250

Please sign in to comment.