67 lines
2.3 KiB
Java
67 lines
2.3 KiB
Java
|
|
package com.newyetai.common.annotation.trimAndUpper;
|
||
|
|
|
||
|
|
import com.fasterxml.jackson.core.JsonParser;
|
||
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||
|
|
import com.fasterxml.jackson.databind.BeanProperty;
|
||
|
|
import com.fasterxml.jackson.databind.DeserializationContext;
|
||
|
|
import com.fasterxml.jackson.databind.JsonDeserializer;
|
||
|
|
import com.fasterxml.jackson.databind.JsonMappingException;
|
||
|
|
import com.fasterxml.jackson.databind.deser.ContextualDeserializer;
|
||
|
|
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
|
||
|
|
import com.google.common.collect.Maps;
|
||
|
|
import com.newyetai.common.annotation.trimAndUpper.TrimAndUpper.ExecuteType;
|
||
|
|
import java.io.IOException;
|
||
|
|
import java.util.Map;
|
||
|
|
import java.util.Objects;
|
||
|
|
import org.apache.commons.lang3.StringUtils;
|
||
|
|
|
||
|
|
public class TrimAndUpperDeserializer extends StdDeserializer<String> implements ContextualDeserializer {
|
||
|
|
|
||
|
|
private static final Map<ExecuteType, TrimAndUpperDeserializer> INSTANCE_MAP = Maps.newHashMap();
|
||
|
|
private static final long serialVersionUID = 3463447204376923373L;
|
||
|
|
|
||
|
|
static {
|
||
|
|
for (ExecuteType value : ExecuteType.values()) {
|
||
|
|
INSTANCE_MAP.put(value,new TrimAndUpperDeserializer(value));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
private ExecuteType executeType;
|
||
|
|
|
||
|
|
public TrimAndUpperDeserializer(){
|
||
|
|
super(String.class);
|
||
|
|
}
|
||
|
|
|
||
|
|
public TrimAndUpperDeserializer(ExecuteType executeType){
|
||
|
|
super(String.class);
|
||
|
|
this.executeType = executeType;
|
||
|
|
}
|
||
|
|
|
||
|
|
@Override
|
||
|
|
public String deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
|
||
|
|
String s = jsonParser.getText();
|
||
|
|
if (!(StringUtils.isEmpty(s) || Objects.isNull(executeType))) {
|
||
|
|
if (ExecuteType.ALL.equals(executeType) || ExecuteType.TRIM.equals(executeType)) {
|
||
|
|
s = StringUtils.replace(StringUtils.trim(s)," ","");
|
||
|
|
}
|
||
|
|
if (ExecuteType.ALL.equals(executeType) || ExecuteType.TRIM.equals(executeType)) {
|
||
|
|
s = StringUtils.upperCase(s);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return s;
|
||
|
|
}
|
||
|
|
|
||
|
|
@Override
|
||
|
|
public JsonDeserializer<?> createContextual(DeserializationContext prov, BeanProperty property) throws JsonMappingException {
|
||
|
|
ExecuteType executeType = null;
|
||
|
|
TrimAndUpper ann = null;
|
||
|
|
if (property != null) {
|
||
|
|
ann = property.getAnnotation(TrimAndUpper.class);
|
||
|
|
}
|
||
|
|
if (ann != null) {
|
||
|
|
executeType = ann.value();
|
||
|
|
}
|
||
|
|
return INSTANCE_MAP.get(executeType);
|
||
|
|
}
|
||
|
|
}
|