diff --git a/src/com/esotericsoftware/yamlbeans/YamlConfig.java b/src/com/esotericsoftware/yamlbeans/YamlConfig.java index bea4c7a..12e5fe1 100644 --- a/src/com/esotericsoftware/yamlbeans/YamlConfig.java +++ b/src/com/esotericsoftware/yamlbeans/YamlConfig.java @@ -252,6 +252,11 @@ public void setFlowStyle(boolean flowStyle) { public boolean isFlowStyle() { return flowStyle; } + + /** If true, the YAML output will be pretty flow. Default is false. */ + public void setPrettyFlow(boolean prettyFlow) { + emitterConfig.setPrettyFlow(prettyFlow); + } } static public class ReadConfig { diff --git a/src/com/esotericsoftware/yamlbeans/emitter/Emitter.java b/src/com/esotericsoftware/yamlbeans/emitter/Emitter.java index a8d038b..16735dc 100644 --- a/src/com/esotericsoftware/yamlbeans/emitter/Emitter.java +++ b/src/com/esotericsoftware/yamlbeans/emitter/Emitter.java @@ -202,6 +202,9 @@ public void expect () throws IOException { writer.writeIndicator("]", false, false, false); state = states.remove(0); } else { + if (config.prettyFlow) { + writer.writeIndent(flowLevel * config.indentSize); + } if (config.canonical || writer.column > config.wrapColumn) writer.writeIndent(indent); states.add(0, S_FLOW_SEQUENCE_ITEM); expectNode(false, true, false, false); @@ -213,6 +216,9 @@ public void expect () throws IOException { if (event.type == SEQUENCE_END) { indent = indents.remove(0); flowLevel--; + if (config.prettyFlow) { + writer.writeIndent(flowLevel * config.indentSize); + } if (config.canonical) { writer.writeIndent(indent); } @@ -220,6 +226,9 @@ public void expect () throws IOException { state = states.remove(0); } else { writer.writeIndicator(",", false, false, false); + if (config.prettyFlow) { + writer.writeIndent(flowLevel * config.indentSize); + } if (config.canonical || writer.column > config.wrapColumn) writer.writeIndent(indent); states.add(0, S_FLOW_SEQUENCE_ITEM); expectNode(false, true, false, false); @@ -234,6 +243,9 @@ public void expect () throws IOException { writer.writeIndicator("}", false, false, false); state = states.remove(0); } else { + if (config.prettyFlow) { + writer.writeIndent(flowLevel * config.indentSize); + } if (config.canonical || writer.column > config.wrapColumn) writer.writeIndent(indent); if (!config.canonical && checkSimpleKey()) { states.add(0, S_FLOW_MAPPING_SIMPLE_VALUE); @@ -266,6 +278,9 @@ public void expect () throws IOException { if (event.type == MAPPING_END) { indent = indents.remove(0); flowLevel--; + if (config.prettyFlow) { + writer.writeIndent(flowLevel * config.indentSize); + } if (config.canonical) { writer.writeIndent(indent); } @@ -273,6 +288,9 @@ public void expect () throws IOException { state = states.remove(0); } else { writer.writeIndicator(",", false, false, false); + if (config.prettyFlow) { + writer.writeIndent(flowLevel * config.indentSize); + } if (config.canonical || writer.column > config.wrapColumn) writer.writeIndent(indent); if (!config.canonical && checkSimpleKey()) { states.add(0, S_FLOW_MAPPING_SIMPLE_VALUE); diff --git a/src/com/esotericsoftware/yamlbeans/emitter/EmitterConfig.java b/src/com/esotericsoftware/yamlbeans/emitter/EmitterConfig.java index bd80c99..5e313b5 100644 --- a/src/com/esotericsoftware/yamlbeans/emitter/EmitterConfig.java +++ b/src/com/esotericsoftware/yamlbeans/emitter/EmitterConfig.java @@ -23,6 +23,7 @@ public class EmitterConfig { int indentSize = 3; int wrapColumn = 100; boolean escapeUnicode = true; + boolean prettyFlow; /** If true, the YAML output will be canonical. Default is false. */ public void setCanonical (boolean canonical) { @@ -50,4 +51,9 @@ public void setUseVerbatimTags (boolean useVerbatimTags) { public void setEscapeUnicode (boolean escapeUnicode) { this.escapeUnicode = escapeUnicode; } + + /** If true, the YAML output will be pretty flow. Default is false. */ + public void setPrettyFlow(boolean prettyFlow) { + this.prettyFlow = prettyFlow; + } } diff --git a/test/com/esotericsoftware/yamlbeans/YamlConfigTest.java b/test/com/esotericsoftware/yamlbeans/YamlConfigTest.java index aaa929c..780d7a8 100644 --- a/test/com/esotericsoftware/yamlbeans/YamlConfigTest.java +++ b/test/com/esotericsoftware/yamlbeans/YamlConfigTest.java @@ -8,6 +8,7 @@ import java.util.ArrayList; import java.util.Date; import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; @@ -502,7 +503,6 @@ public void testSetQuoteChar() throws YamlException { 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()); @@ -511,7 +511,6 @@ public void testSetQuoteChar() throws YamlException { 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()); } @@ -603,6 +602,47 @@ public void testSetFlowStyle() throws YamlException { assertEquals("{key: value}" + LINE_SEPARATOR, stringWriter.toString()); } + @Test + public void testSetPrettyFlow() throws YamlException { + Map map = new LinkedHashMap(); + map.put("key1", "value1"); + map.put("key2", "value2"); + + List list = new ArrayList(); + list.add("111"); + list.add("222"); + list.add("333"); + + yamlConfig.writeConfig.setWriteRootTags(false); + yamlConfig.writeConfig.setFlowStyle(true); + StringWriter stringWriter = new StringWriter(); + YamlWriter yamlWriter = new YamlWriter(stringWriter, yamlConfig); + yamlWriter.write(map); + yamlWriter.close(); + assertEquals("{key1: value1, key2: value2}" + LINE_SEPARATOR, stringWriter.toString()); + + stringWriter = new StringWriter(); + yamlWriter = new YamlWriter(stringWriter, yamlConfig); + yamlWriter.write(list); + yamlWriter.close(); + assertEquals("[111, 222, 333]" + LINE_SEPARATOR, stringWriter.toString()); + + yamlConfig.writeConfig.setPrettyFlow(true); + stringWriter = new StringWriter(); + yamlWriter = new YamlWriter(stringWriter, yamlConfig); + yamlWriter.write(map); + yamlWriter.close(); + assertEquals("{" + LINE_SEPARATOR + " key1: value1," + LINE_SEPARATOR + " key2: value2" + LINE_SEPARATOR + + "}" + LINE_SEPARATOR, stringWriter.toString()); + + stringWriter = new StringWriter(); + yamlWriter = new YamlWriter(stringWriter, yamlConfig); + yamlWriter.write(list); + yamlWriter.close(); + assertEquals("[" + LINE_SEPARATOR + " 111," + LINE_SEPARATOR + " 222," + LINE_SEPARATOR + " 333" + + LINE_SEPARATOR + "]" + LINE_SEPARATOR, stringWriter.toString()); + } + private String multipleSpaces(int indentSize) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < indentSize; i++) {