英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论