Код: Выделить всё
{
"rejectedValue": "rejected"
}
Код: Выделить всё
public record MyClass(Object rejectedValue) {
}
Свойство JSON отклонено всегда будет одним из следующих:
- строка
- число
/> - boolean
- null
- String
- Подкласс Number (Integer, Double, и т.д.)
- Boolean
Код: Выделить всё
import tools.jackson.databind.ValueDeserializer;
import tools.jackson.databind.JsonNode;
public class MyClassDeserializer extends ValueDeserializer {
@Override
public MyClass deserialize(JsonParser parser, DeserializationContext ctx) throws JacksonException {
JsonNode jsonRoot = parser.readValueAsTree();
JsonNode rejectedValue = jsonRoot.get("rejectedValue");
Object convertedValue = toSimpleValue(rejectedValue);
return new MyClass(convertedValue);
}
private Object toSimpleValue(JsonNode node) {
return switch (node.getNodeType()) {
case NULL, MISSING -> null;
case BOOLEAN -> node.asBoolean();
case NUMBER -> node.isInt() ? node.asInt() : node.isLong() ? node.asLong() : node.asDouble();
default -> node.asString();
};
}
}
Подробнее здесь: https://stackoverflow.com/questions/797 ... th-jackson
Мобильная версия