将JSON转换为Protobuf(v3)

huangapple go评论98阅读模式
英文:

JSON to Protobuf (v3)

问题

新手问题。我对Protobuf还不太熟悉。如何将我的JSON数据转换为Protobuf JSON?

{
	"id": "12345678901233456",
	"name": "asdfghjkl",
	"start_time": 1.666058486989E9,
	"trace_id": "1-adg346-dhagjkh348762834jah37846f",
	"end_time": 1.666058487934E9
}

我正在尝试将从AWS X-Ray获取的跟踪信息发送到OTLP HTTP端点(0.0.0.0:4318)。这应该是一个动态的过程,因为无法确定它是否具有相同的结构,这取决于给定的跟踪信息。序列化格式需要是Protobuf JSON。

英文:

Noob question here. I'm still new to the Protobuf thing. How can I convert my JSON data to a Protobuf JSON?

{
	"id": "12345678901233456",
	"name": "asdfghjkl",
	"start_time": 1.666058486989E9,
	"trace_id": "1-adg346-dhagjkh348762834jah37846f",
	"end_time": 1.666058487934E9
}

I'm trying to send the fetched data from the AWS X-Ray which is the trace information to an OTLP HTTP endpoint (0.0.0.0:4318). And it should be a dynamic thing because it is not certain if it will have the same structure or not because it depends on the fetched trace information given. The serialization format needs to be protobuf JSON.

答案1

得分: 2

你可以使用google.golang.org/protobuf/encoding/protojson将JSON文件转换为protobuf。

req := &proto.JobCreateRequest{}
err := protojson.Unmarshal(bytes, req)

你需要手动将JSON转换为字节。这将给你一个带有proto消息的proto请求。

英文:

You can use google.golang.org/protobuf/encoding/protojson to convert your JSON file into protobuf.

req := &proto.JobCreateRequest{}
err := protojson.Unmarshal(bytes, req)

You need to manually convert json into bytes. This will give you a proto request with a proto message.

huangapple
  • 本文由 发表于 2022年10月18日 10:19:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/74104980.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定