1. Overview
1.概述
In this tutorial, we’ll demonstrate how to convert from JSON to Protobuf and from Protobuf to JSON.
在本教程中,我们将演示如何从JSON转换为Protobuf以及从Protobuf转换为JSON。
Protobuf is a free and open-source cross-platform data format used to serialize structured data.
Protobuf是一种免费和开源的跨平台数据格式,用于序列化结构化数据。
2. Maven Dependency
2.Maven的依赖性
To start, let’s create a Spring Boot project by including the protobuf-java-util dependency:
首先,让我们创建一个Spring Boot项目,包括protobuf-java-util依赖。
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java-util</artifactId>
<version>3.21.5</version>
</dependency>
3. Convert JSON to Protobuf
3.将JSON转换成Protobuf
We can convert JSON to a protobuf message by using JsonFormat. JsonFormat is a utility class to convert protobuf messages to/from JSON format. JsonFormat’s parser() creates a Parser, which uses the merge() method to parse JSON to protobuf message.
我们可以通过使用JsonFormat将JSON转换为protobuf消息。 JsonFormat是一个实用类,用于将protobuf消息转换成/从JSON格式。JsonFormat的 parser() 创建了一个Parser,它使用merge()方法来解析JSON到protobuf消息。
Let’s create a method that takes JSON and generates a protobuf message:
让我们创建一个方法,接受JSON并生成一个protobuf消息。
public static Message fromJson(String json) throws IOException {
Builder structBuilder = Struct.newBuilder();
JsonFormat.parser().ignoringUnknownFields().merge(json, structBuilder);
return structBuilder.build();
}
Let’s use the following sample JSON:
让我们使用下面的JSON样本。
{
"boolean": true,
"color": "gold",
"object": {
"a": "b",
"c": "d"
},
"string": "Hello World"
}
Now, let’s write a simple test to validate the conversion from JSON to protobuf message:
现在,让我们写一个简单的测试来验证从JSON到protobuf消息的转换。
@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\""));
}
4. Convert Protobuf to JSON
4.将Protobuf转换为JSON
We can convert protobuf message to JSON by using JsonFormat‘s printer() method, which accepts protobuf as a MessageOrBuilder:
我们可以通过使用JsonFormat的printer()方法将protobuf消息转换为JSON,它接受protobuf作为MessageOrBuilder。
public static String toJson(MessageOrBuilder messageOrBuilder) throws IOException {
return JsonFormat.printer().print(messageOrBuilder);
}
Let’s write a simple test to validate the conversion from protobuf to JSON message:
让我们写一个简单的测试来验证从protobuf到JSON消息的转换。
@Test
public void givenProtobuf_convertToJson() throws IOException {
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\""));
}
5. Conclusion
5.结论
In this article, we’ve demonstrated how we can convert JSON to protobuf and vice versa.
在这篇文章中,我们已经演示了如何将JSON转换成protobuf,反之亦然。
As always, all code samples used in this tutorial are available over on GitHub.
一如既往,本教程中使用的所有代码样本都可以在GitHub上找到。