Skip to content

Commit a656ff5

Browse files
committed
Changed JsonEncoder to not prefix output with spaces.
Starting from the second object JsonEncoder added a space character to each serialised object it generated (as pointed out in issue #152). This commit changes the configuration of the JSON generator used by JsonEncoder to not output these spaces.
1 parent f3a6226 commit a656ff5

File tree

2 files changed

+34
-17
lines changed

2 files changed

+34
-17
lines changed

src/main/java/org/culturegraph/mf/stream/converter/JsonEncoder.java

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ public final class JsonEncoder extends
5353
public JsonEncoder() {
5454
try {
5555
jsonGenerator = new JsonFactory().createGenerator(writer);
56-
} catch (IOException e) {
56+
jsonGenerator.setRootValueSeparator(null);
57+
} catch (final IOException e) {
5758
throw new MetafactureException(e);
5859
}
5960
}
@@ -70,8 +71,8 @@ public void endRecord() {
7071
endGroup();
7172
try {
7273
jsonGenerator.flush();
73-
} catch (IOException e) {
74-
throw new MetafactureException(e);
74+
} catch (final IOException e) {
75+
throw new MetafactureException(e);
7576
}
7677
getReceiver().process(writer.toString());
7778
}
@@ -82,7 +83,7 @@ public void startEntity(final String name) {
8283
}
8384

8485
@Override
85-
public void endEntity() {
86+
public void endEntity() {
8687
endGroup();
8788
}
8889

@@ -98,10 +99,10 @@ public void literal(final String name, final String value) {
9899
} else {
99100
jsonGenerator.writeString(value);
100101
}
101-
} catch (JsonGenerationException e) {
102-
throw new MetafactureException(e);
102+
} catch (final JsonGenerationException e) {
103+
throw new MetafactureException(e);
103104
}
104-
catch (IOException e) {
105+
catch (final IOException e) {
105106
throw new MetafactureException(e);
106107
}
107108
}
@@ -113,17 +114,17 @@ private void startGroup(final String name) {
113114
if (ctx.inObject()) {
114115
jsonGenerator.writeFieldName(name.substring(0, name.length() - ARRAY_MARKER.length()));
115116
}
116-
jsonGenerator.writeStartArray();
117-
} else {
117+
jsonGenerator.writeStartArray();
118+
} else {
118119
if (ctx.inObject()) {
119120
jsonGenerator.writeFieldName(name);
120121
}
121122
jsonGenerator.writeStartObject();
122123
}
123-
} catch (JsonGenerationException e) {
124-
throw new MetafactureException(e);
124+
} catch (final JsonGenerationException e) {
125+
throw new MetafactureException(e);
125126
}
126-
catch (IOException e) {
127+
catch (final IOException e) {
127128
throw new MetafactureException(e);
128129
}
129130
}
@@ -136,10 +137,10 @@ private void endGroup() {
136137
} else if (ctx.inArray()) {
137138
jsonGenerator.writeEndArray();
138139
}
139-
} catch (JsonGenerationException e) {
140-
throw new MetafactureException(e);
140+
} catch (final JsonGenerationException e) {
141+
throw new MetafactureException(e);
141142
}
142-
catch (IOException e) {
143+
catch (final IOException e) {
143144
throw new MetafactureException(e);
144145
}
145146
}

src/test/java/org/culturegraph/mf/stream/converter/JsonEncoderTest.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@
1515
*/
1616
package org.culturegraph.mf.stream.converter;
1717

18+
import static org.mockito.Mockito.inOrder;
1819
import static org.mockito.Mockito.verify;
1920

2021
import org.culturegraph.mf.framework.ObjectReceiver;
2122
import org.junit.After;
2223
import org.junit.Before;
2324
import org.junit.Test;
25+
import org.mockito.InOrder;
2426
import org.mockito.Mock;
2527
import org.mockito.MockitoAnnotations;
2628

@@ -58,7 +60,7 @@ public final class JsonEncoderTest {
5860
public void setup() {
5961
MockitoAnnotations.initMocks(this);
6062
encoder = new JsonEncoder();
61-
encoder.setReceiver(receiver);
63+
encoder.setReceiver(receiver);
6264
}
6365

6466
@After
@@ -107,7 +109,7 @@ public void testShouldEncodeNestedEntities() {
107109
}
108110

109111
@Test
110-
public void testShouldEncodeMarkedEntitiesAsList() {
112+
public void testShouldEncodeMarkedEntitiesAsList() {
111113
encoder.startRecord("");
112114
encoder.startEntity(LIST1);
113115
encoder.literal(LITERAL1, VALUE1);
@@ -164,6 +166,20 @@ public void testShouldOutputDuplicateNames() {
164166

165167
verify(receiver).process(fixQuotes("{'L1':'V1','L1':'V2'}"));
166168
}
169+
170+
@Test
171+
public void testIssue152ShouldNotPrefixOutputWithSpaces() {
172+
encoder.startRecord("");
173+
encoder.literal(LITERAL1, VALUE1);
174+
encoder.endRecord();
175+
encoder.startRecord("");
176+
encoder.literal(LITERAL2, VALUE2);
177+
encoder.endRecord();
178+
179+
final InOrder ordered = inOrder(receiver);
180+
ordered.verify(receiver).process(fixQuotes("{'L1':'V1'}"));
181+
ordered.verify(receiver).process(fixQuotes("{'L2':'V2'}"));
182+
}
167183

168184
/*
169185
* Utility method which replaces all single quotes in a string with double quotes.

0 commit comments

Comments
 (0)