Search in sources :

Example 26 with JsonValue

use of org.apache.jena.atlas.json.JsonValue in project jena by apache.

the class TestAdmin method list_datasets_5.

// Specific dataset
@Test
public void list_datasets_5() {
    JsonValue v = getDatasetDescription(ServerCtl.datasetPath());
    checkJsonDatasetsOne(v.getAsObject());
}
Also used : JsonValue(org.apache.jena.atlas.json.JsonValue) Test(org.junit.Test)

Example 27 with JsonValue

use of org.apache.jena.atlas.json.JsonValue in project jena by apache.

the class JSONInput method parseVars.

private List<Var> parseVars(JsonObject obj) {
    if (!obj.get(kVars).isArray())
        throw new ResultSetException("Key 'vars' must be a JSON array");
    JsonArray a = obj.get(kVars).getAsArray();
    Iterator<JsonValue> iter = a.iterator();
    List<Var> vars = new ArrayList<>();
    for (; iter.hasNext(); ) {
        JsonValue v = iter.next();
        if (!v.isString())
            throw new ResultSetException("Entries in vars array must be strings");
        Var var = Var.alloc(v.getAsString().value());
        vars.add(var);
    }
    return vars;
}
Also used : JsonArray(org.apache.jena.atlas.json.JsonArray) Var(org.apache.jena.sparql.core.Var) JsonValue(org.apache.jena.atlas.json.JsonValue)

Example 28 with JsonValue

use of org.apache.jena.atlas.json.JsonValue 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);
    }
}
Also used : JsonArray(org.apache.jena.atlas.json.JsonArray) LabelToNode(org.apache.jena.riot.lang.LabelToNode) Node(org.apache.jena.graph.Node) JsonValue(org.apache.jena.atlas.json.JsonValue) JsonObject(org.apache.jena.atlas.json.JsonObject) BindingMap(org.apache.jena.sparql.engine.binding.BindingMap)

Aggregations

JsonValue (org.apache.jena.atlas.json.JsonValue)28 Test (org.junit.Test)7 JsonArray (org.apache.jena.atlas.json.JsonArray)4 JsonObject (org.apache.jena.atlas.json.JsonObject)4 TypedInputStream (org.apache.jena.atlas.web.TypedInputStream)3 JsonBuilder (org.apache.jena.atlas.json.JsonBuilder)2 AsyncTask (org.apache.jena.fuseki.async.AsyncTask)2 ServletOutputStream (javax.servlet.ServletOutputStream)1 JsonParseException (org.apache.jena.atlas.json.JsonParseException)1 JsonString (org.apache.jena.atlas.json.JsonString)1 HttpException (org.apache.jena.atlas.web.HttpException)1 AsyncPool (org.apache.jena.fuseki.async.AsyncPool)1 Node (org.apache.jena.graph.Node)1 LabelToNode (org.apache.jena.riot.lang.LabelToNode)1 Var (org.apache.jena.sparql.core.Var)1 BindingMap (org.apache.jena.sparql.engine.binding.BindingMap)1