protoBuf的简单使用

时间:2019.12.16:22:25

环境需求

下载proto包:地址 提取码:51j8
下载之后,将解压后的bin目录设置到系统变量
然后 cmd 打开测试 protoc --version 查看版本,显示为 libproto 3.7.0即安装好

使用proto生成java文件

新建proto文件如下

syntax = "proto3"; //表示建立的版本,默认为2

message Prop {
     string key = 1;
     string value = 2;
}
message GeoData {
    bytes geometry = 1;
    repeated Prop prop = 2;
}

我的示例proto文件需要的字段1:bytes(必须有),字段2:Prop(可重复可有多个),而Prop又包含了两个必须的属性(key&&value)
在本机运行`以下代码,将会根据写好的proto文件 java 文件到 E:\java 目录下,将其复制到java项目中

protoc --java_out=E:\java person.proto

根据编写的proto文件名为 GeoDataOuterClass

java示例代码

需要依赖

    //protobuf  
    compile group: 'com.google.protobuf', name: 'protobuf-java', version: '3.11.1'
    compile group: 'com.google.protobuf', name: 'protobuf-java-util', version: '3.11.1'

以下代码为根据定义字段序列化proto文件和反序列化

package com.atlchain.bcgis.data.protoBuf;

import com.alibaba.fastjson.JSONObject;
import com.atlchain.bcgis.data.Utils;
import com.google.protobuf.ByteString;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.io.ParseException;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Set;

/**
 * 数据与 proto 的序列化和反序列化
 */
public class protoConvert {

    /**
     * 序列化数据
     * @param geometry      空间几何数据
     * @param jsonObject    属性信息
     * @return
     */
    public static byte[] dataToProto(Geometry geometry, JSONObject jsonObject){

		//放入定义好的 bytes
        byte[] bytes = Utils.getBytesFromGeometry(geometry);
        ByteString geometryByte = ByteString.copyFrom(bytes);//proto中没有bytes,只有ByteString
        GeoDataOuterClass.GeoData.Builder protoBuilder = GeoDataOuterClass.GeoData.newBuilder();
        protoBuilder.setGeometry(geometryByte);
        //放入JSONObject,其中在以key value的形式放入
        Set<String> keys =  jsonObject.keySet();
        for(String key : keys){
            GeoDataOuterClass.Prop.Builder prop = GeoDataOuterClass.Prop.newBuilder();
            prop.setKey(key);
            prop.setValue(jsonObject.get(key).toString());
            protoBuilder.addProp(prop);
        }
        GeoDataOuterClass.GeoData outData = protoBuilder.build();
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        try {
            outData.writeTo(output);
        } catch (IOException e) {
            e.printStackTrace();
        }
        byte[] byteArray = output.toByteArray();
        return byteArray;
    }

    /**
     * 反序列化得到 geometry
     * @param byteArray
     * @return
     */
    public static Geometry getGeometryFromProto(byte[] byteArray){
        GeoDataOuterClass.GeoData protoBuilder = parsingProto(byteArray);
        ByteString byteString = protoBuilder.getGeometry();
        byte[] geometryBytes = byteString.toByteArray();
        Geometry geometry = null;
        try {
            geometry = Utils.getGeometryFromBytes(geometryBytes);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return geometry;
    }

    /**
     * 反序列化得到属性
     * @param byteArray
     * @return
     */
    public static JSONObject getPropFromProto(byte[] byteArray){
        GeoDataOuterClass.GeoData protoBuilder = parsingProto(byteArray);
        List<GeoDataOuterClass.Prop> list = protoBuilder.getPropList();
        JSONObject jsonObject = new JSONObject();
        for(int i = 0; i < list.size(); i++){
            String key = list.get(i).getKey();
            String value = list.get(i).getValue();
            jsonObject.put(key, value);
        }
        return jsonObject;
    }

    private static GeoDataOuterClass.GeoData parsingProto(byte[] byteArray){
        ByteArrayInputStream input = new ByteArrayInputStream(byteArray);
        GeoDataOuterClass.GeoData protoBuilder = null;
        try {
            protoBuilder = GeoDataOuterClass.GeoData.parseFrom(input);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return protoBuilder;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值