diff --git a/yaml/src/main/java/com/fasterxml/jackson/dataformat/yaml/YAMLGenerator.java b/yaml/src/main/java/com/fasterxml/jackson/dataformat/yaml/YAMLGenerator.java index 6809b88f..648b8d36 100644 --- a/yaml/src/main/java/com/fasterxml/jackson/dataformat/yaml/YAMLGenerator.java +++ b/yaml/src/main/java/com/fasterxml/jackson/dataformat/yaml/YAMLGenerator.java @@ -591,13 +591,13 @@ public void writeString(String text) throws IOException,JsonGenerationException DumperOptions.ScalarStyle style; if (Feature.MINIMIZE_QUOTES.enabledIn(_formatFeatures)) { // If one of reserved values ("true", "null"), or, number, preserve quoting: - if (_quotingChecker.needToQuoteValue(text) + if (text.indexOf('\n') >= 0) { + style = STYLE_LITERAL; + } else if (_quotingChecker.needToQuoteValue(text) || (Feature.ALWAYS_QUOTE_NUMBERS_AS_STRINGS.enabledIn(_formatFeatures) && PLAIN_NUMBER_P.matcher(text).matches()) ) { style = STYLE_QUOTED; - } else if (text.indexOf('\n') >= 0) { - style = STYLE_LITERAL; } else { style = STYLE_PLAIN; } diff --git a/yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/ser/GeneratorWithMinimizeTest.java b/yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/ser/GeneratorWithMinimizeTest.java index 0b651726..b4dc6938 100644 --- a/yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/ser/GeneratorWithMinimizeTest.java +++ b/yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/ser/GeneratorWithMinimizeTest.java @@ -175,6 +175,16 @@ public void testLiteralStringsMultiLine() throws Exception "key: |-\n first\n second\n third", yaml); } + public void testMinimizeQuotesWithStringsContainingSpecialCharsMultiLine() throws Exception + { + Map content = new HashMap(); + content.put("key", "first\nsecond: third"); + String yaml = MINIM_MAPPER.writeValueAsString(content).trim(); + + assertEquals("---\n" + + "key: |-\n first\n second: third", yaml); + } + public void testQuoteNumberStoredAsString() throws Exception { YAMLFactory f = new YAMLFactory();