Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make top-level serialization context accessible to custom serializers #235

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion BinarySerializer.Test/Custom/CustomTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<CustomWithContextContainerClass>(stream);
}
Expand Down
6 changes: 4 additions & 2 deletions BinarySerializer.Test/Custom/CustomWithContextClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace BinarySerialization.Test.Custom
{
public class CustomWithContextContainerContextClass
{
public string Value { get; set; }
}
}
8 changes: 8 additions & 0 deletions BinarySerializer/Graph/ValueGraph/RootValueNode.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion BinarySerializer/Graph/ValueGraph/ValueNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,7 @@ private async Task ProcessPaddingAsync(BoundedStream stream, Func<BoundedStream,
return Convert.ToInt64(binding.ConstValue);
}

private BinarySerializationContext CreateSerializationContext()
protected virtual BinarySerializationContext CreateSerializationContext()
{
var parent = Parent;
return new BinarySerializationContext(Value, parent?.Value, parent?.TypeNode.Type,
Expand Down