Skip to content

Commit

Permalink
[json] Allow comments to json formatter (all styles)
Browse files Browse the repository at this point in the history
  • Loading branch information
hazendaz committed May 29, 2024
1 parent 97825d4 commit 0eb933b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public void init(final Map<String, String> 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);
Expand All @@ -78,6 +79,10 @@ public DefaultPrettyPrinter withSeparators(final Separators separators) {
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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@
indent=4
spaceBeforeSeparator=true
alphabeticalOrder=false
allowComments=false

0 comments on commit 0eb933b

Please sign in to comment.