diff --git a/README.md b/README.md index 82835b9..ce9ada4 100644 --- a/README.md +++ b/README.md @@ -245,7 +245,9 @@ toml.containsTableArray("a"); // false ### Converting Objects To TOML -You can write any arbitrary object to a TOML `String`, `File`, `Writer`, or `OutputStream` with a `TomlWriter`. Each TomlWriter instance is customisable, immutable and threadsafe, so it can be reused and passed around. Constants and transient fields are ignored. +You can write `Map`s and custom objects to a TOML `String`, `File`, `Writer`, or `OutputStream` with a `TomlWriter`. Each TomlWriter instance is customisable, immutable and threadsafe, so it can be reused and passed around. Constants and transient fields are ignored. + +To write a `List` of objects as a table array, put the list in a `Map` or in a custom object. ```java class AClass { diff --git a/src/test/java/com/moandjiezana/toml/TomlWriterTest.java b/src/test/java/com/moandjiezana/toml/TomlWriterTest.java index 44fc986..89f1a4f 100644 --- a/src/test/java/com/moandjiezana/toml/TomlWriterTest.java +++ b/src/test/java/com/moandjiezana/toml/TomlWriterTest.java @@ -14,6 +14,7 @@ import java.math.BigDecimal; import java.math.BigInteger; import java.net.URI; import java.net.URL; +import java.util.ArrayList; import java.util.Arrays; import java.util.Calendar; import java.util.Date; @@ -168,7 +169,7 @@ public class TomlWriterTest { } @Test - public void should_write_array_of_tables() { + public void should_write_array_of_tables_from_object() { class Table { int anInt; @@ -177,15 +178,43 @@ public class TomlWriterTest { } } class Config { - Table[] table; + Table[] table = new Table[]{new Table(1), new Table(2)}; + List table2 = Arrays.asList(new Table(3), new Table(4)); } Config config = new Config(); - config.table = new Table[]{new Table(1), new Table(2)}; String output = new TomlWriter().write(config); String expected = "[[table]]\n" + "anInt = 1\n\n" + "[[table]]\n" + + "anInt = 2\n" + + "[[table2]]\n" + + "anInt = 3\n\n" + + "[[table2]]\n" + + "anInt = 4\n"; + assertEquals(expected, output); + } + + @Test + public void should_write_array_of_tables_from_map() throws Exception { + List> maps = new ArrayList>(); + + HashMap item1 = new HashMap(); + item1.put("anInt", 1L); + HashMap item2 = new HashMap(); + item2.put("anInt", 2L); + + maps.add(item1); + maps.add(item2); + + Map input = new HashMap(); + input.put("maps", maps); + + String output = new TomlWriter().write(input); + + String expected = "[[maps]]\n" + + "anInt = 1\n\n" + + "[[maps]]\n" + "anInt = 2\n"; assertEquals(expected, output); }