用户可借助该项目实现数据的序列化与反序列化,将丰富数据结构转化为便于存储或传输的二进制格式。它适配 OpenHarmony,支持多种数据操作,还可作为容器文件存储持久数据。【此简介由AI生成】
Access rate limit exceeded. Please log in to continue.
avro-js
Overview
This project is modified based on the open-source software Apache Avro to adapt to OpenHarmony. It supports various data structures and converts them into binary data formats that are easy to store or transmit. It can also serve as a container to store persistent data.
Effect:

How to Install
ohpm install @ohos/avro-js
For details, see Installing an OpenHarmony HAR.
How to Use
Sample Code
- Introduce avro to the page.
import avro from '@ohos/avro-js'
- Use avro to implement data serialization.
let mapType: ESObject = avro.parse({ type: 'map', values: 'long' })
let petMap: petMapType = { key1: 12343, key2: 3445 };
let bufMap: ESObject = mapType.toBuffer(petMap); // Buffer containing 'Hi''s Avro encoding.
let mapSrc: ESObject = mapType.fromBuffer(bufMap); // === 'Hi'
let bufferStr = "";
for (let i = 0; i < bufMap.length; i++) {
bufferStr = bufferStr + bufMap[i];
}
this.mapTestResult = "MapType:{original data: " + JSON.stringify(petMap) +", serialization: " + bufferStr +", Restored data: " + JSON.stringify(mapSrc) + "};";
console.info("----MapType--{original_data:" + JSON.stringify(petMap) + ",serialization: " + bufferStr + ",restore_data: " + JSON.stringify(mapSrc) + "}");
Available APIs
| API | Parameter | Description |
|---|---|---|
| avro.parse(schema:Object, opts? :Object) | schema:Avro schema definition opts: Parse option object |
Parses the schema and returns the corresponding instance. |
| type.toBuffer(val:Object) | value: Data objects that conform to the pattern | Encodes the given value into a binary buffer. |
| type.fromBuffer(buf:Buffer, resolver?:function) | buffer: Binary data encoded in Avro resolver: Type resolution callback (for associative types or dynamic patterns) |
Creates a new instance from the binary buffer. |
| type.decode(buf:Buffer, pos?:number,resolver?:function | buf: Binary data buffer encoded by Avro pos: Decoding start position resolver: Callback function |
Decodes the data in the binary buffer to the specified position and parser. |
| type.encode(val:any, (buf:Buffer,pos:number) | val: Encoded JavaScript object buf: Target buffer pos: Write start position |
Encodes the given value into a binary buffer. |
| type.fromString(str:string) | str:string | Creates an instance from a string. |
| type.toString(val? :any) | val: JavaScript object to be formatted | Converts the given value to a string. |
| type.isValid(val:any, opts? :Object) | val: Data to be verified opts: Verification option object |
Checks whether the given value is valid. |
| type.clone(val:any, opts? :Object) | val: Data object to be cloned opts: Validation option object |
Creates a deep copy of a given value. |
| type.compare(val1:Object, val2:Object) | val1: Data object to be compared val2: Data object to be compared |
Compares two values. |
| type.compareBuffers(buf1:Buffer, buf2:Buffer | buf1: Buffer to be compared buf2:Buffer to be compared |
Compares the sizes of two binary buffers. |
| type.random() | - | Generates a new random value. |
| type.getName() | - | Obtains the type name. |
| type.getSchema() | - | Obtains the type schema. |
| type.getItemsType() | - | Obtains the array or mapping type. |
| type.getAliases() | - | Obtains the type alias. |
| type.getSize() | - | Obtains the type size. |
| type.getValuesType() | - | Obtains values of the enum or fixed type. |
About obfuscation
- Code obfuscation, please seeCode Obfuscation
- If you want the avro-js library not to be obfuscated during code obfuscation, you need to add corresponding exclusion rules in the obfuscation rule configuration file obfuscation-rules.txt:
-keep
./oh_modules/@ohos/avro-js
Constraints
avro has been verified in the following version:
- DevEco Studio: 4.1 Canary (4.1.3.317), OpenHarmony SDK: API11 (4.1.0.36)
Directory Structure
|---- avro
| |---- entry # Sample code
| |---- library # avro library
| |---- README_EN.md # Readme
How to Contribute
If you find any problem when using avro, submit an Issue or a PR.
License
This project is licensed under Apache License 2.0.
Introduction
用户可借助该项目实现数据的序列化与反序列化,将丰富数据结构转化为便于存储或传输的二进制格式。它适配 OpenHarmony,支持多种数据操作,还可作为容器文件存储持久数据。【此简介由AI生成】
Customize your domain