Specifying a JSON Name for the protobuf Extension

I have added an extension post to the post and I need to marshal it as json. However, the field name for the extension message is [message.extension_message_name].

I would prefer it to be called simply extension_message_name, without curly braces and prefix, as this extension message exists elsewhere in our API, and the presence of this strange name adds to the confusion.

As far as I can tell, the person in charge of the code is in protobuf / jsonpb , where the JSONName is set to fmt.Sprintf("[%s]", desc.Nameand cannot seem to be overwritten.

Does anyone have a workaround for this?

+7
source share
2 answers

, , :

  1. json, , .

  2. https://github.com/favadi/protoc-go-inject-tag , , , , , json, , json,

  3. json , , .

0

Go encoding/json / json, - :

type Example struct {
    ExtMessageName string 'json:"extension_message_name"'
}

msg := Example{ExtMessageName: "This is a test"}

jsonBytes, err := json.Marshal(msg)

if err != nil {
    fmt.Printf("error: %v", err)
    return
}

fmt.Println(string(jsonBytes))

play.golang.org

:

{"extension_message_name":"This is a test"}
-1

Source: https://habr.com/ru/post/1659851/


All Articles