Skip to content

Commit

Permalink
Merge pull request #393 from metafacture/makeJsonEncoderArrayMarkerCo…
Browse files Browse the repository at this point in the history
…nfigurable

Make JSON encoder array marker configurable.
  • Loading branch information
blackwinter authored Sep 30, 2021
2 parents a3fd2cd + 0316128 commit fb527e4
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@
@In(StreamReceiver.class)
@Out(String.class)
@FluxCommand("encode-json")
public final class JsonEncoder extends
DefaultStreamPipe<ObjectReceiver<String>> {
public final class JsonEncoder extends DefaultStreamPipe<ObjectReceiver<String>> {

public static final String ARRAY_MARKER = "[]";

Expand All @@ -61,6 +60,8 @@ public final class JsonEncoder extends
private final JsonGenerator jsonGenerator;
private final StringWriter writer = new StringWriter();

private String arrayMarker = ARRAY_MARKER;

public JsonEncoder() {
try {
jsonGenerator = new JsonFactory().createGenerator(writer);
Expand All @@ -71,6 +72,14 @@ public JsonEncoder() {
}
}

public void setArrayMarker(final String arrayMarker) {
this.arrayMarker = arrayMarker;
}

public String getArrayMarker() {
return arrayMarker;
}

public void setPrettyPrinting(final boolean prettyPrinting) {
jsonGenerator.setPrettyPrinter(prettyPrinting ? new DefaultPrettyPrinter((SerializableString) null) : null);
}
Expand Down Expand Up @@ -173,9 +182,9 @@ public void literal(final String name, final String value) {
private void startGroup(final String name) {
try {
final JsonStreamContext ctx = jsonGenerator.getOutputContext();
if (name.endsWith(ARRAY_MARKER)) {
if (name.endsWith(arrayMarker)) {
if (ctx.inObject()) {
jsonGenerator.writeFieldName(name.substring(0, name.length() - ARRAY_MARKER.length()));
jsonGenerator.writeFieldName(name.substring(0, name.length() - arrayMarker.length()));
}
jsonGenerator.writeStartArray();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,37 @@ public void testShouldEncodeNestedLists() {
verify(receiver).process(fixQuotes("{'Li1':[['V1','V2'],['V3','V4']]}"));
}

@Test
public void testShouldNotEncodeEntitiesWithDifferentMarkerAsList() {
encoder.setArrayMarker("*");

encoder.startRecord("");
encoder.startEntity(LIST1);
encoder.literal(LITERAL1, VALUE1);
encoder.literal(LITERAL2, VALUE2);
encoder.literal(LITERAL3, VALUE3);
encoder.endEntity();
encoder.endRecord();

verify(receiver).process(fixQuotes("{'Li1[]':{'L1':'V1','L2':'V2','L3':'V3'}}"));
}

@Test
public void testShouldEncodeMarkedEntitiesWithConfiguredMarkerAsList() {
final String marker = "*";
encoder.setArrayMarker(marker);

encoder.startRecord("");
encoder.startEntity(LIST1.replace(JsonEncoder.ARRAY_MARKER, marker));
encoder.literal(LITERAL1, VALUE1);
encoder.literal(LITERAL2, VALUE2);
encoder.literal(LITERAL3, VALUE3);
encoder.endEntity();
encoder.endRecord();

verify(receiver).process(fixQuotes("{'Li1':['V1','V2','V3']}"));
}

@Test
public void testShouldOutputDuplicateNames() {
encoder.startRecord("");
Expand Down

0 comments on commit fb527e4

Please sign in to comment.