diff --git a/CHANGELOG.md b/CHANGELOG.md index 02399e9..37ca7dc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ JTGT - publish Javadoc artifact - link to hosted Javadoc +- JSON-compatible String representation of TextGrid objects ### Fixed diff --git a/build.gradle b/build.gradle index 61d82b2..fca49b7 100644 --- a/build.gradle +++ b/build.gradle @@ -15,7 +15,10 @@ repositories { } dependencies { + compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.7' + compile group: 'org.json', name: 'json', version: '20180130' testCompile group: 'org.testng', name: 'testng', version: '6.8.13' + testCompile group: 'commons-io', name: 'commons-io', version: '2.6' } test { diff --git a/src/main/java/org/m2ci/msp/jtgt/Annotation.java b/src/main/java/org/m2ci/msp/jtgt/Annotation.java index 107acdb..527c8e2 100644 --- a/src/main/java/org/m2ci/msp/jtgt/Annotation.java +++ b/src/main/java/org/m2ci/msp/jtgt/Annotation.java @@ -1,5 +1,8 @@ package org.m2ci.msp.jtgt; +import org.apache.commons.lang3.builder.ReflectionToStringBuilder; +import org.apache.commons.lang3.builder.ToStringStyle; +import org.json.JSONObject; /** * TextGrid annotation wrapper class @@ -104,6 +107,11 @@ public boolean equals(Object o) { (getEnd() == ((Annotation) o).getEnd()) && getText().equals(((Annotation) o).getText())); } + + public String toString() { + String jsonStr = new ReflectionToStringBuilder(this, ToStringStyle.JSON_STYLE).toString(); + return new JSONObject(jsonStr).toString(4); + } } diff --git a/src/main/java/org/m2ci/msp/jtgt/TextGrid.java b/src/main/java/org/m2ci/msp/jtgt/TextGrid.java index 169625d..e9427e4 100644 --- a/src/main/java/org/m2ci/msp/jtgt/TextGrid.java +++ b/src/main/java/org/m2ci/msp/jtgt/TextGrid.java @@ -1,5 +1,9 @@ package org.m2ci.msp.jtgt; +import org.apache.commons.lang3.builder.ReflectionToStringBuilder; +import org.apache.commons.lang3.builder.ToStringStyle; +import org.json.JSONObject; + import java.util.ArrayList; /** @@ -243,6 +247,11 @@ public boolean equals(Object o) { (((getFilename() == null) && (((TextGrid) o).getFilename() == null)) || getFilename().equals(((TextGrid) o).getFilename())) && (getTiers().equals(((TextGrid) o).getTiers()))); } + + public String toString() { + String jsonStr = new ReflectionToStringBuilder(this, ToStringStyle.JSON_STYLE).toString(); + return new JSONObject(jsonStr).toString(4); + } } diff --git a/src/main/java/org/m2ci/msp/jtgt/Tier.java b/src/main/java/org/m2ci/msp/jtgt/Tier.java index 07c964e..8b881bd 100644 --- a/src/main/java/org/m2ci/msp/jtgt/Tier.java +++ b/src/main/java/org/m2ci/msp/jtgt/Tier.java @@ -1,5 +1,9 @@ package org.m2ci.msp.jtgt; +import org.apache.commons.lang3.builder.ReflectionToStringBuilder; +import org.apache.commons.lang3.builder.ToStringStyle; +import org.json.JSONObject; + import java.util.ArrayList; /** @@ -183,6 +187,11 @@ public boolean equals(Object o) { getName().equals(((Tier) o).getName()) && getAnnotations().equals(((Tier) o).getAnnotations())); } + + public String toString() { + String jsonStr = new ReflectionToStringBuilder(this, ToStringStyle.JSON_STYLE).toString(); + return new JSONObject(jsonStr).toString(4); + } } /* Tier.java ends here */ diff --git a/src/test/java/org/m2ci/msp/jtgt/TextGridTest.java b/src/test/java/org/m2ci/msp/jtgt/TextGridTest.java new file mode 100644 index 0000000..3f0a2e1 --- /dev/null +++ b/src/test/java/org/m2ci/msp/jtgt/TextGridTest.java @@ -0,0 +1,37 @@ +package org.m2ci.msp.jtgt; + +import org.apache.commons.io.IOUtils; +import org.json.JSONObject; +import org.m2ci.msp.jtgt.annotation.IntervalAnnotation; +import org.m2ci.msp.jtgt.tier.IntervalTier; +import org.testng.Assert; +import org.testng.annotations.Test; + +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.util.Map; + +public class TextGridTest { + + @Test + public void testToString() throws IOException { + // create TextGrid + IntervalAnnotation foo = new IntervalAnnotation(0, 0.5, "foo"); + IntervalAnnotation bar = new IntervalAnnotation(0.5, 1, "bar"); + IntervalTier tier = new IntervalTier("foobar"); + tier.addAnnotation(foo); + tier.addAnnotation(bar); + tier.setStart(foo.getStart()); + tier.setEnd(bar.getEnd()); + TextGrid tg = new TextGrid(); + tg.addTier(tier); + tg.setStart(tier.getStart()); + tg.setEnd(tier.getEnd()); + // compare as JSON + String actualStr = tg.toString(); + String expectedStr = IOUtils.resourceToString("/org/m2ci/msp/jtgt/foobar.json", StandardCharsets.UTF_8); + Map actual = new JSONObject(actualStr).toMap(); + Map expected = new JSONObject(expectedStr).toMap(); + Assert.assertEquals(actual, expected); + } +} diff --git a/src/test/resources/org/m2ci/msp/jtgt/foobar.json b/src/test/resources/org/m2ci/msp/jtgt/foobar.json new file mode 100644 index 0000000..6eeb151 --- /dev/null +++ b/src/test/resources/org/m2ci/msp/jtgt/foobar.json @@ -0,0 +1,24 @@ +{ + "_filename": null, + "_start": 0, + "_end": 1, + "_tiers": [ + { + "_start": 0, + "_end": 1, + "_name": "foobar", + "_annotations": [ + { + "_start": 0, + "_end": 0.5, + "_text": "foo" + }, + { + "_start": 0.5, + "_end": 1, + "_text": "bar" + } + ] + } + ] +}