Toml#to(Class) converts to custom class

This commit is contained in:
moandji.ezana 2013-02-28 12:53:46 +02:00
parent 53c2de5519
commit 450e7402ef
9 changed files with 170 additions and 0 deletions

View file

@ -55,6 +55,41 @@ Long b = toml.getLong("b"); // returns 3
Long c = toml.getLong("c"); // returns null
````
### Custom classes
A Toml object can be mapped to a custom class with the `Toml#to(Class<T>)` method.
Any keys not found in both the TOML and the class are ignored.
Key groups can be mapped to other custom classes or to `Map<String, Object>`.
````
name = "Mwanji Ezana"
[address]
street = "123 A Street"
city = "AnyVille"
````
````java
public class Address {
private String street;
private String city;
}
public class User {
private String name;
private Address address;
}
````
````java
Toml toml = new Toml().parse(tomlFile);
User user = toml.to(User.class);
````
When defaults are present, a shallow merge is performed.
## TODO
* Support all special characters

View file

@ -36,6 +36,11 @@
<artifactId>parboiled-java</artifactId>
<version>1.1.4</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.2</version>
</dependency>
</dependencies>
<build>
<plugins>

View file

@ -1,5 +1,7 @@
package com.moandjiezana.toml;
import com.google.gson.Gson;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Date;
@ -116,4 +118,18 @@ public class Toml {
this.values = values;
this.defaults = null;
}
public <T> T to(Class<T> targetClass) {
HashMap<String, Object> valuesCopy = new HashMap<String, Object>(values);
if (defaults != null) {
for (Map.Entry<String, Object> entry : defaults.values.entrySet()) {
if (!valuesCopy.containsKey(entry.getKey())) {
valuesCopy.put(entry.getKey(), entry.getValue());
}
}
}
Gson gson = new Gson();
String json = gson.toJson(valuesCopy);
return gson.fromJson(json, targetClass);
}
}

View file

@ -0,0 +1,75 @@
package com.moandjiezana.toml;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import com.moandjiezana.toml.testutils.KeyGroupAsMap;
import com.moandjiezana.toml.testutils.TomlKeyGroups;
import com.moandjiezana.toml.testutils.TomlPrimitives;
import java.io.File;
import java.util.Calendar;
import java.util.TimeZone;
import org.junit.Test;
public class TomlToClassTest {
@Test
public void should_convert_primitive_values() throws Exception {
Toml toml = new Toml().parse(file("should_convert_primitive_values.toml"));
TomlPrimitives values = toml.to(TomlPrimitives.class);
Calendar calendar = Calendar.getInstance();
calendar.set(1979, Calendar.MAY, 27, 7, 32, 00);
calendar.set(Calendar.MILLISECOND, 0);
calendar.setTimeZone(TimeZone.getTimeZone("UTC"));
assertEquals("string", values.string);
assertEquals((Long) 123L, values.number);
assertEquals(2.1, values.decimal, 0);
assertTrue(values.bool);
assertEquals(calendar.getTime(), values.date);
}
@Test
public void should_convert_key_groups() throws Exception {
String fileName = "should_convert_key_groups.toml";
Toml toml = new Toml().parse(file(fileName));
TomlKeyGroups tomlKeyGroups = toml.to(TomlKeyGroups.class);
assertEquals("value1", tomlKeyGroups.group1.string);
assertEquals("value2", tomlKeyGroups.group2.string);
}
@Test
public void should_use_defaults() throws Exception {
Toml defaults = new Toml().parse(file("should_convert_key_groups.toml"));
Toml toml = new Toml(defaults).parse("");
TomlKeyGroups tomlKeyGroups = toml.to(TomlKeyGroups.class);
assertEquals("value1", tomlKeyGroups.group1.string);
assertEquals("value2", tomlKeyGroups.group2.string);
}
@Test
public void should_ignore_keys_not_in_class() throws Exception {
TomlPrimitives tomlPrimitives = new Toml().parse("a=1\nstring=\"s\"").to(TomlPrimitives.class);
assertEquals("s", tomlPrimitives.string);
}
@Test
public void should_convert_key_group_as_map() throws Exception {
KeyGroupAsMap keyGroupAsMap = new Toml().parse("[group]\nkey=\"value\"").to(KeyGroupAsMap.class);
assertEquals("value", keyGroupAsMap.group.get("key"));
}
private File file(String fileName) {
return new File(getClass().getResource(fileName).getFile());
}
}

View file

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

View file

@ -0,0 +1,8 @@
package com.moandjiezana.toml.testutils;
public class TomlKeyGroups {
public TomlPrimitives group1;
public TomlPrimitives group2;
}

View file

@ -0,0 +1,13 @@
package com.moandjiezana.toml.testutils;
import java.util.Date;
public class TomlPrimitives {
public String string;
public Long number;
public Double decimal;
public Boolean bool;
public Date date;
}

View file

@ -0,0 +1,5 @@
[group1]
string = "value1"
[group2]
string = "value2"

View file

@ -0,0 +1,5 @@
string = "string"
number = 123
decimal = 2.1
bool = true
date = 1979-05-27T07:32:00Z