目录

MoonBit CBOR (RFC 8949)

License: Apache 2.0 MoonBit Version

A CBOR (Concise Binary Object Representation, RFC 8949) serialization and deserialization library written in pure MoonBit.

This project was built for the CCF x MoonBit Open Source Competition (2026).

Core Features

  • RFC 8949 data model coverage for integers, bytes, text, arrays, maps, tags, simple values, and floating-point decoding.
  • CborDecode conversions for CborValue, Bool, Int64, UInt64, String, Bytes, and Double via decode_as.
  • Floating-point decoding across half, single, and double precision inputs.
  • Indefinite-length decoding for byte strings, text strings, arrays, and maps.
  • Deterministic map encoding with canonical key ordering.
  • Decoder validation for integer bounds, trailing bytes, malformed simple values, strict UTF-8, and non-canonical integer and length encodings.
  • Human-readable diagnostic notation output for debugging and examples.

Installation

Add the package with the current MoonBit workflow:

moon add 2515050242/cbor

Or add it manually to moon.pkg:

import = [
  "2515050242/cbor",
]

Usage Example

import "2515050242/cbor"

fn main {
  let value = cbor.Map([
    (cbor.Text("hello"), cbor.Integer(42L)),
    (cbor.Text("world"), cbor.Array([cbor.Integer(1L), cbor.Integer(2L)])),
  ])

  let bytes = cbor.encode(value)
  println("Encoded \{bytes.length()} bytes.")

  let decoded = cbor.decode(bytes) catch {
    err => fail("Failed to decode: \{err}")
  }
  println("Decoded Diagnostic:")
  println(cbor.diagnostic(decoded))

  let count : Int64 = cbor.decode_as(b"\x18\x2a") catch {
    err => fail("Failed to decode Int64: \{err}")
  }
  println("Decoded integer: \{count}")
}

CLI Example

The repository includes a runnable CLI in cmd/main:

moon run ./cmd/main
moon run ./cmd/main decode-hex a26161016162820203
moon run ./cmd/main roundtrip-hex 5f42010243030405ff
moon run ./cmd/main benchmark 20000

Performance Notes

  • The encoder is buffer-backed and streaming-oriented for integers, byte strings, arrays, and maps.
  • The current implementation is not globally zero-allocation: UTF-8 text encoding and canonical map-key sorting still use temporary buffers.
  • The repository documentation has been aligned to this implementation boundary so reviewer expectations match real behavior.

Validation

Local acceptance loop:

moon fmt
git diff --exit-code
moon info
git diff --exit-code
moon check
moon test

Windows PowerShell helper:

powershell -ExecutionPolicy Bypass -File .\scripts\verify_acceptance.ps1

Repository self-check report:

python ./scripts/check_repo_compliance.py

Competition Notes

  • The repository is public and keeps README, license, CI, tests, and MoonBit-first source visible for OSC2026 review.
  • CI runs formatting, moon info, moon check --target all, and moon test --target all on Linux, macOS, and Windows.
  • The test suite includes RFC 8949 Appendix A vectors plus malformed-input regressions for reviewer verification.
  • Source attribution and acceptance notes are kept in docs/source-attribution.md and docs/acceptance-checklist.md.

License

This project is licensed under Apache-2.0. See LICENSE for details.

关于

本项目将从零开始在 MoonBit 中实现一个符合 RFC 8949 标准的高性能 CBOR (Concise Binary Object Representation) 序列化与反序列化库。 CBOR 是一种二进制数据序列化格式,旨在保持较小消息体积的同时,具备极低的代码复杂度与高扩展性(无需进行版本协商)。它在 WebAssembly (Wasm) 生态、物联网、边缘计算以及网络协议(如 C

102.0 KB
邀请码
    Gitlink(确实开源)
  • 加入我们
  • 官网邮箱:gitlink@ccf.org.cn
  • QQ群
  • QQ群
  • 公众号
  • 公众号

版权所有:中国计算机学会技术支持:开源发展技术委员会
京ICP备13000930号-9 京公网安备 11010802047560号