From d6abd32679311c06339e5d69c3fa18caf9a1f101 Mon Sep 17 00:00:00 2001 From: Jens Wille Date: Fri, 27 Nov 2020 13:10:50 +0100 Subject: [PATCH] Add JSON decoder option to allow comments. --- .../org/metafacture/json/JsonDecoder.java | 8 ++++ .../org/metafacture/json/JsonDecoderTest.java | 44 +++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/metafacture-json/src/main/java/org/metafacture/json/JsonDecoder.java b/metafacture-json/src/main/java/org/metafacture/json/JsonDecoder.java index 03f53bdaa..80de6981b 100644 --- a/metafacture-json/src/main/java/org/metafacture/json/JsonDecoder.java +++ b/metafacture-json/src/main/java/org/metafacture/json/JsonDecoder.java @@ -56,6 +56,14 @@ public JsonDecoder() { resetRecordCount(); } + public void setAllowComments(final boolean allowComments) { + jsonFactory.configure(JsonParser.Feature.ALLOW_COMMENTS, allowComments); + } + + public boolean getAllowComments() { + return jsonFactory.isEnabled(JsonParser.Feature.ALLOW_COMMENTS); + } + public void setArrayMarker(final String arrayMarker) { this.arrayMarker = arrayMarker; } diff --git a/metafacture-json/src/test/java/org/metafacture/json/JsonDecoderTest.java b/metafacture-json/src/test/java/org/metafacture/json/JsonDecoderTest.java index 9c566a9ec..86c6ae2e1 100644 --- a/metafacture-json/src/test/java/org/metafacture/json/JsonDecoderTest.java +++ b/metafacture-json/src/test/java/org/metafacture/json/JsonDecoderTest.java @@ -19,6 +19,7 @@ import static org.mockito.Mockito.times; import static org.mockito.Mockito.verifyZeroInteractions; +import org.junit.Assert; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -196,4 +197,47 @@ public void testShouldNotParseTrailingGarbage() { jsonDecoder.process("{\"lit\":\"value\"}XXX"); } + @Test + public void testShouldNotParseComments() { + exception.expect(MetafactureException.class); + exception.expectMessage("Unexpected character ('/' (code 47))"); + + Assert.assertFalse(jsonDecoder.getAllowComments()); + + jsonDecoder.process("//{\"lit\":\"value\"}"); + } + + @Test + public void testShouldParseCommentsIfEnabled() { + jsonDecoder.setAllowComments(true); + Assert.assertTrue(jsonDecoder.getAllowComments()); + + jsonDecoder.process("//{\"lit\":\"value\"}"); + + verifyZeroInteractions(receiver); + } + + @Test + public void testShouldNotParseInlineComments() { + exception.expect(MetafactureException.class); + exception.expectMessage("Unexpected character ('/' (code 47))"); + + Assert.assertFalse(jsonDecoder.getAllowComments()); + + jsonDecoder.process("{\"lit\":/*comment*/\"value\"}"); + } + + @Test + public void testShouldParseInlineCommentsIfEnabled() { + jsonDecoder.setAllowComments(true); + Assert.assertTrue(jsonDecoder.getAllowComments()); + + jsonDecoder.process("{\"lit\":/*comment*/\"value\"}"); + + final InOrder ordered = inOrder(receiver); + ordered.verify(receiver).startRecord("1"); + ordered.verify(receiver).literal("lit", "value"); + ordered.verify(receiver).endRecord(); + } + }