Expand description
This crate is a Rust implementation of the JSON-LD data interchange format.
Linked Data (LD) is a World Wide Web Consortium (W3C) initiative built upon standard Web technologies to create an interrelated network of datasets across the Web. The JavaScript Object Notation (JSON) is a widely used, simple, unstructured data serialization format to describe data objects in a human readable way. JSON-LD brings these two technologies together, adding semantics to JSON to create a lightweight data serialization format that can organize data and help Web applications to inter-operate at a large scale.
§Usage
The entry point for this library is the JsonLdProcessor trait
that provides an access to all the JSON-LD transformation algorithms
(context processing, expansion, compaction, etc.).
If you want to explore and/or transform JsonLdExpandedDocuments, you may
also want to check out the JsonLdObject type representing a JSON object.
§Expansion
If you want to expand a JSON-LD document, first describe the document to
be expanded using either JsonLdDocument or RemoteJsonLdDocument:
JsonLdDocumentwraps the JSON representation of the document alongside its remote URL.RemoteJsonLdDocumentmay represent only an URL, letting some loader fetching the remote document by dereferencing the URL.
After that, you can simply use the JsonLdProcessor::expand function on
the remote document.
§Example
use rdf_syntax::iri;
use json_ld::{JsonLdProcessor, JsonLdDocument, json_syntax::{JsonValue, JsonParse}};
// Parse a JSON-LD document.
let (value, _) = JsonValue::parse_str(r#"{
"@context": { "name": "http://xmlns.com/foaf/0.1/name" },
"@id": "https://www.rust-lang.org",
"name": "Rust Programming Language"
}"#).expect("unable to parse file");
let input = JsonLdDocument::new(
Some(iri!("https://example.com/sample.jsonld").to_owned()),
None,
value,
);
// Use `NoJsonLdLoader` as we won't need to load any remote document.
let expanded = input.expand(json_ld::NoJsonLdLoader).expect("expansion failed");
assert!(!expanded.is_empty());Here is another example using a file-system loader.
use rdf_syntax::iri;
use json_ld::{JsonLdProcessor, JsonLdLoader};
let mut loader = json_ld::JsonLdFsLoader::default();
loader.mount(iri!("https://example.com/").to_owned(), "examples");
let input = loader.load(iri!("https://example.com/sample.jsonld")).expect("loading failed");
let expanded = input.expand(&loader).expect("expansion failed");§Compaction
The JSON-LD Compaction is a transformation that consists in applying a context to a given JSON-LD document reducing its size. There are two ways to get a compact JSON-LD document with this library depending on your starting point:
- If you want to get a compact representation for an arbitrary remote
document, simply use the
JsonLdProcessor::compact(orJsonLdProcessor::compact_with) method. - Otherwise to compact a
JsonLdExpandedDocumentyou can use theJsonLdExpandedDocument::compactmethod.
§Example
Here is an example compacting an arbitrary document using JsonLdProcessor::compact.
use rdf_syntax::iri;
use json_ld::{JsonLdProcessor, JsonLdLoader, RemoteJsonLdContext, json_syntax::JsonPrint};
let mut loader = json_ld::JsonLdFsLoader::default();
loader.mount(iri!("https://example.com/").to_owned(), "examples");
let input = loader.load(iri!("https://example.com/sample.jsonld")).expect("loading failed");
let context = RemoteJsonLdContext::iri(iri!("https://example.com/context.jsonld").to_owned());
let compact = input.compact(context, &loader).expect("compaction failed");
println!("output: {}", compact.pretty_print());§Flattening
The JSON-LD Flattening is a transformation that consists in moving nested nodes out. The result is a list of all the nodes declared in the document. There are two ways to flatten JSON-LD document with this library depending on your starting point:
- If you want to flatten an arbitrary remote document, use
JsonLdProcessor::flatten(orJsonLdProcessor::flatten_with). This returns a compacted JSON-LD document. - To flatten an already-expanded
JsonLdExpandedDocument, use theJsonLdExpandedDocument::flatten(orJsonLdExpandedDocument::flatten_unordered) method, which returns aJsonLdFlattenedDocument.
§Example
Here is an example flattening an arbitrary document using JsonLdProcessor::flatten.
use rdf_syntax::iri;
use json_ld::{JsonLdProcessor, JsonLdLoader, json_syntax::JsonPrint};
let mut loader = json_ld::JsonLdFsLoader::default();
loader.mount(iri!("https://example.com/").to_owned(), "examples");
let input = loader.load(iri!("https://example.com/sample.jsonld")).expect("loading failed");
let flattened = input.flatten(None, &loader).expect("flattening failed");
println!("output: {}", flattened.pretty_print());Re-exports§
pub use algorithms::RdfSerializationOptions;pub use syntax::JsonLdContext;pub use syntax::JsonLdContextEntry;pub use crate::syntax::JsonLdDirection;pub use crate::syntax::JsonLdNullable;pub use json_syntax;pub use linked_data;pub use rdf_syntax::iref;pub use rdf_syntax::rdf_types;
Modules§
- algorithms
- chain
- context
- Context processing algorithm and related types.
- expanded
- ext
- flattened
- fs
- id
- loader
- map
- none
- object
- Nodes, lists and values.
- profile
- reqwest
- Simple document and context loader based on
reqwest - syntax
- This library provide functions to parse JSON-LD contexts and print JSON-LD documents.
- tokio_
fs - utils
- Utility functions.
Structs§
- BlankId
- Blank node identifier.
- Blank
IdBuf - Owned blankid.
- Conflicting
Indexes - Conflicting indexes error.
- Invalid
Iri - Invalid IRI error.
- Invalid
Json LdLang String - Raised when something tried to build a language string without language tag or direction.
- Iri
- Uniform Resource Identifier (IRI).
- IriBuf
- Owned IRI.
- IriRef
- IRI reference.
- IriRef
Buf - Owned IRI reference.
- Json
LdChain Loader JsonLdChainLoader: loads document from the first loader, otherwise falls back to the second one.- Json
LdDocument - Remote document.
- Json
LdExpanded Document - Result of the document expansion algorithm.
- Json
LdFs Loader - File-system loader.
- Json
LdIndexed - Indexed objects.
- Json
LdLang Str - Language string reference.
- Json
LdLang String - Language string.
- Json
LdLoad Error - Loading error.
- Json
LdNode - Node object.
- Json
LdNodes - Iterator through indexed nodes.
- Json
LdObjects - Iterator through indexed objects.
- Json
LdOptions - JSON-LD Processor options.
- Json
LdProcessed Context - Processed JSON-LD context.
- Json
LdProfile - Value for the
profileparameter defined for theapplication/ld+json. - Json
LdRelabeling - Json
LdReqwest Loader reqwest-based loader.- Json
LdSource Code - Json
LdSource File - Source file of a JSON-LD processing backtrace frame.
- Json
LdSource File Ref - Borrowed counterpart of
JsonLdSourceFile, used while building a backtrace. - Json
LdTokio FsLoader - Async file-system loader backed by Tokio.
- NoJson
LdLoader - Dummy loader.
- ToAsync
Json LdLoader
Enums§
- Json
LdDocument Form - Form of a document at a given stage of the JSON-LD processing pipeline.
- Json
LdDocument Form Ref - Borrowed counterpart of
JsonLdDocumentForm. - Json
LdError - Json
LdError Code - Error code.
- Json
LdLenient - String-like value that can be valid or invalid.
- Json
LdObject - Object.
- Json
LdProcessing Mode - Processing mode.
- Json
LdTerm - Identifier, keyword or
@null. - Json
LdType - Object type.
- Json
LdValue - Value object.
- Json
LdWarning - Remote
Json LdDocument - Remote document, loaded or not.
Traits§
- Async
Json LdLoader - Async Document loader.
- Json
LdLoader - Json
LdProcessor - The
JsonLdProcessorinterface is the high-level programming structure that developers use to access the JSON-LD transformation methods. - Json
LdValidate - Json
LdVisit
Functions§
Type Aliases§
- Json
LdBacktrace - Json
LdBacktrace Buf - Json
LdBacktrace Builder - Backtrace builder for the JSON-LD processing algorithms.
- Json
LdCompact Result - Result of the
JsonLdProcessor::compactfunction. - Json
LdCompare Result - Result of the
JsonLdProcessor::comparefunction. - Json
LdContext Document - Json
LdExpand Result - Result returned by the
JsonLdProcessor::expandfunction. - Json
LdFlatten Result - Result of the
JsonLdProcessor::flattenfunction. - Json
LdFlattened Document - Result of the document flattening algorithm.
- Json
LdIndexed Node - Json
LdIndexed Object - Indexed object.
- Json
LdInto Document Result - Result of converting into a
JsonLdDocument. - Json
LdLocated - A value annotated with a JSON-LD processing backtrace.
- Json
LdLocated Error - Json
LdTo RdfResult - Remote
Json LdContext - Remote JSON-LD context.
- Static
Json LdLoader - Lazily initialized map of pre-loaded documents, suitable for a
static.