Skip to main content

Crate json_ld

Crate json_ld 

Source
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:

  • JsonLdDocument wraps the JSON representation of the document alongside its remote URL.
  • RemoteJsonLdDocument may 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:

§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:

§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.
BlankIdBuf
Owned blankid.
ConflictingIndexes
Conflicting indexes error.
InvalidIri
Invalid IRI error.
InvalidJsonLdLangString
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.
IriRefBuf
Owned IRI reference.
JsonLdChainLoader
JsonLdChainLoader: loads document from the first loader, otherwise falls back to the second one.
JsonLdDocument
Remote document.
JsonLdExpandedDocument
Result of the document expansion algorithm.
JsonLdFsLoader
File-system loader.
JsonLdIndexed
Indexed objects.
JsonLdLangStr
Language string reference.
JsonLdLangString
Language string.
JsonLdLoadError
Loading error.
JsonLdNode
Node object.
JsonLdNodes
Iterator through indexed nodes.
JsonLdObjects
Iterator through indexed objects.
JsonLdOptions
JSON-LD Processor options.
JsonLdProcessedContext
Processed JSON-LD context.
JsonLdProfile
Value for the profile parameter defined for the application/ld+json.
JsonLdRelabeling
JsonLdReqwestLoader
reqwest-based loader.
JsonLdSourceCode
JsonLdSourceFile
Source file of a JSON-LD processing backtrace frame.
JsonLdSourceFileRef
Borrowed counterpart of JsonLdSourceFile, used while building a backtrace.
JsonLdTokioFsLoader
Async file-system loader backed by Tokio.
NoJsonLdLoader
Dummy loader.
ToAsyncJsonLdLoader

Enums§

JsonLdDocumentForm
Form of a document at a given stage of the JSON-LD processing pipeline.
JsonLdDocumentFormRef
Borrowed counterpart of JsonLdDocumentForm.
JsonLdError
JsonLdErrorCode
Error code.
JsonLdLenient
String-like value that can be valid or invalid.
JsonLdObject
Object.
JsonLdProcessingMode
Processing mode.
JsonLdTerm
Identifier, keyword or @null.
JsonLdType
Object type.
JsonLdValue
Value object.
JsonLdWarning
RemoteJsonLdDocument
Remote document, loaded or not.

Traits§

AsyncJsonLdLoader
Async Document loader.
JsonLdLoader
JsonLdProcessor
The JsonLdProcessor interface is the high-level programming structure that developers use to access the JSON-LD transformation methods.
JsonLdValidate
JsonLdVisit

Functions§

ignore_warnings

Type Aliases§

JsonLdBacktrace
JsonLdBacktraceBuf
JsonLdBacktraceBuilder
Backtrace builder for the JSON-LD processing algorithms.
JsonLdCompactResult
Result of the JsonLdProcessor::compact function.
JsonLdCompareResult
Result of the JsonLdProcessor::compare function.
JsonLdContextDocument
JsonLdExpandResult
Result returned by the JsonLdProcessor::expand function.
JsonLdFlattenResult
Result of the JsonLdProcessor::flatten function.
JsonLdFlattenedDocument
Result of the document flattening algorithm.
JsonLdIndexedNode
JsonLdIndexedObject
Indexed object.
JsonLdIntoDocumentResult
Result of converting into a JsonLdDocument.
JsonLdLocated
A value annotated with a JSON-LD processing backtrace.
JsonLdLocatedError
JsonLdToRdfResult
RemoteJsonLdContext
Remote JSON-LD context.
StaticJsonLdLoader
Lazily initialized map of pre-loaded documents, suitable for a static.