|
| 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 | +} |
0 commit comments