json-ld icon indicating copy to clipboard operation
json-ld copied to clipboard

Regarding contexts documents as RemoteDocument

Open abhishekmaha23 opened this issue 1 year ago • 1 comments

I'm struggling to identify how to use a context document and need a bit of help here! For instance, adding a Context like the following seems impossible, if I want to compact a JSON with a NoLoader type loader.

let context: &str = r#"
    {
        "@context": {
            "odrl":"http://www.w3.org/ns/odrl/2/",
            "rdf":"http://www.w3.org/1999/02/22-rdf-syntax-ns#",
            "rdfs": "http://www.w3.org/2000/01/rdf-schema#",
            "owl": "http://www.w3.org/2002/07/owl#",
            "xsd": "http://www.w3.org/2001/XMLSchema#",
            "skos": "http://www.w3.org/2004/02/skos/core#",
            "dcterms": "http://purl.org/dc/terms/",
            "vcard": "http://www.w3.org/2006/vcard/ns#",
            "foaf": "http://xmlns.com/foaf/0.1/",
            "schema": "http://schema.org/",
            "cc": "https://creativecommons.org/ns#"
        }
    }
    "#;

The Context docs here don't seem to clarify how I should create a context when I want to keep these contexts in a string locally. All the examples also seem to focus around FileSystem loaders, and my use-case needs to minimize calls outside.

My use-case is deserialization (with some use-case-specific contexts that I must load in beforehand or dynamically), and I'm trying to compact incoming documents with this dynamic context, then flatten the documents to extract nested objects and then finally I'm using json-syntax to cast them into expected structs that I define. Am I misunderstanding the expected sequence of steps in this process?

abhishekmaha23 avatar Jul 29 '23 16:07 abhishekmaha23

I'm not sure I understand completely what you are trying to do here. The JsonLdProcessor::compact method expects a RemoteContextDocument, which is a RemoteDocument pointing to a json_ld::syntax::Context object. You can build such RemoteContextDocument yourself without using any loader by first building a json_ld::syntax::Context object and wrapping it inside a RemoteDocument. I see two ways of creating a json_ld::syntax::Context from your string:

  • json_ld::syntax::Context represents the value of a @context entry. In your case that's the
{
  "odrl":"http://www.w3.org/ns/odrl/2/",
  "rdf":"http://www.w3.org/1999/02/22-rdf-syntax-ns#",
  "rdfs": "http://www.w3.org/2000/01/rdf-schema#",
  "owl": "http://www.w3.org/2002/07/owl#",
  "xsd": "http://www.w3.org/2001/XMLSchema#",
  "skos": "http://www.w3.org/2004/02/skos/core#",
  "dcterms": "http://purl.org/dc/terms/",
  "vcard": "http://www.w3.org/2006/vcard/ns#",
  "foaf": "http://xmlns.com/foaf/0.1/",
  "schema": "http://schema.org/",
  "cc": "https://creativecommons.org/ns#"
}

part. You can parse it into a json_ld::syntax::Context using serde. Then use it to create a RemoteContextDocument that you can pass to the compact function.

  • If you cannot easily extract the @context entry value and need to parse the whole string, then parse it into json_ld::syntax::Value instead, using either serde or the json_ld::syntax::Parse::parse_str trait method. Then you can extract the json_ld::syntax::Context value using the ExtractContext::into_ld_context trait method provided by json_ld. Same as the previous solution you can use it to create a RemoteContextDocument that you can pass to the compact function.

I hope that answers your question.

timothee-haudebourg avatar Apr 09 '24 12:04 timothee-haudebourg