ProtoBuf 将 JSON 解组为时间戳

ProtoBuf Unmarshal JSON into Timestamp

正在尝试查看是否存在类似的内容:

message TimestampedThing {

    string thing = 1;
    Timestamp date = 2 (layout="2018-03-07T01:00:00.000Z");
}

当我将它解组到 GRPC 对象时出现解析错误。

details: 'json: cannot unmarshal string into Go struct field TimestampedThing.createdTimestamp of type timestamp.Timestamp'

protobuf 中的时间戳是众所周知的类型(由秒和纳秒组成),以 ISO 格式表示,这在 JSONFormat class 中得到了注意。

时间戳的表示

message Timestamp{
  int64 seconds,
  int32 nanos
}

Layout 不是 protobuf 中的关键字,据我所知,没有为时间戳定义 layout/format 的选项。

原来我需要 jsonpb:https://godoc.org/github.com/golang/protobuf/jsonpb

jsonpb.Unmarshal(bufio.NewReader(bytes), &pb.TimestampedThing)