13 Star 10 Fork 23

OpenHarmony-TPC / protobuf

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README

protobuf

介绍

ProtoBuf(protocol buffers) 是一种语言无关、平台无关、可扩展的序列化结构数据的方法,它可用于(数据)通信协议、数据存储等。,是一种灵活,高效,自动化机制的结构数据序列化方法比XML更小,更快,更为简单。

本项目主要是OpenHarmony系统下以protobuf.js 7.2.4为主要依赖开发,主要接口针对OpenHarmony系统进行合理的适配研发。

下载安装

1.安装

ohpm install @ohos/protobufjs

OpenHarmony ohpm环境配置等更多内容,请参考 如何安装OpenHarmony ohpm包

2.proto文件

按照.proto文件格式定义消息体结构,如:userproto.proto文件。

syntax = "proto3";

package user;
message UserLoginResponse{
   string sessionId = 1;
   string userPrivilege = 2;
   bool isTokenType = 3;
   int64 formatTimestamp = 5;
   bytes data =6;
}

3.生成js和.d.ts文件

全局安装protobufjs
npm install -g protobufjs@7.2.4
全局安装protobufjs-cli
npm install -g protobufjs-cli

在.proto文件目录下执行下列命令
pbjs -t static-module -w es6 -o user.js user.proto
pbts user.js  -o user.d.ts

4.修改生成的文件

1.将生成的js文件中的 import * as $protobuf from "protobufjs/minimal";
修改为  import * as $protobuf from "@ohos/protobufjs";

2.将生成的.d.ts文件中的 import * as $protobuf from "protobufjs";
修改为  import * as $protobuf from "@ohos/protobufjs";

3.在生成的js文件中 import * as $protobuf from "@ohos/protobufjs";这行代码夏敏添加如下代码
import Long from 'long';
$protobuf.util.Long=Long
$protobuf.configure()

5.在entry目录下安装long

ohpm install long

6.将生成js和.d.ts文件复制到工程中

使用说明

1.proto编码

import { user } from './user.js'

 let msg = user.UserLoginResponse.create({
     sessionId: "testSynchronouslyLoadProtoFile",
     userPrivilege: "John123",
     isTokenType: false,
     formatTimestamp: "12342222"
 });

 let arrayBuffer: Uint8Array = user.UserLoginResponse.encode(msg).finish()

2.proto编码

let decodeMsg = user.UserLoginResponse.decode(arrayBuffer);

接口说明

create

create(properties?: { [k: string]: any }): Message<{}>

生成Message对象

参数:

参数名 类型 必填 说明
properties Object 要设置的属性。

返回值:

类型 说明
Message Message实例。

encode

encode(message: (Message<{}>|{ [k: string]: any }), writer?: Writer): Writer

编码消息

参数

参数名 类型 必填 说明
message Message<{}> |  Object Message示例或者普通对象。
writer Writer 编码的写入器。

返回值:

类型 说明
Writer 协议消息体构建器。

decode

decode(reader: (Reader|Uint8Array), length?: number): Message<{}>

解码消息

参数名 类型 必填 说明
reader Reader | Uint8Array 解码的读取器或缓冲区。
length number 长度。

返回值:

类型 说明
Message<{}> 解码的消息。

verify

static verify(message: { [k: string]: any }): (string|null)

验证消息有效性

参数名 类型 必填 说明
message 普通对象 普通对象。

返回值:

类型 说明
string | null 合法返回null,否则返回具体原因。

fromObject

static fromObject(object: { [k: string]: any }): Message<{}>

从纯对象创建此类型的新消息。还将值转换为各自的内部类型

参数:

参数名 类型 必填 说明
object Object 普通对象

返回值:

类型 说明
object 普通对象。

toObject

static toObject(message: Message<{}>, options?: IConversionOptions): { [k: string]: any }

将一个由键及其各自的值组成的数组转换为对象,省略未定义的值

参数名 类型 必填 说明
message Message Message 休想。
options IConversionOptions 转换选项。

返回值:

类型 说明
object 普通对象。

约束与限制

在下述版本验证通过:

  • DevEco Studio 版本:4.1 Canary(4.1.3.317),OpenHarmony SDK:API11 (4.1.0.36)

目录结构

|---- protobuf
|     |---- AppScrope  # 示例代码文件夹
|     |---- entry  # 示例代码文件夹
|     |---- library  # 核心库
|           |---- src/main  # 模块代码
|                |---- ets/   # 模块代码
|                     |---- dist     # 打包文件
|            |---- index.ets          # 入口文件
|            |---- .ohpmignore        # ohpm发布的忽略文件
|            |---- *.json5      # 配置文件
|     |---- README.md  # 安装使用方法
|     |---- README.OpenSource  # 开源说明
|     |---- CHANGELOG.md  # 更新日志

贡献代码

使用过程中发现任何问题都可以提 Issue 给我们,当然,我们也非常欢迎你给我们发 PR

开源协议

本项目基于 BSD License ,请自由地享受和参与开源。

空文件

简介

取消

发行版 (4)

全部

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/openharmony-tpc/protobuf.git
git@gitee.com:openharmony-tpc/protobuf.git
openharmony-tpc
protobuf
protobuf
master

搜索帮助