From ab05197c9dec95c6ab3108fa919d2456f21c40b7 Mon Sep 17 00:00:00 2001 From: Moandji Ezana Date: Mon, 9 May 2016 22:58:46 -0400 Subject: [PATCH 1/5] Add Toml#toMap() Fixes #29 --- CHANGELOG.md | 6 +++++ README.md | 23 +++++++++++----- src/main/java/com/moandjiezana/toml/Toml.java | 20 ++++++++------ .../com/moandjiezana/toml/TomlWriterTest.java | 5 ++++ .../com/moandjiezana/toml/Toml_ToMapTest.java | 26 +++++++++++++++++++ 5 files changed, 65 insertions(+), 15 deletions(-) create mode 100644 src/test/java/com/moandjiezana/toml/Toml_ToMapTest.java diff --git a/CHANGELOG.md b/CHANGELOG.md index a224247..efa61d8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # 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 ### Fixed diff --git a/README.md b/README.md index f9c085a..0426750 100644 --- a/README.md +++ b/README.md @@ -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`. + +```java +Map map = new Toml().read("a=1").toMap(); +``` + ### Custom classes `Toml#to(Class)` maps a Toml instance to the given class. @@ -214,13 +222,6 @@ for (Map.Entry entry : myToml.entrySet()) { } ``` -You can also convert a Toml instance to a `Map`: - -```java -Toml toml = new Toml().read("a = 1"); -Map 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 map = new HashMap(); +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)); diff --git a/src/main/java/com/moandjiezana/toml/Toml.java b/src/main/java/com/moandjiezana/toml/Toml.java index 27a66d3..4babb93 100644 --- a/src/main/java/com/moandjiezana/toml/Toml.java +++ b/src/main/java/com/moandjiezana/toml/Toml.java @@ -302,6 +302,16 @@ public class Toml { * @return A new instance of targetClass. */ public T to(Class targetClass) { + JsonElement json = DEFAULT_GSON.toJsonTree(toMap()); + + if (targetClass == JsonElement.class) { + return targetClass.cast(json); + } + + return DEFAULT_GSON.fromJson(json, targetClass); + } + + public Map toMap() { HashMap valuesCopy = new HashMap(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; } /** diff --git a/src/test/java/com/moandjiezana/toml/TomlWriterTest.java b/src/test/java/com/moandjiezana/toml/TomlWriterTest.java index f8e3773..8794a18 100644 --- a/src/test/java/com/moandjiezana/toml/TomlWriterTest.java +++ b/src/test/java/com/moandjiezana/toml/TomlWriterTest.java @@ -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; diff --git a/src/test/java/com/moandjiezana/toml/Toml_ToMapTest.java b/src/test/java/com/moandjiezana/toml/Toml_ToMapTest.java new file mode 100644 index 0000000..84e6515 --- /dev/null +++ b/src/test/java/com/moandjiezana/toml/Toml_ToMapTest.java @@ -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 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 toml = new Toml().read("c = 2\n [a]\n b = 1").toMap(); + + Assert.assertEquals(Long.valueOf(1), ((Map) toml.get("a")).get("b")); + Assert.assertEquals(Long.valueOf(2), toml.get("c")); + } +} From a37dfd80a8487ea631f6eca534b204c1f276e5b2 Mon Sep 17 00:00:00 2001 From: Moandji Ezana Date: Tue, 14 Jun 2016 19:51:22 -0400 Subject: [PATCH 2/5] Transient fields are not written to TOML files by TomlWriter Fixes #30 --- CHANGELOG.md | 6 +++++- .../com/moandjiezana/toml/ObjectValueWriter.java | 2 +- .../java/com/moandjiezana/toml/TomlWriterTest.java | 12 ++++++++++++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index efa61d8..2dcb39c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,11 +1,15 @@ # toml4j Changelog -## 0.6.0 / 2016-05-09 +## 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)__) + ## 0.5.1 / 2016-01-24 ### Fixed diff --git a/src/main/java/com/moandjiezana/toml/ObjectValueWriter.java b/src/main/java/com/moandjiezana/toml/ObjectValueWriter.java index 9bd08c3..1295778 100644 --- a/src/main/java/com/moandjiezana/toml/ObjectValueWriter.java +++ b/src/main/java/com/moandjiezana/toml/ObjectValueWriter.java @@ -50,7 +50,7 @@ class ObjectValueWriter implements ValueWriter { Iterator 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(); } } diff --git a/src/test/java/com/moandjiezana/toml/TomlWriterTest.java b/src/test/java/com/moandjiezana/toml/TomlWriterTest.java index 8794a18..44fc986 100644 --- a/src/test/java/com/moandjiezana/toml/TomlWriterTest.java +++ b/src/test/java/com/moandjiezana/toml/TomlWriterTest.java @@ -455,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 { @@ -480,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"); From b7d3b346194e3394b6ad9183d3e0e3961089b56f Mon Sep 17 00:00:00 2001 From: Moandji Ezana Date: Tue, 14 Jun 2016 19:58:13 -0400 Subject: [PATCH 3/5] Support positive offset in datetime Fixes #31 --- CHANGELOG.md | 1 + .../moandjiezana/toml/DateValueReaderWriter.java | 2 +- src/test/java/com/moandjiezana/toml/DateTest.java | 13 ++++++++++++- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2dcb39c..fcd7db1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ ## 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 diff --git a/src/main/java/com/moandjiezana/toml/DateValueReaderWriter.java b/src/main/java/com/moandjiezana/toml/DateValueReaderWriter.java index 8119b39..d6a64cf 100644 --- a/src/main/java/com/moandjiezana/toml/DateValueReaderWriter.java +++ b/src/main/java/com/moandjiezana/toml/DateValueReaderWriter.java @@ -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(); diff --git a/src/test/java/com/moandjiezana/toml/DateTest.java b/src/test/java/com/moandjiezana/toml/DateTest.java index bfe8c16..dce1e57 100644 --- a/src/test/java/com/moandjiezana/toml/DateTest.java +++ b/src/test/java/com/moandjiezana/toml/DateTest.java @@ -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")); From f0da6d4c361852a137b3fb587758fff7846f94aa Mon Sep 17 00:00:00 2001 From: Moandji Ezana Date: Tue, 14 Jun 2016 20:01:22 -0400 Subject: [PATCH 4/5] Bump version to 0.6.0 --- README.md | 6 +++++- pom.xml | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 0426750..38e2be0 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ Add the following dependency to your POM (or equivalent for other dependency man com.moandjiezana.toml toml4j - 0.5.1 + 0.6.0 ``` @@ -305,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). diff --git a/pom.xml b/pom.xml index 2a35408..afeec05 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ com.moandjiezana.toml toml4j - 0.5.0-SNAPSHOT + 0.6.0-SNAPSHOT toml4j A parser for TOML http://moandjiezana.com/toml/toml4j From d977160e1b0d82a8dc205d3baf6172ba8a827271 Mon Sep 17 00:00:00 2001 From: Moandji Ezana Date: Tue, 14 Jun 2016 20:19:33 -0400 Subject: [PATCH 5/5] Normalise line endings on Windows in tests --- .../java/com/moandjiezana/toml/BurntSushiValidEncoderTest.java | 2 +- src/test/java/com/moandjiezana/toml/BurntSushiValidTest.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/com/moandjiezana/toml/BurntSushiValidEncoderTest.java b/src/test/java/com/moandjiezana/toml/BurntSushiValidEncoderTest.java index 456ae8f..2537d95 100644 --- a/src/test/java/com/moandjiezana/toml/BurntSushiValidEncoderTest.java +++ b/src/test/java/com/moandjiezana/toml/BurntSushiValidEncoderTest.java @@ -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); diff --git a/src/test/java/com/moandjiezana/toml/BurntSushiValidTest.java b/src/test/java/com/moandjiezana/toml/BurntSushiValidTest.java index bb55dd5..60b95d2 100644 --- a/src/test/java/com/moandjiezana/toml/BurntSushiValidTest.java +++ b/src/test/java/com/moandjiezana/toml/BurntSushiValidTest.java @@ -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);