use of org.apache.jena.atlas.json.JsonObject in project jena by apache.
the class JSONInput method parse.
private void parse(InputStream in) {
JsonObject obj = JSON.parse(in);
if (obj.hasKey(kBoolean)) {
checkContains(obj, true, true, kHead, kBoolean);
booleanResult = obj.get(kBoolean).getAsBoolean().value();
rows = null;
return;
}
rows = new ArrayList<>(1000);
checkContains(obj, true, true, kHead, kResults);
// process head
if (!obj.get(kHead).isObject())
throw new ResultSetException("Key 'head' must have a JSON object as value: found: " + obj.get(kHead));
JsonObject head = obj.get(kHead).getAsObject();
// -- Link - array.
if (head.hasKey(kLink)) {
List<String> links = new ArrayList<>();
if (head.get(kLink).isString()) {
Log.warn(this, "Link field is a string, should be an array of strings");
links.add(head.get(kLink).getAsString().value());
} else {
if (!head.get(kLink).isArray())
throw new ResultSetException("Key 'link' must have be an array: found: " + obj.get(kLink));
for (JsonValue v : head.get(kLink).getAsArray()) {
if (!v.isString())
throw new ResultSetException("Key 'link' must have be an array of strings: found: " + v);
links.add(v.getAsString().value());
}
}
}
// -- Vars
vars = parseVars(head);
// ---- Results
JsonObject results = obj.get(kResults).getAsObject();
if (!results.get(kBindings).isArray())
throw new ResultSetException("'bindings' must be an array");
JsonArray array = results.get(kBindings).getAsArray();
Iterator<JsonValue> iter = array.iterator();
for (; iter.hasNext(); ) {
BindingMap b = BindingFactory.create();
JsonValue v = iter.next();
if (!v.isObject())
throw new ResultSetException("Entry in 'bindings' array must be an object {}");
JsonObject x = v.getAsObject();
Set<String> varNames = x.keys();
for (String vn : varNames) {
// if ( ! vars.contains(vn) ) {}
JsonValue vt = x.get(vn);
if (!vt.isObject())
throw new ResultSetException("Binding for variable '" + vn + "' is not a JSON object: " + vt);
Node n = parseOneTerm(vt.getAsObject());
b.add(Var.alloc(vn), n);
}
rows.add(b);
}
}
use of org.apache.jena.atlas.json.JsonObject in project jena by apache.
the class TestJsonLDWriter method testFrames.
/**
* Checking frames
*/
@Test
public final void testFrames() throws JsonParseException, IOException {
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);
s = m.createResource();
m.add(s, m.createProperty(ns + "name"), "Gado Salamatou");
m.add(s, m.createProperty(ns + "url"), "http://www.salamatou.com");
m.add(s, RDF.type, person);
s = m.createResource();
m.add(s, m.createProperty(ns + "name"), "Not a person");
m.add(s, RDF.type, m.createResource(ns + "Event"));
Context jenaCtx = new Context();
JsonObject frame = new JsonObject();
// only output the persons using a frame
frame.put("@type", ns + "Person");
jenaCtx.set(JsonLDWriter.JSONLD_FRAME, frame.toString());
String jsonld = toString(m, RDFFormat.JSONLD_FRAME_PRETTY, jenaCtx);
Model m2 = parse(jsonld);
// 2 subjects with a type in m2
assertTrue(m2.listStatements((Resource) null, RDF.type, (RDFNode) null).toList().size() == 2);
// 2 persons in m2
assertTrue(m2.listStatements((Resource) null, RDF.type, person).toList().size() == 2);
// something we hadn't tested in prettyIsNotFlat
assertTrue(jsonld.trim().contains("\n"));
// only output the subjects which have a jobTitle
frame = new JsonObject();
frame.put("http://schema.org/jobTitle", new JsonObject());
jenaCtx.set(JsonLDWriter.JSONLD_FRAME, JsonUtils.fromString(frame.toString()));
jsonld = toString(m, RDFFormat.JSONLD_FRAME_FLAT, jenaCtx);
m2 = parse(jsonld);
// 1 subject with a type in m2
assertTrue(m2.listStatements((Resource) null, RDF.type, (RDFNode) null).toList().size() == 1);
// 1 subject with a jobTitle in m2
assertTrue(m2.listStatements((Resource) null, m.createProperty(ns + "jobTitle"), (RDFNode) null).toList().size() == 1);
// something we hadn't tested in prettyIsNotFlat
assertFalse(jsonld.trim().contains("\n"));
}
use of org.apache.jena.atlas.json.JsonObject 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));
}
Aggregations