diff --git a/spring-protobuf/src/main/java/com/baeldung/protobuf/convert/ProtobufUtil.java b/spring-protobuf/src/main/java/com/baeldung/protobuf/convert/ProtobufUtil.java new file mode 100644 index 0000000000..dca0f4e53f --- /dev/null +++ b/spring-protobuf/src/main/java/com/baeldung/protobuf/convert/ProtobufUtil.java @@ -0,0 +1,24 @@ +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(); + } + +} diff --git a/spring-protobuf/src/test/java/com/baeldung/protobuf/convert/ProtobufUtilUnitTest.java b/spring-protobuf/src/test/java/com/baeldung/protobuf/convert/ProtobufUtilUnitTest.java index 61440a2116..7971d730f8 100644 --- a/spring-protobuf/src/test/java/com/baeldung/protobuf/convert/ProtobufUtilUnitTest.java +++ b/spring-protobuf/src/test/java/com/baeldung/protobuf/convert/ProtobufUtilUnitTest.java @@ -1,13 +1,7 @@ package com.baeldung.protobuf.convert; -import java.io.BufferedReader; -import java.io.ByteArrayInputStream; + import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.Reader; -import java.nio.charset.Charset; -import java.nio.charset.StandardCharsets; import org.junit.Assert; import org.junit.jupiter.api.Test; @@ -16,28 +10,29 @@ import com.google.protobuf.Message; public class ProtobufUtilUnitTest { - public static String jsonInput = "{\r\n" + " \"boolean\": true,\r\n" + " \"color\": \"gold\",\r\n" - + " \"object\": {\r\n" + " \"a\": \"b\",\r\n" + " \"c\": \"d\"\r\n" + " },\r\n" - + " \"string\": \"Hello World\"\r\n" + "}"; + public static String jsonStr = "{\r\n" + + " \"boolean\": true,\r\n" + + " \"color\": \"gold\",\r\n" + + " \"object\": {\r\n" + + " \"a\": \"b\",\r\n" + + " \"c\": \"d\"\r\n" + + " },\r\n" + + " \"string\": \"Hello World\"\r\n" + + "}"; + + @Test + public void givenJson_convertToProtobuf() throws IOException { + Message protobuf = ProtobufUtil.fromJson(jsonStr); + Assert.assertTrue(protobuf.toString().contains("key: \"boolean\"")); + Assert.assertTrue(protobuf.toString().contains("string_value: \"Hello World\"")); + } @Test public void givenProtobuf_convertToJson() throws IOException { - Message fromJson = ProtobuffUtil.fromJson(jsonInput); - - InputStream inputStream = new ByteArrayInputStream(fromJson.toByteArray()); - - StringBuilder textBuilder = new StringBuilder(); - try (Reader reader = new BufferedReader( - new InputStreamReader(inputStream, Charset.forName(StandardCharsets.UTF_8.name())))) { - int c = 0; - while ((c = reader.read()) != -1) { - textBuilder.append((char) c); - } - } - String json = ProtobuffUtil.toJson(fromJson); + Message protobuf = ProtobufUtil.fromJson(jsonStr); + String json = ProtobufUtil.toJson(protobuf); Assert.assertTrue(json.contains("\"boolean\": true")); Assert.assertTrue(json.contains("\"string\": \"Hello World\"")); Assert.assertTrue(json.contains("\"color\": \"gold\"")); } - }