Source:
<code>private void showResultSetJson(final ResultSet rs, boolean alreadyOnRow, long start, Function... show) throws SQLException { JsonArray collector = new JsonArray(); final ResultSetMetaData meta = rs.getMetaData(); final Function[] columnMap; if (show != null && 0 < show.length) { columnMap = show; } else { final int colCnt = meta.getColumnCount(); columnMap = new Function[colCnt]; for (int colId = 0; colId < colCnt; colId++) { final int p = colId + 1; final String name = meta.getColumnLabel(p); columnMap[colId] = new Identity(p, name); } } int rowCnt = 0; final int colCnt = columnMap.length; while (alreadyOnRow || rs.next()) { final JsonObject row = new JsonObject(); final JsonObject cols = new JsonObject(); for (int c = 0; c < colCnt; c++) { String v = columnMap[c].apply(rs); if (v == null) { continue; } cols.addProperty(columnMap[c].name.toLowerCase(), v); } row.addProperty("type", "row"); row.add("columns", cols); <START> collector.add(row); <END> alreadyOnRow = false; rowCnt++; } if (start != 0 ) { final JsonObject tail = new JsonObject(); tail.addProperty("type", "query-stats"); tail.addProperty("rowCount", rowCnt); final long ms = System.currentTimeMillis() - start; tail.addProperty("runTimeMilliseconds", ms); collector.add(tail); } switch (outputFormat) { case JSON: for (JsonElement element : collector) { println(element.toString()); } break; case JSON_SINGLE: println(collector.toString()); break; default: final JsonObject obj = new JsonObject(); obj.addProperty("type", "error"); obj.addProperty("message", "Unsupported Json variant"); println(obj.toString()); break; } }</code><technical_language>Instead of putting in collector looping outputFormat==JSON switch outputFormat allow results printed immediately for JSON collected for JSON_SINGLE</technical_language>

Target:
private void showResultSetJson(final ResultSet rs, boolean alreadyOnRow, long start, Function... show) throws SQLException { JsonArray collector = new JsonArray(); final ResultSetMetaData meta = rs.getMetaData(); final Function[] columnMap; if (show != null && 0 < show.length) { columnMap = show; } else { final int colCnt = meta.getColumnCount(); columnMap = new Function[colCnt]; for (int colId = 0; colId < colCnt; colId++) { final int p = colId + 1; final String name = meta.getColumnLabel(p); columnMap[colId] = new Identity(p, name); } } int rowCnt = 0; final int colCnt = columnMap.length; while (alreadyOnRow || rs.next()) { final JsonObject row = new JsonObject(); final JsonObject cols = new JsonObject(); for (int c = 0; c < colCnt; c++) { String v = columnMap[c].apply(rs); if (v == null) { continue; } cols.addProperty(columnMap[c].name.toLowerCase(), v); } row.addProperty("type", "row"); row.add("columns", cols); switch (outputFormat) { case JSON: println(row.toString()); break; case JSON_SINGLE: collector.add(row); break; default: final JsonObject obj = new JsonObject(); obj.addProperty("type", "error"); obj.addProperty("message", "Unsupported Json variant"); println(obj.toString()); return; } alreadyOnRow = false; rowCnt++; } JsonObject tail = null; if (start != 0) { tail = new JsonObject(); tail.addProperty("type", "query-stats"); tail.addProperty("rowCount", rowCnt); final long ms = System.currentTimeMillis() - start; tail.addProperty("runTimeMilliseconds", ms); } switch (outputFormat) { case JSON: if (tail != null) { println(tail.toString()); } break; case JSON_SINGLE: if (tail != null) { collector.add(tail); } println(collector.toString()); break; default: final JsonObject obj = new JsonObject(); obj.addProperty("type", "error"); obj.addProperty("message", "Unsupported Json variant"); println(obj.toString()); return; } }