Skip to content

Commit

Permalink
Improve dependency between JsonUtil and Configurator
Browse files Browse the repository at this point in the history
  • Loading branch information
sashirestela committed Jan 23, 2025
1 parent 69e37b5 commit af0305b
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.sashirestela.cleverclient.support;

import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.sashirestela.cleverclient.util.JsonUtil;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
Expand Down Expand Up @@ -29,6 +30,7 @@ public Configurator(@Singular("endOfStream") List<String> endsOfStream, ObjectMa
}
configurator.endsOfStream = endsOfStream;
configurator.objectMapper = objectMapper;
JsonUtil.updateObjectMapper(objectMapper);
configurator.wasBuilt = true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import com.fasterxml.jackson.databind.ObjectReader;
import com.fasterxml.jackson.databind.type.CollectionType;
import io.github.sashirestela.cleverclient.support.CleverClientException;
import io.github.sashirestela.cleverclient.support.Configurator;

import java.io.IOException;
import java.util.ArrayList;
Expand All @@ -17,17 +16,24 @@

public class JsonUtil {

private static final ObjectMapper objectMapperStrict = Configurator.one().getObjectMapper();

private static final ObjectReader objectReaderIgnoringUnknown = objectMapperStrict.reader()
private static ObjectMapper objectMapper = new ObjectMapper();
private static ObjectReader objectReader = objectMapper.reader()
.without(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);

private JsonUtil() {
}

public static void updateObjectMapper(ObjectMapper newObjectMapper) {
synchronized (JsonUtil.class) {
objectMapper = newObjectMapper != null ? newObjectMapper : new ObjectMapper();
objectReader = objectMapper.reader()
.without(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
}
}

public static <T> Map<String, Object> objectToMap(T object) {
try {
return objectMapperStrict.convertValue(object, new TypeReference<>() {
return objectMapper.convertValue(object, new TypeReference<>() {
});
} catch (IllegalArgumentException e) {
throw new CleverClientException("Cannot convert object {0} to Map.", object, e);
Expand All @@ -36,35 +42,35 @@ public static <T> Map<String, Object> objectToMap(T object) {

public static <T> String objectToJson(T object) {
try {
return objectMapperStrict.writeValueAsString(object);
return objectMapper.writeValueAsString(object);
} catch (JsonProcessingException e) {
throw new CleverClientException("Cannot convert the object {0} to Json.", object, e);
}
}

public static <T> T jsonToObject(String json, Class<T> clazz) {
try {
return objectReaderIgnoringUnknown.readValue(json, clazz);
return objectReader.readValue(json, clazz);
} catch (IOException e) {
throw new CleverClientException("Cannot convert the Json {0} to class {1}.", json, clazz.getName(), e);
}
}

public static <T> List<T> jsonToList(String json, Class<T> clazz) {
try {
CollectionType listType = objectReaderIgnoringUnknown.getTypeFactory()
CollectionType listType = objectReader.getTypeFactory()
.constructCollectionType(ArrayList.class, clazz);
return objectReaderIgnoringUnknown.forType(listType).readValue(json);
return objectReader.forType(listType).readValue(json);
} catch (JsonProcessingException e) {
throw new CleverClientException("Cannot convert the Json {0} to List of {1}.", json, clazz.getName(), e);
}
}

public static <T, U> T jsonToParametricObject(String json, Class<T> clazzT, Class<U> clazzU) {
try {
JavaType javaType = objectReaderIgnoringUnknown.getTypeFactory()
JavaType javaType = objectReader.getTypeFactory()
.constructParametricType(clazzT, clazzU);
return objectReaderIgnoringUnknown.forType(javaType).readValue(json);
return objectReader.forType(javaType).readValue(json);
} catch (JsonProcessingException e) {
throw new CleverClientException("Cannot convert the Json {0} to class of {1}.", json, clazzT.getName(), e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@
import io.github.sashirestela.cleverclient.client.JavaHttpClientAdapter;
import io.github.sashirestela.cleverclient.http.HttpRequestData;
import io.github.sashirestela.cleverclient.support.ContentType;
import io.github.sashirestela.cleverclient.test.TestSupport;
import io.github.sashirestela.cleverclient.util.HttpRequestBodyTestUtility;
import lombok.Builder;
import lombok.Value;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;

Expand All @@ -36,11 +34,6 @@

class CleverClientTest {

@BeforeAll
static void setup() {
TestSupport.setupConfigurator();
}

@Test
void shouldSetPropertiesToDefaultValuesWhenBuilderIsCalledWithoutThoseProperties() {
var cleverClient = CleverClient.builder()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package io.github.sashirestela.cleverclient.http;

import io.github.sashirestela.cleverclient.support.CleverClientException;
import io.github.sashirestela.cleverclient.test.TestSupport;
import io.github.sashirestela.cleverclient.test.TestSupport.SyncType;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import java.io.ByteArrayInputStream;
Expand Down Expand Up @@ -53,11 +51,6 @@ interface HttpProcessorTest {

void testShutdown();

@BeforeAll
static void setup() {
TestSupport.setupConfigurator();
}

@Test
default void shouldThownExceptionWhenCallingMethodReturnTypeIsUnsupported() {
var service = getHttpProcessor().createProxy(ITest.AsyncService.class);
Expand Down

0 comments on commit af0305b

Please sign in to comment.