diff --git a/src/main/java/com/moandjiezana/toml/PrimitiveArrayValueWriter.java b/src/main/java/com/moandjiezana/toml/PrimitiveArrayValueWriter.java index 0f823e6..13d346c 100644 --- a/src/main/java/com/moandjiezana/toml/PrimitiveArrayValueWriter.java +++ b/src/main/java/com/moandjiezana/toml/PrimitiveArrayValueWriter.java @@ -14,7 +14,11 @@ class PrimitiveArrayValueWriter extends ArrayValueWriter { public void write(Object value, WriterContext context) { Collection values = normalize(value); - context.output.append("[ "); + context.output.append('['); + if (context.getTomlWriter().wantSpaceyArrays()) { + context.output.append(' '); + } + boolean first = true; for (Object elem : values) { if (!first) { @@ -23,7 +27,11 @@ class PrimitiveArrayValueWriter extends ArrayValueWriter { ValueWriters.WRITERS.write(elem, context); first = false; } - context.output.append(" ]"); + + if (context.getTomlWriter().wantSpaceyArrays()) { + context.output.append(' '); + } + context.output.append(']'); } private PrimitiveArrayValueWriter() {} diff --git a/src/main/java/com/moandjiezana/toml/TomlWriter.java b/src/main/java/com/moandjiezana/toml/TomlWriter.java index 28acc70..abb703b 100644 --- a/src/main/java/com/moandjiezana/toml/TomlWriter.java +++ b/src/main/java/com/moandjiezana/toml/TomlWriter.java @@ -26,6 +26,7 @@ import static com.moandjiezana.toml.ValueWriters.WRITERS; public class TomlWriter { private WriterIndentationPolicy indentationPolicy = new WriterIndentationPolicy(); + private boolean wantSpaceyArraysValue = true; /** * Creates a TomlWriter instance. @@ -93,4 +94,35 @@ public class TomlWriter { this.indentationPolicy = indentationPolicy; return this; } + + /** + *

Control spacing around array brackets in the TOML output.

+ * + *

Spacey arrays = true (default):

+ * + *

+   *   a = [ 1, 2, 3 ]
+   * 
+ * + *

Spacey arrays = false:

+ * + *

+   *   a = [1, 2, 3]
+   * 
+ * + * @param value spacey arrays setting + * @return this TomlWriter instance + */ + public TomlWriter wantSpaceyArrays(boolean value) { + this.wantSpaceyArraysValue = value; + return this; + } + + /** + * Get the current array whitespace policy + * @return the current policy + */ + public boolean wantSpaceyArrays() { + return wantSpaceyArraysValue; + } } diff --git a/src/main/java/com/moandjiezana/toml/WriterContext.java b/src/main/java/com/moandjiezana/toml/WriterContext.java index ba6edb6..e7622c7 100644 --- a/src/main/java/com/moandjiezana/toml/WriterContext.java +++ b/src/main/java/com/moandjiezana/toml/WriterContext.java @@ -79,4 +79,8 @@ class WriterContext { return new String(chars); } + + public TomlWriter getTomlWriter() { + return tomlWriter; + } }