Skip to content

Commit 32dde6e

Browse files
committed
Merge remote-tracking branch 'origin/master' into oersi
2 parents ae1fb79 + fdd7035 commit 32dde6e

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

metafacture-json/src/main/java/org/metafacture/json/JsonDecoder.java

+8
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ public JsonDecoder() {
5656
resetRecordCount();
5757
}
5858

59+
public void setAllowComments(final boolean allowComments) {
60+
jsonFactory.configure(JsonParser.Feature.ALLOW_COMMENTS, allowComments);
61+
}
62+
63+
public boolean getAllowComments() {
64+
return jsonFactory.isEnabled(JsonParser.Feature.ALLOW_COMMENTS);
65+
}
66+
5967
public void setArrayMarker(final String arrayMarker) {
6068
this.arrayMarker = arrayMarker;
6169
}

metafacture-json/src/test/java/org/metafacture/json/JsonDecoderTest.java

+44
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static org.mockito.Mockito.times;
2020
import static org.mockito.Mockito.verifyZeroInteractions;
2121

22+
import org.junit.Assert;
2223
import org.junit.Before;
2324
import org.junit.Rule;
2425
import org.junit.Test;
@@ -196,4 +197,47 @@ public void testShouldNotParseTrailingGarbage() {
196197
jsonDecoder.process("{\"lit\":\"value\"}XXX");
197198
}
198199

200+
@Test
201+
public void testShouldNotParseComments() {
202+
exception.expect(MetafactureException.class);
203+
exception.expectMessage("Unexpected character ('/' (code 47))");
204+
205+
Assert.assertFalse(jsonDecoder.getAllowComments());
206+
207+
jsonDecoder.process("//{\"lit\":\"value\"}");
208+
}
209+
210+
@Test
211+
public void testShouldParseCommentsIfEnabled() {
212+
jsonDecoder.setAllowComments(true);
213+
Assert.assertTrue(jsonDecoder.getAllowComments());
214+
215+
jsonDecoder.process("//{\"lit\":\"value\"}");
216+
217+
verifyZeroInteractions(receiver);
218+
}
219+
220+
@Test
221+
public void testShouldNotParseInlineComments() {
222+
exception.expect(MetafactureException.class);
223+
exception.expectMessage("Unexpected character ('/' (code 47))");
224+
225+
Assert.assertFalse(jsonDecoder.getAllowComments());
226+
227+
jsonDecoder.process("{\"lit\":/*comment*/\"value\"}");
228+
}
229+
230+
@Test
231+
public void testShouldParseInlineCommentsIfEnabled() {
232+
jsonDecoder.setAllowComments(true);
233+
Assert.assertTrue(jsonDecoder.getAllowComments());
234+
235+
jsonDecoder.process("{\"lit\":/*comment*/\"value\"}");
236+
237+
final InOrder ordered = inOrder(receiver);
238+
ordered.verify(receiver).startRecord("1");
239+
ordered.verify(receiver).literal("lit", "value");
240+
ordered.verify(receiver).endRecord();
241+
}
242+
199243
}

0 commit comments

Comments
 (0)