Add Toml#toMap()

Fixes #29
This commit is contained in:
Moandji Ezana 2016-05-09 22:58:46 -04:00
parent a5e34f464f
commit ab05197c9d
5 changed files with 65 additions and 15 deletions

View file

@ -1,5 +1,11 @@
# toml4j Changelog # toml4j Changelog
## 0.6.0 / 2016-05-09
## Added
* Toml#toMap() convenience method (thanks to __[andytill](https://github.com/andytill)__ and __[Gyscos](https://github.com/Gyscos))
## 0.5.1 / 2016-01-24 ## 0.5.1 / 2016-01-24
### Fixed ### Fixed

View file

@ -41,6 +41,14 @@ An exception is thrown if the source is not valid TOML.
The data can then be accessed either by converting the Toml instance to your own class or by accessing tables and keys by name. The data can then be accessed either by converting the Toml instance to your own class or by accessing tables and keys by name.
### Maps
`Toml#toMap()` is a quick way to turn a Toml instance into a `Map<String, Object>`.
```java
Map<String, Object> map = new Toml().read("a=1").toMap();
```
### Custom classes ### Custom classes
`Toml#to(Class<T>)` maps a Toml instance to the given class. `Toml#to(Class<T>)` maps a Toml instance to the given class.
@ -214,13 +222,6 @@ for (Map.Entry<String, Object> entry : myToml.entrySet()) {
} }
``` ```
You can also convert a Toml instance to a `Map<String, Object>`:
```java
Toml toml = new Toml().read("a = 1");
Map<String, Object> map = toml.to(Map.class);
```
`Toml#contains(String)` verifies that the instance contains a key of any type (primitive, table or array of tables) of the given name. `Toml#containsPrimitive(String)`, `Toml#containsTable(String)` and `Toml#containsTableArray(String)` return true only if a key exists and is a primitive, table or array of tables, respectively. Compound keys can be used to check existence at any depth. `Toml#contains(String)` verifies that the instance contains a key of any type (primitive, table or array of tables) of the given name. `Toml#containsPrimitive(String)`, `Toml#containsTable(String)` and `Toml#containsTableArray(String)` return true only if a key exists and is a primitive, table or array of tables, respectively. Compound keys can be used to check existence at any depth.
@ -245,7 +246,15 @@ class AClass {
TomlWriter tomlWriter = new TomlWriter(); TomlWriter tomlWriter = new TomlWriter();
AClass obj = new AClass(); AClass obj = new AClass();
Map<String, Object> map = new HashMap<String, Object>();
int[] intArray = { 2, 3 };
map.put("anInt", 1);
map.put("anArray", intArray);
String tomlString = tomlWriter.write(obj); String tomlString = tomlWriter.write(obj);
tomlString = tomlWriter.write(map);
tomlWriter.write(obj, new File("path/to/file")); tomlWriter.write(obj, new File("path/to/file"));
tomlWriter.write(obj, new ByteArrayOutputStream()); tomlWriter.write(obj, new ByteArrayOutputStream());
tomlWriter.write(obj, new OutputStreamWriter(anOutputStream)); tomlWriter.write(obj, new OutputStreamWriter(anOutputStream));

View file

@ -302,6 +302,16 @@ public class Toml {
* @return A new instance of targetClass. * @return A new instance of targetClass.
*/ */
public <T> T to(Class<T> targetClass) { public <T> T to(Class<T> targetClass) {
JsonElement json = DEFAULT_GSON.toJsonTree(toMap());
if (targetClass == JsonElement.class) {
return targetClass.cast(json);
}
return DEFAULT_GSON.fromJson(json, targetClass);
}
public Map<String, Object> toMap() {
HashMap<String, Object> valuesCopy = new HashMap<String, Object>(values); HashMap<String, Object> valuesCopy = new HashMap<String, Object>(values);
if (defaults != null) { if (defaults != null) {
@ -311,14 +321,8 @@ public class Toml {
} }
} }
} }
JsonElement json = DEFAULT_GSON.toJsonTree(valuesCopy); return valuesCopy;
if (targetClass == JsonElement.class) {
return targetClass.cast(json);
}
return DEFAULT_GSON.fromJson(json, targetClass);
} }
/** /**

View file

@ -267,6 +267,11 @@ public class TomlWriterTest {
assertEquals("[b.c]\nanInt = 1\n", new TomlWriter().write(new A())); assertEquals("[b.c]\nanInt = 1\n", new TomlWriter().write(new A()));
} }
@Test
public void should_write_map() throws Exception {
assertEquals("a = 1\n", new TomlWriter().write(new Toml().read("a = 1").toMap()));
}
class Base { class Base {
protected int anInt = 2; protected int anInt = 2;

View file

@ -0,0 +1,26 @@
package com.moandjiezana.toml;
import java.util.HashMap;
import java.util.Map;
import org.junit.Assert;
import org.junit.Test;
public class Toml_ToMapTest {
@Test
public void should_convert_simple_values() {
Map<String, Object> toml = new Toml().read("a = 1").toMap();
Assert.assertEquals(Long.valueOf(1), toml.get("a"));
}
@Test
@SuppressWarnings("unchecked")
public void should_covert_table() throws Exception {
Map<String, Object> toml = new Toml().read("c = 2\n [a]\n b = 1").toMap();
Assert.assertEquals(Long.valueOf(1), ((Map<String, Object>) toml.get("a")).get("b"));
Assert.assertEquals(Long.valueOf(2), toml.get("c"));
}
}