diff --git a/src/main/java/org/veupathdb/service/eda/ss/model/db/FilteredResultFactory.java b/src/main/java/org/veupathdb/service/eda/ss/model/db/FilteredResultFactory.java index 89e07847..53324bda 100644 --- a/src/main/java/org/veupathdb/service/eda/ss/model/db/FilteredResultFactory.java +++ b/src/main/java/org/veupathdb/service/eda/ss/model/db/FilteredResultFactory.java @@ -195,6 +195,7 @@ public static void produceTabularSubsetFromFile(Study study, Entity outputEntity )) { long rowsConsumed = 0L; long rowsSkipped = 0L; + resultConsumer.begin(); while (resultStreamer.hasNext()) { if (rowsSkipped > reportConfig.getOffset()) { resultConsumer.consumeRow(resultStreamer.next()); @@ -206,6 +207,7 @@ public static void produceTabularSubsetFromFile(Study study, Entity outputEntity break; } } + resultConsumer.end(); LOG.info("Completed processing file-based subsetting request"); } catch (Exception e) { throw new RuntimeException("Failed to write result", e); diff --git a/src/main/java/org/veupathdb/service/eda/ss/model/tabular/TabularResponses.java b/src/main/java/org/veupathdb/service/eda/ss/model/tabular/TabularResponses.java index f8bd9aca..b8b50131 100644 --- a/src/main/java/org/veupathdb/service/eda/ss/model/tabular/TabularResponses.java +++ b/src/main/java/org/veupathdb/service/eda/ss/model/tabular/TabularResponses.java @@ -142,13 +142,18 @@ public void end() throws IOException { private boolean _firstWritten = false; @Override - public void consumeRow(byte[][] values) throws IOException { + public void begin() throws IOException { _outputStream.write('['); + } + + @Override + public void consumeRow(byte[][] values) throws IOException { if (_firstWritten) { _outputStream.write(','); } else { _firstWritten = true; } + _outputStream.write('['); for (int i = 0; i < values.length; i++) { if (i != 0) { _outputStream.write(','); @@ -164,7 +169,11 @@ public void consumeRow(byte[][] values) throws IOException { } } _outputStream.write(']'); - _outputStream.write(NEW_LINE_BYTES); + } + + @Override + public void end() throws IOException { + _outputStream.write(']'); } }; diff --git a/src/test/java/org/veupathdb/service/eda/ss/model/tabular/TabularResponsesTest.java b/src/test/java/org/veupathdb/service/eda/ss/model/tabular/TabularResponsesTest.java index f0ed73f3..cb0f19fd 100644 --- a/src/test/java/org/veupathdb/service/eda/ss/model/tabular/TabularResponsesTest.java +++ b/src/test/java/org/veupathdb/service/eda/ss/model/tabular/TabularResponsesTest.java @@ -21,11 +21,14 @@ public void testJsonBinaryFormatter() throws IOException { input[2] = "klmno".getBytes(StandardCharsets.UTF_8); input[3] = "pqrst".getBytes(StandardCharsets.UTF_8); input[4] = "uvwxy".getBytes(StandardCharsets.UTF_8); + formatter.begin(); formatter.consumeRow(input); formatter.consumeRow(input); - String[] outputRows = output.toString().split("\n"); - Assertions.assertEquals(5, new JSONArray(outputRows[0]).length()); - Assertions.assertEquals(5, new JSONArray(outputRows[1]).length()); + formatter.end(); + JSONArray arr = new JSONArray(output.toString()); + Assertions.assertEquals(2, arr.length()); + Assertions.assertEquals(arr.getJSONArray(0).length(), 5); + Assertions.assertEquals(arr.getJSONArray(1).length(), 5); } @Test @@ -39,11 +42,14 @@ public void testJsonBinaryFormatterEscapeChars() throws IOException { input[2] = "klmn/".getBytes(StandardCharsets.UTF_8); input[3] = "pqrst".getBytes(StandardCharsets.UTF_8); input[4] = "uvwxy".getBytes(StandardCharsets.UTF_8); + formatter.begin(); formatter.consumeRow(input); formatter.consumeRow(input); - String[] outputRows = output.toString().split("\n"); - Assertions.assertEquals(5, new JSONArray(outputRows[0]).length()); // Make sure it can be parsed as josn - Assertions.assertEquals(5, new JSONArray(outputRows[1]).length()); + formatter.end(); + JSONArray arr = new JSONArray(output.toString());; + Assertions.assertEquals(2, arr.length()); + Assertions.assertEquals(arr.getJSONArray(0).length(), 5); + Assertions.assertEquals(arr.getJSONArray(1).length(), 5); } }