Skip to content

Commit

Permalink
support jackson custom module. (#14817)
Browse files Browse the repository at this point in the history
  • Loading branch information
heliang666s authored Oct 25, 2024
1 parent d1cc4bf commit 4a3e758
Showing 1 changed file with 18 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@
import org.apache.dubbo.common.extension.Activate;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.Module;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.fasterxml.jackson.databind.json.JsonMapper.Builder;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
Expand All @@ -34,6 +36,7 @@
public class JacksonImpl extends AbstractJsonUtilImpl {

private volatile JsonMapper mapper;
private final List<Module> customModules = new ArrayList<>();

@Override
public String getName() {
Expand Down Expand Up @@ -113,10 +116,24 @@ protected JsonMapper getMapper() {
}

protected Builder createBuilder() {
return JsonMapper.builder()
Builder builder = JsonMapper.builder()
.configure(MapperFeature.PROPAGATE_TRANSIENT_MARKER, true)
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.serializationInclusion(Include.NON_NULL)
.addModule(new JavaTimeModule());

for (Module module : customModules) {
builder.addModule(module);
}

return builder;
}

public void addModule(Module module) {
synchronized (this) {
customModules.add(module);
// Invalidate the mapper to rebuild it
this.mapper = null;
}
}
}

0 comments on commit 4a3e758

Please sign in to comment.