Repository: incubator-apex-malhar Updated Branches: refs/heads/devel-3 e0338537c -> d116d9406
- MLHR-1896 #resolve #comment Added utility functions to SchemaUtils. Project: http://git-wip-us.apache.org/repos/asf/incubator-apex-malhar/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-apex-malhar/commit/51efafe8 Tree: http://git-wip-us.apache.org/repos/asf/incubator-apex-malhar/tree/51efafe8 Diff: http://git-wip-us.apache.org/repos/asf/incubator-apex-malhar/diff/51efafe8 Branch: refs/heads/devel-3 Commit: 51efafe82745bda7e681cd4a186ed9eacb9bbb7d Parents: c1ebde9 Author: Timothy Farkas <[email protected]> Authored: Fri Nov 6 22:44:00 2015 -0800 Committer: Timothy Farkas <[email protected]> Committed: Tue Nov 17 15:33:42 2015 -0800 ---------------------------------------------------------------------- .../lib/appdata/schemas/SchemaUtils.java | 45 +++++++++++++++++--- .../lib/appdata/schemas/SchemaUtilsTest.java | 32 ++++++++++++++ 2 files changed, 72 insertions(+), 5 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-apex-malhar/blob/51efafe8/library/src/main/java/com/datatorrent/lib/appdata/schemas/SchemaUtils.java ---------------------------------------------------------------------- diff --git a/library/src/main/java/com/datatorrent/lib/appdata/schemas/SchemaUtils.java b/library/src/main/java/com/datatorrent/lib/appdata/schemas/SchemaUtils.java index 6b37167..dcfbc8d 100644 --- a/library/src/main/java/com/datatorrent/lib/appdata/schemas/SchemaUtils.java +++ b/library/src/main/java/com/datatorrent/lib/appdata/schemas/SchemaUtils.java @@ -21,17 +21,13 @@ package com.datatorrent.lib.appdata.schemas; import java.io.IOException; import java.io.InputStream; import java.io.StringWriter; - import java.util.Collection; +import java.util.Collections; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; -import com.google.common.base.Preconditions; -import com.google.common.collect.Maps; -import com.google.common.collect.Sets; - import org.codehaus.jettison.json.JSONArray; import org.codehaus.jettison.json.JSONException; import org.codehaus.jettison.json.JSONObject; @@ -40,12 +36,19 @@ import org.slf4j.LoggerFactory; import org.apache.commons.io.IOUtils; +import com.google.common.base.Preconditions; +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import com.google.common.collect.Sets; + /** * This class holds utility methods for processing JSON. * @since 3.0.0 */ public class SchemaUtils { + public static final String FIELD_TAGS = "tags"; + /** * This constructor should not be used. */ @@ -490,5 +493,37 @@ public class SchemaUtils return jo; } + /** + * This is a helper method which converts the given {@link JSONArray} to a {@link List} of Strings. + * + * @param jsonStringArray The {@link JSONArray} to convert. + * @return The converted {@link List} of Strings. + */ + public static List<String> getStringsFromJSONArray(JSONArray jsonStringArray) throws JSONException + { + List<String> stringArray = Lists.newArrayListWithCapacity(jsonStringArray.length()); + + for (int stringIndex = 0; stringIndex < jsonStringArray.length(); stringIndex++) { + stringArray.add(jsonStringArray.getString(stringIndex)); + } + + return stringArray; + } + + /** + * This is a helper method which retrieves the schema tags from the {@link JSONObject} if they are present. + * + * @param jo The {@link JSONObject} to retrieve schema tags from. + * @return A list containing the retrieved schema tags. The list is empty if there are no schema tags present. + */ + public static List<String> getTags(JSONObject jo) throws JSONException + { + if (jo.has(FIELD_TAGS)) { + return getStringsFromJSONArray(jo.getJSONArray(FIELD_TAGS)); + } else { + return Collections.emptyList(); + } + } + private static final Logger LOG = LoggerFactory.getLogger(SchemaUtils.class); } http://git-wip-us.apache.org/repos/asf/incubator-apex-malhar/blob/51efafe8/library/src/test/java/com/datatorrent/lib/appdata/schemas/SchemaUtilsTest.java ---------------------------------------------------------------------- diff --git a/library/src/test/java/com/datatorrent/lib/appdata/schemas/SchemaUtilsTest.java b/library/src/test/java/com/datatorrent/lib/appdata/schemas/SchemaUtilsTest.java index 0278a1d..3abee0b 100644 --- a/library/src/test/java/com/datatorrent/lib/appdata/schemas/SchemaUtilsTest.java +++ b/library/src/test/java/com/datatorrent/lib/appdata/schemas/SchemaUtilsTest.java @@ -18,9 +18,11 @@ */ package com.datatorrent.lib.appdata.schemas; +import com.google.common.collect.Lists; import java.util.Map; import com.google.common.collect.Maps; +import java.util.List; import org.codehaus.jettison.json.JSONArray; import org.codehaus.jettison.json.JSONObject; @@ -114,4 +116,34 @@ public class SchemaUtilsTest Assert.assertEquals("1", jo.get("a")); Assert.assertEquals("2", jo.get("b")); } + + @Test + public void stringsFromJSONArrayTest() throws Exception + { + List<String> expected = Lists.newArrayList("a", "b", "c"); + + JSONArray ja = new JSONArray(); + ja.put("a").put("b").put("c"); + + List<String> result = SchemaUtils.getStringsFromJSONArray(ja); + + Assert.assertEquals(expected, result); + } + + @Test + public void extractTagsTest() throws Exception + { + List<String> expected = Lists.newArrayList("a", "b", "c"); + + JSONObject jo = new JSONObject(); + + JSONArray ja = new JSONArray(); + ja.put("a").put("b").put("c"); + + jo.put(SchemaUtils.FIELD_TAGS, ja); + + Assert.assertEquals(expected, SchemaUtils.getTags(jo)); + } + + }
