protobuf-net反序列化:“算术运算导致溢出。”

coa*_*mee 1 .net c# serialization protobuf-net deserialization

我正在使用protobuf-net序列化/反序列化我的模型。

我的模型相当简单,序列化似乎一直都可以,但是如果我在模型中添加特定类型,反序列化似乎会失败。

我在模型中添加“ int”,“ long”或“ DateTime”后,立即收到“算术运算导致溢出”异常。

模型:

[ProtoContract]
public class MyModel
{
    [ProtoMember(1)]
    public DateTime Time { get; set; }

    [ProtoMember(2)]
    public List<string> SomeList { get; set; }

    [ProtoMember(3)]
    public string Key { get; set; }

    [ProtoMember(4)]
    public string Value { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

当我删除“时间”属性时,它似乎总是可以工作。

例外:

 at ProtoBuf.ProtoReader.TryReadUInt64VariantWithoutMoving(UInt64& value) in c:\Dev\protobuf-net\protobuf-net\ProtoReader.cs:line 375
   at ProtoBuf.ProtoReader.ReadInt64() in c:\Dev\protobuf-net\protobuf-net\ProtoReader.cs:line 357
   at ProtoBuf.BclHelpers.ReadTimeSpanTicks(ProtoReader source) in c:\Dev\protobuf-net\protobuf-net\BclHelpers.cs:line 191
   at ProtoBuf.Serializers.DateTimeSerializer.Read(Object value, ProtoReader source) in c:\Dev\protobuf-net\protobuf-net\Serializers\DateTimeSerializer.cs:line 35
   at ProtoBuf.Serializers.PropertyDecorator.Read(Object value, ProtoReader source) in c:\Dev\protobuf-net\protobuf-net\Serializers\PropertyDecorator.cs:line 77
   at ProtoBuf.Serializers.TypeSerializer.Read(Object value, ProtoReader source) in c:\Dev\protobuf-net\protobuf-net\Serializers\TypeSerializer.cs:line 230
   at ProtoBuf.Meta.TypeModel.DeserializeCore(ProtoReader reader, Type type, Object value, Boolean noAutoCreate) in c:\Dev\protobuf-net\protobuf-net\Meta\TypeModel.cs:line 700
   at ProtoBuf.Meta.TypeModel.Deserialize(Stream source, Object value, Type type, SerializationContext context) in c:\Dev\protobuf-net\protobuf-net\Meta\TypeModel.cs:line 589
   at ProtoBuf.Serializer.Deserialize[T](Stream source) in c:\Dev\protobuf-net\protobuf-net\Serializer.cs:line 77
Run Code Online (Sandbox Code Playgroud)

难道我做错了什么?

[编辑]

private static void Main(string[] args)
        {
            var proto = new SerializeProtoTest();
            var model = new MyModel
                            {
                                Key = "abc",
                                SomeList = new List<string> { "cde" },
                                Time = DateTime.UtcNow,
                                Value = "something"
                            };
            var s = proto.Serialize(model);
            var d = proto.Deserialize<MyModel>(s);
            Console.ReadKey();
        }

        [ProtoContract]
        public class MyModel
        {
            [ProtoMember(3)]
            public string Key { get; set; }

            [ProtoMember(2)]
            public List<string> SomeList { get; set; }

            [ProtoMember(1)]
            public DateTime Time { get; set; }

            [ProtoMember(4)]
            public string Value { get; set; }
        }

        public class SerializeProtoTest
        {
            public T Deserialize<T>(string value)
            {
                using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(value)))
                {
                    return Serializer.Deserialize<T>(ms);
                }
            }

            public string Serialize<T>(T obj)
            {
                using (var ms = new MemoryStream())
                {
                    Serializer.Serialize(ms, obj);
                    var buffer = ms.ToArray();
                    return Encoding.UTF8.GetString(buffer, 0, buffer.Length);
                }
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

Mar*_*ell 5

来自评论:

最有可能:编码不当

叫它!(根据您的编辑)

返回Encoding.UTF8.GetString(buffer,0,buffer.Length);

protobuf数据不是文本。您不能使用文本编码来获取其文本表示形式-实际上,您正在使用此文本编码向后显示,并且此处没有定义的行为。您已损坏数据。较大的文章(因为我经常看到)在这里(第一部分)。

但是,简短的版本是:请勿这样做。如果需要string,请改用base-64或类似的值:

var buffer = ms.GetBuffer();
return Convert.ToBase64String(buffer, 0, (int)ms.Length);
Run Code Online (Sandbox Code Playgroud)

(同样,用于Convert.FromBase64String逆转此过程)

但是,如果可能的话,最好简单地避免需要绕行stringbyte[]可以正常工作(即return ms.ToArray())。