
com.github.robtimus.net.ip.jackson.databind.IPAddressDeserializer Maven / Gradle / Ivy
/*
* IPAddressDeserializer.java
* Copyright 2020 Rob Spoor
*
* 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
*
* http://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 com.github.robtimus.net.ip.jackson.databind;
import java.io.IOException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.BeanProperty;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.deser.ContextualDeserializer;
import com.github.robtimus.net.ip.IPAddress;
import com.github.robtimus.net.ip.IPv4Address;
import com.github.robtimus.net.ip.IPv6Address;
/**
* Base class for all deserializers for {@link IPAddress} and sub classes.
*
* @author Rob Spoor
* @param The type of IP address to deserialize.
*/
public abstract class IPAddressDeserializer> extends JsonDeserializer {
private IPAddressDeserializer() {
}
@Override
public I deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
return deserialize(p.getText());
}
abstract I deserialize(String value);
@Override
public abstract Class> handledType();
/**
* A deserializer for {@link IPv4Address}.
*
* @author Rob Spoor
*/
public static class IPv4 extends IPAddressDeserializer {
static final IPv4 INSTANCE = new IPv4();
/**
* Creates a new {@link IPv4Address} deserializer.
*/
public IPv4() {
super();
}
@Override
IPv4Address deserialize(String value) {
return IPv4Address.valueOf(value);
}
@Override
public Class> handledType() {
return IPv4Address.class;
}
}
/**
* A deserializer for {@link IPv6Address}.
*
* @author Rob Spoor
*/
public static class IPv6 extends IPAddressDeserializer {
static final IPv6 INSTANCE = new IPv6();
/**
* Creates a new {@link IPv6Address} deserializer.
*/
public IPv6() {
super();
}
@Override
IPv6Address deserialize(String value) {
return IPv6Address.valueOf(value);
}
@Override
public Class> handledType() {
return IPv6Address.class;
}
}
/**
* A deserializer for {@link IPAddress}. It can handle both {@link IPv4Address} and {@link IPv6Address}. If a property is declared as either
* {@code IPAddress} or {@code IPAddress}, it will limit the deserialization to only the specified type.
* In other words, trying to deserialize an IPv6 address for a property of type {@code IPAddress} or vice versa will fail.
*
* @author Rob Spoor
*/
public static class AnyVersion extends IPAddressDeserializer> implements ContextualDeserializer {
static final AnyVersion INSTANCE = new AnyVersion();
/**
* Creates a new {@link IPAddress} deserializer.
*/
public AnyVersion() {
super();
}
@Override
IPAddress> deserialize(String value) {
return IPAddress.valueOf(value);
}
@Override
public JsonDeserializer> createContextual(DeserializationContext ctxt, BeanProperty property) throws JsonMappingException {
Class> genericType = property != null
? getGenericType(property.getType())
: null;
if (genericType == IPv4Address.class) {
return IPv4.INSTANCE;
}
if (genericType == IPv6Address.class) {
return IPv6.INSTANCE;
}
return this;
}
private Class> getGenericType(JavaType type) {
return type != null
? type.getBindings().getBoundType(0).getRawClass()
: null;
}
@Override
public Class> handledType() {
return IPAddress.class;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy