如何使用JSON存储协议缓冲区中的“oneof”字段

4

我希望将我的protobuf消息对象转换为json格式,以便保存/加载到redis中。但是其中的oneof字段无法按预期工作。

  • test.proto

一个简单的oneof示例。

syntax = "proto3";

message example {
    oneof test {
        bool one = 1;
        bool two = 2;
    }
}
  • Makefile(构建文件)

如何将protobuf代码编译为golang。

.PHONY: proto

proto:
    protoc -Iproto/ -I/usr/local/include \
        -I$(GOPATH)/src \
        -I$(GOPATH)/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis/ \
        --go_out=plugins=grpc:proto \
        proto/test.proto 
  • 主函数.go

我如何对我的示例对象进行编排/取消编排。

package main

import (
    "encoding/json"
    "fmt"
    pb "test/proto"
)

func main() {
    fmt.Println()

    obj := pb.Example{Test: &pb.Example_One{true}}
    fmt.Println(obj)
    fmt.Println("before one: ", obj.GetOne())
    fmt.Println("before two: ", obj.GetTwo())

    jsonData, _ := json.Marshal(obj)
    fmt.Println(string(jsonData))
    fmt.Println("-----")

    obj2 := pb.Example{}
    _ = json.Unmarshal(jsonData, &obj2)
    fmt.Println(obj2)
    fmt.Println("after one: ", obj2.GetOne())
    fmt.Println("after two: ", obj2.GetTwo())
}

那么,结果就是

$ go run main.go

{{{} [] [] <nil>} 0 [] 0xc0000141a0}
before one:  true
before two:  false
{"Test":{"One":true}}
-----
{{{} [] [] <nil>} 0 [] <nil>}
after one:  false
after two:  false

有人知道原因吗?


4
使用protojson包: "这个包生成的输出与标准的'encoding/json'包不同,后者无法正确地处理协议缓冲区消息。" - Peter
1个回答

2
感谢Peter,我可以将我的消息编码为json。 protojson文档 - 我的protoc环境
// versions:
//  protoc-gen-go v1.25.0-devel
//  protoc        v3.6.

我的答案代码

package main

import (
    "fmt"
    pb "test/proto"

    "google.golang.org/protobuf/encoding/protojson"
)

func main() {
    obj := pb.Example{Test: &pb.Example_One{true}}
    fmt.Println(obj)
    fmt.Println("before one: ", obj.GetOne())
    fmt.Println("before two: ", obj.GetTwo())

    jsonData, _ := protojson.Marshal(&obj)
    fmt.Println(string(jsonData))
    fmt.Println("-----")

    obj2 := pb.Example{}
    _ = protojson.Unmarshal(jsonData, &obj2)
    fmt.Println(obj2)
    fmt.Println("after one: ", obj2.GetOne())
    fmt.Println("after two: ", obj2.GetTwo())
}

和结果

$ go run main.go
{{{} [] [] <nil>} 0 [] 0xc0000141cc}
before one:  true
before two:  false
{"one":true}
-----
{{{} [] [] 0xc0001203c0} 0 [] 0xc000014253}
after one:  true
after two:  false

请注意:生成的 JSON 不会产生您原始问题中的“Test”标签。 - colm.anseo
是的,但这仍然是正确的表示方式;如果你真的想要那个 Test 标签,你应该以不同的方式定义 proto 消息。例如,oneofs 不应影响二进制格式的序列化;它们在 json 中也不应受到影响。 - Rafael Lerm

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接