第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

我如何知道 protobuff 的 json 格式應(yīng)該是什么樣的?

我如何知道 protobuff 的 json 格式應(yīng)該是什么樣的?

一只甜甜圈 2024-01-05 16:36:37
我是 protobuf 的新手,我想將一些 protobuf 保存為 json 格式,并知道 protobuf 的完整格式是什么。我嘗試創(chuàng)建一個(gè)空的 protobuf 實(shí)例,并將其保存為 json,但這只給了我一個(gè)空的 json 對(duì)象,{}.如果我填寫一個(gè)屬性的值,并序列化它,我會(huì)在 json 中得到該屬性,這很好,但我不想對(duì)每個(gè) protobuf 的所有屬性都執(zhí)行此操作,我想這樣做。有沒(méi)有辦法讓我查看 protobuf 的完整 json 格式,而無(wú)需為每個(gè)字段提供值?筆記我在 Java 中使用 Google 的 protobuf 庫(kù),并且可以序列化和反序列化我的對(duì)象,我只是不確定如何為特定對(duì)象編寫 json。我已經(jīng)查看了這個(gè) stackoverflow 問(wèn)題以獲取信息,但沒(méi)有發(fā)現(xiàn)任何幫助。
查看完整描述

2 回答

?
隔江千里

TA貢獻(xiàn)1906條經(jīng)驗(yàn) 獲得超10個(gè)贊

是的,proto3 的 JSON 格式已記錄。

或者,要查看示例而不更改默認(rèn)值,您可以includingDefaultValueFields在打印時(shí)指定:

String?json?=?JsonFormat.printer().includingDefaultValueFields().print(message);

(這至少應(yīng)該適用于基元;我懷疑null如果嵌套消息尚未初始化,它會(huì)打印嵌套消息。)


查看完整回答
反對(duì) 回復(fù) 2024-01-05
?
阿晨1998

TA貢獻(xiàn)2037條經(jīng)驗(yàn) 獲得超6個(gè)贊

是我為了我的目的而總結(jié)的 - 你的結(jié)果可能會(huì)有所不同,哈哈!這允許我從 json 文件加載消息,并反序列化為 grpc 方法的請(qǐng)求。

?import com.google.protobuf.InvalidProtocolBufferException;

? import com.google.protobuf.MessageOrBuilder;

? import com.google.protobuf.util.JsonFormat;


? /**

? ?* Convert gRPC message to Json string.

? ?*

? ?* @param messageOrBuilder the gRPC message

? ?* @return a Json string

? ?*/

? public static String grpcMessageToJson(MessageOrBuilder messageOrBuilder) {

? ? String result = "";

? ? if (messageOrBuilder == null) {

? ? ? return result;

? ? }


? ? try {

? ? ? result = JsonFormat.printer().print(messageOrBuilder);

? ? } catch (InvalidProtocolBufferException e) {

? ? ? LOGGER.warn("Cannot serialize the gRPC message.", e);

? ? }


? ? return result;

? }


查看完整回答
反對(duì) 回復(fù) 2024-01-05
  • 2 回答
  • 0 關(guān)注
  • 154 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)