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
This commit is contained in:
Amitabh Tiwari 2022-09-13 00:53:59 +05:30 committed by GitHub
parent d07ff5878c
commit a04ad088d9
2 changed files with 43 additions and 24 deletions

View File

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

View File

@ -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\""));
}
}