|
25 | 25 | import java.io.IOException;
|
26 | 26 | import java.io.Writer;
|
27 | 27 | import java.time.Instant;
|
| 28 | +import java.util.Arrays; |
28 | 29 | import java.util.Iterator;
|
29 | 30 | import java.util.Set;
|
30 | 31 |
|
|
36 | 37 | import com.fasterxml.jackson.core.TreeNode;
|
37 | 38 | import com.fasterxml.jackson.core.io.JsonStringEncoder;
|
38 | 39 | import com.fasterxml.jackson.core.io.SegmentedStringWriter;
|
| 40 | +import com.fasterxml.jackson.core.json.JsonReadFeature; |
39 | 41 | import com.fasterxml.jackson.core.type.TypeReference;
|
40 | 42 | import com.fasterxml.jackson.core.util.DefaultIndenter;
|
41 | 43 | import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
|
@@ -75,23 +77,16 @@ public final class Jackson {
|
75 | 77 | compactMapper.disable(SerializationFeature.INDENT_OUTPUT);
|
76 | 78 | prettyMapper.enable(SerializationFeature.INDENT_OUTPUT);
|
77 | 79 | // Sort the attributes when serialized via the mapper.
|
78 |
| - compactMapper.enable(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS); |
79 |
| - prettyMapper.enable(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS); |
80 |
| - |
81 |
| - compactMapper.enable(Feature.ALLOW_SINGLE_QUOTES); |
82 |
| - prettyMapper.enable(Feature.ALLOW_SINGLE_QUOTES); |
83 |
| - compactMapper.enable(Feature.ALLOW_UNQUOTED_FIELD_NAMES); |
84 |
| - prettyMapper.enable(Feature.ALLOW_UNQUOTED_FIELD_NAMES); |
85 |
| - compactMapper.enable(Feature.ALLOW_COMMENTS); |
86 |
| - prettyMapper.enable(Feature.ALLOW_COMMENTS); |
87 |
| - compactMapper.enable(Feature.ALLOW_TRAILING_COMMA); |
88 |
| - prettyMapper.enable(Feature.ALLOW_TRAILING_COMMA); |
89 |
| - compactMapper.enable(Feature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER); |
90 |
| - prettyMapper.enable(Feature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER); |
91 |
| - compactMapper.enable(Feature.ALLOW_NON_NUMERIC_NUMBERS); |
92 |
| - prettyMapper.enable(Feature.ALLOW_NON_NUMERIC_NUMBERS); |
93 |
| - compactMapper.enable(Feature.ALLOW_LEADING_DECIMAL_POINT_FOR_NUMBERS); |
94 |
| - prettyMapper.enable(Feature.ALLOW_LEADING_DECIMAL_POINT_FOR_NUMBERS); |
| 80 | + enable(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS); |
| 81 | + |
| 82 | + // Enable JSON5 features. |
| 83 | + enable(JsonReadFeature.ALLOW_SINGLE_QUOTES, |
| 84 | + JsonReadFeature.ALLOW_UNQUOTED_FIELD_NAMES, |
| 85 | + JsonReadFeature.ALLOW_JAVA_COMMENTS, |
| 86 | + JsonReadFeature.ALLOW_TRAILING_COMMA, |
| 87 | + JsonReadFeature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER, |
| 88 | + JsonReadFeature.ALLOW_NON_NUMERIC_NUMBERS, |
| 89 | + JsonReadFeature.ALLOW_LEADING_DECIMAL_POINT_FOR_NUMBERS); |
95 | 90 |
|
96 | 91 | registerModules(new SimpleModule().addSerializer(Instant.class, InstantSerializer.INSTANCE)
|
97 | 92 | .addDeserializer(Instant.class, InstantDeserializer.INSTANT));
|
@@ -145,6 +140,24 @@ public static void registerSubtypes(Class<?>... subtypes) {
|
145 | 140 | prettyMapper.registerSubtypes(subtypes);
|
146 | 141 | }
|
147 | 142 |
|
| 143 | + public static void enable(SerializationFeature... features) { |
| 144 | + for (SerializationFeature feature : features) { |
| 145 | + compactMapper.enable(feature); |
| 146 | + prettyMapper.enable(feature); |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + public static void enable(JsonReadFeature... features) { |
| 151 | + enable(Arrays.stream(features).map(JsonReadFeature::mappedFeature).toArray(Feature[]::new)); |
| 152 | + } |
| 153 | + |
| 154 | + public static void enable(Feature... features) { |
| 155 | + for (Feature feature : features) { |
| 156 | + compactMapper.enable(feature); |
| 157 | + prettyMapper.enable(feature); |
| 158 | + } |
| 159 | + } |
| 160 | + |
148 | 161 | public static <T> T readValue(String data, Class<T> type) throws JsonParseException, JsonMappingException {
|
149 | 162 | try {
|
150 | 163 | return compactMapper.readValue(data, type);
|
|
0 commit comments