From 3b0c9eafa4eb75c6257ec5cd08cf6d25c31119a6 Mon Sep 17 00:00:00 2001 From: Zahary Karadjov Date: Fri, 9 Oct 2020 19:32:25 +0300 Subject: [PATCH] Bugfix: incorrect logic in the parsing of comments fails to detect EOF properly --- json_serialization/lexer.nim | 12 +++++++----- tests/test_serialization.nim | 10 ++++++++++ 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/json_serialization/lexer.nim b/json_serialization/lexer.nim index 50d4a5e..15cbfdb 100644 --- a/json_serialization/lexer.nim +++ b/json_serialization/lexer.nim @@ -225,19 +225,21 @@ proc skipWhitespace(lexer: var JsonLexer) = checkForUnexpectedEof() case lexer.stream.peek() of '/': + advance lexer.stream while true: - advance lexer.stream if not lexer.stream.readable: return case lexer.stream.peek() of '\r': handleCR() + break of '\n': lexer.handleLF() + break else: - discard + advance lexer.stream of '*': + advance lexer.stream while true: - advance lexer.stream if not lexer.stream.readable: return case lexer.stream.peek() of '\r': @@ -249,9 +251,9 @@ proc skipWhitespace(lexer: var JsonLexer) = checkForUnexpectedEof() if lexer.stream.peek() == '/': advance lexer.stream - return + break else: - discard + advance lexer.stream else: error errCommentExpected of ' ', '\t': diff --git a/tests/test_serialization.nim b/tests/test_serialization.nim index 3dbd100..e267782 100644 --- a/tests/test_serialization.nim +++ b/tests/test_serialization.nim @@ -261,3 +261,13 @@ suite "toJson tests": test "Holders of JsonNode": testJsonHolders HasJsonNode + test "Json with comments": + const jsonContent = staticRead "./cases/comments.json" + + try: + let decoded = Json.decode(jsonContent, JsonNode) + check decoded["tasks"][0]["label"] == newJString("nim-beacon-chain build") + except SerializationError as err: + checkpoint err.formatMsg("./cases/comments.json") + fail +