* Initial Changes * Update ProtobufUtilUnitTest.java * Changes for JSON to Protobuf * Corrected the file name * Update ProtobufUtilUnitTest.java * Update ProtobufUtilUnitTest.java * Update ProtobufUtilUnitTest.java * Removed confusing changes
25 lines
751 B
Java
25 lines
751 B
Java
package com.baeldung.protobuf.convert;
|
|
|
|
import java.io.IOException;
|
|
|
|
import com.google.protobuf.AbstractMessage.Builder;
|
|
import com.google.protobuf.Message;
|
|
import com.google.protobuf.MessageOrBuilder;
|
|
import com.google.protobuf.Struct;
|
|
import com.google.protobuf.util.JsonFormat;
|
|
|
|
public class ProtobufUtil {
|
|
|
|
public static String toJson(MessageOrBuilder messageOrBuilder) throws IOException {
|
|
return JsonFormat.printer().print(messageOrBuilder);
|
|
}
|
|
|
|
@SuppressWarnings("unchecked")
|
|
public static Message fromJson(String json) throws IOException {
|
|
Builder structBuilder = Struct.newBuilder();
|
|
JsonFormat.parser().ignoringUnknownFields().merge(json, structBuilder);
|
|
return structBuilder.build();
|
|
}
|
|
|
|
}
|