基于RFC 7049协议的简明二进制对象表示法(Cbor)的Cangjie语言实现
| 文件 | 最后提交记录 | 最后更新时间 |
|---|---|---|
| 1 年前 | ||
| 1 年前 | ||
| 1 年前 | ||
| 1 年前 | ||
| 1 年前 | ||
| 11 个月前 | ||
| 1 年前 | ||
| 1 年前 | ||
| 11 个月前 | ||
| 11 个月前 |
以下内容由 AI 翻译,如有问题请 点此提交 issue 反馈
简介
基于RFC 7049标准的简明二进制对象表示法(Cbor)的仓颉语言实现方案
核心功能
- 🚀 实现Cbor数据编码与解码
- 🚀 支持Cbor数据流式解析
.
├── doc
├── src
└── test
│ ├── DOC
│ ├── HLT
│ └── LLT
├── CHANGELOG.md
├── LICENSE.txt
├── cjpm.toml
├── README.md
└── README.OpenSource
docDocumentation directory for storing API interface documentssrcSource code directory for the librarytestContains HLT test cases, LLT self-test cases, and documentation example cases
Interface Description
For details on main classes and function interfaces, please refer to API
Usage Instructions
Compilation (Windows/Linux)
cjpm build
Feature Examples
Coding
Sample code is as follows:
import std.unittest.*
import std.unittest.testmacro.*
import std.io.*
import cbor4cj.*
@Test
public class ReadMeTest {
@TestCase
public func Example01Test() {
var except: Array<Byte> = [100, 116, 101, 120, 116, 25, 4, 210, 65, 16, 130, 1, 100, 116, 101, 120, 116]
let baos = ByteArrayStream();
CborEncoder(baos).encode(
CborBuilder().add("text") // add string
.add(1234) // add int
.add([0x10]) // add byte array
.addArray() // add array
.add(1).add("text").end().getOrThrow().build());
var encodedBytes = baos.bytes();
println(encodedBytes)
@Assert(encodedBytes,except)
}
}
The execution results are as follows:
[ PASSED ] CASE: Example01Test
Decoding
Sample code is as follows:
import std.unittest.*
import std.unittest.testmacro.*
import std.io.*
import std.collection.*
import std.math.numeric.BigInt
import cbor4cj.*
@Test
public class ReadMeTest2 {
@TestCase
public func Example02Test() {
var except: Array<Byte> = [100, 116, 101, 120, 116, 25, 4, 210, 65, 16, 130, 1, 100, 116, 101, 120, 116]
let baos = ByteArrayStream();
baos.write(except)
var dataItems: LinkedList<DataItem> = CborDecoder(baos).decode();
@Assert(dataItems.size,4)
var iter = dataItems.iterator()
var value1 = iter.next().getOrThrow()
@Assert((value1 as UnicodeString).getOrThrow().toString(), "text")
var value2 = iter.next().getOrThrow()
@Assert((value2 as UnsignedInteger).getOrThrow().getValue(), BigInt(1234))
var value3 = iter.next().getOrThrow()
@Assert((value3 as ByteString).getOrThrow().getBytes(), Array<Byte>([0x10]))
var value4 = iter.next().getOrThrow()
var items = (value4 as CborArray).getOrThrow().getDataItems()
@Assert(items.size, 2)
@Assert((items[0] as UnsignedInteger).getOrThrow(), UnsignedInteger(BigInt(1)))
@Assert((items[1] as UnicodeString).getOrThrow().toString(), "text")
}
}
The execution results are as follows:
[ PASSED ] CASE: Example02Test
Streaming Decoding
Sample code is as follows:
import std.unittest.*
import std.unittest.testmacro.*
import std.io.*
import cbor4cj.*
import std.math.numeric.BigInt
@Test
public class ReadMeTest3 {
@TestCase
public func Example03Test() {
let baos = ByteArrayStream();
CborEncoder(baos).encode(CborBuilder().add(666).add("666").build());
CborDecoder(baos).decode(impleDataItemListener())
}
}
class impleDataItemListener <: DataItemListener {
public func onDataItem(dataItem: DataItem) {
if (dataItem is UnsignedInteger) {
let value = (dataItem as UnsignedInteger).getOrThrow().getValue()
@Assert(value, BigInt(666))
return
}
if (dataItem is UnicodeString) {
let value = (dataItem as UnicodeString).getOrThrow().toString()
@Assert(value, "666")
return
}
@Assert(false)
}
}
The execution results are as follows:
[ PASSED ] CASE: Example03Test
Constraints and Limitations
Verified on the following versions:
Cangjie Version: 1.0.0
Open Source License
This project is licensed under the Apache License 2.0. Feel free to enjoy and contribute to open source.
Contributing
We welcome your Pull Requests, Issue submissions, and any form of contribution.