diff --git a/src/com/esotericsoftware/yamlbeans/YamlConfig.java b/src/com/esotericsoftware/yamlbeans/YamlConfig.java index 02a7d74..bea4c7a 100644 --- a/src/com/esotericsoftware/yamlbeans/YamlConfig.java +++ b/src/com/esotericsoftware/yamlbeans/YamlConfig.java @@ -322,7 +322,7 @@ public static enum WriteClassName { } public static enum Quote { - NONE('\0'), SINGLE('\''), DOUBLE('"'); + NONE('\0'), SINGLE('\''), DOUBLE('"'), LITERAL('|'), FOLDED('>'); char c; diff --git a/src/com/esotericsoftware/yamlbeans/YamlWriter.java b/src/com/esotericsoftware/yamlbeans/YamlWriter.java index b7fd74c..ba34909 100644 --- a/src/com/esotericsoftware/yamlbeans/YamlWriter.java +++ b/src/com/esotericsoftware/yamlbeans/YamlWriter.java @@ -244,7 +244,7 @@ private void writeValue (Object object, Class fieldClass, Class elementType, Cla if (fieldClass.isArray()) { elementType = fieldClass.getComponentType(); - emitter.emit(new SequenceStartEvent(anchor, null, true, false)); + emitter.emit(new SequenceStartEvent(anchor, null, true, config.writeConfig.isFlowStyle())); for (int i = 0, n = Array.getLength(object); i < n; i++) writeValue(Array.get(object, i), elementType, null, null); emitter.emit(Event.SEQUENCE_END); @@ -267,7 +267,7 @@ private void writeValue (Object object, Class fieldClass, Class elementType, Cla } Set properties = Beans.getProperties(valueClass, config.beanProperties, config.privateFields, config); - emitter.emit(new MappingStartEvent(anchor, tag, !showTag, false)); + emitter.emit(new MappingStartEvent(anchor, tag, !showTag, config.writeConfig.isFlowStyle())); for (Property property : properties) { try { Object propertyValue = property.get(object); diff --git a/src/com/esotericsoftware/yamlbeans/emitter/Emitter.java b/src/com/esotericsoftware/yamlbeans/emitter/Emitter.java index 87868ce..8db5dea 100644 --- a/src/com/esotericsoftware/yamlbeans/emitter/Emitter.java +++ b/src/com/esotericsoftware/yamlbeans/emitter/Emitter.java @@ -525,14 +525,20 @@ private char chooseScalarStyle () { ScalarEvent ev = (ScalarEvent)event; if (analysis == null) analysis = ScalarAnalysis.analyze(ev.value, config.escapeUnicode); if (ev.style == '"' || config.canonical) return '"'; - if (ev.style == 0 && !(simpleKeyContext && (analysis.empty || analysis.multiline)) - && ((flowLevel != 0 && analysis.allowFlowPlain) || (flowLevel == 0 && analysis.allowBlockPlain))) return 0; - if (ev.style == 0 && ev.implicit[0] && !(simpleKeyContext && (analysis.empty || analysis.multiline)) - && ((flowLevel != 0 && analysis.allowFlowPlain) || (flowLevel == 0 && analysis.allowBlockPlain))) return 0; - if ((ev.style == '|' || ev.style == '>') && flowLevel == 0 && analysis.allowBlock) return '\''; - if ((ev.style == 0 || ev.style == '\'') && analysis.allowSingleQuoted && !(simpleKeyContext && analysis.multiline)) + if ((ev.style == 0 || ev.style == '|' || ev.style == '>') + && !(simpleKeyContext && (analysis.empty || analysis.multiline)) + && ((flowLevel != 0 && analysis.allowFlowPlain) || (flowLevel == 0 && analysis.allowBlockPlain))) { + return 0; + } + if ((ev.style == 0 || ev.style == '\'') && analysis.allowSingleQuoted + && !(simpleKeyContext && analysis.multiline)) { return '\''; - if (ev.style == 0 && analysis.multiline && flowLevel == 0 && analysis.allowBlock) return '|'; + } + if ((ev.style == 0 || ev.style == '|' || ev.style == '>') && analysis.multiline && flowLevel == 0 + && analysis.allowBlock) { + return ev.style == 0 ? '|' : ev.style; + } + return '"'; } diff --git a/test/com/esotericsoftware/yamlbeans/YamlConfigTest.java b/test/com/esotericsoftware/yamlbeans/YamlConfigTest.java index 37bd6c4..aaa929c 100644 --- a/test/com/esotericsoftware/yamlbeans/YamlConfigTest.java +++ b/test/com/esotericsoftware/yamlbeans/YamlConfigTest.java @@ -496,6 +496,24 @@ public void testSetQuoteChar() throws YamlException { yamlWriter.close(); assertEquals("\"name\": \"xxx\"" + LINE_SEPARATOR + "\"age\": \"18\"" + LINE_SEPARATOR, stringWriter.toString()); + + stringWriter = new StringWriter(); + yamlConfig.writeConfig.setQuoteChar(Quote.LITERAL); + yamlWriter = new YamlWriter(stringWriter, yamlConfig); + yamlWriter.write("test\ntest"); + yamlWriter.close(); + System.out.println(stringWriter.toString()); + assertEquals("|-" + LINE_SEPARATOR + " test" + LINE_SEPARATOR + " test" + LINE_SEPARATOR, + stringWriter.toString()); + + stringWriter = new StringWriter(); + yamlConfig.writeConfig.setQuoteChar(Quote.FOLDED); + yamlWriter = new YamlWriter(stringWriter, yamlConfig); + yamlWriter.write("test\ntest"); + yamlWriter.close(); + System.out.println(stringWriter.toString()); + assertEquals(">-" + LINE_SEPARATOR + " test" + LINE_SEPARATOR + LINE_SEPARATOR + " test" + LINE_SEPARATOR, + stringWriter.toString()); } @Test