mirror of
https://github.com/plexusorg/toml4j.git
synced 2024-12-28 19:24:15 +00:00
Toml#to(Class) converts to custom class
This commit is contained in:
parent
53c2de5519
commit
450e7402ef
9 changed files with 170 additions and 0 deletions
35
README.md
35
README.md
|
@ -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
|
||||
|
|
5
pom.xml
5
pom.xml
|
@ -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>
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
75
src/test/java/com/moandjiezana/toml/TomlToClassTest.java
Normal file
75
src/test/java/com/moandjiezana/toml/TomlToClassTest.java
Normal 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());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package com.moandjiezana.toml.testutils;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public class KeyGroupAsMap {
|
||||
|
||||
public Map<String, Object> group;
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package com.moandjiezana.toml.testutils;
|
||||
|
||||
|
||||
public class TomlKeyGroups {
|
||||
|
||||
public TomlPrimitives group1;
|
||||
public TomlPrimitives group2;
|
||||
}
|
|
@ -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;
|
||||
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
[group1]
|
||||
string = "value1"
|
||||
|
||||
[group2]
|
||||
string = "value2"
|
|
@ -0,0 +1,5 @@
|
|||
string = "string"
|
||||
number = 123
|
||||
decimal = 2.1
|
||||
bool = true
|
||||
date = 1979-05-27T07:32:00Z
|
Loading…
Reference in a new issue