diff --git a/src/main/java/com/fasterxml/jackson/dataformat/yaml/YAMLGenerator.java b/src/main/java/com/fasterxml/jackson/dataformat/yaml/YAMLGenerator.java index 555e5bd..ce91f8b 100644 --- a/src/main/java/com/fasterxml/jackson/dataformat/yaml/YAMLGenerator.java +++ b/src/main/java/com/fasterxml/jackson/dataformat/yaml/YAMLGenerator.java @@ -19,6 +19,8 @@ public class YAMLGenerator extends GeneratorBase { + + /** * Enumeration that defines all togglable features for YAML generators */ @@ -66,7 +68,18 @@ public enum Feature // implements FormatFeature // for 2.7 * * @since 2.6 */ - SPLIT_LINES(true) + 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. + * @since 2.6 + */ + MINIMIZE_QUOTES(false) ; @@ -126,11 +139,15 @@ private Feature(boolean defaultState) { // numbers, booleans, should use implicit private final static Character STYLE_SCALAR = null; // Strings quoted for fun - private final static Character STYLE_STRING = Character.valueOf('"'); - + private final static Character STYLE_QUOTED = Character.valueOf('"'); + // Strings in literal (block) style + private final static Character STYLE_LITERAL = Character.valueOf('|'); + // Which flow style to use for Base64? Maybe basic quoted? private final static Character STYLE_BASE64 = Character.valueOf('"'); + private final static Character STYLE_PLAIN = null; + /* /********************************************************** /* Output state @@ -439,7 +456,16 @@ public void writeString(String text) throws IOException,JsonGenerationException return; } _verifyValueWrite("write String value"); - _writeScalar(text, "string", STYLE_STRING); + Character style = STYLE_QUOTED; + if (Feature.MINIMIZE_QUOTES.enabledIn(_formatFeatures)) { + if (text.contains("\n")) { + style = STYLE_LITERAL; + } + else { + style = STYLE_PLAIN; + } + } + _writeScalar(text, "string", style); } @Override diff --git a/src/test/java/com/fasterxml/jackson/dataformat/yaml/SimpleGenerationTest.java b/src/test/java/com/fasterxml/jackson/dataformat/yaml/SimpleGenerationTest.java index 0d0bb81..f93eadc 100644 --- a/src/test/java/com/fasterxml/jackson/dataformat/yaml/SimpleGenerationTest.java +++ b/src/test/java/com/fasterxml/jackson/dataformat/yaml/SimpleGenerationTest.java @@ -192,6 +192,42 @@ public void testSplitLines() throws Exception yaml); } + public void testLiteralStringsSingleLine() throws Exception + { + YAMLFactory f = new YAMLFactory(); + // verify default settings + assertFalse(f.isEnabled(YAMLGenerator.Feature.MINIMIZE_QUOTES)); + + f.configure(YAMLGenerator.Feature.MINIMIZE_QUOTES, true); + + YAMLMapper mapper = new YAMLMapper(f); + + Map content = new HashMap(); + content.put("key", "some value"); + String yaml = mapper.writeValueAsString(content).trim(); + + assertEquals("---\n" + + "key: some value", yaml); + } + + public void testLiteralStringsMultiLine() throws Exception + { + YAMLFactory f = new YAMLFactory(); + // verify default settings + assertFalse(f.isEnabled(YAMLGenerator.Feature.MINIMIZE_QUOTES)); + + f.configure(YAMLGenerator.Feature.MINIMIZE_QUOTES, true); + + YAMLMapper mapper = new YAMLMapper(f); + + Map content = new HashMap(); + content.put("key", "first\nsecond\nthird"); + String yaml = mapper.writeValueAsString(content).trim(); + + assertEquals("---\n" + + "key: |-\n first\n second\n third", yaml); + } + /* /********************************************************************** /* Helper methods