diff --git a/release-notes/VERSION b/release-notes/VERSION index b123d9eb5..49eb2d36b 100644 --- a/release-notes/VERSION +++ b/release-notes/VERSION @@ -22,6 +22,8 @@ implementations) (suggested by @SebastianSolidwork) #510: JSTEP-8: rename `CsvParser.Feature` as `CsvReadFeature`, `CsvGenerator.Feature` as `CsvWriteFeature` +#512: JSTEP-8: rename `YAMLParser.Feature` as `YAMLReadFeature`, + `YAMLGenerator.Feature` as `YAMLWriteFeature` - Add `XxxMapper.shared()` methods for getting globally shared default instances CSV: diff --git a/toml/src/main/java/tools/jackson/dataformat/toml/TomlReadFeature.java b/toml/src/main/java/tools/jackson/dataformat/toml/TomlReadFeature.java index 8c7619ed6..5a4afb5f8 100644 --- a/toml/src/main/java/tools/jackson/dataformat/toml/TomlReadFeature.java +++ b/toml/src/main/java/tools/jackson/dataformat/toml/TomlReadFeature.java @@ -6,7 +6,8 @@ * Enumeration that defines all togglable features for TOML parsers. */ public enum TomlReadFeature - implements FormatFeature { + implements FormatFeature +{ /** * TOML has special syntax for time types corresponding to {@link java.time.LocalDate}, {@link java.time.LocalTime}, * {@link java.time.LocalDateTime} and {@link java.time.OffsetDateTime}. By default, the TOML parser just returns @@ -17,8 +18,8 @@ public enum TomlReadFeature */ PARSE_JAVA_TIME(false); - final boolean _defaultState; - final int _mask; + private final boolean _defaultState; + private final int _mask; // Method that calculates bit set (flags) of all features that // are enabled by default. diff --git a/toml/src/main/java/tools/jackson/dataformat/toml/TomlWriteFeature.java b/toml/src/main/java/tools/jackson/dataformat/toml/TomlWriteFeature.java index 2c6a48242..df4209b3b 100644 --- a/toml/src/main/java/tools/jackson/dataformat/toml/TomlWriteFeature.java +++ b/toml/src/main/java/tools/jackson/dataformat/toml/TomlWriteFeature.java @@ -6,7 +6,8 @@ /** * Enumeration that defines all togglable features for TOML generators. */ -public enum TomlWriteFeature implements FormatFeature { +public enum TomlWriteFeature implements FormatFeature +{ /** * The TOML spec does not allow null values. We instead write an empty string when * {@link JsonGenerator#writeNull()} by default. @@ -20,8 +21,8 @@ public enum TomlWriteFeature implements FormatFeature { */ static final int INTERNAL_PROHIBIT_INTERNAL_BUFFER_ALLOCATE = 0x80000000; - final boolean _defaultState; - final int _mask; + private final boolean _defaultState; + private final int _mask; // Method that calculates bit set (flags) of all features that // are enabled by default. diff --git a/yaml/src/main/java/tools/jackson/dataformat/yaml/YAMLFactory.java b/yaml/src/main/java/tools/jackson/dataformat/yaml/YAMLFactory.java index 00da38c9b..9ee1193df 100644 --- a/yaml/src/main/java/tools/jackson/dataformat/yaml/YAMLFactory.java +++ b/yaml/src/main/java/tools/jackson/dataformat/yaml/YAMLFactory.java @@ -29,13 +29,13 @@ public class YAMLFactory * Bitfield (set of flags) of all generator features that are enabled * by default. */ - protected final static int DEFAULT_YAML_PARSER_FEATURE_FLAGS = YAMLParser.Feature.collectDefaults(); + protected final static int DEFAULT_YAML_PARSER_FEATURE_FLAGS = YAMLReadFeature.collectDefaults(); /** * Bitfield (set of flags) of all generator features that are enabled * by default. */ - protected final static int DEFAULT_YAML_GENERATOR_FEATURE_FLAGS = YAMLGenerator.Feature.collectDefaults(); + protected final static int DEFAULT_YAML_GENERATOR_FEATURE_FLAGS = YAMLWriteFeature.collectDefaults(); /* /********************************************************************** @@ -64,9 +64,9 @@ public class YAMLFactory /** * Configuration for underlying generator to follow, if specified; * left as {@code null} for backwards compatibility (which means - * the dumper options are derived based on {@link YAMLGenerator.Feature}s). + * the dumper options are derived based on {@link YAMLWriteFeature}s). *
- * These {@link YAMLGenerator.Feature}s are ignored if you provide your own DumperOptions: + * These {@link YAMLWriteFeature}s are ignored if you provide your own DumperOptions: *
- * These {@link YAMLGenerator.Feature}s are ignored if you provide your own DumperOptions: + * These {@link YAMLWriteFeature}s are ignored if you provide your own DumperOptions: *
- * These {@link YAMLGenerator.Feature}s are ignored if you provide your own DumperOptions: + * These {@link YAMLWriteFeature}s are ignored if you provide your own DumperOptions: *
- * These {@link YAMLGenerator.Feature}s are ignored if you provide your own DumperOptions: + * These {@link YAMLWriteFeature}s are ignored if you provide your own DumperOptions: *
- * Ignored if you provide your own {@code DumperOptions}. - *
- */ - CANONICAL_OUTPUT(false), - - /** - * Options passed to SnakeYAML that determines whether longer textual content - * gets automatically split into multiple lines or not. - *- * Feature is enabled by default to conform to SnakeYAML defaults as well as - * backwards compatibility with 2.5 and earlier versions. - *
- *- * Ignored if you provide your own {@code DumperOptions}. - *
- */ - SPLIT_LINES(true), - - /** - * Whether strings will be rendered without quotes (true) or - * with quotes (false, default). - *- * Minimized quote usage makes for more human readable output; however, content is - * limited to printable characters according to the rules of - * literal block style. - *
- */ - MINIMIZE_QUOTES(false), - - /** - * Whether numbers stored as strings will be rendered with quotes (true) or - * without quotes (false, default) when MINIMIZE_QUOTES is enabled. - *- * Minimized quote usage makes for more human readable output; however, content is - * limited to printable characters according to the rules of - * literal block style. - *
- */ - ALWAYS_QUOTE_NUMBERS_AS_STRINGS(false), - - /** - * Whether for string containing newlines a - * literal block style - * should be used. This automatically enabled when {@link #MINIMIZE_QUOTES} is set. - *- * The content of such strings is limited to printable characters according to the rules of - * literal block style. - *
- */ - LITERAL_BLOCK_STYLE(false), - - /** - * Feature enabling of which adds indentation for array entry generation - * (default indentation being 2 spaces). - *- * Default value is {@code false} for backwards compatibility - *
- *- * Ignored if you provide your own {@code DumperOptions}. - *
- * - */ - INDENT_ARRAYS(false), - - /** - * Feature enabling of which adds indentation with indicator for array entry generation - * (default indentation being 2 spaces). - *- * Default value is {@code false} for backwards compatibility - *
- *- * Ignored if you provide your own {@code DumperOptions}. - *
- * - */ - INDENT_ARRAYS_WITH_INDICATOR(false), - - /** - * Option passed to SnakeYAML to allows writing key longer that 128 characters - * (up to 1024 characters). - * If disabled, the max key length is left as 128 characters: longer names - * are truncated. If enabled, limit is raised to 1024 characters. - *- * Ignored if you provide your own {@code DumperOptions}. - *
- */ - ALLOW_LONG_KEYS(false), - ; - - protected final boolean _defaultState; - protected final int _mask; - - /** - * Method that calculates bit set (flags) of all features that - * are enabled by default. - */ - public static int collectDefaults() - { - int flags = 0; - for (Feature f : values()) { - if (f.enabledByDefault()) { - flags |= f.getMask(); - } - } - return flags; - } - - private Feature(boolean defaultState) { - _defaultState = defaultState; - _mask = (1 << ordinal()); - } - - @Override - public boolean enabledByDefault() { return _defaultState; } - @Override - public boolean enabledIn(int flags) { return (flags & _mask) != 0; } - @Override - public int getMask() { return _mask; } - } - /* /********************************************************************** /* Internal constants @@ -213,7 +63,7 @@ private Feature(boolean defaultState) { */ /** - * Bit flag composed of bits that indicate which {@link YAMLGenerator.Feature}s + * Bit flag composed of bits that indicate which {@link YAMLWriteFeature}s * are enabled. */ protected int _formatWriteFeatures; @@ -270,7 +120,7 @@ public YAMLGenerator(ObjectWriteContext writeContext, IOContext ioCtxt, _streamWriteContext = SimpleStreamWriteContext.createRootContext(dups); _formatWriteFeatures = yamlFeatures; - _cfgMinimizeQuotes = Feature.MINIMIZE_QUOTES.enabledIn(_formatWriteFeatures); + _cfgMinimizeQuotes = YAMLWriteFeature.MINIMIZE_QUOTES.enabledIn(_formatWriteFeatures); _quotingChecker = quotingChecker; _writer = out; _docVersion = version; @@ -291,7 +141,7 @@ protected DumpSettings buildDumperOptions(int streamWriteFeatures, int yamlFeatu { DumpSettingsBuilder opt = DumpSettings.builder(); // would we want canonical? - if (Feature.CANONICAL_OUTPUT.enabledIn(_formatWriteFeatures)) { + if (YAMLWriteFeature.CANONICAL_OUTPUT.enabledIn(_formatWriteFeatures)) { opt.setCanonical(true); } else { opt.setCanonical(false); @@ -299,9 +149,9 @@ protected DumpSettings buildDumperOptions(int streamWriteFeatures, int yamlFeatu opt.setDefaultFlowStyle(FlowStyle.BLOCK); } // split-lines for text blocks? - opt.setSplitLines(Feature.SPLIT_LINES.enabledIn(_formatWriteFeatures)); + opt.setSplitLines(YAMLWriteFeature.SPLIT_LINES.enabledIn(_formatWriteFeatures)); // array indentation? - if (Feature.INDENT_ARRAYS.enabledIn(_formatWriteFeatures)) { + if (YAMLWriteFeature.INDENT_ARRAYS.enabledIn(_formatWriteFeatures)) { // But, wrt [dataformats-text#34]: need to set both to diff values to work around bug // (otherwise indentation level is "invisible". Note that this should NOT be necessary // but is needed up to at least SnakeYAML 1.18. @@ -310,11 +160,11 @@ protected DumpSettings buildDumperOptions(int streamWriteFeatures, int yamlFeatu opt.setIndent(2); } // [dataformats-text#175]: further configurability that overrides prev setting - if (Feature.INDENT_ARRAYS_WITH_INDICATOR.enabledIn(_formatWriteFeatures)) { + if (YAMLWriteFeature.INDENT_ARRAYS_WITH_INDICATOR.enabledIn(_formatWriteFeatures)) { opt.setIndicatorIndent(2); opt.setIndentWithIndicator(true); } - if (Feature.ALLOW_LONG_KEYS.enabledIn(_formatWriteFeatures)) { + if (YAMLWriteFeature.ALLOW_LONG_KEYS.enabledIn(_formatWriteFeatures)) { opt.setMaxSimpleKeyLength(1024); } @@ -385,7 +235,7 @@ public JacksonFeatureSet- * Feature is enabled by default in Jackson 2.12 for backwards-compatibility - * reasons. - */ - EMPTY_STRING_AS_NULL(true) - ; - - private final boolean _defaultState; - private final int _mask; - - // Method that calculates bit set (flags) of all features that - // are enabled by default. - public static int collectDefaults() - { - int flags = 0; - for (Feature f : values()) { - if (f.enabledByDefault()) { - flags |= f.getMask(); - } - } - return flags; - } - - private Feature(boolean defaultState) { - _defaultState = defaultState; - _mask = (1 << ordinal()); - } - - @Override - public boolean enabledByDefault() { return _defaultState; } - @Override - public boolean enabledIn(int flags) { return (flags & _mask) != 0; } - @Override - public int getMask() { return _mask; } - } - - // note: does NOT include '0', handled separately -// private final static Pattern PATTERN_INT = Pattern.compile("-?[1-9][0-9]*"); - - // 22-Nov-2020, tatu: Not needed as of 2.12 since SnakeYAML tags - // doubles correctly -// private final static Pattern PATTERN_FLOAT = Pattern.compile( -// "[-+]?([0-9][0-9_]*)?\\.[0-9]*([eE][-+][0-9]+)?"); - /* /********************************************************************** /* Configuration @@ -190,7 +137,7 @@ protected YAMLParser(ObjectReadContext readCtxt, IOContext ioCtxt, BufferRecycle _formatFeatures = formatFeatures; _reader = reader; _yamlParser = yamlParser; - _cfgEmptyStringsToNull = Feature.EMPTY_STRING_AS_NULL.enabledIn(formatFeatures); + _cfgEmptyStringsToNull = YAMLReadFeature.EMPTY_STRING_AS_NULL.enabledIn(formatFeatures); DupDetector dups = StreamReadFeature.STRICT_DUPLICATE_DETECTION.enabledIn(streamReadFeatures) ? DupDetector.rootDetector(this) : null; _streamReadContext = SimpleStreamReadContext.createRootContext(dups); @@ -289,10 +236,10 @@ protected void _closeInput() throws IOException { */ /** - * Method for checking whether specified {@link YAMLParser.Feature} + * Method for checking whether specified {@link YAMLReadFeature} * is enabled. */ - public boolean isEnabled(YAMLParser.Feature f) { + public boolean isEnabled(YAMLReadFeature f) { return (_formatFeatures & f.getMask()) != 0; } diff --git a/yaml/src/main/java/tools/jackson/dataformat/yaml/YAMLReadFeature.java b/yaml/src/main/java/tools/jackson/dataformat/yaml/YAMLReadFeature.java new file mode 100644 index 000000000..1618f17a0 --- /dev/null +++ b/yaml/src/main/java/tools/jackson/dataformat/yaml/YAMLReadFeature.java @@ -0,0 +1,49 @@ +package tools.jackson.dataformat.yaml; + +import tools.jackson.core.FormatFeature; + +/** + * Enumeration that defines all togglable features for YAML parsers. + *
+ * NOTE: in Jackson 2.x this was named {@code YAMLParser.Feature}. + */ +public enum YAMLReadFeature implements FormatFeature +{ + /** + * Feature that determines whether an empty {@link String} will be parsed + * as {@code null}. Logic is part of YAML 1.1 + * Null Language-Independent Type. + *
+ * Feature is enabled by default for backwards-compatibility reasons. + */ + EMPTY_STRING_AS_NULL(true) + ; + + private final boolean _defaultState; + private final int _mask; + + // Method that calculates bit set (flags) of all features that + // are enabled by default. + public static int collectDefaults() + { + int flags = 0; + for (YAMLReadFeature f : values()) { + if (f.enabledByDefault()) { + flags |= f.getMask(); + } + } + return flags; + } + + private YAMLReadFeature(boolean defaultState) { + _defaultState = defaultState; + _mask = (1 << ordinal()); + } + + @Override + public boolean enabledByDefault() { return _defaultState; } + @Override + public boolean enabledIn(int flags) { return (flags & _mask) != 0; } + @Override + public int getMask() { return _mask; } +} diff --git a/yaml/src/main/java/tools/jackson/dataformat/yaml/YAMLWriteFeature.java b/yaml/src/main/java/tools/jackson/dataformat/yaml/YAMLWriteFeature.java new file mode 100644 index 000000000..ded3726a2 --- /dev/null +++ b/yaml/src/main/java/tools/jackson/dataformat/yaml/YAMLWriteFeature.java @@ -0,0 +1,154 @@ +package tools.jackson.dataformat.yaml; + +import tools.jackson.core.FormatFeature; + +/** + * Enumeration that defines all togglable features for YAML generators + *
+ * NOTE: in Jackson 2.x this was named {@code YAMLGenerator.Feature}. + */ +public enum YAMLWriteFeature implements FormatFeature +{ + /** + * Whether we are to write an explicit document start marker ("---") + * or not. + */ + WRITE_DOC_START_MARKER(true), + + /** + * Whether to use YAML native Object Id construct for indicating type (true); + * or "generic" Object Id mechanism (false). Former works better for systems that + * are YAML-centric; latter may be better choice for interoperability, when + * converting between formats or accepting other formats. + */ + USE_NATIVE_OBJECT_ID(true), + + /** + * Whether to use YAML native Type Id construct for indicating type (true); + * or "generic" type property (false). Former works better for systems that + * are YAML-centric; latter may be better choice for interoperability, when + * converting between formats or accepting other formats. + */ + USE_NATIVE_TYPE_ID(true), + + /** + * Do we try to force so-called canonical output or not. + *
+ * Ignored if you provide your own {@code DumperOptions}. + *
+ */ + CANONICAL_OUTPUT(false), + + /** + * Options passed to SnakeYAML that determines whether longer textual content + * gets automatically split into multiple lines or not. + *+ * Feature is enabled by default to conform to SnakeYAML defaults. + *
+ *+ * Ignored if you provide your own {@code DumperOptions}. + *
+ */ + SPLIT_LINES(true), + + /** + * Whether strings will be rendered without quotes (true) or + * with quotes (false, default). + *+ * Minimized quote usage makes for more human readable output; however, content is + * limited to printable characters according to the rules of + * literal block style. + *
+ */ + MINIMIZE_QUOTES(false), + + /** + * Whether numbers stored as strings will be rendered with quotes (true) or + * without quotes (false, default) when MINIMIZE_QUOTES is enabled. + *+ * Minimized quote usage makes for more human readable output; however, content is + * limited to printable characters according to the rules of + * literal block style. + *
+ */ + ALWAYS_QUOTE_NUMBERS_AS_STRINGS(false), + + /** + * Whether for string containing newlines a + * literal block style + * should be used. This automatically enabled when {@link #MINIMIZE_QUOTES} is set. + *+ * The content of such strings is limited to printable characters according to the rules of + * literal block style. + *
+ */ + LITERAL_BLOCK_STYLE(false), + + /** + * Feature enabling of which adds indentation for array entry generation + * (default indentation being 2 spaces). + *+ * Default value is {@code false} for backwards compatibility + *
+ *+ * Ignored if you provide your own {@code DumperOptions}. + *
+ * + */ + INDENT_ARRAYS(false), + + /** + * Feature enabling of which adds indentation with indicator for array entry generation + * (default indentation being 2 spaces). + *+ * Default value is {@code false} for backwards compatibility + *
+ *+ * Ignored if you provide your own {@code DumperOptions}. + *
+ * + */ + INDENT_ARRAYS_WITH_INDICATOR(false), + + /** + * Option passed to SnakeYAML to allows writing key longer that 128 characters + * (up to 1024 characters). + * If disabled, the max key length is left as 128 characters: longer names + * are truncated. If enabled, limit is raised to 1024 characters. + *+ * Ignored if you provide your own {@code DumperOptions}. + *
+ */ + ALLOW_LONG_KEYS(false), + ; + + private final boolean _defaultState; + private final int _mask; + + /** + * Method that calculates bit set (flags) of all features that + * are enabled by default. + */ + public static int collectDefaults() + { + int flags = 0; + for (YAMLWriteFeature f : values()) { + if (f.enabledByDefault()) { + flags |= f.getMask(); + } + } + return flags; + } + + private YAMLWriteFeature(boolean defaultState) { + _defaultState = defaultState; + _mask = (1 << ordinal()); + } + + @Override + public boolean enabledByDefault() { return _defaultState; } + @Override + public boolean enabledIn(int flags) { return (flags & _mask) != 0; } + @Override + public int getMask() { return _mask; } +} diff --git a/yaml/src/test/java/tools/jackson/dataformat/yaml/deser/NullFromEmptyString130Test.java b/yaml/src/test/java/tools/jackson/dataformat/yaml/deser/NullFromEmptyString130Test.java index f00e49f64..89f90515c 100644 --- a/yaml/src/test/java/tools/jackson/dataformat/yaml/deser/NullFromEmptyString130Test.java +++ b/yaml/src/test/java/tools/jackson/dataformat/yaml/deser/NullFromEmptyString130Test.java @@ -2,7 +2,7 @@ import tools.jackson.dataformat.yaml.ModuleTestBase; import tools.jackson.dataformat.yaml.YAMLMapper; -import tools.jackson.dataformat.yaml.YAMLParser; +import tools.jackson.dataformat.yaml.YAMLReadFeature; public class NullFromEmptyString130Test extends ModuleTestBase { @@ -20,7 +20,7 @@ public void setValue(String str) { public void testEmptyValueToNull130() throws Exception { // by default, empty Strings are coerced: - assertTrue(MAPPER.tokenStreamFactory().isEnabled(YAMLParser.Feature.EMPTY_STRING_AS_NULL)); + assertTrue(MAPPER.tokenStreamFactory().isEnabled(YAMLReadFeature.EMPTY_STRING_AS_NULL)); { Value130 v = MAPPER.readValue("value: \n", Value130.class); @@ -33,7 +33,7 @@ public void testEmptyValueToNull130() throws Exception // but can change that: { Value130 v = MAPPER.readerFor(Value130.class) - .without(YAMLParser.Feature.EMPTY_STRING_AS_NULL) + .without(YAMLReadFeature.EMPTY_STRING_AS_NULL) .readValue("value: \n"); assertEquals("", v.value); } diff --git a/yaml/src/test/java/tools/jackson/dataformat/yaml/failing/SimpleGeneration366Test.java b/yaml/src/test/java/tools/jackson/dataformat/yaml/failing/SimpleGeneration366Test.java index c3dabf0e2..4ce33e742 100644 --- a/yaml/src/test/java/tools/jackson/dataformat/yaml/failing/SimpleGeneration366Test.java +++ b/yaml/src/test/java/tools/jackson/dataformat/yaml/failing/SimpleGeneration366Test.java @@ -12,10 +12,10 @@ public void testLiteralBlockStyleMultilineWithTrailingSpace() throws Exception { YAMLFactory f = new YAMLFactory(); // verify default settings - assertFalse(f.isEnabled(YAMLGenerator.Feature.LITERAL_BLOCK_STYLE)); + assertFalse(f.isEnabled(YAMLWriteFeature.LITERAL_BLOCK_STYLE)); YAMLMapper mapper = YAMLMapper.builder() - .configure(YAMLGenerator.Feature.LITERAL_BLOCK_STYLE, true) + .configure(YAMLWriteFeature.LITERAL_BLOCK_STYLE, true) .build(); Map