Move Toml.write() to TomlWriter.

Add TomlWriter methods for writing to File, Writer, and OutputStream
targets.
This commit is contained in:
Jonathan Wood 2015-06-27 14:19:40 -07:00
parent acc952a572
commit ec30be7901
3 changed files with 135 additions and 28 deletions

View file

@ -359,20 +359,6 @@ public class Toml {
}
}
/**
* Serializes an Object into TOML.
*
* The input can comprise arbitrarily nested combinations of Java primitive types,
* other {@link Object}s, {@link Map}s, {@link List}s, and Arrays. {@link Object}s and {@link Map}s
* are output to TOML tables, and {@link List}s and Array to TOML arrays.
*
* @param from the object to be written
* @return a string containing the TOML representation of the given Object
*/
public static String write(Object from) {
return ValueWriters.write(from);
}
@SuppressWarnings("unchecked")
private Object get(String key) {
if (values.containsKey(key)) {

View file

@ -0,0 +1,74 @@
package com.moandjiezana.toml;
import java.io.*;
import java.util.List;
import java.util.Map;
/**
* <p>Converts Objects to TOML</p>
*
* <p>An input Object can comprise arbitrarily nested combinations of Java primitive types,
* other {@link Object}s, {@link Map}s, {@link List}s, and Arrays. {@link Object}s and {@link Map}s
* are output to TOML tables, and {@link List}s and Array to TOML arrays.</p>
*
* <p>Example usage:</p>
* <pre><code>
* class AClass {
* int anInt = 1;
* int[] anArray = { 2, 3 };
* }
*
* String tomlString = new TomlWriter().write(new AClass());
* </code></pre>
*/
public class TomlWriter {
/**
* Creates a TomlWriter instance.
*/
public TomlWriter() {}
/**
* Write an Object into TOML String.
*
* @param from the object to be written
* @return a string containing the TOML representation of the given Object
*/
public String write(Object from) {
return ValueWriters.write(from);
}
/**
* Write an Object in TOML to a {@link Writer}.
*
* @param from the object to be written
* @param target the Writer to which TOML will be written
* @throws IOException if target.write() fails
*/
public void write(Object from, Writer target) throws IOException {
target.write(write(from));
}
/**
* Write an Object in TOML to a {@link OutputStream}.
*
* @param from the object to be written
* @param target the OutputStream to which the TOML will be written
* @throws IOException if target.write() fails
*/
public void write(Object from, OutputStream target) throws IOException {
target.write(write(from).getBytes());
}
/**
* Write an Object in TOML to a {@link File}.
*
* @param from the object to be written
* @param target the File to which the TOML will be written
* @throws IOException if any file operations fail
*/
public void write(Object from, File target) throws IOException {
FileWriter writer = new FileWriter(target);
writer.write(write(from));
writer.close();
}
}

View file

@ -1,14 +1,20 @@
package com.moandjiezana.toml;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import java.io.UnsupportedEncodingException;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;
import static org.junit.Assert.assertEquals;
public class ValueWriterTest {
@Rule
public TemporaryFolder testDirectory = new TemporaryFolder();
@Test
public void should_write_primitive_types() {
class TestClass {
@ -31,7 +37,7 @@ public class ValueWriterTest {
o.aDate = new Date();
String theDate = formatDate(o.aDate);
String output = Toml.write(o);
String output = new TomlWriter().write(o);
String expected = "aString = \"hello\"\n" +
"anInt = 4\n" +
"aFloat = 1.23\n" +
@ -80,7 +86,7 @@ public class ValueWriterTest {
parent.child.subChild.anInt = 4;
parent.aBoolean = true;
String output = Toml.write(parent);
String output = new TomlWriter().write(parent);
String expected = "aBoolean = true\n\n" +
"[aMap]\n" +
" foo = 1\n" +
@ -100,7 +106,7 @@ public class ValueWriterTest {
}
ArrayTest arrayTest = new ArrayTest();
String output = Toml.write(arrayTest);
String output = new TomlWriter().write(arrayTest);
String expected = "array = [ 1, 2, 3 ]\n";
assertEquals(expected, output);
}
@ -120,7 +126,7 @@ public class ValueWriterTest {
Config config = new Config();
config.table = new Table[]{new Table(1), new Table(2)};
String output = Toml.write(config);
String output = new TomlWriter().write(config);
String expected = "[[table]]\n" +
" anInt = 1\n\n" +
"[[table]]\n" +
@ -135,7 +141,7 @@ public class ValueWriterTest {
}
ArrayTest arrayTest = new ArrayTest();
String output = Toml.write(arrayTest);
String output = new TomlWriter().write(arrayTest);
String expected = "array = [ [ 1, 2, 3 ], [ 4, 5, 6 ] ]\n";
assertEquals(expected, output);
}
@ -149,7 +155,7 @@ public class ValueWriterTest {
o.aList.add(1);
o.aList.add(2);
assertEquals("aList = [ 1, 2 ]\n", Toml.write(o));
assertEquals("aList = [ 1, 2 ]\n", new TomlWriter().write(o));
}
@Test
@ -158,7 +164,7 @@ public class ValueWriterTest {
List<Integer> aList = new LinkedList<Integer>();
Float[] anArray = new Float[0];
}
assertEquals("", Toml.write(new TestClass()));
assertEquals("", new TomlWriter().write(new TestClass()));
}
@Test
@ -173,7 +179,7 @@ public class ValueWriterTest {
B b = new B();
}
assertEquals("[b.c]\n anInt = 1\n", Toml.write(new A()));
assertEquals("[b.c]\n anInt = 1\n", new TomlWriter().write(new A()));
}
@Test
@ -235,7 +241,7 @@ public class ValueWriterTest {
"\n";
String output = Toml.write(basket);
String output = new TomlWriter().write(basket);
assertEquals(expected, output);
}
@ -250,17 +256,17 @@ public class ValueWriterTest {
Child child = new Child();
String expected = "aBoolean = true\nanInt = 2\n";
assertEquals(expected, Toml.write(child));
assertEquals(expected, new TomlWriter().write(child));
}
@Test
public void should_write_strings_to_toml_utf8() throws UnsupportedEncodingException {
String input = " é foo € \b \t \n \f \r \" \\ ";
assertEquals("\" \\u00E9 foo \\u20AC \\b \\t \\n \\f \\r \\\" \\ \"", Toml.write(input));
assertEquals("\" \\u00E9 foo \\u20AC \\b \\t \\n \\f \\r \\\" \\ \"", new TomlWriter().write(input));
// Check unicode code points greater than 0XFFFF
input = " \uD801\uDC28 \uD840\uDC0B ";
assertEquals("\" \\U00010428 \\U0002000B \"", Toml.write(input));
assertEquals("\" \\U00010428 \\U0002000B \"", new TomlWriter().write(input));
}
@Test
@ -275,6 +281,47 @@ public class ValueWriterTest {
"\"5€\" = 2\n" +
"\"c$d\" = 3\n" +
"\"e/f\" = 4\n";
assertEquals(expected, Toml.write(aMap));
assertEquals(expected, new TomlWriter().write(aMap));
}
private static class SimpleTestClass {
int a = 1;
}
@Test
public void should_write_to_writer() throws IOException {
StringWriter output = new StringWriter();
new TomlWriter().write(new SimpleTestClass(), output);
assertEquals("a = 1\n", output.toString());
}
@Test
public void should_write_to_outputstream() throws IOException {
ByteArrayOutputStream output = new ByteArrayOutputStream();
new TomlWriter().write(new SimpleTestClass(), output);
assertEquals("a = 1\n", output.toString());
}
@Test
public void should_write_to_file() throws IOException {
File output = testDirectory.newFile();
new TomlWriter().write(new SimpleTestClass(), output);
assertEquals("a = 1\n", readFile(output));
}
private String readFile(File input) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new FileReader(input));
StringBuilder w = new StringBuilder();
String line = bufferedReader.readLine();
while (line != null) {
w.append(line).append('\n');
line = bufferedReader.readLine();
}
return w.toString();
}
}