diff --git a/README.md b/README.md
index 0b46ef9..9d50fc7 100644
--- a/README.md
+++ b/README.md
@@ -11,6 +11,7 @@ The data can be output in several different different forms:
* **csv** - comma separated values format
* **html** - HyperText Markup Language (HTML) format
* **json** - JavaScript Object Notation (JSON) format
+* **yaml** - Yet Another Markup Language (YAML) format
Here's an example of the standard **text** output:
```
@@ -128,6 +129,22 @@ Using the **json** format (e.g. `OutputFormat.JSON`), you get the following outp
]
```
+Using the **yaml** format (e.g. `OutputFormat.YAML`), you get the following output:
+```
+- - "Column A"
+ - "Column 2"
+ - "Column III"
+- Column A: "A"
+ Column 2: "B"
+ Column III: "c"
+- Column A: 1
+ Column 2: 1024
+ Column III: 65535
+- Column A: "Washington"
+ Column 2: "Adams"
+ Column III: "Jefferson"
+```
+
### Alignment
ASCII **text**, **rich**, and **html** support different alignments. Here's how you would update the above table:
```Java
@@ -157,7 +174,7 @@ The **html** output will have the appropriate `style="text-align:xxx"` attribute
```
### Text options
-ASCII **text** and **rich** output can hide the header row, drop the border, draw a rule under data rows, and print a caption under the table. **csv**, **html**, and **json** ignore these settings. A `null` or empty caption is skipped. Any other caption is printed unchanged.
+ASCII **text** and **rich** output can hide the header row, drop the border, draw a rule under data rows, and print a caption under the table. **csv**, **html**, **json**, and **yaml** ignore these settings. A `null` or empty caption is skipped. Any other caption is printed unchanged.
`PrettyTable::setShowHeader(false)` omits the header. Column widths then come only from the data cells, so the columns can get narrower. A table built with no headers already skips that row.
@@ -288,7 +305,7 @@ ASCII **text**, **rich**, and **html** remove redundancy by default. This de-dup
| | | F | E |
+------+------+------+------+
```
-By default, the code recognizes the redundant fields and removes them from the rendering of the data. `PrettyTable::setRemoveRedundant(false)` keeps every repeated value for `toString()` and for `formattedString(OutputFormat)`. One call can still override that setting with `formattedString(OutputFormat, boolean)`. **csv** and **json** keep every cell either way.
+By default, the code recognizes the redundant fields and removes them from the rendering of the data. `PrettyTable::setRemoveRedundant(false)` keeps every repeated value for `toString()` and for `formattedString(OutputFormat)`. One call can still override that setting with `formattedString(OutputFormat, boolean)`. **csv**, **json**, and **yaml** keep every cell either way.
In this case, either of these calls:
```Java
diff --git a/pom.xml b/pom.xml
index a007e12..fe19ae6 100644
--- a/pom.xml
+++ b/pom.xml
@@ -6,7 +6,7 @@
jar1.1.0rickwporter-prettytable
- Displays List and Map object data in a variety of forms (e.g. text [ascii], rich [utf-8], csv, html, json)
+ Displays List and Map object data in a variety of forms (e.g. text [ascii], rich [utf-8], csv, html, json, yaml)https://github.com/rickwporter/prettytable-java
@@ -37,6 +37,7 @@
3.20.01.14.1
+ 2.18.25.14.1
@@ -71,6 +72,13 @@
${version.apache.csv}
+
+
+ com.fasterxml.jackson.dataformat
+ jackson-dataformat-yaml
+ ${version.jackson}
+
+
org.junit.jupiter
diff --git a/src/main/java/rickwporter/prettytable/OutputFormat.java b/src/main/java/rickwporter/prettytable/OutputFormat.java
index 5955c01..15dc2be 100644
--- a/src/main/java/rickwporter/prettytable/OutputFormat.java
+++ b/src/main/java/rickwporter/prettytable/OutputFormat.java
@@ -6,4 +6,5 @@ public enum OutputFormat {
CSV,
HTML,
JSON,
+ YAML,
}
diff --git a/src/main/java/rickwporter/prettytable/PrettyTable.java b/src/main/java/rickwporter/prettytable/PrettyTable.java
index efe642c..c3cdce9 100644
--- a/src/main/java/rickwporter/prettytable/PrettyTable.java
+++ b/src/main/java/rickwporter/prettytable/PrettyTable.java
@@ -206,6 +206,10 @@ String toJson() {
return formattedString(OutputFormat.JSON, false);
}
+ String toYaml() {
+ return formattedString(OutputFormat.YAML, false);
+ }
+
public String formattedString(OutputFormat format) {
return this.formattedString(format, this.omitRedundant);
}
@@ -220,6 +224,8 @@ public String formattedString(OutputFormat format, boolean removeRedundant) {
renderer = new CsvTableRender();
} else if (format == OutputFormat.JSON) {
renderer = new JsonTableRender();
+ } else if (format == OutputFormat.YAML) {
+ renderer = new YamlTableRender();
} else if (format == OutputFormat.HTML) {
renderer = new HtmlTableRender();
} else {
diff --git a/src/main/java/rickwporter/prettytable/YamlTableRender.java b/src/main/java/rickwporter/prettytable/YamlTableRender.java
new file mode 100644
index 0000000..3ceb96a
--- /dev/null
+++ b/src/main/java/rickwporter/prettytable/YamlTableRender.java
@@ -0,0 +1,74 @@
+package rickwporter.prettytable;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+
+import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator;
+import com.fasterxml.jackson.dataformat.yaml.YAMLMapper;
+
+class YamlTableRender implements TableRenderInterface {
+ private static final YAMLMapper MAPPER = yamlMapper();
+
+ private static YAMLMapper yamlMapper() {
+ YAMLMapper mapper = new YAMLMapper();
+ mapper.disable(YAMLGenerator.Feature.WRITE_DOC_START_MARKER);
+ mapper.disable(YAMLGenerator.Feature.SPLIT_LINES);
+ mapper.enable(YAMLGenerator.Feature.LITERAL_BLOCK_STYLE);
+ return mapper;
+ }
+
+ private List