Github user afs commented on a diff in the pull request:

    https://github.com/apache/jena/pull/114#discussion_r175409690
  
    --- 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"?
    
    In the code, numbers still come out as strings.
    
    JSWriter seems to be only good for its current usage. The code below seems 
to work: it manages the outer array itself and uses `JsonValue.output` to write 
each item.
    
    ```
     public static void outputAsJSON(OutputStream outStream, 
Iterator<JsonObject> jsonItems)
        {
            IndentedWriter out = new IndentedWriter(outStream);
            out.println("[");
            out.incIndent();
    
            while (jsonItems.hasNext()) {
                JsonObject jsonItem = jsonItems.next() ;
                jsonItem.output(out);
                if ( jsonItems.hasNext() )
                    out.println(" ,");
                else
                    out.println();
            }
            out.decIndent();
            out.println("]");
            out.flush();
        }
    ```   


---

Reply via email to