JSON-LD 1.1 (expand / compact / flatten / frame / toRDF / fromRDF) + RDFC-1.0 canonicalization for MoonBit.
Dependencies
moon add thy1016/jsonldlet 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,
}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}")
}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}")
}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}")
}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}")
}match @jsonld.parse_json("{\"b\": 1.0, \"a\": 2}") {
Ok(parsed) => println(@jsonld.jcs_serialize(parsed))
Err(err) => println("parse failed: \{err}")
}// ((String) -> Result[String, String])?
let loader = url => fetch_document(url)
// 构造 options 时:load_context: Some(loader)| 入口 | 作用 |
|---|---|
| parse_json | 自带 JSON 解析(JsonValue AST) |
| expand_document | 展开:IRI 全解析 + 词表归一 |
| compact_document | 压缩(文档 + 上下文 + 选项) |
| flatten_document | 展平(上下文可选参) |
| frame_document | 帧(frame) |
| to_rdf_document | JSON-LD → RDF 四元组(JsonLdQuad) |
| from_rdf_document | N-Quads 文本 → JSON-LD 节点对象 |
| html_script_source | HTML <script type="application/ld+json"> 抽取 |
| rdfc10_canonicalize_with_hash | RDFC-1.0 规范化(Sha256 / Sha384) |
| jcs_serialize | JCS(RFC 8785)规范化序列化直出 |
| json_canonical / render_json | 对拍归一(比对用)/ JSON 序列化 |
| canonical_for_suite / isomorphic_for_suite | 套件比对辅助(规范化形 / 数据集同构) |
| jsonld_iri_routes / jsonld_keyword_routes | IRI / 关键字词表路由自省 |
moon run cmd/main -- <command> [options] <inputs># 例:展开并直出 N-Quads
moon run cmd/main -- to-rdf doc.jsonld
# 例:RDFC-1.0 规范化(sha384 档)
moon run cmd/main -- canonicalize --hash sha384 dataset.nqmoon test| 面 | entries | plain | deferred |
|---|---|---|---|
| expand | 385 | 385 | 0 |
| toRdf | 467 | 467 | 0 |
| compact | 246 | 245 | 1(#t0038 版次越界,[设计]) |
| frame | 92 | 92 | 0 |
| canon(RDFC-1.0) | 86 | 86 | 0 |
| flatten | 58 | 58 | 0 |
| fromRdf | 54 | 54 | 0 |
| html | 50 | 50 | 0 |
| 合计 | 1438 | 1437 | 1 |
pub(open) trait ContextProcessor {
fn process_context(Self, active : ActiveContext, local_context : JsonValue, flags : ContextFlags, options : JsonLdOptions) -> Result[ActiveContext, JsonLdError]
}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]
}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]
}pub(open) trait ToRdfProcessor {
fn node_to_quads(Self, expanded : Array[ExpandedValue], options : JsonLdOptions) -> Result[Array[JsonLdQuad], JsonLdError]
}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)]?
}pub(all) struct ContextFlags {
propagate : Bool
override_protected : Bool
base_url : String?
remote_contexts : Array[String]
validate_scoped : Bool
}pub(all) struct ExpandedLiteral {
value : String
datatype : String?
language : String?
direction : String?
raw : JsonValue?
index : String?
included : Array[ExpandedNode]?
} derive(Debug)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)pub(all) enum ExpandedValue {
Node(ExpandedNode)
Value(ExpandedLiteral)
List(Array[ExpandedValue], index~ : String?)
} derive(Debug)pub struct HtmlScriptSource {
source : String
document_base : String?
no_scripts : Bool
}pub(all) struct JsonLdIriRoute {
order : Int
kind : String
condition : String
action : String
handler : String?
}pub(all) struct JsonLdKeywordRoute {
keyword : String
condition : String
action : String
result : String
args_vocab : Bool?
args_document_relative : Bool?
handler : String?
}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
}pub(all) struct JsonLdQuad {
subject : String
predicate : String
object : String
graph : String?
} derive(Debug)pub(all) enum JsonLdStep {
ExpansionEntry
ExpansionExpandValue
ExpansionExpandArray
ExpansionExpandObject
ExpansionPostprocess
TorfEntry
TorfEmit
}pub(all) struct PreviousContext {
base : String?
vocab : String?
terms : Map[String, 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
}fn compact_document(input_text : String, context_text : String, options : JsonLdOptions) -> Result[JsonValue, JsonLdError]fn flatten_document(text : String, context? : String, options : JsonLdOptions) -> Result[JsonValue, JsonLdError]fn frame_document(input_text : String, frame_text : String, options : JsonLdOptions) -> Result[JsonValue, JsonLdError]fn html_script_source(html : String, document_url : String?, fragment : String?, extract_all : Bool) -> Result[HtmlScriptSource, JsonLdError]fn rdfc10_canonicalize_with_hash(input_nquads : String, hash_algorithm : C14nHashAlgorithm) -> Result[(String, Map[String, String]), JsonLdError]fn to_rdf_document(text : String, options : JsonLdOptions) -> Result[Array[JsonLdQuad], JsonLdError]Install
Download zipJSON-LD 1.1 (expand / compact / flatten / frame / toRDF / fromRDF) + RDFC-1.0 canonicalization for MoonBit.
Dependencies