jsonld

    JSON-LD 1.1 (expand / compact / flatten / frame / toRDF / fromRDF) + RDFC-1.0 canonicalization for MoonBit.

    jsonld
    json-ld
    rdf
    linked-data
    canonicalization
    Download zip
    Author
    Version
    0.1.3
    License
    Apache-2.0
    Last updated
    5 days ago
    Downloads
    48

    #MoonJSONLD

    thy1016/jsonld 是一个用 MoonBit 编写的 JSON-LD 1.1 通用库:实现 expand / compact / flatten / frame / toRDF / fromRDF 六个算法,另含 RDFC-1.0 数据集规范化、JCS(RFC 8785)规范化 JSON 序列化,以及 HTML application/ld+json 脚本抽取。

    #特性

    • JSON-LD 1.1 六算法:processing_mode 支持 1.0 / 1.1 双档;flatten 的 上下文为可选参数,展平后可直接压缩。
    • RDFC-1.0 规范化:W3C RDF Dataset Canonicalization,SHA-256 / SHA-384 双哈希档,返回规范化形与空白节点哈希表。
    • JCS(RFC 8785):jcs_serialize 按 JSON 键码点序、RFC 8785 §3.2.2.2 转义规则与 ECMAScript 数值格式输出规范化 JSON。
    • 自带 JSON 解析器:parse_json / JsonValue AST;库本身仅依赖 MoonBit 标准库(moonbitlang/async 只用于测试与命令行工具)。
    • 强类型错误面:JsonLdError 九变体,可按变体 match 处理。

    #安装

    确保环境中已安装 MoonBit 工具链,然后在模块根目录:

    moon add thy1016/jsonld

    模块源码正式地址:gitlink https://gitlink.org.cn/thy7/mbt.jsonld 与 GitHub 镜像 https://github.com/72-j/mbt.jsonld;工作区多模块场景可 直接以本地树路径引用。

    再在用到它的包的 moon.pkg import 块中添加 "thy1016/jsonld", 代码内以 @jsonld 前缀使用。

    #快速上手

    JsonLdOptions 需全字段显式构造:

    let opts = @jsonld.JsonLdOptions::{
    base: None,
    expand_context: None,
    processing_mode: @jsonld.ProcessingMode::JsonLd11,
    load_context: None,
    rdf_direction: None,
    produce_generalized_rdf: false,
    compact_arrays: true,
    compact_to_relative: true,
    frame_expansion: false,
    omit_graph: false,
    use_native_types: false,
    use_rdf_type: false,
    }

    #展开(expand)

    let doc = "{\"@context\": {\"name\": \"http://schema.org/name\"}, \"name\": \"Ada\"}"
    match @jsonld.expand_document(doc, opts) {
    Ok(expanded) => println(@jsonld.render_json(expanded))
    Err(err) => println("expand failed: \{err}")
    }

    #压缩(compact)

    let context = "{\"name\": \"http://schema.org/name\"}"
    match @jsonld.compact_document(doc, context, opts) {
    Ok(compacted) => println(@jsonld.render_json(compacted))
    Err(err) => println("compact failed: \{err}")
    }

    #toRDF / fromRDF

    to_rdf_document 产出 JsonLdQuad 数组(术语为 N-Quads 形字符串); from_rdf_document 的输入为 N-Quads 文本。

    match @jsonld.to_rdf_document(doc, opts) {
    Ok(quads) => println("quads: \{quads.length()}")
    Err(err) => println("toRDF failed: \{err}")
    }

    let nq = "<http://example.org/s> <http://example.org/p> \"v\" .\n"
    match @jsonld.from_rdf_document(nq, opts) {
    Ok(node_object) => println(@jsonld.render_json(node_object))
    Err(err) => println("fromRDF failed: \{err}")
    }

    #RDFC-1.0 规范化

    match @jsonld.rdfc10_canonicalize_with_hash(nq, @jsonld.C14nHashAlgorithm::Sha384) {
    Ok((canonical, hashes)) => {
    for id, hash in hashes {
    println("\{id} => \{hash}")
    }
    }
    Err(err) => println("canonicalize failed: \{err}")
    }

    #JCS 规范化 JSON

    match @jsonld.parse_json("{\"b\": 1.0, \"a\": 2}") {
    Ok(parsed) => println(@jsonld.jcs_serialize(parsed))
    Err(err) => println("parse failed: \{err}")
    }

    #远程上下文注入

    库内不做网络 I/O;远程上下文经 load_context 回调由调用方注入:

    // ((String) -> Result[String, String])?
    let loader = url => fetch_document(url)
    // 构造 options 时:load_context: Some(loader)

    #API 总览

    入口作用
    parse_json自带 JSON 解析(JsonValue AST)
    expand_document展开:IRI 全解析 + 词表归一
    compact_document压缩(文档 + 上下文 + 选项)
    flatten_document展平(上下文可选参)
    frame_document帧(frame)
    to_rdf_documentJSON-LD → RDF 四元组(JsonLdQuad)
    from_rdf_documentN-Quads 文本 → JSON-LD 节点对象
    html_script_sourceHTML <script type="application/ld+json"> 抽取
    rdfc10_canonicalize_with_hashRDFC-1.0 规范化(Sha256 / Sha384)
    jcs_serializeJCS(RFC 8785)规范化序列化直出
    json_canonical / render_jsonJSON 归一化(比对用途,非 JCS)/ JSON 序列化
    canonical_for_suite / isomorphic_for_suite套件比对辅助(规范化形 / 数据集同构)
    jsonld_iri_routes / jsonld_keyword_routesIRI / 关键字词表路由自省

    #错误面

    JsonLdError 九变体:Syntax / InvalidContext / InvalidIri / InvalidValue / InvalidList / Unsupported / ContextOverflow / LoadingDocumentFailed / InvalidScriptElement。

    #设计边界

    • 远程文档抓取:库内不做网络 I/O,远程上下文经 load_context 回调由调用方 注入;W3C remote-doc 套件用例不纳入测试范围。

    定位与边界的完整说明见 spec.md §9。

    #命令行工具

    可运行示例:examples/quickstart(六个算法与 JCS 的完整走览),运行 moon run examples/quickstart。

    库随附命令行工具(不做网络访问,输入与上下文均读本地文件):

    moon run cmd/main -- <command> [options] <inputs>

    八个命令:expand / compact / flatten / frame / to-rdf / from-rdf / canonicalize / json-canonical;选项 --base <iri>、--mode <1.0|1.1>、 --hash <sha256|sha384>。错误信息输出到 stderr,数据输出到 stdout。

    # 例:展开并直出 N-Quads moon run cmd/main -- to-rdf doc.jsonld # 例:RDFC-1.0 规范化(sha384 档) moon run cmd/main -- canonicalize --hash sha384 dataset.nq

    #测试与验证

    moon test

    运行时输出各测试面的计数行(下表实测于 2026-10-30):

    面entriesplaindeferred
    expand3853850
    toRdf4674670
    compact2462451(#t0038 版次越界超面,有意保留)
    frame92920
    canon(RDFC-1.0)86860
    flatten58580
    fromRdf54540
    html50500
    合计143814371

    测试语料为 W3C json-ld-api / json-ld-framing / rdf-canon 三套件,存于 .rdf-tests/,由 SHA256SUMS 锁定版本。

    #quicktest 数据集守恒机

    quicktest/ 内置随机状态机测试件(pbtgen QuickMachine 脚手架生成),以数据集 守恒律为被测性质:

    • 字面集守恒:to_rdf(from_rdf(台账)) 输出的 quad 键集与台账键集等值 (空白节点标签在键面规约 _:* → _:B);
    • 图同构守恒:两轮 from_rdf 产物经 isomorphic_for_suite 判定同构, 空白节点结构身份保真。

    状态机 Empty —Seed→ Ledgered,环上 AppendQuad(载荷选取六条 quad 池:IRI 宾词、朴素 / 语变 / 型标字面量、空白节点主词与宾词)与 Roundtrip;台账为 集合语义,重复追加在生成期即被过滤。随机 60 例 + 固定脚本回归基线 + 覆盖门, native 与 wasm 读数一致(quicktest 13 例,计入全仓测试总数)。

    moon test -p thy1016/jsonld/quicktest

    版本沿革见 CHANGELOG.md。

    #许可证

    • 本项目代码采用 Apache License 2.0(见 LICENSE)。
    • .rdf-tests/ 内的 W3C 测试文件按 W3C 双许可(W3C 3-clause BSD License 与 W3C Test Suite License,任选其一)分发,原始文件逐字节未修改;该目录随 仓库保存,不进入发布包。
    • 无背书声明:上文套件通过计数是本项目自测结果,只表示本实现能通过相应 用例,不代表 W3C 认证、合规认定或任何形式的背书。

    ContextProcessor

    pub(open) trait ContextProcessor {
    fn process_context(Self, active : ActiveContext, local_context : JsonValue, flags : ContextFlags, options : JsonLdOptions) -> Result[ActiveContext, JsonLdError]
    }

    Context 处理接口 —— 手写实现(递归策略不 gen)

    Expander

    pub(open) trait Expander {
    fn expand_element(Self, active : ActiveContext, element : JsonValue, active_property : String?, options : JsonLdOptions) -> Result[ExpandedValue, JsonLdError]
    fn expand_value(Self, active : ActiveContext, value : JsonValue, active_property : String?) -> Result[ExpandedValue, JsonLdError]
    fn expand_array(Self, active : ActiveContext, items : Array[JsonValue], active_property : String?, options : JsonLdOptions) -> Result[ExpandedValue, JsonLdError]
    fn expand_term(Self, active : ActiveContext, term : String, value : JsonValue, active_property : String?, options : JsonLdOptions) -> Result[Array[ExpandedValue], JsonLdError]
    fn postprocess_value(Self, active : ActiveContext, value : ExpandedValue) -> Result[ExpandedValue, JsonLdError]
    fn set_language(Self, active : ActiveContext, value : JsonValue) -> Result[ExpandedValue, JsonLdError]
    fn set_direction(Self, active : ActiveContext, value : JsonValue) -> Result[ExpandedValue, JsonLdError]
    fn expand_list(Self, active : ActiveContext, value : JsonValue, active_property : String?, options : JsonLdOptions) -> Result[ExpandedValue, JsonLdError]
    fn expand_set(Self, active : ActiveContext, value : JsonValue, active_property : String?, options : JsonLdOptions) -> Result[ExpandedValue, JsonLdError]
    fn expand_graph(Self, active : ActiveContext, value : JsonValue, active_property : String?, options : JsonLdOptions) -> Result[ExpandedValue, JsonLdError]
    fn expand_reverse(Self, active : ActiveContext, value : JsonValue, active_property : String?, options : JsonLdOptions) -> Result[ExpandedValue, JsonLdError]
    fn expand_nest(Self, active : ActiveContext, value : JsonValue, active_property : String?, options : JsonLdOptions) -> Result[ExpandedValue, JsonLdError]
    fn expand_included(Self, active : ActiveContext, value : JsonValue, active_property : String?, options : JsonLdOptions) -> Result[ExpandedValue, JsonLdError]
    fn set_index(Self, active : ActiveContext, value : JsonValue) -> Result[ExpandedValue, JsonLdError]
    }

    Expansion 接口 —— 手写实现(树递归) 契约面与 jsonld_gen.toml 的 handler_hook 一一对应(接口一致门 interface_gate_test.mbt 校验住:trait 方法改名 ⇒ 门红)。

    IriExpander

    pub(open) trait IriExpander {
    fn expand_iri(Self, active : ActiveContext, value : String, document_relative : Bool, vocab : Bool) -> Result[String, JsonLdError]
    fn handle_keyword(Self, value : String) -> Result[String, JsonLdError]
    fn expand_compact(Self, active : ActiveContext, value : String, document_relative : Bool, vocab : Bool) -> Result[String, JsonLdError]
    fn concat_vocab(Self, active : ActiveContext, value : String) -> Result[String, JsonLdError]
    fn resolve_base(Self, active : ActiveContext, value : String) -> Result[String, JsonLdError]
    fn lookup_term(Self, active : ActiveContext, value : String) -> Result[String, JsonLdError]
    }

    IRI 展开接口 契约面与 jsonld_gen.toml iri_rules / keyword_dispatch 的 handler 一一对应 (接口一致门校验住:方法改名 ⇒ 门红)。

    ToRdfProcessor

    pub(open) trait ToRdfProcessor {
    fn node_to_quads(Self, expanded : Array[ExpandedValue], options : JsonLdOptions) -> Result[Array[JsonLdQuad], JsonLdError]
    }

    toRDF 处理接口 —— 手写实现 契约面与 jsonld_gen.toml 的 handler_hook 对应(接口一致门校验住)。

    ActiveContext

    pub(all) struct ActiveContext {
    base : String?
    vocab : String?
    terms : Map[String, TermDefinition]
    previous : PreviousContext?
    default_language : String?
    default_direction : String?
    compact_by_iri : Map[String, Array[String]]?
    compact_reverse : Array[(String, String)]?
    compact_prefixes : Array[(String, String)]?
    }

    活动 context:term 定义 + 作用域链 作用域是树递归的关键——子节点继承父节点 context

    C14nHashAlgorithm

    pub(all) enum C14nHashAlgorithm {
    Sha256
    Sha384
    } derive(Eq)

    Hash algorithm option (REC section 3.1 hashAlgorithm; suite pins the SHA384 variant on test075). Default SHA256.

    C14nHashAlgorithm::equal

    [0079] 消警:Eq 派生的 equal/not_equal 显式升格(const §5——derive 须同笔补 extend)

    C14nHashAlgorithm::not_equal

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

    [0079] 消警:Eq 派生的 equal/not_equal 显式升格(const §5——derive 须同笔补 extend)

    ContainerType

    pub(all) enum ContainerType {
    List
    Set
    Language
    Index
    Id
    Type
    Graph
    }

    container 类型

    ContextFlags

    pub(all) struct ContextFlags {
    propagate : Bool
    override_protected : Bool
    base_url : String?
    remote_contexts : Array[String]
    validate_scoped : Bool
    }

    Context Processing 的调用面旗标(REC 4.1.2 的可选输入;己批扩到五件)—— 用结构体而非相邻裸 bool,防调用点位置错配;面语义见各构造器注释

    ContextFlags::element

    fn ContextFlags::element() -> ContextFlags

    元素级 @context 面(propagate 默认 true、override protected 默认 false)—— 新空链 + validate=true;base_url 留 None(回落 options.base——documentUrl)

    ContextFlags::property_scoped

    fn ContextFlags::property_scoped() -> ContextFlags

    property-scoped 应用面(REC 5.1.2:"and true for override protected";propagate 默认 true)——故属性作用域内可清保护(#pr06/#pr14/#pr15/#pr16 oracle)。 应用面取新空链 + validate=true(REC expansion 13.9.6 不传 remote contexts)

    ContextFlags::type_scoped

    fn ContextFlags::type_scoped() -> ContextFlags

    type-scoped 应用面(REC 5.1.2:不传 override protected、false for propagate)—— 故 type-scoped 内空化保护 term 即错(#pr17/#pr18/#pr20/#pr21 oracle)。 应用面取新空链 + validate=true(REC expansion 算法不传 remote contexts)

    ExpandedLiteral

    pub(all) struct ExpandedLiteral {
    value : String
    datatype : String?
    language : String?
    direction : String?
    raw : JsonValue?
    index : String?
    included : Array[ExpandedNode]?
    } derive(
    Debug
    )

    展开后的字面量 datatype = @type(IRI);数值/布尔字面量以词形入 value(xsd 词形挂 toRDF 面)

    ExpandedLiteral::to_repr

    [0079] 消警:Debug 派生的 to_repr 显式升格

    ExpandedNode

    pub(all) struct ExpandedNode {
    id : String?
    types : Array[String]
    properties : Array[(String, Array[ExpandedValue])]
    reverse_props : Array[(String, Array[ExpandedValue])]
    graph : Array[ExpandedValue]?
    included : Array[ExpandedNode]?
    index : String?
    } derive(
    Debug
    )

    展开后的节点对象(树递归的中间态) 字段只列骨架需要的;完整字段随覆盖面扩展

    ExpandedNode::to_repr

    [0079] 消警:Debug 派生的 to_repr 显式升格

    ExpandedValue

    pub(all) enum ExpandedValue {
    Node(ExpandedNode)
    Value(ExpandedLiteral)
    List(Array[ExpandedValue], index~ : String?)
    } derive(
    Debug
    )

    展开后的值(@value / @list / @set / 节点)

    ExpandedValue::to_repr

    [0079] 消警:Debug 派生的 to_repr 显式升格

    HtmlScriptSource

    pub struct HtmlScriptSource {
    source : String
    document_base : String?
    no_scripts : Bool
    }

    REC 9.5.1 Process HTML——script 源选择 + 逐 script JSON 校验。 fragment = 输入 URL # 后段(调用方剥离,percent 解码在此); extract_all = extractAllScripts 选项。返回 = 喂下游文档函数的 JSON 源 文本:单件/fragment 返回 textContent 原文(零变换保真);extract_all 返回合并数组重文(数组并尾/标量追加——REC 9.5.1 末支;下游重解析)。

    JsonLdError

    pub(all) enum JsonLdError {
    Syntax(String)
    InvalidContext(String)
    InvalidIri(String)
    InvalidValue(String)
    InvalidList(String)
    Unsupported(String)
    ContextOverflow(String)
    LoadingDocumentFailed(String)
    InvalidScriptElement(String)
    } derive(Eq,
    Debug
    )

    JSON-LD 处理阶段的错误(derive(Eq, Debug)——assert_eq Result 需错误型 实现 Eq/Debug;变体全 String 包装,结构性成立)

    JsonLdError::equal

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

    [0079]:Eq/Debug 的隐式方法晋升弃用警告——extend 双清(显式晋升 只保留需要的成员)

    JsonLdError::not_equal

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

    [0079]:Eq/Debug 的隐式方法晋升弃用警告——extend 双清(显式晋升 只保留需要的成员)

    JsonLdIriRoute

    pub(all) struct JsonLdIriRoute {
    order : Int
    kind : String
    condition : String
    action : String
    handler : String?
    }

    IRI 规则路由行([[iri_rules]];handler = None 的行走骨架缺省实现)

    JsonLdKeywordRoute

    pub(all) struct JsonLdKeywordRoute {
    keyword : String
    condition : String
    action : String
    result : String
    args_vocab : Bool?
    args_document_relative : Bool?
    handler : String?
    }

    关键字路由行([[keyword_dispatch]];condition/result 为可枚举词表)

    JsonLdOptions

    pub(all) struct JsonLdOptions {
    base : String?
    expand_context : JsonValue?
    processing_mode : ProcessingMode
    load_context : (String) -> Result[String, String]?
    rdf_direction : String?
    produce_generalized_rdf : Bool
    compact_arrays : Bool
    compact_to_relative : Bool
    frame_expansion : Bool
    omit_graph : Bool
    use_native_types : Bool
    use_rdf_type : Bool
    }

    处理选项(对应 expansion 的 options)

    JsonLdQuad

    pub(all) struct JsonLdQuad {
    subject : String
    predicate : String
    object : String
    graph : String?
    } derive(
    Debug
    )

    RDF 四元组(与现有 trig/nquads 产物同形,便于复用发射层)

    JsonLdQuad::to_repr

    [0079] 消警:Debug 派生的 to_repr 显式升格

    JsonLdStep

    pub(all) enum JsonLdStep {
    ExpansionEntry
    ExpansionExpandValue
    ExpansionExpandArray
    ExpansionExpandObject
    ExpansionPostprocess
    TorfEntry
    TorfEmit
    }

    步骤表全量枚举([[steps]])

    JsonLdStep::dispatch

    fn JsonLdStep::dispatch(self : JsonLdStep, shape : String) -> JsonLdStep?

    形态分派([[steps]].dispatch;词表 scalar/array/object;未声明步 → None)

    JsonLdStep::handler

    fn JsonLdStep::handler(self : JsonLdStep) -> String?

    步骤手写落点([[steps]].handler_hook;None = 无手写,调度直通)

    JsonLdStep::id

    fn JsonLdStep::id(self : JsonLdStep) -> String

    步骤 id(账面对照用)

    JsonLdStep::next_step

    fn JsonLdStep::next_step(self : JsonLdStep) -> JsonLdStep?

    next 边([[steps]].next;缺省 = 本阶段终点)

    JsonValue

    pub(all) enum JsonValue {
    Null
    Bool(Bool)
    Number(Double)
    String(String)
    Array(Array[JsonValue])
    Object(Array[(String, JsonValue)])
    } derive(Eq,
    Debug
    )

    JSON 值(内部文档模型) 实施卷规则(spec.md §7):包装官方 core 解析器 (moonbitlang/core/json,工具链内置零依赖),不自建解析层。 Object 收有序对——core 按 Map 折叠后转出:重复键 last-wins(占首现位)、 键序 ≈ 首现序(1.1 算法按属性排序 + 输出侧自排序)。

    JsonValue::equal

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

    [0079] 消警:Eq 派生的 equal/not_equal 显式升格(2026-09-25,比对结构相等用)

    JsonValue::not_equal

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

    [0079] 消警:Eq 派生的 equal/not_equal 显式升格(2026-09-25,比对结构相等用)

    JsonValue::to_repr

    [0079] 消警:Debug 派生的 to_repr 显式升格为常规方法(2026-09-25)

    LanguageMember

    pub(all) enum LanguageMember {
    Set(String)
    Null
    }

    语言/方向成员三态(丙批)

    PreviousContext

    pub(all) struct PreviousContext {
    base : String?
    vocab : String?
    terms : Map[String, TermDefinition]
    }

    前一 context 快照(type-scoped 批):只承载回退所需的三个字段

    ProcessingMode

    pub(all) enum ProcessingMode {
    JsonLd10
    JsonLd11
    } derive(
    Debug
    )

    处理模式:json-ld-1.0 / json-ld-1.1

    ProcessingMode::to_repr

    [0079] 消警:Debug 派生的 to_repr 显式升格

    StandardExpander

    pub(all) struct StandardExpander {
    }

    TermDefinition

    pub(all) struct TermDefinition {
    iri : String?
    datatype : String?
    container : ContainerType?
    reverse : String?
    graph_secondary : ContainerType?
    local_context : JsonValue?
    context_base_url : String?
    index_mapping : String?
    is_protected : Bool
    is_prefix : Bool?
    language_mapping : LanguageMember?
    direction_mapping : LanguageMember?
    nest : String?
    simple_form : Bool
    graph_set : Bool
    container_set : Bool
    }

    term 定义

    canonical_for_suite

    fn canonical_for_suite(value : JsonValue) -> JsonValue

    JSON-LD 文档比对(W3C README「JSON-LD Object comparison」移植): 通用文档比对器——对象键序不敏感;数组默认无序——唯一 例外 @list 键下的数组保序;标量严格相等。语言标签大小写不敏感项未采 语言标签大小写不敏感项未采(从严)。「suite」系历史名非用途限定。tn004 双 nest 保序输出 [v2,v4,v3] 与 expected [v2,v3,v4] 同判——合法置换。

    compact_document

    fn compact_document(input_text : String, context_text : String, options : JsonLdOptions) -> Result[JsonValue, JsonLdError]

    compaction 文档入口(J6——spec §8.41 规则一):JSON 文本 + 主动 context 文本 → 展开全链 → context 处理 → 压缩序列化(compact_standard.mbt)。 负例 = 任一段 Err。输出形三条断言:展开空 → {};单节点 → 对象;n 节点 → @graph 包裹。@context 键 = 原文逐字发射(223/223 实证),context 空 不发射(6 例实证——ctx {} 族)。

    expand_document

    fn expand_document(text : String, options : JsonLdOptions) -> Result[JsonValue, JsonLdError]

    expansion 文档入口:JSON 文本 → 展开形态(顶层恒为数组;free-floating 顶层节点丢弃)

    flatten_document

    fn flatten_document(text : String, context? : String, options : JsonLdOptions) -> Result[JsonValue, JsonLdError]

    flatten 文档入口(J5——spec §8.40):JSON 文本 → 展开全链 → F2 同 id 预合并 → 作用域成员注册 → 顶层作用域枚举。负例 = 任一段 Err。 flatten 唯一入口(REC flatten(input, context, options) 同形——context 可选,原双入口合一)。context 缺省 = 展平直出 (数组形);context 给定 = 展平后以该 context 压缩(flatten_with_context_inner)。

    frame_document

    fn frame_document(input_text : String, frame_text : String, options : JsonLdOptions) -> Result[JsonValue, JsonLdError]

    framing 文档入口(spec §8.93–§8.95): ①expand(input) ②F2 同 id 预合并(flatten 同源)③frame 原文 @context 抽取 + 展开(骨架 = 常规展开;frameExpansion 保形批随帧旗簇)④ process_context 建 ActiveContext ⑤graph map(Node Map Generation 骨架
    • frame 顶层 @graph ⇒ @default 否则 @merged)⑥匹配嵌套递归 ⑦prune bnodes(mode≠1.0——tp 簇批)⑧@preserve 替换(发射批同笔)⑨compaction (frame @context,compact_node_jv 复用)⑩@graph 包裹 + @context 回填 +"@null"→null(发射批同笔)

    from_rdf_document

    fn from_rdf_document(text : String, options : JsonLdOptions) -> Result[JsonValue, JsonLdError]

    fromRDF 文档入口(REC fromRdf;spec §8.130)

    html_script_source

    fn html_script_source(html : String, document_url : String?, fragment : String?, extract_all : Bool) -> Result[HtmlScriptSource, JsonLdError]

    REC 9.5.1 Process HTML——script 源选择 + 逐 script JSON 校验 + 文档基。 document_url = REC url 参数(检索 URL:option.base ?? 文档位置,调用方 剥 fragment);fragment = 输入 URL # 后段;extract_all = extractAllScripts 选项。返回见 HtmlScriptSource:单件/fragment 返回 textContent 原文 (零变换保真);extract_all 返回合并数组重文(数组并尾/标量追加—— REC 9.5.1 末支;下游重解析)。

    isomorphic_for_suite

    fn isomorphic_for_suite(ours : JsonValue, expected : JsonValue) -> Bool

    JSON-LD 文档同构判定(bnode 双射兜底):通用同构比对器—— 直比比对不过时,bnode 词面升为存在变量——存在标签双射 f 使 relabel 后规范形逐字节相等 ⇒ 同构成立。词面位三条断言(flatten 输出形): 谓词键位 / "@id" 值位 / "@type" 数组串值位;"@value" 字面量串不重标 (字面量 "_:x" 非节点词面)。标签数 > 8 直接 false——8 是排列数工作 界,非经验值:回溯最坏 = n! 次全文档重标规范渲染,8! = 40320 次为可 接受上界;套件实测最大 4 标签(#t0038,24 排列),8 留 4 倍标签余量。 「suite」系历史名非用途限定。

    jcs_serialize

    fn jcs_serialize(value : JsonValue) -> String

    JCS 规范化序列化出口(RFC 8785;评审处置后晋 pub): 键 = Unicode 码点序(nq_key_compare)、字符串转义 §3.2.2.2、数值词形 = ECMAScript Number::toString——本体 nq_jcs(J3.3,#tjs01~23 oracle 全绿)。 与 json_canonical(比对归一器,比对用)分账:对外规范化序列化用本品。

    json_canonical

    fn json_canonical(value : JsonValue) -> JsonValue

    比对归一:对象键序不敏感(键按字典序重排,递归);数组保序。 定位:本品 = 比对归一器,非 RFC 8785 JCS;对外 JCS 规范化用 jcs_serialize(to_rdf.mbt,#tjs01~23)。

    jsonld_iri_routes

    fn jsonld_iri_routes() -> Array[JsonLdIriRoute]

    IRI 规则表(order 升序 = 求值顺序)

    jsonld_keyword_route

    fn jsonld_keyword_route(keyword : String) -> JsonLdKeywordRoute?

    关键字路由(未知键落缺省臂 _term;表无缺省臂时返回 None)

    jsonld_keyword_routes

    fn jsonld_keyword_routes() -> Array[JsonLdKeywordRoute]

    关键字路由全表(表序;覆盖门用)

    parse_json

    fn parse_json(text : String) -> Result[JsonValue, JsonLdError]

    JSON 解析接口:官方解析器包装 待办注记(J0):数词形超 double 精确域时 core 留 repr~,本层暂弃—— toRDF 数词形按值还是按原文,J3 对套件实证(DataRoundTripping 族 + useJCS 23 例)后再定。

    rdfc10_canonicalize_with_hash

    fn rdfc10_canonicalize_with_hash(input_nquads : String, hash_algorithm : C14nHashAlgorithm) -> Result[(String, Map[String, String]), JsonLdError]

    Full-option entry (REC section 3.1): the hash algorithm selects the SHA384 variant pinned by suite test075. Output shape matches the mapping variant.

    定位:本入口=RDFC-1.0 规范入口(库特征,非 harness 专用)—— hash 算法可配(SHA-256/384),返回规范形 + bnode 映射,是 RDFC-1.0 对外能力的直出口。包内其余入口(rdfc10_canonicalize / _with_mapping / sha256_hex / sha256_utf8_hex / sha384_hex / sha384_utf8_hex) 为内部面,收 priv。

    render_json

    fn render_json(value : JsonValue) -> String

    JsonValue → JSON 文本(往返保真 min-JSON)。真调用点 = html extract_all 合并源 + isomorphic_for_suite 规范渲染 + 去重键;库出口 承诺 = compact/expand 返回 JsonValue 须有官方序列化器。 ≠ json_canonical:键序/数词形/数组序一概不动(规范形归 json_canonical,本函数只管词法保真)。字符串转义 " \ 与全部控制 字符(\b\f\n\r\t 命名形 + 其余 \u00XX);数词形 = Double 插值词形 (render∘parse 往返值等价,wbtest 校验)。

    to_rdf_document

    fn to_rdf_document(text : String, options : JsonLdOptions) -> Result[Array[JsonLdQuad], JsonLdError]

    toRDF 文档入口(J3.1——库级 API,gen 调度面 handler_hook 的调用面): JSON 文本 → 展开形 → 四元组。负例 = 任一段 Err。