{"id":13726767,"url":"https://github.com/timothee-haudebourg/json-ld","last_synced_at":"2025-05-07T22:30:32.896Z","repository":{"id":37787230,"uuid":"255977609","full_name":"timothee-haudebourg/json-ld","owner":"timothee-haudebourg","description":"JSON-LD implementation for Rust","archived":false,"fork":false,"pushed_at":"2024-12-19T17:25:34.000Z","size":2035,"stargazers_count":139,"open_issues_count":14,"forks_count":19,"subscribers_count":12,"default_branch":"main","last_synced_at":"2025-04-29T09:47:25.466Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/timothee-haudebourg.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE-APACHE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"patreon":"thaudebourg"}},"created_at":"2020-04-15T16:38:31.000Z","updated_at":"2025-04-27T14:59:04.000Z","dependencies_parsed_at":"2024-01-24T11:58:06.709Z","dependency_job_id":"c617da87-3e8c-42f5-87e2-354612b05833","html_url":"https://github.com/timothee-haudebourg/json-ld","commit_stats":{"total_commits":331,"total_committers":8,"mean_commits":41.375,"dds":0.5075528700906344,"last_synced_commit":"7434c9490453cf575dee2402c2b5a6427c0560a9"},"previous_names":[],"tags_count":29,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/timothee-haudebourg%2Fjson-ld","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/timothee-haudebourg%2Fjson-ld/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/timothee-haudebourg%2Fjson-ld/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/timothee-haudebourg%2Fjson-ld/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/timothee-haudebourg","download_url":"https://codeload.github.com/timothee-haudebourg/json-ld/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252965073,"owners_count":21832817,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-03T01:03:20.691Z","updated_at":"2025-05-07T22:30:31.859Z","avatar_url":"https://github.com/timothee-haudebourg.png","language":"Rust","funding_links":["https://patreon.com/thaudebourg"],"categories":["Rust","Implementations","Semantic Web"],"sub_categories":["Rust","RDF, Linked Data \u0026 SPARQL"],"readme":"# A JSON-LD implementation for Rust\n\n[![GitHub Actions Workflow Status](https://img.shields.io/github/actions/workflow/status/timothee-haudebourg/json-ld/ci.yml?style=flat-square\u0026logo=github)](https://github.com/timothee-haudebourg/json-ld/actions)\n[![Crate informations](https://img.shields.io/crates/v/json-ld.svg?style=flat-square)](https://crates.io/crates/json-ld)\n[![Crates.io MSRV](https://img.shields.io/crates/msrv/json-ld?style=flat-square)](https://crates.io/crates/json-ld)\n[![License](https://img.shields.io/crates/l/json-ld.svg?style=flat-square)](https://github.com/timothee-haudebourg/json-ld#license)\n[![Documentation](https://img.shields.io/badge/docs-latest-blue.svg?style=flat-square)](https://docs.rs/json-ld)\n\n\n\u003c!-- cargo-rdme start --\u003e\n\nThis crate is a Rust implementation of the\n[JSON-LD](https://www.w3.org/TR/json-ld/)\ndata interchange format.\n\n[Linked Data (LD)](https://www.w3.org/standards/semanticweb/data)\nis a [World Wide Web Consortium (W3C)](https://www.w3.org/)\ninitiative built upon standard Web technologies to create an\ninterrelated network of datasets across the Web.\nThe [JavaScript Object Notation (JSON)](https://tools.ietf.org/html/rfc7159) is\na widely used, simple, unstructured data serialization format to describe\ndata objects in a human readable way.\nJSON-LD brings these two technologies together, adding semantics to JSON\nto create a lightweight data serialization format that can organize data and\nhelp Web applications to inter-operate at a large scale.\n\n## Usage\n\nThe entry point for this library is the `JsonLdProcessor` trait\nthat provides an access to all the JSON-LD transformation algorithms\n(context processing, expansion, compaction, etc.).\nIf you want to explore and/or transform `ExpandedDocument`s, you may also\nwant to check out the [`Object`] type representing a JSON object.\n\n\n### Expansion\n\nIf you want to expand a JSON-LD document, first describe the document to\nbe expanded using either `RemoteDocument` or `RemoteDocumentReference`:\n  - `RemoteDocument` wraps the JSON representation of the document\n    alongside its remote URL.\n  - `RemoteDocumentReference` may represent only an URL, letting\n    some loader fetching the remote document by dereferencing the URL.\n\nAfter that, you can simply use the [`JsonLdProcessor::expand`] function on\nthe remote document.\n\n[`JsonLdProcessor::expand`]: JsonLdProcessor::expand\n\n#### Example\n\n```rust\nuse iref::IriBuf;\nuse static_iref::iri;\nuse json_ld::{JsonLdProcessor, Options, RemoteDocument, syntax::{Value, Parse}};\n\n// Create a \"remote\" document by parsing a file manually.\nlet input = RemoteDocument::new(\n  // We use `IriBuf` as IRI type.\n  Some(iri!(\"https://example.com/sample.jsonld\").to_owned()),\n\n  // Optional content type.\n  Some(\"application/ld+json\".parse().unwrap()),\n  \n  // Parse the file.\n  Value::parse_str(r#\"\n    {\n      \"@context\": {\n        \"name\": \"http://xmlns.com/foaf/0.1/name\"\n      },\n      \"@id\": \"https://www.rust-lang.org\",\n      \"name\": \"Rust Programming Language\"\n    }\"#).expect(\"unable to parse file\").0\n);\n\n// Use `NoLoader` as we won't need to load any remote document.\nlet mut loader = json_ld::NoLoader;\n\n// Expand the \"remote\" document.\nlet expanded = input\n  .expand(\u0026mut loader)\n  .await\n  .expect(\"expansion failed\");\n\nfor object in expanded {\n  if let Some(id) = object.id() {\n    let name = object.as_node().unwrap()\n      .get_any(\u0026iri!(\"http://xmlns.com/foaf/0.1/name\")).unwrap()\n      .as_str().unwrap();\n\n    println!(\"id: {id}\");\n    println!(\"name: {name}\");\n  }\n}\n```\n\nHere is another example using `RemoteDocumentReference`.\n\n```rust\nuse static_iref::iri;\nuse json_ld::{JsonLdProcessor, Options, RemoteDocumentReference};\n\nlet input = RemoteDocumentReference::iri(iri!(\"https://example.com/sample.jsonld\").to_owned());\n\n// Use `FsLoader` to redirect any URL starting with `https://example.com/` to\n// the local `example` directory. No HTTP query.\nlet mut loader = json_ld::FsLoader::default();\nloader.mount(iri!(\"https://example.com/\").to_owned(), \"examples\");\n\nlet expanded = input.expand(\u0026mut loader)\n  .await\n  .expect(\"expansion failed\");\n```\n\nLastly, the same example replacing [`IriBuf`] with the lightweight\n[`rdf_types::vocabulary::Index`] type.\n\n[`IriBuf`]: https://docs.rs/iref/latest/iref/struct.IriBuf.html\n\n```rust\nuse rdf_types::{Subject, vocabulary::{IriVocabularyMut, IndexVocabulary}};\nuse contextual::WithContext;\n// Creates the vocabulary that will map each `rdf_types::vocabulary::Index`\n// to an actual `IriBuf`.\nlet mut vocabulary: IndexVocabulary = IndexVocabulary::new();\n\nlet iri_index = vocabulary.insert(iri!(\"https://example.com/sample.jsonld\"));\nlet input = RemoteDocumentReference::iri(iri_index);\n\n// Use `FsLoader` to redirect any URL starting with `https://example.com/` to\n// the local `example` directory. No HTTP query.\nlet mut loader = json_ld::FsLoader::default();\nloader.mount(iri!(\"https://example.com/\").to_owned(), \"examples\");\n\nlet expanded = input\n  .expand_with(\u0026mut vocabulary, \u0026mut loader)\n  .await\n  .expect(\"expansion failed\");\n\n// `foaf:name` property identifier.\nlet name_id = Subject::Iri(vocabulary.insert(iri!(\"http://xmlns.com/foaf/0.1/name\")));\n\nfor object in expanded {\n  if let Some(id) = object.id() {\n    let name = object.as_node().unwrap()\n      .get_any(\u0026name_id).unwrap()\n      .as_value().unwrap()\n      .as_str().unwrap();\n\n    println!(\"id: {}\", id.with(\u0026vocabulary));\n    println!(\"name: {name}\");\n  }\n}\n```\n\n### Compaction\n\nThe JSON-LD Compaction is a transformation that consists in applying a\ncontext to a given JSON-LD document reducing its size.\nThere are two ways to get a compact JSON-LD document with this library\ndepending on your starting point:\n  - If you want to get a compact representation for an arbitrary remote\n    document, simply use the `JsonLdProcessor::compact`\n    (or `JsonLdProcessor::compact_with`) method.\n  - Otherwise to compact an `ExpandedDocument` you can use the\n    `Compact::compact` method.\n\n\n#### Example\n\nHere is an example compaction an arbitrary `RemoteDocumentReference`\nusing `JsonLdProcessor::compact`.\n\n```rust\nuse static_iref::iri;\nuse json_ld::{JsonLdProcessor, Options, RemoteDocumentReference, RemoteContextReference, syntax::Print};\n\nlet input = RemoteDocumentReference::iri(iri!(\"https://example.com/sample.jsonld\").to_owned());\n\nlet context = RemoteContextReference::iri(iri!(\"https://example.com/context.jsonld\").to_owned());\n\n// Use `FsLoader` to redirect any URL starting with `https://example.com/` to\n// the local `example` directory. No HTTP query.\nlet mut loader = json_ld::FsLoader::default();\nloader.mount(iri!(\"https://example.com/\").to_owned(), \"examples\");\n\nlet compact = input\n  .compact(context, \u0026mut loader)\n  .await\n  .expect(\"compaction failed\");\n\nprintln!(\"output: {}\", compact.pretty_print());\n```\n\n### Flattening\n\nThe JSON-LD Flattening is a transformation that consists in moving nested\nnodes out. The result is a list of all the nodes declared in the document.\nThere are two ways to flatten JSON-LD document with this library\ndepending on your starting point:\n  - If you want to get a compact representation for an arbitrary remote\n    document, simply use the `JsonLdProcessor::flatten`\n    (or `JsonLdProcessor::flatten_with`) method.\n    This will return a JSON-LD document.\n  - Otherwise to compact an `ExpandedDocument` you can use the\n    `Flatten::flatten` (or `Flatten::flatten_with`) method.\n    This will return the list of nodes as a `FlattenedDocument`.\n\nFlattening requires assigning an identifier to nested anonymous nodes,\nwhich is why the flattening functions take an [`rdf_types::MetaGenerator`]\nas parameter. This generator is in charge of creating new fresh identifiers\n(with their metadata). The most common generator is\n[`rdf_types::generator::Blank`] that creates blank node identifiers.\n\n[`rdf_types::MetaGenerator`]: https://docs.rs/rdf-types/latest/rdf_types/generator/trait.MetaGenerator.html\n[`rdf_types::generator::Blank`]: https://docs.rs/rdf-types/latest/rdf_types/generator/struct.Blank.html\n\n#### Example\n\nHere is an example compaction an arbitrary `RemoteDocumentReference`\nusing `JsonLdProcessor::flatten`.\n\n```rust\nuse static_iref::iri;\nuse json_ld::{JsonLdProcessor, Options, RemoteDocumentReference, syntax::Print};\n\nlet input = RemoteDocumentReference::iri(iri!(\"https://example.com/sample.jsonld\").to_owned());\n\n// Use `FsLoader` to redirect any URL starting with `https://example.com/` to\n// the local `example` directory. No HTTP query.\nlet mut loader = json_ld::FsLoader::default();\nloader.mount(iri!(\"https://example.com/\").to_owned(), \"examples\");\n\nlet mut generator = rdf_types::generator::Blank::new();\n\nlet nodes = input\n  .flatten(\u0026mut generator, \u0026mut loader)\n  .await\n  .expect(\"flattening failed\");\n\nprintln!(\"output: {}\", nodes.pretty_print());\n```\n\n## Fast IRIs and Blank Node Identifiers\n\nThis library gives you the opportunity to use any datatype you want to\nrepresent IRIs an Blank Node Identifiers. Most types have them\nparameterized.\nTo avoid unnecessary allocations and expensive comparisons, it is highly\nrecommended to use a cheap, lightweight datatype such as\n[`rdf_types::vocabulary::Index`]. This type will represent each distinct\nIRI/blank node identifier with a unique index. In this case a\n[`rdf_types::IndexVocabulary`] that maps each index back/to its\noriginal IRI/Blank identifier representation can be passed to every\nfunction.\n\nYou can also use your own index type, with your own\n[`rdf_types::Vocabulary`] implementation.\n\n[`rdf_types::vocabulary::Index`]: https://docs.rs/rdf-types/latest/rdf_types/vocabulary/struct.Index.html\n[`rdf_types::IndexVocabulary`]: https://docs.rs/rdf-types/latest/rdf_types/vocabulary/struct.IndexVocabulary.html\n[`rdf_types::Vocabulary`]: https://docs.rs/rdf-types/latest/rdf_types/vocabulary/trait.Vocabulary.html\n\n### Displaying vocabulary-dependent values\n\nSince using vocabularies separates IRIs and Blank ids from their textual\nrepresentation, it complicates displaying data using them.\nFortunately many types defined by `json-ld` implement the\n[`contextual::DisplayWithContext`] trait that allow displaying value with\na \"context\", which here would be the vocabulary.\nBy importing the [`contextual::WithContext`] which provides the `with`\nmethod you can display such value like this:\n```rust\nuse static_iref::iri;\nuse rdf_types::vocabulary::{IriVocabularyMut, IndexVocabulary};\nuse contextual::WithContext;\n\nlet mut vocabulary: IndexVocabulary = IndexVocabulary::new();\nlet i = vocabulary.insert(iri!(\"https://docs.rs/contextual\"));\nlet value = rdf_types::Subject::Iri(i);\n\nprintln!(\"{}\", value.with(\u0026vocabulary))\n```\n\n[`contextual::DisplayWithContext`]: https://docs.rs/contextual/latest/contextual/trait.DisplayWithContext.html\n[`contextual::WithContext`]: https://docs.rs/contextual/latest/contextual/trait.WithContext.html\n\n\u003c!-- cargo-rdme end --\u003e\n\n## Testing\n\nTo run the tests for the first time use the following commands in a shell:\n```sh\ngit submodule init\ngit submodule update\ncargo test\n```\n\nThis will clone the\n[W3C JSON-LD API repository](https://github.com/w3c/json-ld-api) containing the\nofficial test suite, generate the associated Rust tests using the procedural\nmacros provided by the [`json-ld-testing`](crates/testing) crate and run the\ntests.\n\nAfterward a simple `cargo test` will rerun the tests.\n\n## Sponsor\n\nMany thanks to [SpruceID](https://www.spruceid.com/) for sponsoring this project!\n\n## License\n\nLicensed under either of\n\n * Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)\n * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)\n\nat your option.\n\n### Contribution\n\nUnless you explicitly state otherwise, any contribution intentionally submitted\nfor inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any\nadditional terms or conditions.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftimothee-haudebourg%2Fjson-ld","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftimothee-haudebourg%2Fjson-ld","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftimothee-haudebourg%2Fjson-ld/lists"}