/**
* Converts a protobuf message to a JSON object
* <p>
* Note: Preserves the field names as defined in the *.proto definition
* Note:
*
* @param input the protobuf message to convert
* @return the converted JSON object
*/
public static JsonObject protobufToJsonWithDefaultValues(Message input) {
JsonObject object = new JsonObject();
if (input == null) {
logger.warn("Protobuf message was null");
} else {
try {
String jsonString = JsonFormat.printer()
.preservingProtoFieldNames()
.includingDefaultValueFields()
.print(input);
object = new JsonParser().parse(jsonString).getAsJsonObject();
} catch (Exception e) {
throw new RuntimeException("Error deserializing protobuf to json", e);
}
}
return object;
}
ProtobufUtil.java 文件源码
java
阅读 28
收藏 0
点赞 0
评论 0
项目:ja-micro
作者:
评论列表
文章目录