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

Deserialization of byte array from network stream, with different subtypes for payload, depending on version #231

Open
abrasat opened this issue Dec 15, 2023 · 0 comments

Comments

@abrasat
Copy link

abrasat commented Dec 15, 2023

I am using the BinarySerializer to deserialize a byte array received via network from embedded devices. I need to support several versions which are not compatible. The information about the actual version is not present in the message header, so I have to set it manually using some "Version" property with the "Ignore" attribute. These are basically the classes (I did not post the detailed content of the V1_Data and V2_Data classes, as not relevant for the issue):

public class ControlDataMessage
{
    [Ignore]
    public string ControlDataVersion { get; set; }

    [FieldOrder(0)]
    public ControlHeader Header { get; set; }

    [FieldOrder(1)]
    [Subtype(nameof(ControlDataVersion), "V1", typeof(V1_ControlData))]
    [Subtype(nameof(ControlDataVersion), "V2", typeof(V2_ControlData))]
    [SubtypeDefault(typeof(EmptyControlData))]
    public ControlData Payload { get; set; }
}
public abstract class ControlData
{
}
public class V1_ControlData: ControlData
{
    public V1_Data Data { get; set; }
}
public class V2_ControlData: ControlData
{
    public V2_Data Data { get; set; }
}
public class EmptyControlData: ControlData { }

I would like to be able to use the BinarySerializer something like this:

...
var controlMsgVersion = "V1";
var _binSerializer = new BinarySerializer();
byte[] recvMsgBytes = ReceiveControlData();
var recvMsg = _binSerializer.Deserialize(recvMsgBytes, controlMsgVersion);
...

How could this be achieved?

@abrasat abrasat changed the title Deserialization of byte array from network stream, with different subtypes depending on version Deserialization of byte array from network stream, with different subtypes for payload, depending on version Dec 15, 2023
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

No branches or pull requests

1 participant