Is “google/protobuf/struct.proto” the best way to send dynamic JSON over GRPC?

前端 未结 3 478
暗喜
暗喜 2021-02-13 17:14

I have a written a simple GRPC server and a client to call the server (both in Go). Please tell me if using golang/protobuf/struct is the best way to send a dynamic JSON with GR

3条回答
  •  名媛妹妹
    2021-02-13 17:37

    I ended up using a two-step conversion with protojson, from map to json to struct:

    m := map[string]interface{}{
      "foo":"bar",
      "baz":123,
    }
    b, err := json.Marshal(m)
    s := &structpb.Struct{}
    err = protojson.Unmarshal(b, s)
    

    I don't find it elegant but could not really find any official documentation of how to do this differently. I also prefer to produce structures using "official" functions rather than trying to build a structure myself.

提交回复
热议问题