Skip to content
This repository has been archived by the owner on Jun 15, 2023. It is now read-only.

Commit

Permalink
Issue #3: llow automatic deserialization of immutable classes using p…
Browse files Browse the repository at this point in the history
…arameter names
  • Loading branch information
magnet committed Oct 16, 2017
1 parent 4e133c8 commit e15f3f2
Showing 1 changed file with 24 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,18 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonCreator.Mode;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.Module;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.cfg.MapperConfig;
import com.fasterxml.jackson.databind.introspect.Annotated;
import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;
Expand All @@ -45,10 +51,11 @@
@Component(immediate = true, service = { JacksonMapper.class, JsonSerDes.class, JsonSerializer.class,
JsonDeserializer.class })
public final class JacksonMapperImpl implements JacksonMapper {

private static final Logger LOGGER = LoggerFactory.getLogger(JacksonMapperImpl.class);

private final List<Module> BASE_MODULES = Arrays.asList(new Jdk8Module(), new JavaTimeModule(),
new ParameterNamesModule());
private static final List<Module> BASE_MODULES = Arrays.asList(new Jdk8Module(), new JavaTimeModule(),
new ParameterNamesModule(Mode.PROPERTIES));

private final Object lock = new Object();
private ObjectMapper mapper;
Expand Down Expand Up @@ -79,7 +86,21 @@ public void ensureMapperConfig() {
this.mapper.enable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
this.mapper.configure(JsonParser.Feature.ALLOW_NON_NUMERIC_NUMBERS, true);

this.mapper.enable(MapperFeature.INFER_CREATOR_FROM_CONSTRUCTOR_PROPERTIES);
this.mapper.setAnnotationIntrospector(new JacksonAnnotationIntrospector() {
public JsonCreator.Mode findCreatorAnnotation(MapperConfig<?> config, Annotated a) {
JsonCreator ann = _findAnnotation(a, JsonCreator.class);
if (ann != null) {
JsonCreator.Mode mode = ann.mode();
// but keep in mind that there may be explicit default for this module
if (mode == JsonCreator.Mode.DEFAULT) {
mode = Mode.PROPERTIES;
}
return mode;
}
return Mode.PROPERTIES;
}
});
// TODO Make Filters & AnnotationIntrospecter settable with service
}

Expand Down

0 comments on commit e15f3f2

Please sign in to comment.