Skip to content

Commit

Permalink
Sure JsonNode is serializable/deserializable by default mapper (#9175)
Browse files Browse the repository at this point in the history
  • Loading branch information
graemerocher authored Apr 28, 2023
1 parent 633fc76 commit 0db6f7d
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
* @since 3.1
*/
@Singleton
final class JsonNodeDeserializer extends JsonDeserializer<JsonNode> {
public final class JsonNodeDeserializer extends JsonDeserializer<JsonNode> {
@Override
public JsonNode deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
return JsonNodeTreeCodec.getInstance().readTree(p);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
* @since 3.1
*/
@Singleton
final class JsonNodeSerializer extends JsonSerializer<JsonNode> {
public final class JsonNodeSerializer extends JsonSerializer<JsonNode> {
@Override
public void serialize(JsonNode value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
if (value == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 0db6f7d

Please sign in to comment.