diff --git a/common/pom.xml b/common/pom.xml index 9355dff..248e47c 100644 --- a/common/pom.xml +++ b/common/pom.xml @@ -18,12 +18,6 @@ - - - com.fasterxml.jackson.core - jackson-databind - 2.20.2 - diff --git a/common/src/main/java/lt/satsyuk/common/util/JsonUtil.java b/common/src/main/java/lt/satsyuk/common/util/JsonUtil.java deleted file mode 100644 index eb341bf..0000000 --- a/common/src/main/java/lt/satsyuk/common/util/JsonUtil.java +++ /dev/null @@ -1,47 +0,0 @@ -package lt.satsyuk.common.util; - -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.core.type.TypeReference; -import com.fasterxml.jackson.databind.ObjectMapper; - -import java.util.Objects; - -public final class JsonUtil { - - private static final ObjectMapper MAPPER = new ObjectMapper().findAndRegisterModules(); - - public static String toJson(Object o) { - Objects.requireNonNull(o, "Object to serialize must not be null"); - try { - return MAPPER.writeValueAsString(o); - } catch (JsonProcessingException e) { - throw new JsonUtilException("JSON serialization error", e); - } - } - - public static T fromJson(String json, Class type) { - if (json == null || json.isBlank()) { - throw new IllegalArgumentException("JSON input must not be null or blank"); - } - Objects.requireNonNull(type, "Target type must not be null"); - try { - return MAPPER.readValue(json, type); - } catch (JsonProcessingException e) { - throw new JsonUtilException("JSON deserialization error for type: " + type.getName(), e); - } - } - - public static T fromJson(String json, TypeReference typeReference) { - if (json == null || json.isBlank()) { - throw new IllegalArgumentException("JSON input must not be null or blank"); - } - Objects.requireNonNull(typeReference, "Target type reference must not be null"); - try { - return MAPPER.readValue(json, typeReference); - } catch (JsonProcessingException e) { - throw new JsonUtilException("JSON deserialization error for generic type", e); - } - } - - private JsonUtil() {} -} \ No newline at end of file diff --git a/common/src/main/java/lt/satsyuk/common/util/JsonUtilException.java b/common/src/main/java/lt/satsyuk/common/util/JsonUtilException.java deleted file mode 100644 index 237c7f1..0000000 --- a/common/src/main/java/lt/satsyuk/common/util/JsonUtilException.java +++ /dev/null @@ -1,8 +0,0 @@ -package lt.satsyuk.common.util; - -public class JsonUtilException extends RuntimeException { - - public JsonUtilException(String message, Throwable cause) { - super(message, cause); - } -} diff --git a/common/src/test/java/lt/satsyuk/common/util/JsonUtilTest.java b/common/src/test/java/lt/satsyuk/common/util/JsonUtilTest.java deleted file mode 100644 index 6fead97..0000000 --- a/common/src/test/java/lt/satsyuk/common/util/JsonUtilTest.java +++ /dev/null @@ -1,60 +0,0 @@ -package lt.satsyuk.common.util; - -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.core.type.TypeReference; -import org.junit.jupiter.api.Test; - -import java.util.List; -import java.util.Map; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertInstanceOf; -import static org.junit.jupiter.api.Assertions.assertThrows; - -class JsonUtilTest { - - @Test - void fromJsonTypeReferenceShouldDeserializeGenericPayload() { - String json = "{\"ja\":[1,2],\"jav\":[3]}"; - - Map> result = JsonUtil.fromJson( - json, - new TypeReference>>() { - } - ); - - assertEquals(List.of(1, 2), result.get("ja")); - assertEquals(List.of(3), result.get("jav")); - } - - @Test - void fromJsonTypeReferenceShouldFailOnBlankJson() { - TypeReference> typeReference = new TypeReference>() { - }; - - IllegalArgumentException ex = assertThrows( - IllegalArgumentException.class, - () -> JsonUtil.fromJson(" ", typeReference) - ); - - assertEquals("JSON input must not be null or blank", ex.getMessage()); - } - - @Test - void fromJsonTypeReferenceShouldFailOnNullTypeReference() { - assertThrows(NullPointerException.class, () -> JsonUtil.fromJson("{}", (TypeReference>) null)); - } - - @Test - void fromJsonTypeReferenceShouldWrapJacksonException() { - TypeReference> typeReference = new TypeReference>() { - }; - - JsonUtilException ex = assertThrows( - JsonUtilException.class, - () -> JsonUtil.fromJson("{bad json}", typeReference) - ); - - assertInstanceOf(JsonProcessingException.class, ex.getCause()); - } -}