Skip to content

Commit

Permalink
Add serde for jackson-databind JsonNode (#981)
Browse files Browse the repository at this point in the history
I added it to the support module so that it still works with a non-jackson ObjectMapper.
  • Loading branch information
yawkat authored Nov 26, 2024
1 parent b166cd7 commit 55a3cc1
Show file tree
Hide file tree
Showing 6 changed files with 217 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/*
* Copyright 2017-2024 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micronaut.serde.jackson.builder.introspected;

import io.micronaut.core.annotation.Introspected;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package io.micronaut.serde.jackson

import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.databind.node.JsonNodeFactory
import com.fasterxml.jackson.databind.node.ObjectNode
import io.micronaut.serde.ObjectMapper
import io.micronaut.test.extensions.spock.annotation.MicronautTest
import jakarta.inject.Inject
import spock.lang.Specification

@MicronautTest(startApplication = false)
class JacksonJsonNodeSerdeSpec extends Specification {
@Inject
ObjectMapper objectMapper

def 'back and forth'(JsonNode node) {
when:
def json = objectMapper.writeValueAsString(node)
def back = objectMapper.readValue(json, JsonNode)
then:
back == node

where:
// commented nodes have ambiguous encoding with other nodes
node << [
JsonNodeFactory.instance.nullNode(),
JsonNodeFactory.instance.booleanNode(true),
JsonNodeFactory.instance.booleanNode(false),
JsonNodeFactory.instance.textNode("foo"),
JsonNodeFactory.instance.numberNode((byte) 1),
//JsonNodeFactory.instance.numberNode((short) 1),
JsonNodeFactory.instance.numberNode((int) 1),
JsonNodeFactory.instance.numberNode(Long.MAX_VALUE),
//JsonNodeFactory.instance.numberNode((float) 1),
JsonNodeFactory.instance.numberNode((double) 1),
JsonNodeFactory.instance.numberNode(BigInteger.valueOf(Long.MAX_VALUE) + 1),
//JsonNodeFactory.instance.numberNode(BigDecimal.valueOf(Double.MAX_VALUE) + 1.5),
//JsonNodeFactory.instance.binaryNode("foo".bytes),
JsonNodeFactory.instance.objectNode()
.<ObjectNode> set("p1", JsonNodeFactory.instance.numberNode(1))
.<ObjectNode> set("p2", JsonNodeFactory.instance.numberNode(2)),
JsonNodeFactory.instance.arrayNode().add(1).add(2),
]
}
}
1 change: 1 addition & 0 deletions serde-support/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ dependencies {

compileOnly(mn.micronaut.management)
compileOnly(libs.jetbrains.annotations)
compileOnly(mn.jackson.databind)

api(projects.micronautSerdeApi)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/*
* Copyright 2017-2024 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micronaut.serde.support.serdes;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.annotation.Nullable;
import io.micronaut.core.type.Argument;
import io.micronaut.serde.Decoder;
import io.micronaut.serde.Encoder;
import io.micronaut.serde.support.SerdeRegistrar;

import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Map;

/**
* Serde for jackson-databind {@link JsonNode}.
*
* @since 2.13.0
* @author Jonas Konrad
* @implNote Recursive implementation. Recursion depth is limited by decoder/encoder constraints ({@link io.micronaut.serde.LimitingStream}) for security.
*/
final class JacksonJsonNodeSerde implements SerdeRegistrar<JsonNode> {
private static final @NonNull Argument<JsonNode> JSON_NODE_ARGUMENT = Argument.of(JsonNode.class);

@Override
public @NonNull Argument<JsonNode> getType() {
return JSON_NODE_ARGUMENT;
}

@Override
public JsonNode deserializeNullable(@NonNull Decoder decoder, @NonNull DecoderContext context, @NonNull Argument<? super JsonNode> type) throws IOException {
return deserialize(decoder, context, type);
}

@Override
public @Nullable JsonNode deserialize(@NonNull Decoder decoder, @NonNull DecoderContext context, @NonNull Argument<? super JsonNode> type) throws IOException {
return toJacksonNode(JsonNodeFactory.instance, decoder.decodeNode());
}

private static JsonNode toJacksonNode(JsonNodeFactory factory, io.micronaut.json.tree.JsonNode mnNode) {
if (mnNode.isArray()) {
ArrayNode n = factory.arrayNode(mnNode.size());
for (io.micronaut.json.tree.JsonNode value : mnNode.values()) {
n.add(toJacksonNode(factory, value));
}
return n;
} else if (mnNode.isObject()) {
ObjectNode n = factory.objectNode();
for (Map.Entry<String, io.micronaut.json.tree.JsonNode> entry : mnNode.entries()) {
n.set(entry.getKey(), toJacksonNode(factory, entry.getValue()));
}
return n;
} else if (mnNode.isNull()) {
return factory.nullNode();
} else if (mnNode.isBoolean()) {
return factory.booleanNode(mnNode.getBooleanValue());
} else if (mnNode.isString()) {
return factory.textNode(mnNode.getStringValue());
} else {
Number numberValue = mnNode.getNumberValue();
if (numberValue instanceof Integer) {
return factory.numberNode(numberValue.intValue());
} else if (numberValue instanceof Long) {
return factory.numberNode(numberValue.longValue());
} else if (numberValue instanceof Short) {
return factory.numberNode(numberValue.shortValue());
} else if (numberValue instanceof Byte) {
return factory.numberNode(numberValue.byteValue());
} else if (numberValue instanceof Float) {
return factory.numberNode(numberValue.floatValue());
} else if (numberValue instanceof BigInteger bi) {
return factory.numberNode(bi);
} else if (numberValue instanceof BigDecimal bd) {
return factory.numberNode(bd);
} else {
// double, other number types
return factory.numberNode(numberValue.doubleValue());
}
}
}

@Override
public void serialize(@NonNull Encoder encoder, @NonNull EncoderContext context, @NonNull Argument<? extends JsonNode> type, @NonNull JsonNode node) throws IOException {
switch (node.getNodeType()) {
case BOOLEAN -> encoder.encodeBoolean(node.booleanValue());
case NULL -> encoder.encodeNull();
case NUMBER -> JsonNodeSerde.encodeNumber(encoder, node.numberValue());
case STRING -> encoder.encodeString(node.textValue());
case BINARY -> encoder.encodeBinary(node.binaryValue());
case ARRAY -> {
try (Encoder array = encoder.encodeArray(JSON_NODE_ARGUMENT)) {
for (JsonNode member : node) {
serialize(array, context, type, member);
}
}
}
case OBJECT -> {
try (Encoder obj = encoder.encodeObject(JSON_NODE_ARGUMENT)) {
for (String k : (Iterable<String>) node::fieldNames) {
obj.encodeKey(k);
serialize(encoder, context, type, node.get(k));
}
}
}
default -> throw new IllegalArgumentException("Cannot serialize jackson-databind JsonNode of type " + node.getNodeType());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@

@Internal
final class JsonNodeSerde implements SerdeRegistrar<JsonNode> {

@Override
public void serialize(Encoder encoder, EncoderContext context, Argument<? extends JsonNode> type, JsonNode value)
throws IOException {
Expand All @@ -47,23 +46,7 @@ private void serialize0(Encoder encoder, JsonNode value) throws IOException {
} else if (value.isString()) {
encoder.encodeString(value.getStringValue());
} else if (value.isNumber()) {
Number numberValue = value.getNumberValue();
if (numberValue instanceof Integer) {
encoder.encodeInt(numberValue.intValue());
} else if (numberValue instanceof Long) {
encoder.encodeLong(numberValue.longValue());
} else if (numberValue instanceof Short) {
encoder.encodeShort(numberValue.shortValue());
} else if (numberValue instanceof Byte) {
encoder.encodeByte(numberValue.byteValue());
} else if (numberValue instanceof BigInteger bi) {
encoder.encodeBigInteger(bi);
} else if (numberValue instanceof BigDecimal bd) {
encoder.encodeBigDecimal(bd);
} else {
// double, float, other number types
encoder.encodeDouble(numberValue.doubleValue());
}
encodeNumber(encoder, value.getNumberValue());
} else if (value.isObject()) {
Encoder objectEncoder = encoder.encodeObject(Argument.of(JsonNode.class));
for (Map.Entry<String, JsonNode> entry : value.entries()) {
Expand All @@ -82,6 +65,25 @@ private void serialize0(Encoder encoder, JsonNode value) throws IOException {
}
}

static void encodeNumber(Encoder encoder, Number numberValue) throws IOException {
if (numberValue instanceof Integer) {
encoder.encodeInt(numberValue.intValue());
} else if (numberValue instanceof Long) {
encoder.encodeLong(numberValue.longValue());
} else if (numberValue instanceof Short) {
encoder.encodeShort(numberValue.shortValue());
} else if (numberValue instanceof Byte) {
encoder.encodeByte(numberValue.byteValue());
} else if (numberValue instanceof BigInteger bi) {
encoder.encodeBigInteger(bi);
} else if (numberValue instanceof BigDecimal bd) {
encoder.encodeBigDecimal(bd);
} else {
// double, float, other number types
encoder.encodeDouble(numberValue.doubleValue());
}
}

@Override
public JsonNode deserialize(Decoder decoder, DecoderContext decoderContext, Argument<? super JsonNode> type)
throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,15 @@ public static void register(SerdeConfiguration serdeConfiguration,
consumer.accept(new ZonedDateTimeSerde(serdeConfiguration));
consumer.accept(new EnumSerde<>(introspections));
consumer.accept(new InetAddressSerde(serdeConfiguration));
SerdeRegistrar<?> jacksonJsonNodeSerde;
try {
jacksonJsonNodeSerde = new JacksonJsonNodeSerde();
} catch (LinkageError ignored) {
jacksonJsonNodeSerde = null;
}
if (jacksonJsonNodeSerde != null) {
consumer.accept(jacksonJsonNodeSerde);
}
}

}

0 comments on commit 55a3cc1

Please sign in to comment.