protobuf-net Serialize To String and Store in Database Then De Serialize

Viewed 13283

I'd like to serialize/de-serialize an object using a string. Just to note, when I serialize/de-serialize to a file everything works fine. What I'm trying to do is get a string so I can store it in the database and then pull it out later to de-serialize.

Here is code that works:

MemoryStream msTest = new MemoryStream();
Serializer.Serialize(msTest, registrationBlocks);
msTest.Position = 0;
List<RVRegistrationBlock> CopiedBlocks = new List<RVRegistrationBlock>();
CopiedBlocks = Serializer.Deserialize<List<RVRegistrationBlock>>(msTest);

The "CopiedBlocks" object is the same list that was in "registrationBlocks" Works great, everything serialized/de-serialized. I'm keeping everything in streams here.

Here is the code that doesn't work when I try to get a string involved:

MemoryStream msTestString = new MemoryStream();
Serializer.Serialize(msTestString, registrationBlocks);


msTestString.Position = 0;
StreamReader srRegBlock = new StreamReader(msTestString);

byte[] bytedata64 = System.Text.Encoding.Default.GetBytes(srRegBlock.ReadToEnd());

string stringBase64 = Convert.ToBase64String(bytedata64);

byte[] byteAfter64 = Convert.FromBase64String(stringBase64);
MemoryStream afterStream = new MemoryStream(byteAfter64);


List<RVRegistrationBlock> CopiedBlocksString = new List<RVRegistrationBlock>();
CopiedBlocksString = Serializer.Deserialize<List<RVRegistrationBlock>>(afterStream);

On the last line when it goes to deserialize, I get an exception: Exception of type 'ProtoBuf.ProtoException' was thrown. I can't drill into it, inner exception is null. I can't figure out why it's doing it.

I've definitely narrowed it down to the fact that when I get a string involved it goes haywire. I'm storing the string in a database in an nvarchar(max) which is why I want the string.

Any help would be super appreciated!

3 Answers
        public static string ToProtobufString(this object model) {
            using var memString = new MemoryStream();
            Serializer.Serialize(memString, model);
            return Convert.ToBase64String(memString.GetBuffer(), 0, (int)memString.Length);
        }

        public static T FromProtobufString<T>(this string protobuf) where T : class {
            var byteAfter64 = Convert.FromBase64String(protobuf);
            using var mem = new MemoryStream(byteAfter64);
            return mem.FromProtobuf<T>();
        }
Related