Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在java程序中反序列化protobuf net序列化的数据_Java_C#_Serialization_Protocol Buffers - Fatal编程技术网

如何在java程序中反序列化protobuf net序列化的数据

如何在java程序中反序列化protobuf net序列化的数据,java,c#,serialization,protocol-buffers,Java,C#,Serialization,Protocol Buffers,数据由c#protobuf net序列化并设置为redis, 现在我想在java程序中获取数据并对其进行反序列化,除了datetime字段之外,一切都正常,它们无法正确序列化。 在c#程序中,数据从实体序列化如下,并设置为redis [Serializable] [ProtoContract] public class TestMsg { public TestMsg(); [ProtoMember(1)] public string message { get; se

数据由c#protobuf net序列化并设置为redis, 现在我想在java程序中获取数据并对其进行反序列化,除了datetime字段之外,一切都正常,它们无法正确序列化。 在c#程序中,数据从实体序列化如下,并设置为redis

[Serializable]
[ProtoContract]
public class TestMsg 
{
    public TestMsg();
    [ProtoMember(1)]
    public string message { get; set; }
    [ProtoMember(2)]
    public DateTime UpdateTime { get; set; }
}
在我的java程序中,协议如下:

syntax = "proto2";
option java_outer_classname = "TestMsgEntity";
message TestMsg {
    required string Message = 1;//
    required string UpdateTime = 2;// it can not be deserialized properly.if I change is to long,then i get zero .
}
//java代码

byte[]  byteArrayRedis  = provider.getbyte("keyname"); //get the data from redis

ByteArrayInputStream baiContent = new ByteArrayInputStream(byteArrayRedis);
try {
        TestMsgEntity.TestMsg msg = TestMsgEntity.TestMsg.parseFrom(baiContent);
        String message = msg.getMessage (); //it is ok
        String lastUpdate =msg.getLastUpdateTime();//how can i get the value? 
    }
最近有新的“知名”合同来描述时间。最好的办法是在java.proto中使用“timestamp”,并通过
[ProtoMember]
众所周知的
数据格式注释DateTime成员

要更简单地了解这一点:编写一个使用“timestamp”的.proto(您无论如何都需要为java编写),并运行它以查看它的输出。这是您的.proto的一个修改版本,您只需单击“生成”:-如您所见,关键点如下:

[global::ProtoBuf.ProtoMember(2,
    DataFormat = global::ProtoBuf.DataFormat.WellKnown, IsRequired = true)]
public global::System.DateTime? UpdateTime { get; set; }
DataFormat=global::ProtoBuf.DataFormat.WellKnown
告诉它使用
timestamp
表示法。类似地,这将导致
TimeSpan
使用众所周知的
duration
表示法


如果protobuf net存储的数据没有已知的格式,那么必须通过bcl.proto手动解码;很抱歉,它并没有那么简单,但是:它必须是某种东西,而且当时没有“众所周知”的东西。

可能的重复请参见:&