Search in sources :

Example 1 with JsonIterator

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());
}
Also used : JsonIterator(org.apache.jena.sparql.engine.JsonIterator) QueryExecException(org.apache.jena.query.QueryExecException)

Example 2 with JsonIterator

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\""));
        }
    }
}
Also used : Model(org.apache.jena.rdf.model.Model) Resource(org.apache.jena.rdf.model.Resource) JsonIterator(org.apache.jena.sparql.engine.JsonIterator) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream) Property(org.apache.jena.rdf.model.Property) RDFNode(org.apache.jena.rdf.model.RDFNode) Test(org.junit.Test)

Aggregations

JsonIterator (org.apache.jena.sparql.engine.JsonIterator)2 ByteArrayOutputStream (org.apache.commons.io.output.ByteArrayOutputStream)1 QueryExecException (org.apache.jena.query.QueryExecException)1 Model (org.apache.jena.rdf.model.Model)1 Property (org.apache.jena.rdf.model.Property)1 RDFNode (org.apache.jena.rdf.model.RDFNode)1 Resource (org.apache.jena.rdf.model.Resource)1 Test (org.junit.Test)1