protobuf-net序列化为字符串并存储在数据库中然后进行序列化

时间:2011-08-01 23:18:35

标签: c# protobuf-net

我想使用字符串序列化/反序列化对象。需要注意的是,当我序列化/反序列化到文件时,一切正常。我要做的是获取一个字符串,这样我就可以将它存储在数据库中,然后将其拉出来反序列化。

以下是有效的代码:

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

“CopiedBlocks”对象与“registrationBlocks”中的列表相同。工作正常,所有序列化/反序列化。我在这里保留所有内容。

以下是我尝试获取字符串时无效的代码:

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);

在反序列化的最后一行,我得到一个异常:抛出了类型'ProtoBuf.ProtoException'的异常。我无法钻进它,内部异常为null。我无法弄清楚它为什么会这样做。

我肯定已经把它缩小到这样一个事实:当我得到一个字符串时它会变得混乱。我将字符串存储在nvarchar(max)的数据库中,这就是我想要字符串的原因。

任何帮助都会受到超级赞赏!

3 个答案:

答案 0 :(得分:24)

在这种情况下使用StreamReader我有点迷失,在我看来你可以省略它并做下面的事情以确保没有单向编码发生。

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

string stringBase64 = Convert.ToBase64String(msTestString.ToArray());

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

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

答案 1 :(得分:2)

根据答案和评论,我使用这些:

        internal static string SerializeToString_PB<T>(this T obj)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                ProtoBuf.Serializer.Serialize(ms, obj);
                return Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length);
            }
        }
        internal static T DeserializeFromString_PB<T>(this string txt)
        {
            byte[] arr = Convert.FromBase64String(txt);
            using (MemoryStream ms = new MemoryStream(arr))
                return ProtoBuf.Serializer.Deserialize<T>(ms);
        }

答案 2 :(得分:0)

        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>();
        }