generated from micronaut-projects/micronaut-project-template
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add serde for jackson-databind JsonNode (#981)
I added it to the support module so that it still works with a non-jackson ObjectMapper.
- Loading branch information
Showing
6 changed files
with
217 additions
and
18 deletions.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
serde-jackson-tck/src/main/java/io/micronaut/serde/jackson/builder/introspected/Address.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
serde-jackson/src/test/groovy/io/micronaut/serde/jackson/JacksonJsonNodeSerdeSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
serde-support/src/main/java/io/micronaut/serde/support/serdes/JacksonJsonNodeSerde.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters