ProtobufUtil.java 文件源码

java
阅读 28 收藏 0 点赞 0 评论 0

项目:ja-micro 作者:
/**
 * 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;
}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号