目录

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 signed/unsigned 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.
  • Full-width unsigned values are preserved as CborValue::Unsigned(UInt64) instead of being narrowed through Int64.
  • 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.
  • Versioned application message envelopes for requests, responses, events, and commands.
  • Batch message encoding with producer/sequence metadata for queue and log transports.
  • Nested path queries, immutable document patches, object projection, and field merging.
  • Length-delimited streaming frames with partial-chunk decoding and bounded admission control.
  • Composable application validation reports, lifecycle message storage, retry/dead-letter state, document metrics, and recursive sensitive-field redaction.
  • Bounded application ingress policies that aggregate byte/node/depth limits across documents, messages, batches, and framed streams with indexed rejection reports.

Installation

The project is verified with the MoonBit 0.10.x toolchain. CI installs the current official toolchain on every supported runner; the local acceptance environment currently uses moonc v0.10.7.

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
moon run ./cmd/main -- application-demo

The benchmark command reports four reproducible workloads: a small map, UTF-8 text containing a non-BMP scalar, a 256-byte binary payload, and a nested array/map value. Each line includes the iteration count, encoded payload size, and elapsed time.

The application demo exercises a versioned request/response batch, a framed transport buffer, nested field queries, validation reports, recursive redaction, and bounded-ingress accounting in one reproducible command.

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 --check
moon info
moon check --target wasm-gc --deny-warn
moon test --target wasm-gc --deny-warn

CI additionally runs moon check --target all --deny-warn and moon test --target all --deny-warn on Linux, macOS, and Windows, and checks that formatting and generated interface steps leave a clean checkout.

Windows PowerShell helper:

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

Repository self-check report:

python ./scripts/check_repo_compliance.py

The self-check reports both physical MoonBit source lines and effective production source lines (non-empty, non-comment .mbt lines excluding tests). The local production-source gate is 3300 lines so that the project scope is backed by reusable implementation rather than documentation or generated files.

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, warning-free checks/tests for all backends, and CLI smoke tests on Linux, macOS, and Windows.
  • The test suite includes RFC 8949 Appendix A vectors plus malformed-input regressions for reviewer verification.
  • The application suite includes 52 deterministic tests covering policy limits, batch ingress, frame recovery, lifecycle transitions, redaction, and aggregate budget accounting.
  • 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

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

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