gpt4 book ai didi

c# - protobuf-net 反序列化 : "Arithmetic operation resulted in an overflow."

转载 作者:太空宇宙 更新时间:2023-11-03 20:06:34 29 4
gpt4 key购买 nike

我正在使用 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; }
}

当我删除“时间”属性时,它似乎总是有效。

异常(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

我做错了什么吗?

[编辑]

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

最佳答案

来自评论:

most likely : encoding inappropriately

调用它! (来自您的编辑)

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

protobuf 数据不是文本。您不能使用文本编码来获取它的文本表示 - 事实上,您正在使用此文本编码向后,并且它在这里没有定义的行为。您已损坏数据。更大的写了(因为我经常看到这个)is here (第一部分)。

但是,简短的版本是:不要那样做。如果您需要 string,请改用 base-64 或类似格式:

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

(同样,使用 Convert.FromBase64String 来逆转这个过程)

但是,如果可能的话,最好避免传递 stringbyte[] 可以正常工作(即 return ms.ToArray())。

关于c# - protobuf-net 反序列化 : "Arithmetic operation resulted in an overflow.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23045750/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com