Skip to content

Commit

Permalink
Fix leading whitespace multiplying on multiline comments. Fixes #35
Browse files Browse the repository at this point in the history
  • Loading branch information
falkreon committed Feb 2, 2020
1 parent 045476e commit d025078
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class CommentParserContext implements ParserContext<String> {

int prevChar = -1;

boolean startOfLine = true;
boolean multiLine = false;
boolean done = false;

Expand Down Expand Up @@ -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;
Expand Down
16 changes: 16 additions & 0 deletions src/test/java/blue/endless/jankson/TestDeserializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
* <p>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));
}
}

0 comments on commit d025078

Please sign in to comment.