gpt4 book ai didi

java - Jackson 反序列化 : Can I inject a value with an annotation on the field of the to deserializable object?

转载 作者:行者123 更新时间:2023-12-04 08:31:50 25 4
gpt4 key购买 nike

我有一个这样的对象要反序列化:

public class RelationsInput {

Relation relation1;

Relation relation2;

}
而类 Relation看起来像这样:
public class Relation {

RelationType relationtype;
... (more fields)

}
RelationType是枚举并且不是将被反序列化的值,而所有其他值都是。
是否有可能,我可以“注入(inject)”字段 relationType 的枚举值?在类 RelationInput 中的字段上带有注释?
像下面这样

public class RelationsInput {

@RelationType(RelationType.OWNER)
Relation owner;

@RelationType(RelationType.TENANT)
Relation tenant;

}
jackson 提供这样的东西吗?

最佳答案

您可以尝试使用 com.fasterxml.jackson.databind.deser.ContextualDeserializer 实现自定义反序列化器界面。它允许创建带有上下文的反序列化器实例。
请参见下面的示例:

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.BeanProperty;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.deser.ContextualDeserializer;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import com.fasterxml.jackson.databind.json.JsonMapper;
import lombok.Data;

import java.io.File;
import java.io.IOException;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

public class JsonContextualDeserializerApp {
public static void main(String[] args) throws IOException {
File jsonFile = new File("./resource/test.json").getAbsoluteFile();

ObjectMapper mapper = JsonMapper.builder().build();
RelationsInput info = mapper.readValue(jsonFile, RelationsInput.class);

System.out.println(info.toString());
}
}

@Data
class RelationsInput {

@JsonDeserialize(using = RelationStdDeserializer.class)
@RelationTypeInfo(RelationType.OWNER)
private Relation owner;

@JsonDeserialize(using = RelationStdDeserializer.class)
@RelationTypeInfo(RelationType.TENANT)
private Relation tenant;

}

@Data
class Relation {

private int id;
private RelationType relationtype;
}

enum RelationType {OWNER, TENANT}

@Retention(RetentionPolicy.RUNTIME)
@interface RelationTypeInfo {
RelationType value();
}

class RelationStdDeserializer extends StdDeserializer<Relation> implements ContextualDeserializer {

private RelationType propertyRelationType;

public RelationStdDeserializer() {
this(null);
}

public RelationStdDeserializer(RelationType relationType) {
super(Relation.class);
this.propertyRelationType = relationType;
}

@Override
public Relation deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
JsonDeserializer<Object> deser = ctxt.findRootValueDeserializer(ctxt.getTypeFactory().constructType(Relation.class));
Relation instance = (Relation) deser.deserialize(p, ctxt);
if (this.propertyRelationType != null) {
instance.setRelationtype(this.propertyRelationType);
}
return instance;
}

@Override
public JsonDeserializer<?> createContextual(DeserializationContext ctxt, BeanProperty property) {
RelationTypeInfo typeInfo = property.getMember().getAllAnnotations().get(RelationTypeInfo.class);

return new RelationStdDeserializer(typeInfo.value());
}
}
上面的有效载荷代码:
{
"owner": {
"id": 1
},
"tenant": {
"id": 2
}
}
打印:
RelationsInput(owner=Relation(id=1, relationtype=OWNER), tenant=Relation(id=2, relationtype=TENANT))
也可以看看:
  • Deserialize to String or Object using Jackson
  • How to inject dependency into Jackson Custom deserializer
  • Jackson - deserialize inner list of objects to list of one higher level
  • 关于java - Jackson 反序列化 : Can I inject a value with an annotation on the field of the to deserializable object?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64986233/

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