From d0250787d2e2304f4910e68e89c6181135f83df5 Mon Sep 17 00:00:00 2001 From: Falkreon Date: Sat, 1 Feb 2020 20:26:29 -0600 Subject: [PATCH] Fix leading whitespace multiplying on multiline comments. Fixes #35 --- .../jankson/impl/CommentParserContext.java | 9 +++++++++ .../blue/endless/jankson/TestDeserializer.java | 16 ++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/main/java/blue/endless/jankson/impl/CommentParserContext.java b/src/main/java/blue/endless/jankson/impl/CommentParserContext.java index c24faa5..1303182 100644 --- a/src/main/java/blue/endless/jankson/impl/CommentParserContext.java +++ b/src/main/java/blue/endless/jankson/impl/CommentParserContext.java @@ -35,6 +35,7 @@ public class CommentParserContext implements ParserContext { int prevChar = -1; + boolean startOfLine = true; boolean multiLine = false; boolean done = false; @@ -74,6 +75,14 @@ public boolean consume(int codePoint, Jankson loader) throws SyntaxError { //We're past the initiating character(s) if (multiLine) { + if (codePoint!='\n' && Character.isWhitespace(codePoint)) { + if (startOfLine) return true; + } else if (codePoint=='\n') { + startOfLine = true; + } else { + if (startOfLine) startOfLine = false; + } + if (codePoint=='/' && prevChar=='*') { result.deleteCharAt(result.length()-1); //Get rid of the * done = true; diff --git a/src/test/java/blue/endless/jankson/TestDeserializer.java b/src/test/java/blue/endless/jankson/TestDeserializer.java index 354775d..88aba3e 100644 --- a/src/test/java/blue/endless/jankson/TestDeserializer.java +++ b/src/test/java/blue/endless/jankson/TestDeserializer.java @@ -183,4 +183,20 @@ public void testUnicodeEscapes() throws SyntaxError { String slightlyUnescaped = ((JsonPrimitive)jankson.load("{'test': \"\\uu003C\" }").get("test")).asString(); Assert.assertEquals("Unicode escapes which are themselves escaped must be unescaped exactly one level.", "\\u003c", slightlyUnescaped); //implied here is that hex digit case MUST be lost } + + /** + * Issue #35: Serialize/deserialize cycles of multiline comments cause whitespace of lines after the first to creep larger and larger + * + *

Now leading whitespace is stripped from each line of multiline comments. + */ + @Test + public void testMultilineCommentReads() throws SyntaxError { + JsonObject obj = new JsonObject(); + JsonPrimitive p = new JsonPrimitive(42); + obj.put("thing", p, "this is a multiline\ncomment"); + + JsonObject recyc = jankson.load(obj.toJson(JsonGrammar.JSON5)); + + Assert.assertEquals(obj.toJson(JsonGrammar.JSON5), recyc.toJson(JsonGrammar.JSON5)); + } }