All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.maarlakes.enumx.jackson.EnumValueKeyDeserializer Maven / Gradle / Ivy

Go to download

enumx 提供枚举值的统一接口,枚举值扩展接口,以及一些常见的枚举工具方法。

The newest version!
package cn.maarlakes.enumx.jackson;

import cn.maarlakes.enumx.EnumValue;
import cn.maarlakes.enumx.Enums;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.KeyDeserializer;
import jakarta.annotation.Nonnull;

import java.io.IOException;

/**
 * @author linjpxc
 */
@SuppressWarnings("unchecked")
public class EnumValueKeyDeserializer & EnumValue, V> extends KeyDeserializer {

    private final Class enumType;
    private final Class enumValueType;

    public EnumValueKeyDeserializer(@Nonnull Class enumType) {
        if (!(enumType.isEnum() && EnumValue.class.isAssignableFrom(enumType))) {
            throw new IllegalArgumentException();
        }
        this.enumType = (Class) enumType;
        this.enumValueType = Enums.getValueType((Class) enumType);
    }

    @Override
    public Object deserializeKey(String key, DeserializationContext ctxt) throws IOException {
        if (key == null) {
            return null;
        }
        final String newKey = key.trim();
        if (newKey.isEmpty()) {
            return key;
        }
        return Enums.valueOf(this.enumType, this.enumValueType, newKey, true);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy