简体   繁体   English

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

[英]protobuf-net deserializing: “Arithmetic operation resulted in an overflow.”

I'm using protobuf-net for serializing/deserialzing my models. 我正在使用protobuf-net序列化/反序列化我的模型。

My model is rather simple, serialization seems to work all the time, but if I add specific types to my model deserialization later seems to fail. 我的模型相当简单,序列化似乎一直都可以,但是如果我在模型中添加特定类型,反序列化似乎会失败。

I get an "Arithmetic operation resulted in an Overflow" exception as soon as I add "int", "long" or "DateTime" to my models. 我在模型中添加“ int”,“ long”或“ DateTime”后,立即收到“算术运算导致溢出”异常。

Model: 模型:

[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; }
}

When I remove the "Time" Property it always seems to work. 当我删除“时间”属性时,它似乎总是起作用。

Exception: 例外:

 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

Am I doing something wrong? 难道我做错了什么?

[EDIT] [编辑]

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

From comments: 来自评论:

most likely : encoding inappropriately 最有可能:编码不当

Called it! 叫它! (from your edit) (根据您的编辑)

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

protobuf data is not text . protobuf数据不是文本 You cannot use a text encoding to get a text representation of it - in fact, you are using this text encoding backwards , and it has no defined behaviour here. 您不能使用文本编码来获取其文本表示形式-实际上,您正在使用此文本编码向后显示 ,并且此处没有定义的行为。 You have corrupted the data. 您已损坏数据。 A larger write up (because I see this often) is here (the first section). 较大的文章(因为我经常看到) 在这里 (第一部分)。

However, the short version is: don't do that. 但是,简短的版本是:请勿这样做。 If you need a string , use base-64 or similar instead: 如果您需要一个string ,请使用base-64或类似的string

var buffer = ms.GetBuffer();
return Convert.ToBase64String(buffer, 0, (int)ms.Length);

(likewise, use Convert.FromBase64String to reverse this process) (同样,使用Convert.FromBase64String来逆转此过程)

However, if possible it would be preferable to simply avoid the need to pass around string . 但是,如果可能的话,最好避免直接传递string byte[] would work fine (ie return ms.ToArray() ). byte[]可以正常工作(即return ms.ToArray() )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQL错误“算术运算导致溢出。” - Sql error “Arithmetic operation resulted in an overflow.” 算术运算导致溢出。 在oAuthTwitterWrapper中 - Arithmetic operation resulted in an overflow. in oAuthTwitterWrapper 算术运算导致溢出。 (加整数) - Arithmetic operation resulted in an overflow. (Adding integers) 算术运算导致溢出。 在AS400中删除数据时 - Arithmetic operation resulted in an overflow. When Delete Data in AS400 System.OverflowException:&#39;算术运算导致溢出。&#39; - System.OverflowException: 'Arithmetic operation resulted in an overflow.' WinForm控件ListView“算术运算导致溢出。” - WinForm Control ListView “Arithmetic operation resulted in an overflow.” 为什么找不到此HashSet的Sum()。 说:“算术运算导致溢出。” - Why can't I find Sum() of this HashSet. says “Arithmetic operation resulted in an overflow.” CSharp:无法读取大型 .npy 文件。 例外是“NumSharp.dll 算术运算导致溢出。” - CSharp: Failed to read LARGE .npy file. Exception is "NumSharp.dll Arithmetic operation resulted in an overflow." protobuf-net不反序列化0 - protobuf-net not deserializing 0's Xceed-&gt;算术运算导致溢出 - Xceed -> Arithmetic operation resulted in an overflow
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM