[GH-ISSUE #1096] ProtoBuf Deserialization #797

Closed
opened 2026-02-27 15:51:52 +03:00 by kerem · 6 comments
Owner

Originally created by @Senc3951 on GitHub (Sep 1, 2022).
Original GitHub issue: https://github.com/quasar/Quasar/issues/1096

Hi, I tried making a project based on this rat, but according to ProtoBuf documentation, in order to deserialize parent object to a child one, eg. IMessage to ClientIdentification, IMessage must contain an attribute saying [ProtoInclude... child object] which in this project wasn't done, but somehow it still works, but when I tried deserializing IMessage to a child without this attribute, it didn't work. Only when I included the attribute, it worked.

Originally created by @Senc3951 on GitHub (Sep 1, 2022). Original GitHub issue: https://github.com/quasar/Quasar/issues/1096 Hi, I tried making a project based on this rat, but according to ProtoBuf documentation, in order to deserialize parent object to a child one, eg. IMessage to ClientIdentification, IMessage must contain an attribute saying [ProtoInclude... child object] which in this project wasn't done, but somehow it still works, but when I tried deserializing IMessage to a child without this attribute, it didn't work. Only when I included the attribute, it worked.
kerem closed this issue 2026-02-27 15:51:52 +03:00
Author
Owner

@MaxXor commented on GitHub (Sep 1, 2022):

Please check the code: github.com/quasar/Quasar@16a4702d0e/Quasar.Common/Networking/PayloadReader.cs (L45-L54)

<!-- gh-comment-id:1234405105 --> @MaxXor commented on GitHub (Sep 1, 2022): Please check the code: https://github.com/quasar/Quasar/blob/16a4702d0e2064070ee7e6319bd708b786294820/Quasar.Common/Networking/PayloadReader.cs#L45-L54
Author
Owner

@Senc3951 commented on GitHub (Sep 1, 2022):

The problem is not deserializing to IMessage, but converting from iMessage to the wanted class

<!-- gh-comment-id:1234588636 --> @Senc3951 commented on GitHub (Sep 1, 2022): The problem is not deserializing to IMessage, but converting from iMessage to the wanted class
Author
Owner

@MaxXor commented on GitHub (Sep 1, 2022):

This is shown here: github.com/quasar/Quasar@16a4702d0e/Quasar.Client/Networking/QuasarClient.cs (L99-L112)

<!-- gh-comment-id:1234601273 --> @MaxXor commented on GitHub (Sep 1, 2022): This is shown here: https://github.com/quasar/Quasar/blob/16a4702d0e2064070ee7e6319bd708b786294820/Quasar.Client/Networking/QuasarClient.cs#L99-L112
Author
Owner

@Senc3951 commented on GitHub (Sep 1, 2022):

I probably didn't explain myself well, This project depends heavily on the ability to convert from IMessage to any class that inherits from it. This conversion should only work when adding the ProtoInclude attribute, to inform ProtoBuf that IMessage may actually be a child class, but this project doesn't include this attribute and somehow the conversion still works.

You can find an example here: https://stackoverflow.com/questions/12988964/how-to-serialize-inherited-class-with-protobuf-net (this is about serializing, but it's same as deserializing)

<!-- gh-comment-id:1234610776 --> @Senc3951 commented on GitHub (Sep 1, 2022): I probably didn't explain myself well, This project depends heavily on the ability to convert from IMessage to any class that inherits from it. This conversion should only work when adding the ProtoInclude attribute, to inform ProtoBuf that IMessage may actually be a child class, but this project doesn't include this attribute and somehow the conversion still works. You can find an example here: https://stackoverflow.com/questions/12988964/how-to-serialize-inherited-class-with-protobuf-net (this is about serializing, but it's same as deserializing)
Author
Owner

@MaxXor commented on GitHub (Sep 1, 2022):

Can you test using the same Protobuf version used in this project? Maybe it only works with the rather "outdated" version here.

<!-- gh-comment-id:1234622457 --> @MaxXor commented on GitHub (Sep 1, 2022): Can you test using the same Protobuf version used in this project? Maybe it only works with the rather "outdated" version here.
Author
Owner

@Senc3951 commented on GitHub (Sep 1, 2022):

From some reason it still doesn't work, here is my serialization, deserialization code

public static byte[]? Serialize<T>(T message) where T : notnull, IMessage
{
    try
    {
        using MemoryStream ms = new();
        Serializer.Serialize(ms, message);
        
        return ms.ToArray();
    }
    catch { return null; }
}

public static IMessage? Deserialize(byte[] data)
{
    try
    {
        using MemoryStream stream = new(data);
        return Serializer.Deserialize<IMessage>(stream);
    }
    catch { return default; }
}

I went around the problem by including a bunch of ProtoInclude's, but I thought that maybe you had a different solution

<!-- gh-comment-id:1234634429 --> @Senc3951 commented on GitHub (Sep 1, 2022): From some reason it still doesn't work, here is my serialization, deserialization code ```cs public static byte[]? Serialize<T>(T message) where T : notnull, IMessage { try { using MemoryStream ms = new(); Serializer.Serialize(ms, message); return ms.ToArray(); } catch { return null; } } public static IMessage? Deserialize(byte[] data) { try { using MemoryStream stream = new(data); return Serializer.Deserialize<IMessage>(stream); } catch { return default; } } ``` I went around the problem by including a bunch of ProtoInclude's, but I thought that maybe you had a different solution
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/Quasar#797
No description provided.