gpt4 book ai didi

java - 如何进行(递归?)自定义反序列化

转载 作者:行者123 更新时间:2023-12-02 04:52:03 27 4
gpt4 key购买 nike

我正在使用自定义反序列化,但我需要比我更好的大脑来解决这个问题。

简化:我有一个 json 文件、一个实体、一个反序列化类和一个 main

重点关注EntityDeserialization(以实体为参数的searchEntitie函数)和Main(实体变量)

实体.json(rootEntity是一个Entity对象)

[
{
"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;

实体反序列化

 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);
}

主要

 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(ListEntity, String rootEntity) 中使用我的函数,该函数位于主函数中并且正在填充过程中。

最佳答案

我找到了一种方法,连续两次反序列化。一是填充实体列表,二是使用该列表构建 ENtity 对象。

关于java - 如何进行(递归?)自定义反序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56430006/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com