From 5600316c616aaa6a0028d5a2d041fcc100e4c79b Mon Sep 17 00:00:00 2001 From: henbagle Date: Sat, 25 Nov 2023 23:18:00 -0500 Subject: [PATCH 1/2] CreateSerializationContext on a RootValueNode will set the input context object, if available, as the parent of the created serialization context --- BinarySerializer/Graph/ValueGraph/RootValueNode.cs | 8 ++++++++ BinarySerializer/Graph/ValueGraph/ValueNode.cs | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/BinarySerializer/Graph/ValueGraph/RootValueNode.cs b/BinarySerializer/Graph/ValueGraph/RootValueNode.cs index cc5afdd3..a7d3fbd5 100644 --- a/BinarySerializer/Graph/ValueGraph/RootValueNode.cs +++ b/BinarySerializer/Graph/ValueGraph/RootValueNode.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; @@ -101,6 +102,13 @@ protected override Encoding GetFieldEncoding() return EncodingCallback(); } + protected override BinarySerializationContext CreateSerializationContext() + { + var parent = Children.FirstOrDefault()?.Parent; + return new BinarySerializationContext(Value, parent?.Value, parent?.TypeNode.Type, + null, TypeNode.MemberInfo); + } + private static RootTypeNode GetContextGraph(Type valueType) { lock (ContextCacheLock) diff --git a/BinarySerializer/Graph/ValueGraph/ValueNode.cs b/BinarySerializer/Graph/ValueGraph/ValueNode.cs index 67d71f01..c9d86c8f 100644 --- a/BinarySerializer/Graph/ValueGraph/ValueNode.cs +++ b/BinarySerializer/Graph/ValueGraph/ValueNode.cs @@ -960,7 +960,7 @@ private async Task ProcessPaddingAsync(BoundedStream stream, Func Date: Sat, 5 Oct 2024 14:04:27 -0500 Subject: [PATCH 2/2] Test custom serialization with context --- BinarySerializer.Test/Custom/CustomTests.cs | 3 ++- BinarySerializer.Test/Custom/CustomWithContextClass.cs | 6 ++++-- .../Custom/CustomWithContextContainerContextClass.cs | 7 +++++++ 3 files changed, 13 insertions(+), 3 deletions(-) create mode 100644 BinarySerializer.Test/Custom/CustomWithContextContainerContextClass.cs diff --git a/BinarySerializer.Test/Custom/CustomTests.cs b/BinarySerializer.Test/Custom/CustomTests.cs index 5446b2d1..737e9984 100644 --- a/BinarySerializer.Test/Custom/CustomTests.cs +++ b/BinarySerializer.Test/Custom/CustomTests.cs @@ -28,11 +28,12 @@ public void TestCustomDateTime() public void CustomWithContextTest() { var expected = new CustomWithContextContainerClass {Value = new CustomWithContextClass()}; + var context = new CustomWithContextContainerContextClass { Value = "context" }; var serializer = new BinarySerializer(); var stream = new MemoryStream(); - serializer.Serialize(stream, expected, "context"); + serializer.Serialize(stream, expected, context); stream.Position = 0; serializer.Deserialize(stream); } diff --git a/BinarySerializer.Test/Custom/CustomWithContextClass.cs b/BinarySerializer.Test/Custom/CustomWithContextClass.cs index 31ebf4eb..334232b2 100644 --- a/BinarySerializer.Test/Custom/CustomWithContextClass.cs +++ b/BinarySerializer.Test/Custom/CustomWithContextClass.cs @@ -9,8 +9,10 @@ public void Serialize(Stream stream, BinarySerialization.Endianness endianness, BinarySerializationContext serializationContext) { Assert.AreEqual(typeof(CustomWithContextContainerClass), serializationContext.ParentType); - //Assert.AreEqual("context", serializationContext.ParentContext.ParentValue); - // TODO check root context + + + Assert.AreEqual(typeof(CustomWithContextContainerContextClass), serializationContext.ParentContext.ParentContext.ParentType); + Assert.AreEqual("context", (serializationContext.ParentContext.ParentContext.ParentValue as CustomWithContextContainerContextClass).Value); } public void Deserialize(Stream stream, BinarySerialization.Endianness endianness, diff --git a/BinarySerializer.Test/Custom/CustomWithContextContainerContextClass.cs b/BinarySerializer.Test/Custom/CustomWithContextContainerContextClass.cs new file mode 100644 index 00000000..c1dd2983 --- /dev/null +++ b/BinarySerializer.Test/Custom/CustomWithContextContainerContextClass.cs @@ -0,0 +1,7 @@ +namespace BinarySerialization.Test.Custom +{ + public class CustomWithContextContainerContextClass + { + public string Value { get; set; } + } +} \ No newline at end of file