A JSON-LD implementation for Rust
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 iri;
use ;
// Parse a JSON-LD document.
let = parse_str.expect;
let input = new;
// Use `NoJsonLdLoader` as we won't need to load any remote document.
let expanded = input.expand.expect;
assert!;
Here is another example using a file-system loader.
use iri;
use ;
let mut loader = default;
loader.mount;
let input = loader.load.expect;
let expanded = input.expand.expect;
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 iri;
use ;
let mut loader = default;
loader.mount;
let input = loader.load.expect;
let context = iri;
let compact = input.compact.expect;
println!;
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 iri;
use ;
let mut loader = default;
loader.mount;
let input = loader.load.expect;
let flattened = input.flatten.expect;
println!;
Testing
To run the tests for the first time use the following commands in a shell:
This will clone the
W3C JSON-LD API repository containing the
official test suite, generate the associated Rust tests using the procedural
macros provided by the json-ld-testing crate and run the
tests.
Afterward a simple cargo test will rerun the tests.
Sponsor
Many thanks to SpruceID for sponsoring this project!
License
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.