Files
spring-soap/spring-protobuf/src/main/java/com/baeldung/protobuf/convert/ProtobufUtil.java
Amitabh Tiwari 8d12b3874f Bael 5679 json to protobuf (#12702)
* 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
2022-09-12 12:23:59 -07:00

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();
}
}