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

Support customize jaskson module in JsonUtil #14647

Closed
wants to merge 5 commits into from
Closed
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
Expand Up @@ -40,6 +40,14 @@ public String getName() {
return "jackson";
}

private volatile JsonMapper customizedMapper = null;

public void setCustomizedMapper(JsonMapper customizedMapper) {
this.customizedMapper = customizedMapper;
// Reset the cache to apply custom configurations
this.jacksonCache = null;
}

@Override
public boolean isJson(String json) {
try {
Expand Down Expand Up @@ -103,6 +111,17 @@ protected JsonMapper getMapper() {
JsonMapper mapper = this.mapper;
if (mapper == null) {
synchronized (this) {
if (jacksonCache == null || !(jacksonCache instanceof JsonMapper)) {
if (customizedMapper != null) {
jacksonCache = customizedMapper;
} else {
jacksonCache = JsonMapper.builder()
.configure(MapperFeature.PROPAGATE_TRANSIENT_MARKER, true)
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.serializationInclusion(Include.NON_NULL)
.addModule(new JavaTimeModule())
.build();
}
mapper = this.mapper;
if (mapper == null) {
this.mapper = mapper = createBuilder().build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,19 @@
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.mockito.MockedConstruction;
import org.mockito.Mockito;

import static org.junit.jupiter.api.Assertions.assertEquals;

class JsonUtilsTest {
private AtomicBoolean allowFastjson2 = new AtomicBoolean(true);
private AtomicBoolean allowFastjson = new AtomicBoolean(true);
Expand Down Expand Up @@ -394,6 +401,25 @@ void testGetJson2() {
setJson(null);
}

@Test
public void customizeJackson() {
JsonMapper customize = JsonMapper.builder()
.configure(MapperFeature.PROPAGATE_TRANSIENT_MARKER, true)
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.serializationInclusion(Include.NON_NULL)
// customize model
.addModule(new SimpleModule())
.build();
JacksonImpl jackson = new JacksonImpl();
jackson.setCustomizedMapper(customize);

String name = "dubbo";

String json = jackson.toJson(name);
System.out.println("Serialized JSON: " + json);
assertEquals("\"dubbo\"", json); // JSON 字符串会加上双引号
}

private static Field jsonFieldCache;

private static void setJson(JsonUtil json) {
Expand Down
Loading