Protobuf C# Message Translation to JAVA

你。 提交于 2019-12-10 10:53:31

问题


I am trying to translate a message generated using C# to JAVA. As a first step, i generated proto file and this is what i got

package Om.Business.Scanner;

message ScannerActivityDetail {
   optional string ActivityId = 1;
   optional string ContextId = 2;
   optional int32 ActivityStart = 3;
   optional bcl.DateTime ActivityEnd = 4;
}

How do i interpret bcl.DateTime in java world?

I am using protobuf-net and trying to de-serialize message generated by C# app.

Thanks in advance for your help.


回答1:


Looking at bcl.proto, it should be pretty straightforward. Create a Map<DateTime.TimeSpanScale, TimeUnit> in the obvious way, then:

public static Date toDate(bcl.DateTime proto) {
    TimeUnit unit = SCALE_TO_UNIT_MAP.get(proto.getScale());
    if (unit == null) {
        throw new IllegalArgumentException("Invalid scale: " + proto.getScale());
    }
    long millis = unit.toMillis(proto.getValue());
    return new Date(millis);
}

You could use Joda Time's DateTime type in exactly the same way, as it has a constructor accepting a long too. (You might want to think about which time zone to specify though...)



来源:https://stackoverflow.com/questions/12186349/protobuf-c-sharp-message-translation-to-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!