本文介绍了指定protobuf扩展名的JSON名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在消息中添加了扩展消息,需要将其封送为json.但是,扩展消息的字段名称为 [message.extension_message_name] .

I've added an extending message to a message and need to marshal it as a json. However the field name for the extension message is [message.extension_message_name].

我希望将其命名为 extension_message_name ,而不使用花括号和前缀,因为此扩展消息存在于我们API的其他位置,并且具有此怪异的名称会增加混乱.

I would prefer it to be named just extension_message_name, without the braces and prefix, since this extension message exists elsewhere in our API and and having this weird name adds confusion.

据我所知,负责的代码在 protobuf/jsonpb ,其中JSONName是使用 fmt.Sprintf("[%s]",desc.Name 设置的,似乎无法覆盖.

As far as I can tell the bit of code responsible is in protobuf/jsonpb, where the JSONName is set with fmt.Sprintf("[%s]", desc.Name and cannot be overwritten it seems.

有人对此有解决方法吗?

Anyone have a workaround for this?

推荐答案

按照语言指南:

因此,使用 json_name 标记您的字段应该可以解决问题,例如:

So tagging your field with json_name should do the trick, for example this:

message TestMessage {
    string myField = 1 [json_name="my_special_field_name"];
}

当编组为JSON时,应使 myField 的名称为 my_special_field_name .

Should make myField have the name my_special_field_name when marshalled to JSON.

这篇关于指定protobuf扩展名的JSON名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 14:09