Java:JSON->;Protobuf&;反向转换
我有一个现有的系统,它在GUI和服务器之间使用基于protobuf的通信协议。现在我想添加一些持久性,但目前protobuf消息直接转换为第三方自定义对象 是否有一种方法可以将原始消息转换为json,然后将其持久化到数据库中 N.B.:我不太喜欢将二进制protobuf写入数据库的想法,因为有一天它可能会与较新版本不向后兼容,从而破坏系统。我们目前正在使用它来转换protobuf消息(任何Java:JSON->;Protobuf&;反向转换,java,json,serialization,persistence,protocol-buffers,Java,Json,Serialization,Persistence,Protocol Buffers,我有一个现有的系统,它在GUI和服务器之间使用基于protobuf的通信协议。现在我想添加一些持久性,但目前protobuf消息直接转换为第三方自定义对象 是否有一种方法可以将原始消息转换为json,然后将其持久化到数据库中 N.B.:我不太喜欢将二进制protobuf写入数据库的想法,因为有一天它可能会与较新版本不向后兼容,从而破坏系统。我们目前正在使用它来转换protobuf消息(任何消息的子类)转换为JSON格式,通过我们的web API发送 简单地做: JsonFormat.prin
消息
的子类)转换为JSON格式,通过我们的web API发送
简单地做:
JsonFormat.printToString(protoMessage)
我不太喜欢将二进制protobuf写入数据库的想法,因为有一天它会变得与新版本不向后兼容,从而破坏系统
将protobuf转换为JSON进行存储,然后在加载时再转换回protobuf很可能会产生兼容性问题,因为:
- 如果执行转换的进程不是使用最新版本的protobuf模式构建的,那么转换将自动删除进程不知道的任何字段。存储端和加载端都是如此
- 即使使用最新的模式,JSON Protobuf转换在存在不精确的浮点值和类似情况时也可能有损
- Protobufs实际上比JSON具有(稍微)更强的向后兼容性保证。与JSON一样,如果您添加一个新字段,旧客户端将忽略它。与JSON不同,Protobufs允许声明默认值,这可以使新客户机更容易处理缺少字段的旧数据。这只是一个小小的优势,但Protobuf和JSON在其他方面具有等效的向后兼容性属性,因此在JSON中存储不会获得任何向后兼容性优势
com.google.Protobuf.Message
接口提供)。如中所述,因为这是ProtocolBuffers支持的特性。对于Java,包括扩展模块,并使用如下方式:
在Protobuf3.0之前,JsonFormat就已经可用了。但是,方法有点不同
在Protobuf3.0+中,JsonFormat类是一个单例类,因此执行如下操作
String jsonString = "";
JsonFormat.parser().ignoringUnknownFields().merge(jsonString,yourObjectBuilder);
在Protobuf 2.5+中,以下各项应起作用
String jsonString = "";
JsonFormat jsonFormat = new JsonFormat();
jsonString = jsonFormat.printToString(yourProtobufMessage);
下面是一个链接,指向我编写的一个在TypeAdapter中使用JsonFormat类的类,该类可以注册到GsonBuilder对象。然后,您可以使用Gson的toJson和fromJson方法将原始数据转换为Java并返回
答复。如果文件中有protobuf数据,并且希望将其解析为protobuf消息对象,请使用merge方法类。请参阅以下代码段:
// Let your proto text data be in a file MessageDataAsProto.prototxt
// Read it into string
String protoDataAsString = FileUtils.readFileToString(new File("MessageDataAsProto.prototxt"));
// Create an object of the message builder
MyMessage.Builder myMsgBuilder = MyMessage.newBuilder();
// Use text format to parse the data into the message builder
TextFormat.merge(protoDataAsString, ExtensionRegistry.getEmptyRegistry(), myMsgBuilder);
// Build the message and return
return myMsgBuilder.build();
请尝试
JsonFormat.printer().print(MessageOrBuilder)
,它对proto3很有用。然而,目前尚不清楚如何将实际的protobuf
消息(作为我在.proto文件中定义的java包提供)转换为com.google.protbuf.message对象。对于protobuf 2.5,使用依赖项:
"com.googlecode.protobuf-java-format" % "protobuf-java-format" % "1.2"
然后使用代码:
com.googlecode.protobuf.format.JsonFormat.merge(json, builder)
com.googlecode.protobuf.format.JsonFormat.printToString(proto)
嗯,根据我的发现,没有捷径可走,但不知何故,你
只需几个简单的步骤就可以实现 首先,您必须声明一个类型为“ProtobufJsonFormatHttpMessageConverter”的bean
@Bean
@Primary
public ProtobufJsonFormatHttpMessageConverter protobufHttpMessageConverter() {
return new ProtobufJsonFormatHttpMessageConverter(JsonFormat.parser(), JsonFormat.printer());
}
然后您可以编写一个实用程序类,比如ResponseBuilder,因为它可以在默认情况下解析请求,但如果没有这些更改,它就无法生成Json响应。然后,您可以编写一些方法将响应类型转换为相关的对象类型
public static <T> T build(Message message, Class<T> type) {
Printer printer = JsonFormat.printer();
Gson gson = new Gson();
try {
return gson.fromJson(printer.print(message), type);
} catch (JsonSyntaxException | InvalidProtocolBufferException e) {
throw new ApiException(HttpStatus.INTERNAL_SERVER_ERROR, "Response conversion Error", e);
}
}
publicstatict构建(消息,类类型){
打印机打印机=JsonFormat.Printer();
Gson Gson=新的Gson();
试一试{
返回gson.fromJson(printer.print(message),type);
}捕获(JsonSyntaxException | InvalidProtocolBufferException e){
抛出新的ApiException(HttpStatus.INTERNAL_SERVER_错误,“响应转换错误”,e);
}
}
然后,您可以从控制器类中调用此方法,如最后一行所示-
return ResponseBuilder.build(<returned_service_object>, <Type>);
返回响应builder.build(,);
希望这将帮助您实现json格式的protobuf 仿制药解决方案
下面是Json转换器的通用版本
package com.github.platform.util;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import com.google.protobuf.AbstractMessage.Builder;
import com.google.protobuf.Message;
import com.google.protobuf.MessageOrBuilder;
import com.google.protobuf.util.JsonFormat;
/**
* Generic ProtoJsonUtil to be used to serialize and deserialize Proto to json
*
* @author Marcello.deeSales@gmail.com
*
*/
public final class ProtoJsonUtil {
/**
* Makes a Json from a given message or builder
*
* @param messageOrBuilder is the instance
* @return The string representation
* @throws IOException if any error occurs
*/
public static String toJson(MessageOrBuilder messageOrBuilder) throws IOException {
return JsonFormat.printer().print(messageOrBuilder);
}
/**
* Makes a new instance of message based on the json and the class
* @param <T> is the class type
* @param json is the json instance
* @param clazz is the class instance
* @return An instance of T based on the json values
* @throws IOException if any error occurs
*/
@SuppressWarnings({"unchecked", "rawtypes"})
public static <T extends Message> T fromJson(String json, Class<T> clazz) throws IOException {
// https://stackoverflow.com/questions/27642021/calling-parsefrom-method-for-generic-protobuffer-class-in-java/33701202#33701202
Builder builder = null;
try {
// Since we are dealing with a Message type, we can call newBuilder()
builder = (Builder) clazz.getMethod("newBuilder").invoke(null);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException
| NoSuchMethodException | SecurityException e) {
return null;
}
// The instance is placed into the builder values
JsonFormat.parser().ignoringUnknownFields().merge(json, builder);
// the instance will be from the build
return (T) builder.build();
}
}
到Json泛型
来自Json泛型
这是我的实用程序类,您可以使用:
包;
导入com.google.protobuf.Message;
导入com.google.protobuf.MessageOrBuilder;
导入com.google.protobuf.util.JsonFormat;
/**
*作者@espresso stackoverflow。
*样本使用:
*Model.Person-reqObj=ProtoUtil.toProto(reqJson,Model.Person.getDefaultInstance());
Model.Person res=personSvc.update(reqObj);
最终字符串resJson=ProtoUtil.toJson(res);
**/
公共类协议{
公共静态字符串toJson(T obj){
试一试{
返回JsonFormat.printer().print(obj);
}捕获(例外e){
抛出新的RuntimeException(“将Proto转换为json时出错”,e);
}
}
公共静态T toProto(字符串protoJsonStr,T消息){
试一试{
Message.Builder=Message.getDefaultInstanceFrType().toBuilder();
JsonFormat.parser().ignoringunknowfields().merge(protoJsonStr,builder);
T out=(T)builder.build();
返回;
}捕获(例外e){
抛出新的RuntimeException((“将Json转换为proto时出错”,e);
}
}
}
谷歌protobuf 3.7.0的最新答案:
Maven更改--将以下内容添加到pom.xml中:
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>3.7.0</version>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java-util</artifactId>
<version>3.7.0</version>
</dependency>
</dependencies>
<build>
<extensions>
<extension>
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
<version>1.6.0</version>
</extension>
</extensions>
<plugins>
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>0.6.1</version>
<extensions>true</extensions>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<configuration>
<additionalProtoPathElements>
<additionalProtoPathElement>${project.basedir}/src/main/resources</additionalProtoPathElement>
</additionalProtoPathElements>
<protocArtifact>com.google.protobuf:protoc:3.7.0:exe:${os.detected.classifier}</protocArtifact>
</configuration>
</plugin>
你所说的“因为它有一天会变得不落后”到底是什么意思
GetAllGreetings.Builder allGreetingsBuilder = GetAllGreetings.newBuilder();
allGreetingsBuilder.addGreeting(makeNewGreeting("Marcello", "Hi %s, how are you", Language.EN))
.addGreeting(makeNewGreeting("John", "Today is hot, %s, get some ice", Language.ES))
.addGreeting(makeNewGreeting("Mary", "%s, summer is here! Let's go surfing!", Language.PT));
GetAllGreetings allGreetings = allGreetingsBuilder.build();
String json = ProtoJsonUtil.toJson(allGreetingsLoaded);
log.info("Json format: " + json);
GetAllGreetings parsed = ProtoJsonUtil.fromJson(json, GetAllGreetings.class);
log.info("The Proto deserialized from Json " + parsed);
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>3.7.0</version>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java-util</artifactId>
<version>3.7.0</version>
</dependency>
</dependencies>
<build>
<extensions>
<extension>
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
<version>1.6.0</version>
</extension>
</extensions>
<plugins>
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>0.6.1</version>
<extensions>true</extensions>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<configuration>
<additionalProtoPathElements>
<additionalProtoPathElement>${project.basedir}/src/main/resources</additionalProtoPathElement>
</additionalProtoPathElements>
<protocArtifact>com.google.protobuf:protoc:3.7.0:exe:${os.detected.classifier}</protocArtifact>
</configuration>
</plugin>
public class ProtobufTrial {
public static void main(String[] args) throws Exception {
String jsonString = "";
MyProto.MyEventMsg.Builder builder = MyProto.MyEventMsg.newBuilder();
JsonFormat.parser().ignoringUnknownFields().merge(jsonString, builder);
MyProto.MyEventMsg value = builder.build();
// This is for printing the proto in string format
System.out.println(JsonFormat.printer().print(value));
}
}