Tested and documented extra possibilities of conversion to Java

This commit is contained in:
moandji.ezana 2014-04-09 10:49:41 +02:00
parent b0a8de8b19
commit 2f355b47f3
6 changed files with 75 additions and 18 deletions

View file

@ -47,6 +47,14 @@ Any keys not found in both the TOML and the class are ignored.
Key groups can be mapped to other custom classes. Fields may be private.
All TOML primitives can be mapped, as well as a number of Java-specific types:
* A TOML Number can be converted to any primitive type (or the wrapper equivalent), `BigInteger` or `BigDecimal`
* A single-letter TOML string can be converted to a `Character`
* A TOML string can be converted to an enum or a `java.net.URL`
* A TOML array can be converted to a `Set`
* A TOML table can be converted to a `Map<String, Object>`
````
name = "Mwanji Ezana"

View file

@ -8,12 +8,16 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.parboiled.Parboiled;
import org.parboiled.parserunners.RecoveringParseRunner;
@ -201,7 +205,15 @@ public class Toml {
* The target's field names must match keys or tables.
* Keys not present in targetClass will be ignored.</p>
*
* <p>Tables are recursively converted to custom classes.</p>
* <p>Tables are recursively converted to custom classes or to {@link Map Map&lt;String, Object&gt;}.</p>
*
* <p>In addition to straight-forward conversion of TOML primitives, the following are also available:</p>
*
* <ul>
* <li>TOML string to {@link Character}, {@link URL} or enum</li>
* <li>TOML number to any primitive (or wrapper), {@link BigInteger} or {@link BigDecimal}</li>
* <li>TOML array to {@link Set}</li>
* </ul>
*
* @param targetClass
*/

View file

@ -4,12 +4,18 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.io.File;
import java.lang.annotation.ElementType;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.URL;
import java.util.Arrays;
import java.util.Calendar;
import java.util.HashSet;
import java.util.TimeZone;
import org.junit.Test;
import com.moandjiezana.toml.testutils.TableAsMap;
import com.moandjiezana.toml.testutils.ExtraPrimitives;
import com.moandjiezana.toml.testutils.TomlPrimitives;
import com.moandjiezana.toml.testutils.TomlTableArrays;
import com.moandjiezana.toml.testutils.TomlTables;
@ -17,7 +23,7 @@ import com.moandjiezana.toml.testutils.TomlTables;
public class TomlToClassTest {
@Test
public void should_convert_primitive_values() throws Exception {
public void should_convert_toml_primitives() throws Exception {
Toml toml = new Toml().parse(file("should_convert_primitive_values.toml"));
TomlPrimitives values = toml.to(TomlPrimitives.class);
@ -34,6 +40,21 @@ public class TomlToClassTest {
assertEquals(calendar.getTime(), values.date);
}
@Test
public void should_convert_to_non_toml_primitives() throws Exception {
ExtraPrimitives extraPrimitives = new Toml().parse(file("should_convert_extra_primitives.toml")).to(ExtraPrimitives.class);
assertEquals("Did not convert table to map", "value", extraPrimitives.group.get("key"));
assertEquals("Did not convert double to BigDecimal", BigDecimal.valueOf(1.2), extraPrimitives.bigDecimal);
assertEquals("Did not convert integer to BigInteger", BigInteger.valueOf(5), extraPrimitives.bigInteger);
assertEquals("Did not convert integer to short", Short.parseShort("3"), extraPrimitives.aShort);
assertEquals("Did not convert integer to Integer", Integer.valueOf(7), extraPrimitives.anInteger);
assertEquals("Did not convert string to Character", Character.valueOf('u'), extraPrimitives.character);
assertEquals("Did not convert string to URL", new URL("http://example.com"), extraPrimitives.url);
assertEquals("Did not convert list to Set", new HashSet<String>(Arrays.asList("a", "b")), extraPrimitives.set);
assertEquals("Did not convert string to enum", ElementType.CONSTRUCTOR, extraPrimitives.elementType);
}
@Test
public void should_convert_tables() throws Exception {
String fileName = "should_convert_tables.toml";
@ -63,13 +84,6 @@ public class TomlToClassTest {
assertEquals("s", tomlPrimitives.string);
}
@Test
public void should_convert_table_as_map() throws Exception {
TableAsMap tableAsMap = new Toml().parse("[group]\nkey=\"value\"").to(TableAsMap.class);
assertEquals("value", tableAsMap.group.get("key"));
}
@Test
public void should_convert_table_array() throws Exception {
TomlTableArrays toml = new Toml().parse(file("should_convert_table_array_to_class.toml")).to(TomlTableArrays.class);

View file

@ -0,0 +1,21 @@
package com.moandjiezana.toml.testutils;
import java.lang.annotation.ElementType;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.URL;
import java.util.Map;
import java.util.Set;
public class ExtraPrimitives {
public Map<String, Object> group;
public BigDecimal bigDecimal;
public BigInteger bigInteger;
public short aShort;
public Integer anInteger;
public Character character;
public ElementType elementType;
public URL url;
public Set<String> set;
}

View file

@ -1,8 +0,0 @@
package com.moandjiezana.toml.testutils;
import java.util.Map;
public class TableAsMap {
public Map<String, Object> group;
}

View file

@ -0,0 +1,10 @@
bigDecimal=1.2
bigInteger=5
aShort=3
anInteger=7
character="u"
elementType="CONSTRUCTOR"
url="http://www.example.com"
set=["a", "b"]
[group]
key="value"