Skip to content
Open
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 @@ -15,6 +15,7 @@
import tools.jackson.core.JsonToken;
import tools.jackson.core.type.TypeReference;
import tools.jackson.databind.ObjectMapper;
import tools.jackson.databind.cfg.MapperBuilder;
import tools.jackson.databind.json.JsonMapper;
import io.netty.buffer.ByteBufInputStream;
import io.vertx.core.buffer.Buffer;
Expand All @@ -28,13 +29,14 @@
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

/**
* @author <a href="mailto:julien@julienviet.com">Julien Viet</a>
*/
public class DatabindCodec extends JacksonCodec {

private static final ObjectMapper mapper = JsonMapper
private static ObjectMapper mapper = JsonMapper
.builder(JacksonCodec.factory)
.addModule(new VertxModule())
.build();
Expand All @@ -46,6 +48,10 @@ public static ObjectMapper mapper() {
return mapper;
}

public static void updateMapper(Function<MapperBuilder<ObjectMapper, ?>, ObjectMapper> f) {
mapper = f.apply(mapper().rebuild());
}

@Override
public <T> T fromValue(Object json, Class<T> clazz) {
T value = DatabindCodec.mapper.convertValue(json, clazz);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package io.vertx.tests.json.jackson.v3;

import io.vertx.core.json.jackson.v3.DatabindCodec;
import org.junit.Test;
import tools.jackson.core.JacksonException;
import tools.jackson.databind.DeserializationFeature;
import tools.jackson.databind.ObjectMapper;

import static org.junit.Assert.assertThrows;

public class MapperConfigurationTest {

public static class User {
private int age;

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}
}

@Test
public void testConfigurationChanges() {
final ObjectMapper oldMapper = DatabindCodec.mapper();
assertThrows(JacksonException.class, () -> oldMapper.readValue("{\"age\": null}", User.class));

DatabindCodec.updateMapper(builder -> builder.disable(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES).build());

final ObjectMapper newMapper = DatabindCodec.mapper();
newMapper.readValue("{\"age\": null}", User.class);
}
}