use of org.apache.jena.sparql.engine.JsonIterator in project jena by apache.
the class QueryExecDataset method execJsonItems.
@Override
public Iterator<JsonObject> execJsonItems() {
checkNotClosed();
if (!query.isJsonType())
throw new QueryExecException("Attempt to get a JSON result from a " + labelForQuery(query) + " query");
startQueryIterator();
return new JsonIterator(queryIterator, query.getResultVars());
}
use of org.apache.jena.sparql.engine.JsonIterator in project jena by apache.
the class TestResultSetFormatter method testIterator.
@Test
public void testIterator() throws IOException {
Model model = ModelFactory.createDefaultModel();
{
Resource r = model.createResource(AnonId.create("first"));
Property p = model.getProperty("");
RDFNode node = ResourceFactory.createTypedLiteral("123", XSDDatatype.XSDdecimal);
model.add(r, p, node);
r = model.createResource(AnonId.create("second"));
p = model.getProperty("");
node = ResourceFactory.createTypedLiteral("abc", XSDDatatype.XSDstring);
model.add(r, p, node);
r = model.createResource(AnonId.create("third"));
p = model.getProperty("");
node = ResourceFactory.createLangLiteral("def", "en");
model.add(r, p, node);
r = model.createResource(AnonId.create("fourth"));
p = model.getProperty("");
node = ResourceFactory.createTypedLiteral("true", XSDDatatype.XSDboolean);
model.add(r, p, node);
}
Query query = QueryFactory.create("JSON { \"s\": ?s , \"p\": ?p , \"o\" : ?o } " + "WHERE { ?s ?p ?o }", Syntax.syntaxARQ);
try (QueryExecution qexec = QueryExecutionFactory.create(query, model)) {
JsonIterator execJsonItems = (JsonIterator) qexec.execJsonItems();
try (final ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
ResultSetFormatter.output(baos, execJsonItems);
String output = baos.toString(Charset.forName("UTF-8"));
assertTrue(output.contains("\"_:first\""));
assertTrue(output.contains("\"_:second\""));
assertTrue(output.contains("\"_:third\""));
assertTrue(output.contains("\"_:fourth\""));
assertFalse(output.contains("\"true\""));
assertTrue(output.contains("true"));
assertTrue(output.contains("123"));
assertFalse(output.contains("\"123\""));
assertTrue(output.contains("\"abc\""));
}
}
}
Aggregations