cbor4cj:基于RFC 7049的Cbor数据编解码与流式解码实现

基于RFC 7049协议的简明二进制对象表示法(Cbor)的Cangjie语言实现

分支6Tags3
文件最后提交记录最后更新时间
1 年前
1 年前
1 年前
1 年前
1 年前
11 个月前
1 年前
1 年前
11 个月前
11 个月前

cbor4cj

简介

基于RFC 7049标准的简明二进制对象表示法(Cbor)的仓颉语言实现方案

核心功能

  • 🚀 实现Cbor数据编码与解码
  • 🚀 支持Cbor数据流式解析
.
├── doc
├── src
└── test
│   ├── DOC
│   ├── HLT
│   └── LLT
├── CHANGELOG.md
├── LICENSE.txt
├── cjpm.toml
├── README.md
└── README.OpenSource
  • doc Documentation directory for storing API interface documents
  • src Source code directory for the library
  • test Contains 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.

项目介绍

基于RFC 7049协议的简明二进制对象表示法(Cbor)的Cangjie语言实现

定制我的领域