【问题标题】:protobuf-net Serialize To String and Store in Database Then De Serializeprotobuf-net 序列化为字符串并存储在数据库中,然后反序列化
【发布时间】:2011-10-17 20:21:31
【问题描述】:

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

这是有效的代码:

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”类型的异常。我无法深入研究,内部异常为空。我不知道为什么会这样。

我确实将其范围缩小到这样一个事实,即当我涉及到一个字符串时,它会变得混乱。我将字符串存储在数据库中的 nvarchar(max) 中,这就是我想要字符串的原因。

任何帮助将不胜感激!

【问题讨论】:

    标签: c# protobuf-net


    【解决方案1】:

    在这种情况下使用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);
    

    【讨论】:

    • 好答案;如果需要,您可以进行次要优化 - Convert.ToBase64String(msTestString.GetBuffer(), 0, (int)msTestString.Length),它可以避免创建额外的 byte[]。但它会“按原样”工作。
    【解决方案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);
            }
    

    【讨论】:

      【解决方案3】:
              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>();
              }
      

      【讨论】:

        猜你喜欢
        • 2012-04-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-11-06
        • 2014-12-30
        • 1970-01-01
        相关资源
        最近更新 更多