diff --git a/jackson-databind/src/main/java/io/micronaut/jackson/ObjectMapperFactory.java b/jackson-databind/src/main/java/io/micronaut/jackson/ObjectMapperFactory.java index 3aa1b639ba5..d0a44545798 100644 --- a/jackson-databind/src/main/java/io/micronaut/jackson/ObjectMapperFactory.java +++ b/jackson-databind/src/main/java/io/micronaut/jackson/ObjectMapperFactory.java @@ -99,6 +99,24 @@ public JsonFactory jsonFactory(JacksonConfiguration jacksonConfiguration) { return jsonFactoryBuilder.build(); } + /** + * Set additional serializers. + * @param serializers The serializers + * @since 4.0 + */ + public void setSerializers(JsonSerializer... serializers) { + this.serializers = serializers; + } + + /** + * Set additional deserializers. + * @param deserializers The deserializers + * @since 4.0 + */ + public void setDeserializers(JsonDeserializer... deserializers) { + this.deserializers = deserializers; + } + /** * Builds the core Jackson {@link ObjectMapper} from the optional configuration and {@link JsonFactory}. * diff --git a/jackson-databind/src/main/java/io/micronaut/jackson/databind/JacksonDatabindMapper.java b/jackson-databind/src/main/java/io/micronaut/jackson/databind/JacksonDatabindMapper.java index 5e84d5b621a..ae2b596d4b9 100644 --- a/jackson-databind/src/main/java/io/micronaut/jackson/databind/JacksonDatabindMapper.java +++ b/jackson-databind/src/main/java/io/micronaut/jackson/databind/JacksonDatabindMapper.java @@ -37,6 +37,8 @@ import io.micronaut.jackson.core.parser.JacksonCoreProcessor; import io.micronaut.jackson.core.tree.JsonNodeTreeCodec; import io.micronaut.jackson.core.tree.TreeGenerator; +import io.micronaut.jackson.serialize.JsonNodeDeserializer; +import io.micronaut.jackson.serialize.JsonNodeSerializer; import io.micronaut.json.JsonFeatures; import io.micronaut.json.JsonMapper; import io.micronaut.json.JsonStreamConfig; @@ -79,7 +81,14 @@ public JacksonDatabindMapper(ObjectMapper objectMapper) { @Internal public JacksonDatabindMapper() { - this(new ObjectMapperFactory().objectMapper(null, null)); + this(createDefaultMapper()); + } + + private static ObjectMapper createDefaultMapper() { + ObjectMapperFactory objectMapperFactory = new ObjectMapperFactory(); + objectMapperFactory.setDeserializers(new JsonNodeDeserializer()); + objectMapperFactory.setSerializers(new JsonNodeSerializer()); + return objectMapperFactory.objectMapper(null, null); } @Internal diff --git a/jackson-databind/src/main/java/io/micronaut/jackson/serialize/JsonNodeDeserializer.java b/jackson-databind/src/main/java/io/micronaut/jackson/serialize/JsonNodeDeserializer.java index 69ad58537af..41d2a305543 100644 --- a/jackson-databind/src/main/java/io/micronaut/jackson/serialize/JsonNodeDeserializer.java +++ b/jackson-databind/src/main/java/io/micronaut/jackson/serialize/JsonNodeDeserializer.java @@ -31,7 +31,7 @@ * @since 3.1 */ @Singleton -final class JsonNodeDeserializer extends JsonDeserializer { +public final class JsonNodeDeserializer extends JsonDeserializer { @Override public JsonNode deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { return JsonNodeTreeCodec.getInstance().readTree(p); diff --git a/jackson-databind/src/main/java/io/micronaut/jackson/serialize/JsonNodeSerializer.java b/jackson-databind/src/main/java/io/micronaut/jackson/serialize/JsonNodeSerializer.java index fa413a82e65..9a391aae546 100644 --- a/jackson-databind/src/main/java/io/micronaut/jackson/serialize/JsonNodeSerializer.java +++ b/jackson-databind/src/main/java/io/micronaut/jackson/serialize/JsonNodeSerializer.java @@ -31,7 +31,7 @@ * @since 3.1 */ @Singleton -final class JsonNodeSerializer extends JsonSerializer { +public final class JsonNodeSerializer extends JsonSerializer { @Override public void serialize(JsonNode value, JsonGenerator gen, SerializerProvider serializers) throws IOException { if (value == null) { diff --git a/jackson-databind/src/test/groovy/io/micronaut/jackson/databind/JacksonDatabindMapperSpec.groovy b/jackson-databind/src/test/groovy/io/micronaut/jackson/databind/JacksonDatabindMapperSpec.groovy index a15a432a38e..ae6cb258d2b 100644 --- a/jackson-databind/src/test/groovy/io/micronaut/jackson/databind/JacksonDatabindMapperSpec.groovy +++ b/jackson-databind/src/test/groovy/io/micronaut/jackson/databind/JacksonDatabindMapperSpec.groovy @@ -13,6 +13,14 @@ import io.micronaut.json.tree.JsonNode import spock.lang.Specification class JacksonDatabindMapperSpec extends Specification { + def "test default parsing to JsonNode"() { + given: + def mapper = JsonMapper.createDefault() + + expect: + mapper.readValue('{}', Argument.of(JsonNode)) == JsonNode.createObjectNode([:]) + } + def 'parsing to JsonNode'() { given: def ctx = ApplicationContext.run()