From bd4afc7e4a962b7ff9599e008b71eef975df362c Mon Sep 17 00:00:00 2001 From: Jeremy Landis Date: Tue, 7 Feb 2023 20:18:35 -0500 Subject: [PATCH] [json] Allow comments to json formatter (all styles) --- .../java/net/revelc/code/formatter/json/JsonFormatter.java | 5 +++++ .../resources/formatter-maven-plugin/jackson/json.properties | 1 + 2 files changed, 6 insertions(+) diff --git a/src/main/java/net/revelc/code/formatter/json/JsonFormatter.java b/src/main/java/net/revelc/code/formatter/json/JsonFormatter.java index 5716b9df..7a4c2568 100644 --- a/src/main/java/net/revelc/code/formatter/json/JsonFormatter.java +++ b/src/main/java/net/revelc/code/formatter/json/JsonFormatter.java @@ -51,6 +51,7 @@ public void init(final Map options, final ConfigurationSource cf final var lineEnding = options.getOrDefault("lineending", System.lineSeparator()); final var spaceBeforeSeparator = Boolean.parseBoolean(options.getOrDefault("spaceBeforeSeparator", "true")); final var useAlphabeticalOrder = Boolean.parseBoolean(options.getOrDefault("alphabeticalOrder", "false")); + final var allowComments = Boolean.parseBoolean(options.getOrDefault("allowComments", "false")); // Setup a pretty printer with an indenter (indenter has 4 spaces in this case) final DefaultPrettyPrinter.Indenter indenter = new DefaultIndenter(" ".repeat(indent), lineEnding); @@ -72,6 +73,10 @@ public DefaultPrettyPrinter createInstance() { this.formatter = new ObjectMapper(); this.formatter.setDefaultPrettyPrinter(printer); this.formatter.enable(SerializationFeature.INDENT_OUTPUT); + if (allowComments) { + this.formatter.enable(JsonParser.Feature.ALLOW_COMMENTS); + this.formatter.enable(JsonParser.Feature.ALLOW_YAML_COMMENTS); + } this.formatter.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, useAlphabeticalOrder); this.options = options; } diff --git a/src/main/resources/formatter-maven-plugin/jackson/json.properties b/src/main/resources/formatter-maven-plugin/jackson/json.properties index f4e3319a..7ada5a38 100644 --- a/src/main/resources/formatter-maven-plugin/jackson/json.properties +++ b/src/main/resources/formatter-maven-plugin/jackson/json.properties @@ -15,3 +15,4 @@ indent=4 spaceBeforeSeparator=true alphabeticalOrder=false +allowComments=false