Skip to content
This repository has been archived by the owner on Apr 14, 2023. It is now read-only.

Commit

Permalink
Merge pull request #1651 from finos/fix/ndjson-output
Browse files Browse the repository at this point in the history
#1650: Ensure JSON output matches NDJSON format
  • Loading branch information
Simon Laing authored Jan 23, 2020
2 parents 1b96875 + 2a5e638 commit d0525de
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@

package com.scottlogic.datahelix.generator.output.writer.json;

import com.fasterxml.jackson.core.PrettyPrinter;
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
import com.fasterxml.jackson.core.util.MinimalPrettyPrinter;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.databind.SequenceWriter;
Expand All @@ -41,7 +43,10 @@ public JsonOutputWriterFactory(@Named("config:streamOutput") boolean streamOutpu

@Override
public DataSetWriter createWriter(OutputStream stream, Fields fields) throws IOException {
ObjectWriter objectWriter = new ObjectMapper().writer(new DefaultPrettyPrinter(NEW_LINE_DELIMITER));
PrettyPrinter prettyPrinter = streamOutput
? new MinimalPrettyPrinter(NEW_LINE_DELIMITER)
: new DefaultPrettyPrinter();
ObjectWriter objectWriter = new ObjectMapper().writer(prettyPrinter);
SequenceWriter writer = objectWriter.writeValues(stream);
writer.init(!streamOutput);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ void writer_whereStreamingJson__shouldOutputNewLineDelimiterRows() throws IOExce
expectJson(
fields,
true,
Matchers.equalTo("{\n \"my_field\" : \"my_value\"\n}\n{\n \"my_field\" : \"my_value\"\n}"));
Matchers.equalTo("{\"my_field\":\"my_value\"}\n{\"my_field\":\"my_value\"}"));
}

@Test
Expand All @@ -68,7 +68,7 @@ void writeRow_withInternalFields_shouldNotWriteInternalFields() throws IOExcepti
expectJson(
fields,
true,
Matchers.equalTo("{\n \"External\" : \"my_value\"\n}\n{\n \"External\" : \"my_value\"\n}")
Matchers.equalTo("{\"External\":\"my_value\"}\n{\"External\":\"my_value\"}")
);
}

Expand Down

0 comments on commit d0525de

Please sign in to comment.