1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
//! This crate is a Rust implementation of the
//! [JSON-LD](https://www.w3.org/TR/json-ld/)
//! data interchange format.
//!
//! [Linked Data (LD)](https://www.w3.org/standards/semanticweb/data)
//! is a [World Wide Web Consortium (W3C)](https://www.w3.org/)
//! initiative built upon standard Web technologies to create an
//! interrelated network of datasets across the Web.
//! The [JavaScript Object Notation (JSON)](https://tools.ietf.org/html/rfc7159) 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 [`ExpandedDocument`]s, you may also
//! want to check out the [`Object`] type representing a JSON object.
//!
//! [`JsonLdProcessor`]: crate::JsonLdProcessor
//!
//! ## Expansion
//!
//! If you want to expand a JSON-LD document, first describe the document to
//! be expanded using either [`RemoteDocument`] or [`RemoteDocumentReference`]:
//! - [`RemoteDocument`] wraps the JSON representation of the document
//! alongside its remote URL.
//! - [`RemoteDocumentReference`] 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.
//!
//! [`RemoteDocument`]: crate::RemoteDocument
//! [`RemoteDocumentReference`]: crate::RemoteDocumentReference
//! [`JsonLdProcessor::expand`]: JsonLdProcessor::expand
//!
//! ### Example
//!
//! ```
//! use iref::IriBuf;
//! use static_iref::iri;
//! use json_ld::{JsonLdProcessor, Options, RemoteDocument, syntax::{Value, Parse}};
//!
//! # #[async_std::main]
//! # async fn main() {
//! // Create a "remote" document by parsing a file manually.
//! let input = RemoteDocument::new(
//! // We use `IriBuf` as IRI type.
//! Some(iri!("https://example.com/sample.jsonld").to_owned()),
//!
//! // Optional content type.
//! Some("application/ld+json".parse().unwrap()),
//!
//! // Parse the file.
//! Value::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").0
//! );
//!
//! // Use `NoLoader` as we won't need to load any remote document.
//! let mut loader = json_ld::NoLoader;
//!
//! // Expand the "remote" document.
//! let expanded = input
//! .expand(&mut loader)
//! .await
//! .expect("expansion failed");
//!
//! for object in expanded {
//! if let Some(id) = object.id() {
//! let name = object.as_node().unwrap()
//! .get_any(&iri!("http://xmlns.com/foaf/0.1/name")).unwrap()
//! .as_str().unwrap();
//!
//! println!("id: {id}");
//! println!("name: {name}");
//! }
//! }
//! # }
//! ```
//!
//! Here is another example using `RemoteDocumentReference`.
//!
//! ```
//! use static_iref::iri;
//! use json_ld::{JsonLdProcessor, Options, RemoteDocumentReference};
//!
//! # #[async_std::main]
//! # async fn main() {
//! let input = RemoteDocumentReference::iri(iri!("https://example.com/sample.jsonld").to_owned());
//!
//! // Use `FsLoader` to redirect any URL starting with `https://example.com/` to
//! // the local `example` directory. No HTTP query.
//! let mut loader = json_ld::FsLoader::default();
//! loader.mount(iri!("https://example.com/").to_owned(), "examples");
//!
//! let expanded = input.expand(&mut loader)
//! .await
//! .expect("expansion failed");
//! # }
//! ```
//!
//! Lastly, the same example replacing [`IriBuf`] with the lightweight
//! [`rdf_types::vocabulary::Index`] type.
//!
//! [`IriBuf`]: https://docs.rs/iref/latest/iref/struct.IriBuf.html
//!
//! ```
//! # use static_iref::iri;
//! # use json_ld::{JsonLdProcessor, Options, RemoteDocumentReference};
//! use rdf_types::{Subject, vocabulary::{IriVocabularyMut, IndexVocabulary}};
//! use contextual::WithContext;
//! # #[async_std::main]
//! # async fn main() {
//! // Creates the vocabulary that will map each `rdf_types::vocabulary::Index`
//! // to an actual `IriBuf`.
//! let mut vocabulary: IndexVocabulary = IndexVocabulary::new();
//!
//! let iri_index = vocabulary.insert(iri!("https://example.com/sample.jsonld"));
//! let input = RemoteDocumentReference::iri(iri_index);
//!
//! // Use `FsLoader` to redirect any URL starting with `https://example.com/` to
//! // the local `example` directory. No HTTP query.
//! let mut loader = json_ld::FsLoader::default();
//! loader.mount(vocabulary.insert(iri!("https://example.com/")), "examples");
//!
//! let expanded = input
//! .expand_with(&mut vocabulary, &mut loader)
//! .await
//! .expect("expansion failed");
//!
//! // `foaf:name` property identifier.
//! let name_id = Subject::Iri(vocabulary.insert(iri!("http://xmlns.com/foaf/0.1/name")));
//!
//! for object in expanded {
//! if let Some(id) = object.id() {
//! let name = object.as_node().unwrap()
//! .get_any(&name_id).unwrap()
//! .as_value().unwrap()
//! .as_str().unwrap();
//!
//! println!("id: {}", id.with(&vocabulary));
//! println!("name: {name}");
//! }
//! }
//! # }
//! ```
//!
//! ## 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`]
//! (or [`JsonLdProcessor::compact_with`]) method.
//! - Otherwise to compact an [`ExpandedDocument`] you can use the
//! [`Compact::compact`] method.
//!
//! [`JsonLdProcessor::compact`]: crate::JsonLdProcessor::compact
//! [`JsonLdProcessor::compact_with`]: crate::JsonLdProcessor::compact_with
//! [`ExpandedDocument`]: crate::ExpandedDocument
//! [`Compact::compact`]: crate::Compact::compact
//!
//! ### Example
//!
//! Here is an example compaction an arbitrary [`RemoteDocumentReference`]
//! using [`JsonLdProcessor::compact`].
//!
//! ```
//! use static_iref::iri;
//! use json_ld::{JsonLdProcessor, Options, RemoteDocumentReference, RemoteContextReference, syntax::Print};
//!
//! # #[async_std::main]
//! # async fn main() {
//! let input = RemoteDocumentReference::iri(iri!("https://example.com/sample.jsonld").to_owned());
//!
//! let context = RemoteContextReference::iri(iri!("https://example.com/context.jsonld").to_owned());
//!
//! // Use `FsLoader` to redirect any URL starting with `https://example.com/` to
//! // the local `example` directory. No HTTP query.
//! let mut loader = json_ld::FsLoader::default();
//! loader.mount(iri!("https://example.com/").to_owned(), "examples");
//!
//! let compact = input
//! .compact(context, &mut loader)
//! .await
//! .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 get a compact representation for an arbitrary remote
//! document, simply use the [`JsonLdProcessor::flatten`]
//! (or [`JsonLdProcessor::flatten_with`]) method.
//! This will return a JSON-LD document.
//! - Otherwise to compact an [`ExpandedDocument`] you can use the
//! [`Flatten::flatten`] (or [`Flatten::flatten_with`]) method.
//! This will return the list of nodes as a [`FlattenedDocument`].
//!
//! Flattening requires assigning an identifier to nested anonymous nodes,
//! which is why the flattening functions take an [`rdf_types::MetaGenerator`]
//! as parameter. This generator is in charge of creating new fresh identifiers
//! (with their metadata). The most common generator is
//! [`rdf_types::generator::Blank`] that creates blank node identifiers.
//!
//! [`JsonLdProcessor::flatten`]: crate::JsonLdProcessor::flatten
//! [`JsonLdProcessor::flatten_with`]: crate::JsonLdProcessor::flatten_with
//! [`Flatten::flatten`]: crate::Flatten::flatten
//! [`Flatten::flatten_with`]: crate::Flatten::flatten_with
//! [`FlattenedDocument`]: crate::FlattenedDocument
//! [`rdf_types::MetaGenerator`]: https://docs.rs/rdf-types/latest/rdf_types/generator/trait.MetaGenerator.html
//! [`rdf_types::generator::Blank`]: https://docs.rs/rdf-types/latest/rdf_types/generator/struct.Blank.html
//!
//! ### Example
//!
//! Here is an example compaction an arbitrary [`RemoteDocumentReference`]
//! using [`JsonLdProcessor::flatten`].
//!
//! ```
//! use static_iref::iri;
//! use json_ld::{JsonLdProcessor, Options, RemoteDocumentReference, syntax::Print};
//!
//! # #[async_std::main]
//! # async fn main() {
//! let input = RemoteDocumentReference::iri(iri!("https://example.com/sample.jsonld").to_owned());
//!
//! // Use `FsLoader` to redirect any URL starting with `https://example.com/` to
//! // the local `example` directory. No HTTP query.
//! let mut loader = json_ld::FsLoader::default();
//! loader.mount(iri!("https://example.com/").to_owned(), "examples");
//!
//! let mut generator = rdf_types::generator::Blank::new();
//!
//! let nodes = input
//! .flatten(&mut generator, &mut loader)
//! .await
//! .expect("flattening failed");
//!
//! println!("output: {}", nodes.pretty_print());
//! # }
//! ```
//!
//! # Fast IRIs and Blank Node Identifiers
//!
//! This library gives you the opportunity to use any datatype you want to
//! represent IRIs an Blank Node Identifiers. Most types have them
//! parameterized.
//! To avoid unnecessary allocations and expensive comparisons, it is highly
//! recommended to use a cheap, lightweight datatype such as
//! [`rdf_types::vocabulary::Index`]. This type will represent each distinct
//! IRI/blank node identifier with a unique index. In this case a
//! [`rdf_types::IndexVocabulary`] that maps each index back/to its
//! original IRI/Blank identifier representation can be passed to every
//! function.
//!
//! You can also use your own index type, with your own
//! [`rdf_types::Vocabulary`] implementation.
//!
//! [`rdf_types::vocabulary::Index`]: https://docs.rs/rdf-types/latest/rdf_types/vocabulary/struct.Index.html
//! [`rdf_types::IndexVocabulary`]: https://docs.rs/rdf-types/latest/rdf_types/vocabulary/struct.IndexVocabulary.html
//! [`rdf_types::Vocabulary`]: https://docs.rs/rdf-types/latest/rdf_types/vocabulary/trait.Vocabulary.html
//!
//! ## Displaying vocabulary-dependent values
//!
//! Since using vocabularies separates IRIs and Blank ids from their textual
//! representation, it complicates displaying data using them.
//! Fortunately many types defined by `json-ld` implement the
//! [`contextual::DisplayWithContext`] trait that allow displaying value with
//! a "context", which here would be the vocabulary.
//! By importing the [`contextual::WithContext`] which provides the `with`
//! method you can display such value like this:
//! ```
//! use static_iref::iri;
//! use rdf_types::vocabulary::{IriVocabularyMut, IndexVocabulary};
//! use contextual::WithContext;
//!
//! let mut vocabulary: IndexVocabulary = IndexVocabulary::new();
//! let i = vocabulary.insert(iri!("https://docs.rs/contextual"));
//! let value = rdf_types::Subject::Iri(i);
//!
//! println!("{}", value.with(&vocabulary))
//! ```
//!
//! [`contextual::DisplayWithContext`]: https://docs.rs/contextual/latest/contextual/trait.DisplayWithContext.html
//! [`contextual::WithContext`]: https://docs.rs/contextual/latest/contextual/trait.WithContext.html
pub use json_ld_compaction as compaction;
pub use json_ld_context_processing as context_processing;
pub use json_ld_core::*;
pub use json_ld_expansion as expansion;
pub use json_ld_serialization as ser;
pub use json_ld_syntax as syntax;
pub use compaction::Compact;
pub use context_processing::Process;
pub use expansion::Expand;
mod processor;
pub use processor::*;