diff --git a/README.md b/README.md index bcf1327..c5ab81f 100644 --- a/README.md +++ b/README.md @@ -185,3 +185,4 @@ ## Dependencies ## - reflection-utils +- json-utils diff --git a/pom.xml b/pom.xml index 7966ce4..3c6a36e 100644 --- a/pom.xml +++ b/pom.xml @@ -1,4 +1,5 @@ - 4.0.0 @@ -23,6 +24,11 @@ 1.0.0-SNAPSHOT + com.ljunggren + json-utils + 0.0.1-SNAPSHOT + + org.apache.commons commons-lang3 3.10 diff --git a/src/main/java/com/ljunggren/storm/context/ContextFactory.java b/src/main/java/com/ljunggren/storm/context/ContextFactory.java index b753246..235f937 100644 --- a/src/main/java/com/ljunggren/storm/context/ContextFactory.java +++ b/src/main/java/com/ljunggren/storm/context/ContextFactory.java @@ -7,8 +7,8 @@ import org.apache.commons.io.IOUtils; import com.fasterxml.jackson.core.JsonProcessingException; +import com.ljunggren.jsonUtils.JsonUtils; import com.ljunggren.storm.exception.ContextException; -import com.ljunggren.storm.util.JSONUtils; public class ContextFactory { @@ -46,7 +46,7 @@ private List parseContextFile(String json) { try { - return JSONUtils.jsonToListObject(json, Context.class); + return JsonUtils.jsonToList(json, Context.class); } catch (JsonProcessingException e) { throw new ContextException(String.format("Unable to parse context.json", definition)); } diff --git a/src/main/java/com/ljunggren/storm/util/JSONUtils.java b/src/main/java/com/ljunggren/storm/util/JSONUtils.java deleted file mode 100644 index 29561a9..0000000 --- a/src/main/java/com/ljunggren/storm/util/JSONUtils.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.ljunggren.storm.util; - -import java.io.IOException; -import java.util.List; - -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.JavaType; -import com.fasterxml.jackson.databind.JsonMappingException; -import com.fasterxml.jackson.databind.ObjectMapper; - -public class JSONUtils { - - public static boolean isValidJSON(String json) { - if (json == null) { - return false; - } - try { - final ObjectMapper mapper = new ObjectMapper(); - mapper.readTree(json); - return true; - } catch (IOException e) { - return false; - } - } - - public static String objectToJson(Object object) throws JsonProcessingException { - ObjectMapper mapper = new ObjectMapper(); - String json = ""; - json = mapper.writeValueAsString(object); - return json; - } - - @SuppressWarnings("rawtypes") - public static T jsonToListObject(String json, Class clazz) throws JsonMappingException, JsonProcessingException { - ObjectMapper mapper = new ObjectMapper(); - JavaType type = mapper.getTypeFactory().constructCollectionType(List.class, clazz); - return mapper.readValue(json, type); - } - -} diff --git a/src/test/java/com/ljunggren/storm/util/JSONUtilsTest.java b/src/test/java/com/ljunggren/storm/util/JSONUtilsTest.java deleted file mode 100644 index bac8796..0000000 --- a/src/test/java/com/ljunggren/storm/util/JSONUtilsTest.java +++ /dev/null @@ -1,78 +0,0 @@ -package com.ljunggren.storm.util; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - -import java.io.IOException; -import java.util.Arrays; -import java.util.List; - -import org.junit.Test; - -import com.fasterxml.jackson.core.JsonGenerationException; -import com.fasterxml.jackson.core.JsonParseException; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.JsonMappingException; -import com.ljunggren.storm.TestUser; - -public class JSONUtilsTest { - - private TestUser getUser() { - TestUser user = new TestUser(); - user.setFirstName("Alex"); - user.setEmployeeID(100); - return user; - } - - @Test - public void instantiationTest() { - JSONUtils jsonUtils = new JSONUtils(); - assertTrue(jsonUtils instanceof JSONUtils); - } - - @Test - public void isValidJsonTest() throws JsonGenerationException, JsonMappingException, IOException { - String json = JSONUtils.objectToJson(getUser()); - boolean actual = JSONUtils.isValidJSON(json); - assertTrue(actual); - } - - @Test - public void isValidJsonFailTest() throws JsonGenerationException, JsonMappingException, IOException { - String json = JSONUtils.objectToJson(getUser()); - String brokenJson = json.substring(0, json.length()/2); - boolean actual = JSONUtils.isValidJSON(brokenJson); - assertFalse(actual); - } - - @Test - public void isValidJsonNullValueTest() throws JsonParseException, IOException { - boolean actual = JSONUtils.isValidJSON(null); - assertFalse(actual); - } - - @Test - public void objectToJsonTest() throws JsonGenerationException, JsonMappingException, IOException { - String json = JSONUtils.objectToJson(getUser()); - Boolean valid = JSONUtils.isValidJSON(json); - assertTrue(valid); - } - - @Test - public void objectToJsonNullTest() throws JsonGenerationException, JsonMappingException, IOException { - String json = JSONUtils.objectToJson(null); - Boolean valid = JSONUtils.isValidJSON(json); - assertTrue(valid); - } - - @Test - public void jsonToListObjectTest() throws JsonMappingException, JsonProcessingException { - List expected = Arrays.asList(new String[] {"Alex", "Christie", "Gage"}); - String json = JSONUtils.objectToJson(expected); - List actual = JSONUtils.jsonToListObject(json, String.class); - assertEquals(expected, actual); - } - - -}