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 @@ -17,6 +17,7 @@
import tools.jackson.core.StreamWriteFeature;
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 @@ -26,20 +27,20 @@
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
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 volatile ObjectMapper mapper = JsonMapper
.builder(JacksonCodec.factory)
.addModule(new VertxModule())
.build();
Expand All @@ -51,6 +52,16 @@ public static ObjectMapper mapper() {
return mapper;
}

/**
* this creates a @{@link MapperBuilder} from the already configured @{@link ObjectMapper}
* this builder is passed to the function which returns a new mapper
*
* @param f update mapper function
*/
public static void rebuildMapper(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.rebuildMapper(builder -> builder.disable(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES).build());

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