use of org.apache.jena.riot.JsonLDWriteContext in project jena by apache.
the class TestJsonLDWriter method testSubstitutingContext.
/**
* Test using a context to compute the output, and replacing the @context with a given value
*/
@Test
public void testSubstitutingContext() {
Model m = ModelFactory.createDefaultModel();
String ns = "http://schema.org/";
Resource person = m.createResource(ns + "Person");
Resource s = m.createResource();
m.add(s, m.createProperty(ns + "name"), "Jane Doe");
m.add(s, m.createProperty(ns + "url"), "http://www.janedoe.com");
m.add(s, m.createProperty(ns + "jobTitle"), "Professor");
m.add(s, RDF.type, person);
// change @context to a URI
JsonLDWriteContext jenaCtx = new JsonLDWriteContext();
jenaCtx.setJsonLDContextSubstitution((new JsonString(ns)).toString());
String jsonld;
jsonld = toString(m, RDFFormat.JSONLD_COMPACT_FLAT, jenaCtx);
String c = "\"@context\":\"http://schema.org/\"";
assertTrue(jsonld.contains(c));
// change @context to a given ctx
String ctx = "{\"jobTitle\":{\"@id\":\"http://ex.com/jobTitle\"},\"url\":{\"@id\":\"http://ex.com/url\"},\"name\":{\"@id\":\"http://ex.com/name\"}}";
jenaCtx.setJsonLDContextSubstitution(ctx);
jsonld = toString(m, RDFFormat.JSONLD_COMPACT_FLAT, jenaCtx);
assertTrue(jsonld.contains("http://ex.com/name"));
}
use of org.apache.jena.riot.JsonLDWriteContext in project jena by apache.
the class TestJsonLDWriter method testSettingContextAsJsonString.
/** verify that one may pass a context as a JSON string, and that it is actually used in the output */
@Test
public void testSettingContextAsJsonString() {
// 1) get the context generated by default by jena
// for a simple model with a prefix declaration
// 2) remove prefix declaration from model,
// output as jsonld is different
// 3) output the model as jsonld using the context:
// we should get the same output as in 1
String ns = "http://www.a.com/foo/";
Model m = simpleModel(ns);
m.setNsPrefix("ex", ns);
String s1 = toString(m, RDFFormat.JSONLD_COMPACT_FLAT, null);
// there's a prefix in m, and we find it in the output
String prefixStringInResult = "\"ex\":\"" + ns + "\"";
assertTrue(s1.contains(prefixStringInResult));
Model m1 = parse(s1);
// this is the json object associated to "@context" in s1
// it includes the "ex" prefix
// String js = "{\"p\":{\"@id\":\"http://www.a.com/foo/p\",\"@type\":\"@id\"},\"ex\":\"http://www.a.com/foo/\"}";
// constructing the js string ny hand:
JsonObject obj = new JsonObject();
obj.put("@id", ns + "p");
obj.put("@type", "@id");
JsonObject json = new JsonObject();
json.put("p", obj);
json.put("ex", ns);
String js = json.toString();
// remove the prefix from m
m.removeNsPrefix("ex");
String s2 = toString(m, RDFFormat.JSONLD_COMPACT_PRETTY, null);
// model wo prefix -> no more prefix string in result:
assertFalse(s2.contains(prefixStringInResult));
// the model wo prefix, output as jsonld using a context that defines the prefix
JsonLDWriteContext jenaCtx = new JsonLDWriteContext();
jenaCtx.setJsonLDContext(js);
String s3 = toString(m, RDFFormat.JSONLD_COMPACT_FLAT, jenaCtx);
assertTrue(s3.length() == s1.length());
assertTrue(s3.contains(prefixStringInResult));
Model m3 = parse(s3);
assertTrue(m3.isIsomorphicWith(m));
assertTrue(m3.isIsomorphicWith(m1));
// to be noted: things also work if passing the "@context"
js = "{\"@context\":" + js + "}";
jenaCtx.setJsonLDContext(js);
String s4 = toString(m, RDFFormat.JSONLD_COMPACT_FLAT, jenaCtx);
assertTrue(s4.length() == s1.length());
assertTrue(s4.contains(prefixStringInResult));
Model m4 = parse(s4);
assertTrue(m4.isIsomorphicWith(m));
assertTrue(m4.isIsomorphicWith(m1));
}
use of org.apache.jena.riot.JsonLDWriteContext in project jena by apache.
the class TestJsonLDWriter method jsonldOptions.
/** Test passing a JsonLdOptions through Context */
@Test
public final void jsonldOptions() {
Model m = ModelFactory.createDefaultModel();
String ns = "http://schema.org/";
Resource s = m.createResource();
m.add(s, m.createProperty(ns + "name"), "Jane Doe");
m.add(s, m.createProperty(ns + "url"), "http://www.janedoe.com");
m.add(s, m.createProperty(ns + "jobTitle"), "Professor");
// our default uses true for compactArrays
String jsonld = toString(m, RDFFormat.JSONLD, null);
// compactArrays is true -> no "@graph"
assertFalse(jsonld.contains("@graph"));
// compactArrays is true -> string, not an array for props with one value
assertTrue(jsonld.contains("\"jobTitle\" : \"Professor\""));
// now output using a value for JsonLdOptions in Context that sets compactArrays to false
JsonLDWriteContext jenaCtx = new JsonLDWriteContext();
JsonLdOptions opts = new JsonLdOptions(null);
opts.setCompactArrays(false);
jenaCtx.setOptions(opts);
jsonld = toString(m, RDFFormat.JSONLD, jenaCtx);
// compactArrays is false -> a "@graph" node
assertTrue(jsonld.contains("@graph"));
// compactArrays is false -> an array for all props, even when there's only one value
assertTrue(jsonld.contains("\"jobTitle\" : [ \"Professor\" ]"));
}
Aggregations