From f2354179948f0bd972ed93a1a357b3ed908af737 Mon Sep 17 00:00:00 2001 From: Guillaume Raffin Date: Mon, 13 May 2024 08:44:08 +0200 Subject: [PATCH] json: Actually apply setting trailingDataAccepted --- .../nightconfig/json/JsonParser.java | 40 +++++++------------ 1 file changed, 15 insertions(+), 25 deletions(-) diff --git a/json/src/main/java/com/electronwill/nightconfig/json/JsonParser.java b/json/src/main/java/com/electronwill/nightconfig/json/JsonParser.java index 16fbce87..c8ad9fc1 100644 --- a/json/src/main/java/com/electronwill/nightconfig/json/JsonParser.java +++ b/json/src/main/java/com/electronwill/nightconfig/json/JsonParser.java @@ -124,16 +124,21 @@ public Object parseDocument(Reader reader, Config configModel) { } else { throw new ParsingException("Invalid first character for a json document: " + firstChar); } + checkNoTrailingData(input); + return result; + } - int trailing = input.readAndSkip(SPACES); - if (trailing >= 0) { - input.pushBack((char) trailing); - String msg = String.format( - "Invalid data at the end of the JSON document: %s (use JsonParser.setTrailingDataAccepted(true) if you intend this to work)", - input.read(6).toString()); - throw new ParsingException(msg); + private void checkNoTrailingData(CharacterInput input) { + if (!trailingDataAccepted) { + int trailing = input.readAndSkip(SPACES); + if (trailing >= 0) { + input.pushBack((char) trailing); + String msg = String.format( + "Invalid data at the end of the JSON document: %s (use JsonParser.setTrailingDataAccepted(true) if you intend this to work)", + input.read(6).toString()); + throw new ParsingException(msg); + } } - return result; } /** @@ -173,14 +178,7 @@ public void parse(Reader reader, Config destination, ParsingMode parsingMode) { parsingMode.prepareParsing(destination); parseObject(input, destination, parsingMode); } - int trailing = input.readAndSkip(SPACES); - if (trailing >= 0) { - input.pushBack((char) trailing); - String msg = String.format( - "Invalid data at the end of the JSON document: %s (use JsonParser.setTrailingDataAccepted(true) if you intend this to work)", - input.read(6).toString()); - throw new ParsingException(msg); - } + checkNoTrailingData(input); } /** @@ -236,15 +234,7 @@ public void parseList(Reader reader, List destination, ParsingMode parsingMod throw new ParsingException("Invalid first character for a json array: " + firstChar); } parseArray(input, destination, parsingMode, configModel); - - int trailing = input.readAndSkip(SPACES); - if (trailing >= 0) { - input.pushBack((char) trailing); - String msg = String.format( - "Invalid data at the end of the JSON document: %s (use JsonParser.setTrailingDataAccepted(true) if you intend this to work)", - input.read(6).toString()); - throw new ParsingException(msg); - } + checkNoTrailingData(input); } private T parseObject(CharacterInput input, T config, ParsingMode parsingMode) {