oops, there’s a typo in the code that I sent. Here is it again:
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.StringReader;
import java.util.Map;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.ModelFactory;
import org.junit.Test;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.github.jsonldjava.utils.JsonUtils;
public class JsonLdReadTest {
@Test
public final void test() throws JsonGenerationException, IOException {
// read the jsonld file (with a URI as value of @context)
File f = new File("/Users/fps/Desktop/jsonldTest.json");
Object jsonLdObject = JsonUtils.fromInputStream(new FileInputStream(f));
// read the context file
// (here from disk, normally read file at URI defined in @context)
File ctxf = new File("/Users/fps/Desktop/jsonldTestContext.json");
Object jsonLdContext = JsonUtils.fromInputStream(new
FileInputStream(ctxf));
// jsonldObject is a Map.
// Replace value associated to @context
((Map) jsonLdObject).put("@context", jsonLdContext);
// the jsonld with the @context replaced
String jsonld = JsonUtils.toString(jsonLdObject);
System.out.println(jsonld);
// no problem now to parse it
Model m = parse(jsonld);
}
private Model parse(String jsonld) {
Model m = ModelFactory.createDefaultModel();
StringReader reader = new StringReader(jsonld);
m.read(reader, null, "JSON-LD");
return m;
}
}