gpt4 book ai didi

java - Jackson,基于另一个属性(依赖属性)反序列化属性

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

使用 Jackson,有没有办法反序列化依赖于另一个属性值的属性?

如果我有这个 json {"foo":"a","bar":"b"} 我想将它反序列化到下面的测试类作为 Test [ foo=a, bar=b_a],其中bar是json属性“bar”的值和属性“foo”的值。

当然这是一个简单的例子,真正的交易是反序列化数据模型实体:{"line":"C12", "machine": {"line":"C12", "code":"A"}} machine.line 和 line 总是一样的,我想这样表达:{"line":"C12", "machine": "A"}

import java.io.IOException;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;

public abstract class Main{

private static class Test {
@JsonProperty
private String foo;
@JsonProperty
@JsonDeserialize(using = CustomDeserializer.class)
private String bar;

// ...other fields to be deserialized with default behaviour

private Test() {
}

public Test(String a, String bar) {
this.foo = a;
this.bar = bar;
}

@Override
public String toString() {
return "Test [foo=" + foo + ", bar=" + bar + "]";
}
}

private static class CustomDeserializer extends StdDeserializer<String> {

protected CustomDeserializer() {
super(String.class);
}

@Override
public String deserialize(JsonParser p, DeserializationContext ctxt)
throws IOException, JsonProcessingException {
String foo = //how to get foo property?
String value = p.getValueAsString();
if (!foo.isEmpty()) {
return value + "_" + foo;
} else {
return value;
}
}

}

public static void main(String[] args) throws IOException {

ObjectMapper mapper = new ObjectMapper();

Test foo2 = mapper.readValue("{\"foo\":\"a\",\"bar\":\"b\"}", Test.class);
System.out.println(foo2); // Test [foo=a, bar=b_a]

}

}

最佳答案

解决您的问题的一种方法是指定一个涉及您的 Test 类而不是您的字符串字段的自定义反序列化程序,因为您的属性的反序列化是基于另一个属性的值:

public class CustomDeserializer extends JsonDeserializer<Test> {}

@JsonDeserialize(using = CustomDeserializer.class)
public class Test {}

然后你可以反序列化你的对象读取JsonNode根据您的输入字符串构建的树:

public class CustomDeserializer extends JsonDeserializer<Test> {

@Override
public Test deserialize(JsonParser jp, DeserializationContext dc) throws IOException, JsonProcessingException {
JsonNode node = jp.getCodec().readTree(jp);
String foo = node.get("foo").asText();
String bar = node.get("bar").asText();
if (!foo.isEmpty()) {
bar = (bar + '_' + foo);
}
return new Test(foo, bar);
}

}

//your example
public class Main {
public static void main(String[] args) throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
Test foo2 = mapper.readValue("{\"foo\":\"a\",\"bar\":\"b\"}", Test.class);
System.out.println(foo2); // Test [foo=a, bar=b_a]
}
}

关于java - Jackson,基于另一个属性(依赖属性)反序列化属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69287943/

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