Skip to content

Commit

Permalink
Fix json formatter for real and test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
dmgaldi committed May 11, 2023
1 parent e035df2 commit 9068bc8
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(',');
Expand All @@ -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(']');
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}

}

0 comments on commit 9068bc8

Please sign in to comment.