基于protobuf2.6序列化 反序列化

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基于protobuf2.6序列化 反序列化相关的知识,希望对你有一定的参考价值。

代码:  protobuf26_serialize_deserialize

 

序列化、反序列化工具类:

package org.cgl.util.protobuf26;

import com.google.protobuf.GeneratedMessage;
import com.google.protobuf.TextFormat;
import org.cgl.util.protobuf26.message.Message;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * Created by agui on 3/14/2017.
 */
public class ProtoBufUtil {
    public static String shortDebugString(GeneratedMessage gmsg){
        return null == gmsg? "空": TextFormat.shortDebugString( gmsg);
    }

    public static String to_binaray(GeneratedMessage.Builder builder) throws IOException {
        ByteArrayOutputStream bs = new ByteArrayOutputStream();
        bs.write(builder.build().toByteArray());
        return bs.toString(Name.ISO_8859_1);
    }
    public static String to_binaray(GeneratedMessage msg_obj) throws IOException {
        ByteArrayOutputStream bs = new ByteArrayOutputStream();
        bs.write(msg_obj.toByteArray());
        String msg_bin = bs.toString(Name.ISO_8859_1);
        return msg_bin ;
    }

    //String msg_bin...
    //TrmRtrDvcAgtSvGgPrtbf.Cmd cmd_default_to_net_g = TrmRtrDvcAgtSvGgPrtbf.Cmd.parseFrom(msg_bin.getBytes("iso-8859-1"))
    public static GeneratedMessage to_object(Class  msg_clz, String msg_bin) throws IOException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {

        byte[] msg_bin0 = msg_bin.getBytes(Name.ISO_8859_1);
        Method parseFrom_method = msg_clz.getMethod("parseFrom",byte[].class);
        Object msg_obj = parseFrom_method.invoke(null, msg_bin0);
        return (GeneratedMessage) msg_obj;
    }


    public static void main(String[] args) throws IOException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        /**
         * 将 req 序列化为 req_bin_string ,  再将 req_bin_string 反序列化为 req_object_from_bin_string
         */
        Message.LoginRequest req = Message.LoginRequest.newBuilder().setApplicationId(10).setUserName("zhangsan").setPasswordMd5("aaaaaaaaaaaaaaaaaaaa").build();
        String message = null;
        String req_bin_string = to_binaray(req);
        Message.LoginRequest req_object_from_bin_string = (Message.LoginRequest) to_object(Message.LoginRequest.class, req_bin_string);

        //req_object_from_bin_string 和 req 一样的
        int debug = 0;

    }
}

  命令(常量)类:

package org.cgl.util.protobuf26;

/**
 * Created by agui on 4/22/2017.
 */
public class Name {
    public static final String ISO_8859_1 = "ISO-8859-1";
}

  

 

protobuf报文定义:

option java_package = "org.cgl.util.protobuf26.message";
option java_outer_classname = "Message";

message LoginRequest{
  optional int32 applicationId = 1;
  optional string userName = 2;
  optional string passwordMd5 = 3;
}

  

生成报文解析器命令:

protoc message.proto  --java_out=.

  

以上是关于基于protobuf2.6序列化 反序列化的主要内容,如果未能解决你的问题,请参考以下文章

消息序列化工具-protobuf介绍及安装使用技巧

基于 BERT 的 NER 模型在反序列化时给出不一致的预测

2.序列化与反序列化

Flink实战系列Flink 1.14.0 消费 kafka 数据自定义反序列化器

将 C++ 类序列化为文件,然后在 Python 中进行基于事件的反序列​​化?

C++ 基于rapidjson对json字符串的进行序列化与反序列化