我正在使用一个自定义的反序列化,但我需要一个比我的大脑更好的大脑来解决这个问题。
为了简单起见:我有一个json文件、一个实体、一个反序列化类和一个main
关注EntityDeserialization (作为参数实体的searchEntitie函数)和Main (实体变量)
Entity.json (rootEntity是一个实体对象)
[
{
"name": "BFA",
"entityType": "secteur",
"rootEntity": ""
},
{
"name": "IT",
"entityType": "service",
"rootEntity": ""
},
{
"name": "EX",
"entityType": "offre",
"rootEntity": "BFA"
}
]
实体类
@JsonDeserialize(using = EntityDeserialization.class)
public class Entity {
private String name;
private String entityType;
private Entity rootEntity;
EntityDeserialization
public Entity deserialize(JsonParser jp, DeserializationContext ctxt)
throws IOException {
JsonNode node = jp.getCodec().readTree(jp);
String name = node.get("name").asText();
String entitype = node.get("entityType").asText();
String rootEntity = node.get("rootEntity").asText();
Entity entity = new Entity();
entity = entity.searchEntity(entities, rootEntity);
return new Entity(name, entitype,entity);
}
Main
public static void main(String[] args) throws FileNotFoundException {
com.fasterxml.jackson.databind.ObjectMapper mapper = new ObjectMapper();
mapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
TypeReference<List<User>> typeReferenceUser = new TypeReference<>() {};
TypeReference<List<Entity>> typeReferenceEntity = new TypeReference<>() {};
FileInputStream inputStreamUser = new FileInputStream("C:\\Users\\oraph\\Desktop\\user.json");
FileInputStream inputStreamEntity = new FileInputStream("C:\\Users\\oraph\\Desktop\\entity.json");
try {
List<User> users = mapper.readValue(inputStreamUser,typeReferenceUser);
List<Entity> entities = mapper.readValue(inputStreamEntity,typeReferenceEntity);
...
这里的问题是:我在EntityDeserialization searchEntity(List entity, String rootEntity)
中注意使用我的函数,它是主要的,并且处于填充过程中。
https://stackoverflow.com/questions/56430006
复制相似问题