[#5973] Allow for formatting / indenting JSON output through JSONFormat
This commit is contained in:
parent
bedf7e78b3
commit
cb2dc73cb9
@ -63,29 +63,104 @@ public final class JSONFormat {
|
||||
public static final JSONFormat DEFAULT_FOR_RESULTS = new JSONFormat();
|
||||
public static final JSONFormat DEFAULT_FOR_RECORDS = new JSONFormat().header(false);
|
||||
|
||||
final boolean format;
|
||||
final String newline;
|
||||
final int indent;
|
||||
final boolean header;
|
||||
final RecordFormat recordFormat;
|
||||
|
||||
public JSONFormat() {
|
||||
this(
|
||||
false,
|
||||
"\n",
|
||||
2,
|
||||
true,
|
||||
RecordFormat.ARRAY
|
||||
);
|
||||
}
|
||||
|
||||
private JSONFormat(
|
||||
boolean format,
|
||||
String newline,
|
||||
int indent,
|
||||
boolean header,
|
||||
RecordFormat recordFormat) {
|
||||
RecordFormat recordFormat
|
||||
) {
|
||||
this.format = format;
|
||||
this.newline = newline;
|
||||
this.indent = indent;
|
||||
this.header = header;
|
||||
this.recordFormat = recordFormat;
|
||||
}
|
||||
|
||||
/**
|
||||
* The new value for the formatting flag, defaulting to <code>false</code>.
|
||||
*/
|
||||
public JSONFormat format(boolean newFormat) {
|
||||
return new JSONFormat(
|
||||
newFormat,
|
||||
newline,
|
||||
indent,
|
||||
header,
|
||||
recordFormat);
|
||||
}
|
||||
|
||||
/**
|
||||
* The formatting flag.
|
||||
*/
|
||||
public boolean format() {
|
||||
return format;
|
||||
}
|
||||
|
||||
/**
|
||||
* The new newline character, defaulting to <code>\n</code>.
|
||||
*/
|
||||
public JSONFormat newline(String newNewline) {
|
||||
return new JSONFormat(
|
||||
format,
|
||||
newNewline,
|
||||
indent,
|
||||
header,
|
||||
recordFormat
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* The formatting flag.
|
||||
*/
|
||||
public String newline() {
|
||||
return format ? newline : "";
|
||||
}
|
||||
|
||||
/**
|
||||
* The new indentation value, defaulting to <code>2</code>.
|
||||
*/
|
||||
public JSONFormat indent(int newIndent) {
|
||||
return new JSONFormat(
|
||||
format,
|
||||
newline,
|
||||
newIndent,
|
||||
header,
|
||||
recordFormat
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* The indentation.
|
||||
*/
|
||||
public int indent() {
|
||||
return indent;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether to emit a header row with column names, defaulting to
|
||||
* <code>true</code>.
|
||||
*/
|
||||
public JSONFormat header(boolean newHeader) {
|
||||
return new JSONFormat(
|
||||
format,
|
||||
newline,
|
||||
indent,
|
||||
newHeader,
|
||||
recordFormat);
|
||||
}
|
||||
@ -104,6 +179,9 @@ public final class JSONFormat {
|
||||
*/
|
||||
public JSONFormat recordFormat(RecordFormat newRecordFormat) {
|
||||
return new JSONFormat(
|
||||
format,
|
||||
newline,
|
||||
indent,
|
||||
header,
|
||||
newRecordFormat);
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user