问题描述
我有一个非常简单的原型:
I have a very simple proto:
message ChargeCardResponse
{
bool success = 1;
}
当我尝试将其解析为String时,JsonFormat.printer().print()会得到非常奇怪的输出,具体取决于成功值.
When I'm trying to parse it to String I get a very strange output from the JsonFormat.printer().print() which depends on the success value.
ChargeCardResponse.Builder builder = ChargeCardResponse.newBuilder();
System.out.println(JsonFormat.printer().print(builder.setSuccess(true).build()));
输出:{ 成功":正确}
Output:{ "success": true}
ChargeCardResponse.Builder builder2 = ChargeCardResponse.newBuilder();
System.out.println(JsonFormat.printer().print(builder2.setSuccess(false).build()));
输出:{}
因此,如果我使用成功属性为打印机赋予错误的值,则无法制作JSON格式.我究竟做错了什么?有人可以确认吗?
So if I give false value to the printer with the success property it can't make the JSON format.What am I doing wrong? Is there anybody who can confirm it?
推荐答案
问题是将成功设置为"false"等同于保留默认的"false"值.
The thing is that setting success to "false" is equivalent to leaving the default "false" value intact.
JsonPrinter忽略默认的原型值,除非该选项已关闭:
JsonPrinter omits default proto values, unless that option it turned off:
这应该有效,我认为:
ChargeCardResponse.Builder builder2 = ChargeCardResponse.newBuilder();
System.out.println(JsonFormat.printer().includingDefaultValueFields().print(builder2.setSuccess(false).build()));
这篇关于JsonFormat.printer().print()如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!