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

IBinarySerializable - Access to the optional SerializationContext on serialization and deserialization #229

Open
henbagle opened this issue Nov 24, 2023 · 0 comments · May be fixed by #235

Comments

@henbagle
Copy link

Hi!

I'm working on a project where I am trying to handle multiple versions of a binary format natively, so you could for example deserialize conforming to one version and reserialize to another, handle conversions gracefully. I'm passing in the version using the optional Context parameter upon deserialization, which works great most of the time doing things like the following:

public class BankStringUtf8
{
    [FieldOrder(0)]
    [SerializeWhen(nameof(BankSerializationContext.Version), 136,
        ComparisonOperator.LessThanOrEqual,
        RelativeSourceMode = RelativeSourceMode.SerializationContext)]
    public uint Length;
...

But when creating a custom class with IBinarySerializable, it appears that I have no way of accessing my version field to control the behavior. Through the BinarySerializationContext that gets passed in to the Serialize and Deserialize methods, I appear to have access to just about anything I could use for a [SerializeWhen] except for this serialization context. Is there an easy way to get access to this value when doing custom serialization?

So far I've been able to navigate around this by not using IBinarySerializable, but there are several instances where it would vastly cut down on code, subclasses, subclass factories, etc. by just using custom serialization. My version number gets serialized as part of the format, so it's accessible in theory, but it's nested pretty deep in the tree, and I can't do things like test smaller pieces to make sure they serialize properly without access to the version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant