Github user kinow commented on a diff in the pull request:
https://github.com/apache/jena/pull/114#discussion_r175680295
--- Diff:
jena-arq/src/main/java/org/apache/jena/query/ResultSetFormatter.java ---
@@ -516,6 +520,41 @@ static public void outputAsJSON(boolean booleanResult)
static public void outputAsJSON(OutputStream outStream, boolean
booleanResult)
{ output(outStream, booleanResult, SPARQLResultSetJSON) ; }
+ /** Output an iterator of JSON values.
+ *
+ * @param outStream output stream
+ * @param jsonItems The JSON values
+ */
+ public static void outputAsJSON(OutputStream outStream,
Iterator<JsonObject> jsonItems)
+ {
+ JSWriter jWriter = new JSWriter(outStream) ;
+ jWriter.startArray() ;
+ jWriter.startOutput() ;
+ while (jsonItems.hasNext())
+ {
+ jWriter.startObject() ;
+ JsonObject jsonItem = jsonItems.next() ;
+ for (Entry<String, JsonValue> entry: jsonItem.entrySet())
+ {
+ JsonValue value = entry.getValue() ;
+ String val = "";
+ if (value.isString()) {
--- End diff --
>"outputAsJSON" is a bit confusing because the other functions of the same
name produce SPARQL results in JSON. How about "output"?
Sounds good to me. Done!
>In the code, numbers still come out as strings.
Oh, d'oh me! You raised the issue, I updated the code but not the part that
writes it back to the user. And even added a test that was testing it was
getting "123" and "true" in the output. Fixed the test, and thanks heaps for
the code snippet! Otherwise I'd be scratching my head and thinking how to use
JSWriter.
Updated, and uploaded the data below
```
@prefix : <http://example.org/> .
:s :p 123 .
:s :p "abc" .
:s :p "def"@en .
```
Then queried `JSON { "s": ?s , "p": ?p , "o" : ?o } WHERE { ?s ?p ?o }`.
Screen shot below with the `123` value - hopefully - correctly displayed!
:man_dancing: :tada:

---