mirror of
https://github.com/plexusorg/toml4j.git
synced 2025-02-22 07:54:28 +00:00
Add a way to control spacing around array brackets.
This commit is contained in:
parent
72941c146d
commit
ecbd9f048d
3 changed files with 46 additions and 2 deletions
|
@ -14,7 +14,11 @@ class PrimitiveArrayValueWriter extends ArrayValueWriter {
|
||||||
public void write(Object value, WriterContext context) {
|
public void write(Object value, WriterContext context) {
|
||||||
Collection values = normalize(value);
|
Collection values = normalize(value);
|
||||||
|
|
||||||
context.output.append("[ ");
|
context.output.append('[');
|
||||||
|
if (context.getTomlWriter().wantSpaceyArrays()) {
|
||||||
|
context.output.append(' ');
|
||||||
|
}
|
||||||
|
|
||||||
boolean first = true;
|
boolean first = true;
|
||||||
for (Object elem : values) {
|
for (Object elem : values) {
|
||||||
if (!first) {
|
if (!first) {
|
||||||
|
@ -23,7 +27,11 @@ class PrimitiveArrayValueWriter extends ArrayValueWriter {
|
||||||
ValueWriters.WRITERS.write(elem, context);
|
ValueWriters.WRITERS.write(elem, context);
|
||||||
first = false;
|
first = false;
|
||||||
}
|
}
|
||||||
context.output.append(" ]");
|
|
||||||
|
if (context.getTomlWriter().wantSpaceyArrays()) {
|
||||||
|
context.output.append(' ');
|
||||||
|
}
|
||||||
|
context.output.append(']');
|
||||||
}
|
}
|
||||||
|
|
||||||
private PrimitiveArrayValueWriter() {}
|
private PrimitiveArrayValueWriter() {}
|
||||||
|
|
|
@ -26,6 +26,7 @@ import static com.moandjiezana.toml.ValueWriters.WRITERS;
|
||||||
public class TomlWriter {
|
public class TomlWriter {
|
||||||
|
|
||||||
private WriterIndentationPolicy indentationPolicy = new WriterIndentationPolicy();
|
private WriterIndentationPolicy indentationPolicy = new WriterIndentationPolicy();
|
||||||
|
private boolean wantSpaceyArraysValue = true;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a TomlWriter instance.
|
* Creates a TomlWriter instance.
|
||||||
|
@ -93,4 +94,35 @@ public class TomlWriter {
|
||||||
this.indentationPolicy = indentationPolicy;
|
this.indentationPolicy = indentationPolicy;
|
||||||
return this;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -79,4 +79,8 @@ class WriterContext {
|
||||||
|
|
||||||
return new String(chars);
|
return new String(chars);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public TomlWriter getTomlWriter() {
|
||||||
|
return tomlWriter;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue