Skip to content

Commit 55a3cc1

Browse files
authored
Add serde for jackson-databind JsonNode (#981)
I added it to the support module so that it still works with a non-jackson ObjectMapper.
1 parent b166cd7 commit 55a3cc1

File tree

6 files changed

+217
-18
lines changed

6 files changed

+217
-18
lines changed

serde-jackson-tck/src/main/java/io/micronaut/serde/jackson/builder/introspected/Address.java

+15
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
/*
2+
* Copyright 2017-2024 original authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
116
package io.micronaut.serde.jackson.builder.introspected;
217

318
import io.micronaut.core.annotation.Introspected;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package io.micronaut.serde.jackson
2+
3+
import com.fasterxml.jackson.databind.JsonNode
4+
import com.fasterxml.jackson.databind.node.JsonNodeFactory
5+
import com.fasterxml.jackson.databind.node.ObjectNode
6+
import io.micronaut.serde.ObjectMapper
7+
import io.micronaut.test.extensions.spock.annotation.MicronautTest
8+
import jakarta.inject.Inject
9+
import spock.lang.Specification
10+
11+
@MicronautTest(startApplication = false)
12+
class JacksonJsonNodeSerdeSpec extends Specification {
13+
@Inject
14+
ObjectMapper objectMapper
15+
16+
def 'back and forth'(JsonNode node) {
17+
when:
18+
def json = objectMapper.writeValueAsString(node)
19+
def back = objectMapper.readValue(json, JsonNode)
20+
then:
21+
back == node
22+
23+
where:
24+
// commented nodes have ambiguous encoding with other nodes
25+
node << [
26+
JsonNodeFactory.instance.nullNode(),
27+
JsonNodeFactory.instance.booleanNode(true),
28+
JsonNodeFactory.instance.booleanNode(false),
29+
JsonNodeFactory.instance.textNode("foo"),
30+
JsonNodeFactory.instance.numberNode((byte) 1),
31+
//JsonNodeFactory.instance.numberNode((short) 1),
32+
JsonNodeFactory.instance.numberNode((int) 1),
33+
JsonNodeFactory.instance.numberNode(Long.MAX_VALUE),
34+
//JsonNodeFactory.instance.numberNode((float) 1),
35+
JsonNodeFactory.instance.numberNode((double) 1),
36+
JsonNodeFactory.instance.numberNode(BigInteger.valueOf(Long.MAX_VALUE) + 1),
37+
//JsonNodeFactory.instance.numberNode(BigDecimal.valueOf(Double.MAX_VALUE) + 1.5),
38+
//JsonNodeFactory.instance.binaryNode("foo".bytes),
39+
JsonNodeFactory.instance.objectNode()
40+
.<ObjectNode> set("p1", JsonNodeFactory.instance.numberNode(1))
41+
.<ObjectNode> set("p2", JsonNodeFactory.instance.numberNode(2)),
42+
JsonNodeFactory.instance.arrayNode().add(1).add(2),
43+
]
44+
}
45+
}

serde-support/build.gradle.kts

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ dependencies {
1616

1717
compileOnly(mn.micronaut.management)
1818
compileOnly(libs.jetbrains.annotations)
19+
compileOnly(mn.jackson.databind)
1920

2021
api(projects.micronautSerdeApi)
2122

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/*
2+
* Copyright 2017-2024 original authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.micronaut.serde.support.serdes;
17+
18+
import com.fasterxml.jackson.databind.JsonNode;
19+
import com.fasterxml.jackson.databind.node.ArrayNode;
20+
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
21+
import com.fasterxml.jackson.databind.node.ObjectNode;
22+
import io.micronaut.core.annotation.NonNull;
23+
import io.micronaut.core.annotation.Nullable;
24+
import io.micronaut.core.type.Argument;
25+
import io.micronaut.serde.Decoder;
26+
import io.micronaut.serde.Encoder;
27+
import io.micronaut.serde.support.SerdeRegistrar;
28+
29+
import java.io.IOException;
30+
import java.math.BigDecimal;
31+
import java.math.BigInteger;
32+
import java.util.Map;
33+
34+
/**
35+
* Serde for jackson-databind {@link JsonNode}.
36+
*
37+
* @since 2.13.0
38+
* @author Jonas Konrad
39+
* @implNote Recursive implementation. Recursion depth is limited by decoder/encoder constraints ({@link io.micronaut.serde.LimitingStream}) for security.
40+
*/
41+
final class JacksonJsonNodeSerde implements SerdeRegistrar<JsonNode> {
42+
private static final @NonNull Argument<JsonNode> JSON_NODE_ARGUMENT = Argument.of(JsonNode.class);
43+
44+
@Override
45+
public @NonNull Argument<JsonNode> getType() {
46+
return JSON_NODE_ARGUMENT;
47+
}
48+
49+
@Override
50+
public JsonNode deserializeNullable(@NonNull Decoder decoder, @NonNull DecoderContext context, @NonNull Argument<? super JsonNode> type) throws IOException {
51+
return deserialize(decoder, context, type);
52+
}
53+
54+
@Override
55+
public @Nullable JsonNode deserialize(@NonNull Decoder decoder, @NonNull DecoderContext context, @NonNull Argument<? super JsonNode> type) throws IOException {
56+
return toJacksonNode(JsonNodeFactory.instance, decoder.decodeNode());
57+
}
58+
59+
private static JsonNode toJacksonNode(JsonNodeFactory factory, io.micronaut.json.tree.JsonNode mnNode) {
60+
if (mnNode.isArray()) {
61+
ArrayNode n = factory.arrayNode(mnNode.size());
62+
for (io.micronaut.json.tree.JsonNode value : mnNode.values()) {
63+
n.add(toJacksonNode(factory, value));
64+
}
65+
return n;
66+
} else if (mnNode.isObject()) {
67+
ObjectNode n = factory.objectNode();
68+
for (Map.Entry<String, io.micronaut.json.tree.JsonNode> entry : mnNode.entries()) {
69+
n.set(entry.getKey(), toJacksonNode(factory, entry.getValue()));
70+
}
71+
return n;
72+
} else if (mnNode.isNull()) {
73+
return factory.nullNode();
74+
} else if (mnNode.isBoolean()) {
75+
return factory.booleanNode(mnNode.getBooleanValue());
76+
} else if (mnNode.isString()) {
77+
return factory.textNode(mnNode.getStringValue());
78+
} else {
79+
Number numberValue = mnNode.getNumberValue();
80+
if (numberValue instanceof Integer) {
81+
return factory.numberNode(numberValue.intValue());
82+
} else if (numberValue instanceof Long) {
83+
return factory.numberNode(numberValue.longValue());
84+
} else if (numberValue instanceof Short) {
85+
return factory.numberNode(numberValue.shortValue());
86+
} else if (numberValue instanceof Byte) {
87+
return factory.numberNode(numberValue.byteValue());
88+
} else if (numberValue instanceof Float) {
89+
return factory.numberNode(numberValue.floatValue());
90+
} else if (numberValue instanceof BigInteger bi) {
91+
return factory.numberNode(bi);
92+
} else if (numberValue instanceof BigDecimal bd) {
93+
return factory.numberNode(bd);
94+
} else {
95+
// double, other number types
96+
return factory.numberNode(numberValue.doubleValue());
97+
}
98+
}
99+
}
100+
101+
@Override
102+
public void serialize(@NonNull Encoder encoder, @NonNull EncoderContext context, @NonNull Argument<? extends JsonNode> type, @NonNull JsonNode node) throws IOException {
103+
switch (node.getNodeType()) {
104+
case BOOLEAN -> encoder.encodeBoolean(node.booleanValue());
105+
case NULL -> encoder.encodeNull();
106+
case NUMBER -> JsonNodeSerde.encodeNumber(encoder, node.numberValue());
107+
case STRING -> encoder.encodeString(node.textValue());
108+
case BINARY -> encoder.encodeBinary(node.binaryValue());
109+
case ARRAY -> {
110+
try (Encoder array = encoder.encodeArray(JSON_NODE_ARGUMENT)) {
111+
for (JsonNode member : node) {
112+
serialize(array, context, type, member);
113+
}
114+
}
115+
}
116+
case OBJECT -> {
117+
try (Encoder obj = encoder.encodeObject(JSON_NODE_ARGUMENT)) {
118+
for (String k : (Iterable<String>) node::fieldNames) {
119+
obj.encodeKey(k);
120+
serialize(encoder, context, type, node.get(k));
121+
}
122+
}
123+
}
124+
default -> throw new IllegalArgumentException("Cannot serialize jackson-databind JsonNode of type " + node.getNodeType());
125+
}
126+
}
127+
}

serde-support/src/main/java/io/micronaut/serde/support/serdes/JsonNodeSerde.java

+20-18
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929

3030
@Internal
3131
final class JsonNodeSerde implements SerdeRegistrar<JsonNode> {
32-
3332
@Override
3433
public void serialize(Encoder encoder, EncoderContext context, Argument<? extends JsonNode> type, JsonNode value)
3534
throws IOException {
@@ -47,23 +46,7 @@ private void serialize0(Encoder encoder, JsonNode value) throws IOException {
4746
} else if (value.isString()) {
4847
encoder.encodeString(value.getStringValue());
4948
} else if (value.isNumber()) {
50-
Number numberValue = value.getNumberValue();
51-
if (numberValue instanceof Integer) {
52-
encoder.encodeInt(numberValue.intValue());
53-
} else if (numberValue instanceof Long) {
54-
encoder.encodeLong(numberValue.longValue());
55-
} else if (numberValue instanceof Short) {
56-
encoder.encodeShort(numberValue.shortValue());
57-
} else if (numberValue instanceof Byte) {
58-
encoder.encodeByte(numberValue.byteValue());
59-
} else if (numberValue instanceof BigInteger bi) {
60-
encoder.encodeBigInteger(bi);
61-
} else if (numberValue instanceof BigDecimal bd) {
62-
encoder.encodeBigDecimal(bd);
63-
} else {
64-
// double, float, other number types
65-
encoder.encodeDouble(numberValue.doubleValue());
66-
}
49+
encodeNumber(encoder, value.getNumberValue());
6750
} else if (value.isObject()) {
6851
Encoder objectEncoder = encoder.encodeObject(Argument.of(JsonNode.class));
6952
for (Map.Entry<String, JsonNode> entry : value.entries()) {
@@ -82,6 +65,25 @@ private void serialize0(Encoder encoder, JsonNode value) throws IOException {
8265
}
8366
}
8467

68+
static void encodeNumber(Encoder encoder, Number numberValue) throws IOException {
69+
if (numberValue instanceof Integer) {
70+
encoder.encodeInt(numberValue.intValue());
71+
} else if (numberValue instanceof Long) {
72+
encoder.encodeLong(numberValue.longValue());
73+
} else if (numberValue instanceof Short) {
74+
encoder.encodeShort(numberValue.shortValue());
75+
} else if (numberValue instanceof Byte) {
76+
encoder.encodeByte(numberValue.byteValue());
77+
} else if (numberValue instanceof BigInteger bi) {
78+
encoder.encodeBigInteger(bi);
79+
} else if (numberValue instanceof BigDecimal bd) {
80+
encoder.encodeBigDecimal(bd);
81+
} else {
82+
// double, float, other number types
83+
encoder.encodeDouble(numberValue.doubleValue());
84+
}
85+
}
86+
8587
@Override
8688
public JsonNode deserialize(Decoder decoder, DecoderContext decoderContext, Argument<? super JsonNode> type)
8789
throws IOException {

serde-support/src/main/java/io/micronaut/serde/support/serdes/Serdes.java

+9
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,15 @@ public static void register(SerdeConfiguration serdeConfiguration,
122122
consumer.accept(new ZonedDateTimeSerde(serdeConfiguration));
123123
consumer.accept(new EnumSerde<>(introspections));
124124
consumer.accept(new InetAddressSerde(serdeConfiguration));
125+
SerdeRegistrar<?> jacksonJsonNodeSerde;
126+
try {
127+
jacksonJsonNodeSerde = new JacksonJsonNodeSerde();
128+
} catch (LinkageError ignored) {
129+
jacksonJsonNodeSerde = null;
130+
}
131+
if (jacksonJsonNodeSerde != null) {
132+
consumer.accept(jacksonJsonNodeSerde);
133+
}
125134
}
126135

127136
}

0 commit comments

Comments
 (0)