Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve dependency between JsonUtil and Configurator #102

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading