Bael 5679 json to protobuf (#12696)

* Initial Changes

* Update ProtobufUtilUnitTest.java

* Changes for JSON to Protobuf
This commit is contained in:
Amitabh Tiwari 2022-09-07 10:56:18 +05:30 committed by GitHub
parent 2f9967dcd3
commit 82dfba062c
3 changed files with 85 additions and 0 deletions

View File

@ -20,6 +20,11 @@
<artifactId>protobuf-java</artifactId>
<version>${protobuf-java.version}</version>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java-util</artifactId>
<version>${protobuf-java.version}</version>
</dependency>
<dependency>
<groupId>com.googlecode.protobuf-java-format</groupId>
<artifactId>protobuf-java-format</artifactId>

View File

@ -0,0 +1,37 @@
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 ProtobuffUtil {
public static String toJson(MessageOrBuilder messageOrBuilder) throws IOException {
return JsonFormat.printer().print(messageOrBuilder);
}
@SuppressWarnings("unchecked")
public static <T extends Message> T fromJson(String json, Class<T> clazz) throws IOException {
Builder<?> builder = null;
try {
builder = (Builder<?>) clazz.getMethod("newBuilder").invoke(null);
} catch (Exception e) {
return null;
}
JsonFormat.parser().ignoringUnknownFields().merge(json, builder);
return (T) builder.build();
}
@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

@ -0,0 +1,43 @@
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;
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" + "}";
@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);
Assert.assertTrue(json.contains("\"boolean\": true"));
Assert.assertTrue(json.contains("\"string\": \"Hello World\""));
Assert.assertTrue(json.contains("\"color\": \"gold\""));
}
}