`package com.byd.plm.authority.auth.dto.jsonSerializer;
import com.byd.plm.authority.auth.dto.enums.AuthTypeEnum;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import java.io.IOException;
import java.util.List;
/**
- @description:
- @Author: fu.yao2
- @Date: 2022/12/6 09:26
- @throws:
**/
public class AuthTypeEnumListJsonSerializer extends JsonSerializer<List> {
@Override
public void serialize(Listlist, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
int[] integers = new int[list.size()];
for (int i = 0; i < list.size(); i++) {
integers[i] = list.get(i).getCode();
}
jsonGenerator.writeArray(integers, 0, integers.length);
}
}
package com.byd.plm.authority.auth.dto.jsonDeserializer;
import com.byd.plm.authority.auth.dto.enums.AuthTypeEnum;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
-
@description:
-
@Author: fu.yao2
-
@Date: 2022/11/11 15:20
-
@throws:
**/
public class AuthTypeListEnumJsonDeserializer extends JsonDeserializer<List> {
@Override
public Listdeserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException, IllegalArgumentException {
ArrayNode treeNode = jsonParser.readValueAsTree();
Field field;
try {
field = jsonParser.getCurrentValue().getClass().getDeclaredField(jsonParser.currentName());
} catch (NoSuchFieldException e) {
return null;
}
field.setAccessible(true);
if (!field.getType().equals(List.class)) {
return null;
}List<AuthTypeEnum> result = new ArrayList<>(); Iterator<JsonNode> elements = treeNode.elements(); while (elements.hasNext()) { Integer appCase = elements.next().asInt(); AuthTypeEnum anEnum = AuthTypeEnum.codeFrom(appCase); result.add(anEnum); } if (result.isEmpty()) { return null; } else { return result; }
}
}
`
标签:jackson,java,list,枚举,import,序列化,com,fasterxml From: https://www.cnblogs.com/orangeJuiceRain/p/17074398.html