return this ;
}
private void parse(InputStream in)
{
JsonObject obj = JSON.parse(in) ;
if ( obj.hasKey(kBoolean) )
{
checkContains(obj, kHead, kBoolean) ;
// TODO check kHead is empty.
booleanResult = obj.get(kBoolean).getAsBoolean().value() ;
rows = null ;
return ;
}
rows = new ArrayList<Binding>(1000) ;
checkContains(obj, kHead, kResults) ;
if ( ! obj.hasKey(kHead) ) throw new ResultSetException("No 'head' for results") ;
if ( ! obj.hasKey(kResults) ) throw new ResultSetException("No 'results' for results") ;
// 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() ;
// ---- Head
// -- Link - array.
if ( head.hasKey(kLink) )
{
List<String> links = new ArrayList<String>() ;
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) )
; // Warning
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) ;
}