Merge branch 'wip'

This commit is contained in:
Moandji Ezana 2016-06-14 20:22:05 -04:00
commit 55d5b91e1d
10 changed files with 103 additions and 21 deletions

View file

@ -1,5 +1,16 @@
# toml4j Changelog
## 0.6.0 / 2016-06-14
## Added
* Toml#toMap() convenience method (thanks to __[andytill](https://github.com/andytill)__ and __[Gyscos](https://github.com/Gyscos))
## Fixed
* Transient fields are not written to TOML files (thanks to __[lare96](https://github.com/lare96)__)
* Support positive timezone offset in datetime (thanks to __[aloyse](https://github.com/aloyse)__)
## 0.5.1 / 2016-01-24
### Fixed

View file

@ -14,7 +14,7 @@ Add the following dependency to your POM (or equivalent for other dependency man
<dependency>
<groupId>com.moandjiezana.toml</groupId>
<artifactId>toml4j</artifactId>
<version>0.5.1</version>
<version>0.6.0</version>
</dependency>
```
@ -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.
### 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
`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.
@ -245,7 +246,15 @@ class AClass {
TomlWriter tomlWriter = new TomlWriter();
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);
tomlString = tomlWriter.write(map);
tomlWriter.write(obj, new File("path/to/file"));
tomlWriter.write(obj, new ByteArrayOutputStream());
tomlWriter.write(obj, new OutputStreamWriter(anOutputStream));
@ -296,6 +305,10 @@ Output:
Date precision is limited to milliseconds.
## Changelog
Please see the [changelog](CHANGELOG.md).
## Contributing
Please see the [contribution guidelines](CONTRIBUTING.md).

View file

@ -40,7 +40,7 @@ class DateValueReaderWriter implements ValueReader, ValueWriter {
for (int i = index.get(); i < original.length(); i = index.incrementAndGet()) {
char c = original.charAt(i);
if (Character.isDigit(c) || c == '-' || c == ':' || c == '.' || c == 'T' || c == 'Z') {
if (Character.isDigit(c) || c == '-' || c == '+' || c == ':' || c == '.' || c == 'T' || c == 'Z') {
sb.append(c);
} else {
index.decrementAndGet();

View file

@ -50,7 +50,7 @@ class ObjectValueWriter implements ValueWriter {
Iterator<Field> iterator = fields.iterator();
while (iterator.hasNext()) {
Field field = iterator.next();
if ((Modifier.isFinal(field.getModifiers()) && Modifier.isStatic(field.getModifiers())) || field.isSynthetic()) {
if ((Modifier.isFinal(field.getModifiers()) && Modifier.isStatic(field.getModifiers())) || field.isSynthetic() || Modifier.isTransient(field.getModifiers())) {
iterator.remove();
}
}

View file

@ -302,6 +302,16 @@ public class Toml {
* @return A new instance of 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);
if (defaults != null) {
@ -311,14 +321,8 @@ public class Toml {
}
}
}
JsonElement json = DEFAULT_GSON.toJsonTree(valuesCopy);
if (targetClass == JsonElement.class) {
return targetClass.cast(json);
}
return DEFAULT_GSON.fromJson(json, targetClass);
return valuesCopy;
}
/**

View file

@ -134,7 +134,7 @@ public class BurntSushiValidEncoderTest {
private void runEncoder(String testName, TomlWriter tomlWriter) {
InputStream inputTomlStream = getClass().getResourceAsStream("burntsushi/valid/" + testName + ".toml");
String expectedToml = convertStreamToString(inputTomlStream);
String expectedToml = convertStreamToString(inputTomlStream).replaceAll("\r\n", "\n");
Reader inputJsonReader = new InputStreamReader(getClass().getResourceAsStream("burntsushi/valid/" + testName + ".json"));
JsonElement jsonInput = GSON.fromJson(inputJsonReader, JsonElement.class);

View file

@ -248,7 +248,7 @@ public class BurntSushiValidTest {
private void run(String testName) {
InputStream inputTomlStream = getClass().getResourceAsStream("burntsushi/valid/" + testName + ".toml");
// InputStream inputToml = getClass().getResourceAsStream("burntsushi/valid/" + testName + ".toml");
String inputToml = convertStreamToString(inputTomlStream);
String inputToml = convertStreamToString(inputTomlStream).replaceAll("\r\n", "\n");
Reader expectedJsonReader = new InputStreamReader(getClass().getResourceAsStream("burntsushi/valid/" + testName + ".json"));
JsonElement expectedJson = GSON.fromJson(expectedJsonReader, JsonElement.class);

View file

@ -27,7 +27,18 @@ public class DateTest {
Toml toml = new Toml().read("a_date = 1979-05-27T00:32:00-07:00");
Calendar calendar = Calendar.getInstance(UTC);
calendar.set(1979, Calendar.MAY, 27, 7, 32, 00);
calendar.set(1979, Calendar.MAY, 27, 7, 32, 0);
calendar.set(Calendar.MILLISECOND, 0);
assertEquals(calendar.getTime(), toml.getDate("a_date"));
}
@Test
public void should_get_date_with_positive_offset() throws Exception {
Toml toml = new Toml().read("a_date = 1979-05-27T07:32:00+07:00");
Calendar calendar = Calendar.getInstance(UTC);
calendar.set(1979, Calendar.MAY, 27, 0, 32, 0);
calendar.set(Calendar.MILLISECOND, 0);
assertEquals(calendar.getTime(), toml.getDate("a_date"));

View file

@ -267,6 +267,11 @@ public class TomlWriterTest {
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 {
protected int anInt = 2;
@ -450,6 +455,11 @@ public class TomlWriterTest {
private static class SimpleTestClass {
int a = 1;
}
private static class TransientClass {
int a = 2;
transient int b = 3;
}
@Test
public void should_write_to_writer() throws IOException {
@ -475,6 +485,13 @@ public class TomlWriterTest {
assertEquals("a = 1\n", readFile(output));
}
@Test
public void should_skip_transient_fields() throws Exception {
String toml = new TomlWriter().write(new TransientClass());
assertEquals("a = 2\n", toml);
}
@Test(expected = IllegalArgumentException.class)
public void should_refuse_to_write_string_fragment() {
new TomlWriter().write("fragment");

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"));
}
}