0

I am using MessagePack version 3.1.4 for binary serialization/deserialization. With the most basic code and decorating my fields/properties with the Key attribute:

public static byte[] Serialize<T>(T source)
{   
    using (var stream = new MemoryStream())
    {
        MessagePackSerializer.Serialize(stream, source);
        return stream.ToArray();
    }      
}

public static T Deserialize<T>(byte[] source)
{
    using (var stream = new MemoryStream(source))
    {
        return MessagePackSerializer.Deserialize<T>(stream);
    }
}

Then the byte array is stored in a database.

I would like to serialize an object once and have the other references to that object serialized as references. Perhaps this is not the right approach.

I want to preserve these references during deserialization. To summarize: the goal is to obtain two identical objects between serialization and deserialization.

How can this be done?

7
  • 3
    One can serialize the "keys" of other objects (that can subsequently be used to "retrieve" the data for that object); but you can't serialize "references"; which are addresses that no longer point to anything meaningful after a time. ("ceased to exist"). Commented Aug 21 at 18:23
  • Please take the tour to learn how Stack Overflow works and read How to Ask on how to improve the quality of your question. It is unclear what you are asking or what the problem is. Please edit your question to include a more detailed description of the problem you have. Commented Aug 21 at 18:58
  • The best way is to create a class object with the array and referenced items in the same class. Then serialize the class object. Commented Aug 21 at 21:23
  • 1
    Do you have any reason to believe message pack support such reference handling out of the box? Some serialization libraries do, but the feature is not common. You could always do it yourself by replacing references with indices into a list of objects, and do the opposite on the other side. But it may require a fair bit of work. Commented Aug 22 at 6:29
  • @JonasH, I know that MessagePack doesn't support reference handling, this is why I'm asking for advices. Yours sounds interessting but, yes it's a lot of work. Commented Aug 22 at 8:24

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.