Add a way to control spacing around array brackets.

This commit is contained in:
Jonathan Wood 2015-06-28 11:26:49 -07:00
parent 72941c146d
commit ecbd9f048d
3 changed files with 46 additions and 2 deletions

View file

@ -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() {}

View file

@ -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;
}
/**
* <p>Control spacing around array brackets in the TOML output.</p>
*
* <p>Spacey arrays = true (default):</p>
*
* <pre><code>
* a = [ 1, 2, 3 ]
* </code></pre>
*
* <p>Spacey arrays = false:</p>
*
* <pre><code>
* a = [1, 2, 3]
* </code></pre>
*
* @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;
}
}

View file

@ -79,4 +79,8 @@ class WriterContext {
return new String(chars);
}
public TomlWriter getTomlWriter() {
return tomlWriter;
}
}