@Override public void element(long currLine, long currCol) { arrays.peek().add(value) ; value = null ; }
@Override public void element(long currLine, long currCol) { arrays.peek().add(value) ; value = null ; }
private void valueString(String s) { if (top instanceof JsonObject) ((JsonObject)top).put(key, s); else ((JsonArray)top).add(s); }
private void valueInteger(Integer i) { if (top instanceof JsonObject) ((JsonObject)top).put(key, i); else ((JsonArray)top).add(i); }
private void maybeObjectOrArray(JsonValue value) { if ( stack.size() == 0 ) // Error. return ; switch (stack.peek()) { case OBJECT : { String k = keys.pop() ; JsonObject obj = objects.peek() ; if ( obj.hasKey(k) ) Log.warn(this, "Duplicate key '" + k + "' for object") ; obj.put(k, value) ; return ; } case ARRAY : { arrays.peek().add(value) ; return ; } } }
private void maybeObjectOrArray(JsonValue value) { if ( stack.size() == 0 ) // Error. return ; switch (stack.peek()) { case OBJECT : { String k = keys.pop() ; JsonObject obj = objects.peek() ; if ( obj.hasKey(k) ) Log.warn(this, "Duplicate key '" + k + "' for object") ; obj.put(k, value) ; return ; } case ARRAY : { arrays.peek().add(value) ; return ; } } }
@Override public JSONWriterFacade value(boolean b) { try { if (top instanceof JsonObject) { ((JsonObject)top).put(key, b); } else { ((JsonArray)top).add(b); } } catch (JsonException e) { throw new EncodingException(e.getMessage(), e); } return this; }
@Override public JSONWriterFacade endObject() { if (! (top instanceof JsonObject)) { throw new EncodingException("endObject called on non object"); } JsonObject o = (JsonObject)top; if (stack.isEmpty()) return this; top = stack.pop(); key = keystack.pop(); if (top instanceof JsonObject) { try { ((JsonObject)top).put(key, o); } catch (JsonException e) { throw new EncodingException(e.getMessage(), e); } } else { ((JsonArray)top).add(o); } return this; }
@Override public JSONWriterFacade endArray() { if (! (top instanceof JsonArray)) { throw new EncodingException("endArray called on non array"); } JsonArray o = (JsonArray)top; if (stack.isEmpty()) return this; top = stack.pop(); key = keystack.pop(); if (top instanceof JsonObject) { try { ((JsonObject)top).put(key, o); } catch (JsonException e) { throw new EncodingException(e.getMessage(), e); } } else { ((JsonArray)top).add(o); } return this; }
@Override public JSONWriterFacade value(long l) { try { if (top instanceof JsonObject) { ((JsonObject)top).put(key, l); } else { ((JsonArray)top).add(l); } } catch (JsonException e) { throw new EncodingException(e.getMessage(), e); } return this; }
@Test public void js_value_7() { JsonArray array = new JsonArray() ; array.add(JsonNumber.value(123)) ; read("[ 123 ]", array) ; }
@Test public void js_value_8() { JsonObject obj = new JsonObject() ; JsonArray array = new JsonArray() ; array.add(JsonNumber.value(123)) ; obj.put("a", array) ; read("{ \"a\" : [ 123 ] }", obj) ; }
@Override public JsonArray execJson() { checkNotClosed() ; if ( ! query.isJsonType() ) throw new QueryExecException("Attempt to get a JSON result from a " + labelForQuery(query)+" query") ; startQueryIterator() ; JsonArray jsonArray = new JsonArray() ; List<String> resultVars = query.getResultVars() ; while (queryIterator.hasNext()) { Binding binding = queryIterator.next() ; JsonObject jsonObject = new JsonObject() ; for (String resultVar : resultVars) { Node n = binding.get(Var.alloc(resultVar)) ; JsonValue value = RDFTerm2Json.fromNode(n) ; jsonObject.put(resultVar, value) ; } jsonArray.add(jsonObject) ; } return jsonArray ; }