Search in sources :

Example 6 with JsonArray

use of org.apache.jena.atlas.json.JsonArray 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

JsonArray (org.apache.jena.atlas.json.JsonArray)6 JsonValue (org.apache.jena.atlas.json.JsonValue)4 JsonObject (org.apache.jena.atlas.json.JsonObject)3 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 TDBException (org.apache.jena.tdb.TDBException)1