moonion

    Amazon Ion 1.0 text and binary codec for MoonBit

    ion
    amazon-ion
    codec
    binary
    serialization
    Download zip
    Version
    0.1.2
    License
    Apache-2.0
    Last updated
    19 hours ago
    Downloads
    10

    #MoonIon

    MoonIon 是一个用 MoonBit 编写的 Amazon Ion 1.0 文本/二进制编解码库,面向日志、配置、离线校验,以及需要读写 AWS 风格 Ion payload 的工具。JSON 表达不了 decimal、timestamp、annotation 和 sexp;现有 CBOR、MessagePack、protobuf 包走的是另一套编码,不能直接读 $ion_1_0 文本或带本地符号表的 Ion 二进制。

    MoonIon 把 Ion 1.0 的值模型、文本编码和二进制编码做成纯库。调用方可以在没有 Python / Java 运行时的情况下完成往返,并把字段定义留在自己的业务层。

    #主要能力

    • 文本 load / loads / dumps:null、bool、int、float、decimal、timestamp、symbol、string、clob、blob、list、sexp、struct。
    • 二进制 load_bytes / dumps_bytes:BVM、type nibble、VarUInt / VarInt。
    • annotation 与 $ion_1_0 版本标记。
    • 写出/读入二进制时维护本地 $ion_symbol_table。
    • 十进制系数与指数、带精度的 timestamp、blob/clob。
    • 稳定错误码 ION001–ION013,截断二进制、空文档、非法 timestamp 会返回诊断。
    • 不绑定 HTTP、AWS SDK 或共享符号表网络目录。

    #处理流程

    Ion text ──► 词法 / 容器解析 ──► IonValue Ion binary ─► BVM + type descriptor ─► IonValue │ ▼ dumps / dumps_bytes │ Ion text 或 Ion binary

    解码按相反方向进行。传输层、文件 IO 和云服务客户端都不在本库范围内。

    #环境要求

    • MoonBit 工具链:moonc 0.10.14 或更高版本。
    • 默认后端:wasm-gc;CI 同时检查 wasm-gc、wasm、js 和 native。
    • 仅使用 MoonBit 标准库,核心编解码不依赖外部运行时。

    #快速开始

    调用方包的 moon.pkg:

    import { "ZJH-666-ZJH/moonion" @ion, }

    #1. 解析文本并写成二进制

    ///|
    fn main {
    match @ion.load("Product::{ id: \"sku-42\", count: 3 }") {
    Ok(v) => {
    println(@ion.dumps(v))
    let binary = @ion.dumps_bytes(v)
    match @ion.load_bytes(binary) {
    Ok(again) => println(again.annotations[0])
    Err(e) => println(e.message())
    }
    }
    Err(e) => println(e.message())
    }
    }

    #2. 构造 catalog 样例

    ///|
    fn main {
    let product = @ion.catalog_sample()
    println(@ion.dumps_all([product, @ion.sexp_sample()]))
    }

    #安装

    moon add ZJH-666-ZJH/moonion@0.1.1

    #示例

    moon run examples/catalog_record moon run examples/binary_roundtrip moon run examples/sexp_filter moon run cmd/main

    #当前不做

    • Ion 1.1
    • Ion Schema
    • Ion Hash
    • 共享符号表网络目录
    • AWS 服务客户端

    #许可证

    Apache-2.0。行为参考 ion-python 与 Amazon Ion 1.0 规范;源码为 MoonBit 重写,见 LICENSE 和 THIRD_PARTY.md。

    BigInt

    pub(all) struct BigInt {
    negative : Bool
    digits : Bytes
    } derive(Eq,
    Debug
    )

    BigInt::compare

    fn BigInt::compare(self : BigInt, other : BigInt) -> Int

    BigInt::eq_value

    fn BigInt::eq_value(self : BigInt, other : BigInt) -> Bool

    BigInt::equal

    fn BigInt::equal(BigInt, BigInt) -> Bool

    BigInt::is_zero

    fn BigInt::is_zero(self : BigInt) -> Bool

    BigInt::not_equal

    fn BigInt::not_equal(x : BigInt, y : BigInt) -> Bool

    BigInt::sign

    fn BigInt::sign(self : BigInt) -> Int

    BigInt::to_decimal_string

    fn BigInt::to_decimal_string(self : BigInt) -> String

    BigInt::to_int64

    fn BigInt::to_int64(self : BigInt) -> Int64?

    BigInt::to_repr

    IonDatum

    pub(all) enum IonDatum {
    NullD(IonType)
    BoolD(Bool)
    IntD(BigInt)
    FloatD(Double)
    DecimalD(IonDecimal)
    TimestampD(IonTimestamp)
    SymbolD(String)
    StringD(String)
    ClobD(Bytes)
    BlobD(Bytes)
    ListD(Array[IonValue])
    SexpD(Array[IonValue])
    StructD(Array[IonField])
    } derive(Eq,
    Debug
    )

    IonDatum::equal

    fn IonDatum::equal(IonDatum, IonDatum) -> Bool

    IonDatum::not_equal

    fn IonDatum::not_equal(x : IonDatum, y : IonDatum) -> Bool

    IonDatum::to_repr

    IonDecimal

    pub(all) struct IonDecimal {
    coefficient : BigInt
    exponent : Int
    } derive(Eq,
    Debug
    )

    IonDecimal::equal

    fn IonDecimal::equal(IonDecimal, IonDecimal) -> Bool

    IonDecimal::is_zero

    fn IonDecimal::is_zero(self : IonDecimal) -> Bool

    IonDecimal::not_equal

    fn IonDecimal::not_equal(x : IonDecimal, y : IonDecimal) -> Bool

    IonDecimal::to_ion_text

    fn IonDecimal::to_ion_text(self : IonDecimal) -> String

    IonError

    pub(all) enum IonError {
    UnexpectedEof
    InvalidBinary(String)
    InvalidText(String)
    InvalidUtf8
    InvalidSymbol(String)
    InvalidTimestamp(String)
    InvalidDecimal(String)
    InvalidNumber(String)
    InvalidBlob(String)
    InvalidAnnotation(String)
    InvalidSymbolTable(String)
    Unsupported(String)
    EmptyDocument
    } derive(Eq,
    Debug
    )

    IonError::code

    fn IonError::code(self : IonError) -> String

    IonError::equal

    fn IonError::equal(IonError, IonError) -> Bool

    IonError::message

    fn IonError::message(self : IonError) -> String

    IonError::not_equal

    fn IonError::not_equal(x : IonError, y : IonError) -> Bool

    IonError::to_repr

    IonField

    pub(all) struct IonField {
    name : String
    value : IonValue
    } derive(Eq,
    Debug
    )

    IonField::equal

    fn IonField::equal(IonField, IonField) -> Bool

    IonField::not_equal

    fn IonField::not_equal(x : IonField, y : IonField) -> Bool

    IonField::to_repr

    IonTimestamp

    pub(all) struct IonTimestamp {
    year : Int
    month : Int
    day : Int
    hour : Int
    minute : Int
    second : Int
    fraction : IonDecimal
    offset_minutes : Int
    offset_unknown : Bool
    precision : TimestampPrecision
    } derive(Eq,
    Debug
    )

    IonTimestamp::equal

    IonTimestamp::not_equal

    fn IonTimestamp::not_equal(x : IonTimestamp, y : IonTimestamp) -> Bool

    IonTimestamp::to_ion_text

    fn IonTimestamp::to_ion_text(self : IonTimestamp) -> String

    IonType

    pub(all) enum IonType {
    NullT
    BoolT
    IntT
    FloatT
    DecimalT
    TimestampT
    SymbolT
    StringT
    ClobT
    BlobT
    ListT
    SexpT
    StructT
    } derive(Eq,
    Debug
    )

    IonType::equal

    fn IonType::equal(IonType, IonType) -> Bool

    IonType::name

    fn IonType::name(self : IonType) -> String

    IonType::not_equal

    fn IonType::not_equal(x : IonType, y : IonType) -> Bool

    IonType::to_repr

    IonValue

    pub(all) struct IonValue {
    annotations : Array[String]
    datum : IonDatum
    } derive(Eq,
    Debug
    )

    IonValue::as_bool

    fn IonValue::as_bool(self : IonValue) -> Bool?

    IonValue::as_int

    fn IonValue::as_int(self : IonValue) -> BigInt?

    IonValue::as_list

    fn IonValue::as_list(self : IonValue) -> Array[IonValue]?

    IonValue::as_string

    fn IonValue::as_string(self : IonValue) -> String?

    IonValue::as_struct

    fn IonValue::as_struct(self : IonValue) -> Array[IonField]?

    IonValue::as_symbol

    fn IonValue::as_symbol(self : IonValue) -> String?

    IonValue::equal

    fn IonValue::equal(IonValue, IonValue) -> Bool

    IonValue::field

    fn IonValue::field(self : IonValue, name : String) -> IonValue?

    IonValue::ion_type

    fn IonValue::ion_type(self : IonValue) -> IonType

    IonValue::is_null

    fn IonValue::is_null(self : IonValue) -> Bool

    IonValue::is_symbol_table

    fn IonValue::is_symbol_table(self : IonValue) -> Bool

    IonValue::not_equal

    fn IonValue::not_equal(x : IonValue, y : IonValue) -> Bool

    IonValue::to_repr

    IonValue::with_annotation

    fn IonValue::with_annotation(self : IonValue, name : String) -> IonValue

    SymbolTable

    pub(all) struct SymbolTable {
    locals : Array[String]
    }

    SymbolTable::define_local

    fn SymbolTable::define_local(self : SymbolTable, name : String) -> Unit

    SymbolTable::intern

    fn SymbolTable::intern(self : SymbolTable, name : String) -> Int

    SymbolTable::local_names

    fn SymbolTable::local_names(self : SymbolTable) -> Array[String]

    SymbolTable::name_of

    fn SymbolTable::name_of(self : SymbolTable, id : Int) -> Result[String, IonError]

    SymbolTable::reset_locals

    fn SymbolTable::reset_locals(self : SymbolTable) -> Unit

    TimestampPrecision

    pub(all) enum TimestampPrecision {
    Year
    Month
    Day
    Minute
    Second
    Fractional
    } derive(Eq,
    Debug
    )

    TimestampPrecision::equal

    TimestampPrecision::not_equal

    ION_VERSION

    let ION_VERSION : String

    bigint_from_decimal_string

    fn bigint_from_decimal_string(text : String) -> Result[BigInt, IonError]

    bigint_from_int

    fn bigint_from_int(value : Int) -> BigInt

    bigint_from_int64

    fn bigint_from_int64(value : Int64) -> BigInt

    bigint_from_magnitude

    fn bigint_from_magnitude(negative : Bool, mag : Bytes) -> BigInt

    bigint_from_radix_string

    fn bigint_from_radix_string(text : String, radix : Int) -> Result[BigInt, IonError]

    bigint_zero

    fn bigint_zero() -> BigInt

    binary_to_text

    fn binary_to_text(data : Bytes) -> Result[String, IonError]

    catalog_sample

    fn catalog_sample() -> IonValue

    decimal_from_int

    fn decimal_from_int(value : Int) -> IonDecimal

    decimal_from_parts

    fn decimal_from_parts(coefficient : BigInt, exponent : Int) -> IonDecimal

    dumps

    fn dumps(value : IonValue) -> String

    dumps_all

    fn dumps_all(values : Array[IonValue]) -> String

    dumps_binary

    fn dumps_binary(value : IonValue) -> Bytes

    dumps_binary_all

    fn dumps_binary_all(values : Array[IonValue]) -> Bytes

    dumps_bytes

    fn dumps_bytes(value : IonValue) -> Bytes

    dumps_bytes_all

    fn dumps_bytes_all(values : Array[IonValue]) -> Bytes

    dumps_text

    fn dumps_text(value : IonValue) -> String

    dumps_text_all

    fn dumps_text_all(values : Array[IonValue]) -> String

    f64_from_be4

    fn f64_from_be4(b0 : Int, b1 : Int, b2 : Int, b3 : Int) -> Double

    f64_from_be_bytes

    fn f64_from_be_bytes(b0 : Int, b1 : Int, b2 : Int, b3 : Int, b4 : Int, b5 : Int, b6 : Int, b7 : Int) -> Double

    f64_to_be_bytes

    fn f64_to_be_bytes(value : Double) -> Bytes

    hex_decode

    fn hex_decode(text : String) -> Result[Bytes, IonError]

    hex_encode

    fn hex_encode(data : Bytes, upper? : Bool) -> String

    ion_annotated

    fn ion_annotated(annotations : Array[String], datum : IonDatum) -> IonValue

    ion_bigint

    fn ion_bigint(value : BigInt) -> IonValue

    ion_blob

    fn ion_blob(value : Bytes) -> IonValue

    ion_bool

    fn ion_bool(value : Bool) -> IonValue

    ion_clob

    fn ion_clob(value : Bytes) -> IonValue

    ion_decimal

    fn ion_decimal(value : IonDecimal) -> IonValue

    ion_field

    fn ion_field(name : String, value : IonValue) -> IonField

    ion_float

    fn ion_float(value : Double) -> IonValue

    ion_int

    fn ion_int(value : Int) -> IonValue

    ion_int64

    fn ion_int64(value : Int64) -> IonValue

    ion_list

    fn ion_list(values : Array[IonValue]) -> IonValue

    ion_null

    fn ion_null(typ : IonType) -> IonValue

    ion_sexp

    fn ion_sexp(values : Array[IonValue]) -> IonValue

    ion_string

    fn ion_string(value : String) -> IonValue

    ion_struct

    fn ion_struct(fields : Array[IonField]) -> IonValue

    ion_symbol

    fn ion_symbol(value : String) -> IonValue

    ion_timestamp

    fn ion_timestamp(value : IonTimestamp) -> IonValue

    ion_value

    fn ion_value(datum : IonDatum) -> IonValue

    ion_version

    fn ion_version() -> String

    load

    fn load(input : String) -> Result[IonValue, IonError]

    load_binary

    fn load_binary(data : Bytes) -> Result[IonValue, IonError]

    load_bytes

    fn load_bytes(data : Bytes) -> Result[IonValue, IonError]

    load_text

    fn load_text(text : String) -> Result[IonValue, IonError]

    loads

    fn loads(input : String) -> Result[Array[IonValue], IonError]

    loads_binary

    fn loads_binary(data : Bytes) -> Result[Array[IonValue], IonError]

    loads_bytes

    fn loads_bytes(data : Bytes) -> Result[Array[IonValue], IonError]

    loads_text

    fn loads_text(text : String) -> Result[Array[IonValue], IonError]

    needs_quoting

    fn needs_quoting(name : String) -> Bool

    parse_decimal_text

    fn parse_decimal_text(text : String) -> Result[IonDecimal, IonError]

    parse_timestamp_text

    fn parse_timestamp_text(text : String) -> Result[IonTimestamp, IonError]

    quote_symbol

    fn quote_symbol(name : String) -> String

    roundtrip_binary

    fn roundtrip_binary(data : Bytes) -> Result[Bytes, IonError]

    roundtrip_text

    fn roundtrip_text(text : String) -> Result[String, IonError]

    sexp_sample

    fn sexp_sample() -> IonValue

    symbol_table_new

    fn symbol_table_new() -> SymbolTable

    system_max_id

    fn system_max_id() -> Int

    system_symbol_id

    fn system_symbol_id(name : String) -> Int?

    system_symbol_name

    fn system_symbol_name(id : Int) -> String?

    text_to_binary

    fn text_to_binary(text : String) -> Result[Bytes, IonError]

    timestamp_of

    fn timestamp_of(year : Int, month : Int, day : Int, hour : Int, minute : Int, second : Int, fraction : IonDecimal, offset_minutes : Int, offset_unknown : Bool, precision : TimestampPrecision) -> Result[IonTimestamp, IonError]

    timestamp_year

    fn timestamp_year(year : Int) -> Result[IonTimestamp, IonError]

    values_equal

    fn values_equal(a : IonValue, b : IonValue) -> Bool