Den*_*gin 36 java serialization persistence json protocol-buffers
我有一个现有的系统,它在GUI和服务器之间使用基于protobuf的通信协议.现在我想添加一些持久性,但目前protobuf消息直接转换为第三方自定义对象.
有没有办法将原型消息转换为json,然后可以将其保存到数据库.
注意:我不太喜欢将二进制protobuf写入数据库的想法,因为它有一天会变得不能与新版本向后兼容并以这种方式破坏系统.
小智 38
我们目前正在使用protobuf-java-format将我们的Protobuf消息(任何子类Message)转换为JSON格式,以通过我们的Web API发送.
简单地说:
  JsonFormat.printToString(protoMessage)
Oph*_*itz 37
正如在回答类似问题时所提到的,从v3.1.0开始,这是ProtocolBuffers的支持功能.对于Java,请包含扩展模块com.google.protobuf:protobuf-java-util并使用JsonFormat,如下所示:
JsonFormat.parser().ignoringUnknownFields().merge(json, yourObjectBuilder);
YourObject value = yourObjectBuilder.build();
Ken*_*rda 34
我不太喜欢将二进制protobuf写入数据库的想法,因为它有一天会变得与新版本不向后兼容并以这种方式破坏系统.
将protobuf转换为JSON进行存储,然后在加载时转换回protobuf 更有可能产生兼容性问题,因为:
尽管如此,有很多库可以将protobufs转换为JSON,通常建立在Protobuf反射界面上(不要与Java反射界面混淆; Protobuf反射由com.google.protobuf.Message界面提供).
Mos*_*ses 13
除了Ophir的回答,JsonFormat甚至可以在protobuf 3.0之前使用.但是,这样做的方式有点不同.
在Protobuf 3.0+中,JsonFormat类是一个单例,因此执行类似下面的操作
String jsonString = "";
JsonFormat.parser().ignoringUnknownFields().merge(json,yourObjectBuilder);
在Protobuf 2.5+中,以下内容应该有效
String jsonString = "";
JsonFormat jsonFormat = new JsonFormat();
jsonString = jsonFormat.printToString(yourProtobufMessage);
这是我编写的教程的链接,该教程在TypeAdapter中使用JsonFormat类,可以注册到GsonBuilder对象.然后,您可以使用Gson的toJson和fromJson方法将原型数据转换为Java并返回.
回复牛仔裤.如果我们在文件中有protobuf数据并想要将其解析为protobuf消息对象,请使用merge方法TextFormat类.请参阅以下代码段:
// 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();
Mar*_*les 13
这是 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 {
    // /sf/ask/1934941501/#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();
  }
}
使用它很简单,如下所示:
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);
这是我的实用程序类,您可以使用:
package <removed>;
import com.google.protobuf.Message;
import com.google.protobuf.MessageOrBuilder;
import com.google.protobuf.util.JsonFormat;
/**
 * Author @espresso stackoverflow.
 * Sample use:
 *      Model.Person reqProto = ProtoUtil.toProto(reqJson, Model.Person.getDefaultInstance());
        Model.Person resProto = personSvc.update(reqProto); // service layer call
        final String resJson = ProtoUtil.toJson(resProto);
 **/
public class ProtoUtil {
    public static <T extends Message> String toJson(T obj){
        try{
            return JsonFormat.printer().print(obj);
        }catch(Exception e){
            throw new RuntimeException("Error converting Proto to json", e);
        }
    }
   public static <T extends MessageOrBuilder> T toProto(String protoJsonStr, T message){
        try{
            Message.Builder builder = message.getDefaultInstanceForType().toBuilder();
            JsonFormat.parser().ignoringUnknownFields().merge(protoJsonStr,builder);
            T out = (T) builder.build();
            return out;
        }catch(Exception e){
            throw new RuntimeException(("Error converting Json to proto", e);
        }
    }
}
试试看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这样的Utility类,因为它默认可以解析请求,但没有这些变化就不能产生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);
  }
}
然后你可以从你的控制器类调用这个方法作为最后一行 -
return ResponseBuilder.build(<returned_service_object>, <Type>);
希望这能帮助您以 json 格式实现 protobuf。
| 归档时间: | 
 | 
| 查看次数: | 72187 次 | 
| 最近记录: |