|
| 1 | +/* |
| 2 | + * Copyright 2021-2023 DiffPlug |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.diffplug.spotless.glue.json; |
| 17 | + |
| 18 | +import com.fasterxml.jackson.core.JsonFactory; |
| 19 | +import com.fasterxml.jackson.core.JsonFactoryBuilder; |
| 20 | +import com.fasterxml.jackson.core.JsonGenerator; |
| 21 | +import com.fasterxml.jackson.core.util.DefaultIndenter; |
| 22 | +import com.fasterxml.jackson.core.util.DefaultPrettyPrinter; |
| 23 | +import com.fasterxml.jackson.core.util.Separators; |
| 24 | + |
| 25 | +import com.diffplug.spotless.FormatterFunc; |
| 26 | +import com.diffplug.spotless.json.JacksonJsonConfig; |
| 27 | + |
| 28 | +/** |
| 29 | + * A {@link FormatterFunc} based on Jackson library |
| 30 | + */ |
| 31 | +// https://github.com/FasterXML/jackson-dataformats-text/issues/372 |
| 32 | +public class JacksonJsonFormatterFunc extends AJacksonFormatterFunc { |
| 33 | + private JacksonJsonConfig jacksonConfig; |
| 34 | + |
| 35 | + public JacksonJsonFormatterFunc(JacksonJsonConfig jacksonConfig) { |
| 36 | + super(jacksonConfig); |
| 37 | + this.jacksonConfig = jacksonConfig; |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * @return a {@link JsonFactory}. May be overridden to handle alternative formats. |
| 42 | + * @see <a href="https://github.com/FasterXML/jackson-dataformats-text">jackson-dataformats-text</a> |
| 43 | + */ |
| 44 | + protected JsonFactory makeJsonFactory() { |
| 45 | + JsonFactory jsonFactory = new JsonFactoryBuilder().build(); |
| 46 | + |
| 47 | + // Configure the ObjectMapper |
| 48 | + // https://github.com/FasterXML/jackson-databind#commonly-used-features |
| 49 | + jacksonConfig.getJsonFeatureToToggle().forEach((rawFeature, toggle) -> { |
| 50 | + // https://stackoverflow.com/questions/3735927/java-instantiating-an-enum-using-reflection |
| 51 | + JsonGenerator.Feature feature = JsonGenerator.Feature.valueOf(rawFeature); |
| 52 | + |
| 53 | + jsonFactory.configure(feature, toggle); |
| 54 | + }); |
| 55 | + |
| 56 | + return jsonFactory; |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + protected DefaultPrettyPrinter makePrettyPrinter() { |
| 61 | + boolean spaceBeforeSeparator = jacksonConfig.isSpaceBeforeSeparator(); |
| 62 | + |
| 63 | + // DefaultIndenter default constructor relies on 2 whitespaces as default tabulation |
| 64 | + // By we want to force '\n' as eol given Spotless provides LF-input (whatever the actual File content/current OS) |
| 65 | + DefaultPrettyPrinter.Indenter indenter = new DefaultIndenter(" ", "\n"); |
| 66 | + DefaultPrettyPrinter printer = new SpotlessJsonPrettyPrinter(spaceBeforeSeparator); |
| 67 | + |
| 68 | + printer.indentObjectsWith(indenter); |
| 69 | + printer.indentArraysWith(indenter); |
| 70 | + return printer; |
| 71 | + } |
| 72 | + |
| 73 | + protected static class SpotlessJsonPrettyPrinter extends DefaultPrettyPrinter { |
| 74 | + private static final long serialVersionUID = 1L; |
| 75 | + private final boolean spaceBeforeSeparator; |
| 76 | + |
| 77 | + public SpotlessJsonPrettyPrinter(boolean spaceBeforeSeparator) { |
| 78 | + this.spaceBeforeSeparator = spaceBeforeSeparator; |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public DefaultPrettyPrinter createInstance() { |
| 83 | + return new SpotlessJsonPrettyPrinter(spaceBeforeSeparator); |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public DefaultPrettyPrinter withSeparators(Separators separators) { |
| 88 | + this._separators = separators; |
| 89 | + if (spaceBeforeSeparator) { |
| 90 | + // This is Jackson default behavior |
| 91 | + this._objectFieldValueSeparatorWithSpaces = " " + separators.getObjectFieldValueSeparator() + " "; |
| 92 | + } else { |
| 93 | + this._objectFieldValueSeparatorWithSpaces = separators.getObjectFieldValueSeparator() + " "; |
| 94 | + } |
| 95 | + return this; |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments