-
Hello ! I would like to parse JSON files that either do not declare @context at all, and pass in the @context programmatically when parsing. How can I specify the @context programmatically when parsing ? I found this : #395, is this related ? Little code snippet would be much appreciated ! Thomas |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
It seems the library can't do that, the closest I was able to find is if the the I wrote this little example to read the original.json (the Wikipedia example) {
"@context": "http://bad.com/url",
"@id": "https://me.example.com",
"@type": "Person",
"name": "John Smith",
"homepage": "https://www.example.com/"
} the removed context is "@context": {
"name": "http://xmlns.com/foaf/0.1/name",
"homepage": {
"@id": "http://xmlns.com/foaf/0.1/workplaceHomepage",
"@type": "@id"
},
"Person": "http://xmlns.com/foaf/0.1/Person"
}, Parser code try (InputStream inputStream = new FileInputStream("original.json");
OutputStream stream = new FileOutputStream("original.ttl")) {
RDFParser rdfParser = Rio.createParser(RDFFormat.JSONLD);
// replace the document loader used to read remote documents
rdfParser.set(JSONLDSettings.DOCUMENT_LOADER, new DocumentLoader() {
@Override
public RemoteDocument loadDocument(String url) throws JsonLdError {
// here you can check the URL if you want
// this method return a RemoteDocument, the first argument isn't important for the
// context, but the second argument should be a Map<String, Object> describing your
// json tree
return new RemoteDocument("whatever", Map.<String, Object>of(
"@context", Map.<String, Object>of(
"name", "http://xmlns.com/foaf/0.1/name",
"homepage", Map.<String, Object>of(
"@id", "http://xmlns.com/foaf/0.1/workplaceHomepage",
"@type", "@id"
),
"Person", "http://xmlns.com/foaf/0.1/Person"
)
));
}
});
// here I'm using a RDFWriter to handle it, but you can pass it to a repository
RDFWriter writer = Rio.createWriter(RDFFormat.TURTLE, stream);
rdfParser.setRDFHandler(writer);
// parse the document
rdfParser.parse(inputStream);
} Result: original.ttl <https://me.example.com> a <http://xmlns.com/foaf/0.1/Person>;
<http://xmlns.com/foaf/0.1/name> "John Smith";
<http://xmlns.com/foaf/0.1/workplaceHomepage> <https://www.example.com/> . This solution is a bit hacky I admit |
Beta Was this translation helpful? Give feedback.
It seems the library can't do that, the closest I was able to find is if the the
"@context"
URL is a fake one, but still an URL, you can use theJSONLDSettings.DOCUMENT_LOADER
option to override the context downloadI wrote this little example to read the
original.json
file and convert it into a ttl file.original.json (the Wikipedia example)
the removed context is