Jackson mixin:如何将 class 中的字段忽略或重命名为树
Jackson mixin: How to Ignore or Rename fields from class as tree
我正在尝试从表示为树的 class 进行深度过滤(重命名/忽略字段)。
使用 Jackson Mixin,我可以从根级别重命名或忽略字段。我想要实现的是如何在多个级别上进行过滤(重命名/忽略)?
例如,我有一棵有两个 class 的树。 Class A 是根,B 是树中的第二层深度。通过应用 Jackson Mxiin,我想从根 A 过滤 属性 a2,从 B 过滤 属性 b1。
Class 代表根的A class
public class A {
private String a1;
private String a2;
private B b;
public A(String a1, String a2, B b) {
this.a1 = a1;
this.a2 = a2;
this.b = b;
}
public String getA1() {
return a1;
}
public void setA1(String a1) {
this.a1 = a1;
}
public String getA2() {
return a2;
}
public void setA2(String a2) {
this.a2 = a2;
}
public B getB() {
return b;
}
public void setB(B b) {
this.b = b;
}
}
Class B - 二级深度
public class B {
private String b1;
private String b2;
public B(String b2, String b1) {
this.b1 = b1;
this.b2 = b2;
}
public String getB1() {
return b1;
}
public void setB1(String b1) {
this.b1 = b1;
}
public String getB2() {
return b2;
}
public void setB2(String b2) {
this.b2 = b2;
}
}
过滤器
public interface AMixIn {
// Filter for A (implemented to filter second depth as well)
@JsonIgnore
String getA2();
@JsonIgnore
public BMixIn getB();
}
public interface BMixIn {
// Filter for B
@JsonIgnore
public String getB1();
}
测试
public class SecondLevelTest {
// Test
private ObjectMapper mapper = null;
private ObjectWriter writer = null;
@Before
public void setUp() {
// init
mapper = new ObjectMapper();
mapper.setMixIns(ImmutableMap.<Class<?>, Class<?>>of(A.class, AMixIn.class));
writer = mapper.writer().with(SerializationFeature.INDENT_OUTPUT);
}
@Test
public void rename_field_jackson() throws JsonProcessingException {
B b = new B("vb1", "vb2");
A a = new A("va1", "va2", b);
// I want to get this result
// {
// "a1" : "va1",
// "b2" : "vb2"
// }
String json = writer.writeValueAsString(a);
System.out.println(json);
}
}
这应该会为您提供所需的输出。将 setUp()
更改为也使用 BMixIn
:
@Before
public void setUp() {
// init
mapper = new ObjectMapper();
mapper.setMixIns(ImmutableMap.of(A.class, AMixIn.class, B.class, BMixIn.class));
writer = mapper.writer().with(SerializationFeature.INDENT_OUTPUT);
}
并更改 AMixIn
以展开 B
public interface AMixIn {
// Filter for A (implemented to filter second depth as well)
@JsonIgnore
String getA2();
@JsonUnwrapped
public BMixIn getB();
}
忘记注册 BMixIn
导致其 @JsonIgnore
永远无法使用。 @JsonUnwrapped
取消嵌套 b
所以你得到一个平面结构。
我正在尝试从表示为树的 class 进行深度过滤(重命名/忽略字段)。
使用 Jackson Mixin,我可以从根级别重命名或忽略字段。我想要实现的是如何在多个级别上进行过滤(重命名/忽略)?
例如,我有一棵有两个 class 的树。 Class A 是根,B 是树中的第二层深度。通过应用 Jackson Mxiin,我想从根 A 过滤 属性 a2,从 B 过滤 属性 b1。
Class 代表根的A class
public class A {
private String a1;
private String a2;
private B b;
public A(String a1, String a2, B b) {
this.a1 = a1;
this.a2 = a2;
this.b = b;
}
public String getA1() {
return a1;
}
public void setA1(String a1) {
this.a1 = a1;
}
public String getA2() {
return a2;
}
public void setA2(String a2) {
this.a2 = a2;
}
public B getB() {
return b;
}
public void setB(B b) {
this.b = b;
}
}
Class B - 二级深度
public class B {
private String b1;
private String b2;
public B(String b2, String b1) {
this.b1 = b1;
this.b2 = b2;
}
public String getB1() {
return b1;
}
public void setB1(String b1) {
this.b1 = b1;
}
public String getB2() {
return b2;
}
public void setB2(String b2) {
this.b2 = b2;
}
}
过滤器
public interface AMixIn {
// Filter for A (implemented to filter second depth as well)
@JsonIgnore
String getA2();
@JsonIgnore
public BMixIn getB();
}
public interface BMixIn {
// Filter for B
@JsonIgnore
public String getB1();
}
测试
public class SecondLevelTest {
// Test
private ObjectMapper mapper = null;
private ObjectWriter writer = null;
@Before
public void setUp() {
// init
mapper = new ObjectMapper();
mapper.setMixIns(ImmutableMap.<Class<?>, Class<?>>of(A.class, AMixIn.class));
writer = mapper.writer().with(SerializationFeature.INDENT_OUTPUT);
}
@Test
public void rename_field_jackson() throws JsonProcessingException {
B b = new B("vb1", "vb2");
A a = new A("va1", "va2", b);
// I want to get this result
// {
// "a1" : "va1",
// "b2" : "vb2"
// }
String json = writer.writeValueAsString(a);
System.out.println(json);
}
}
这应该会为您提供所需的输出。将 setUp()
更改为也使用 BMixIn
:
@Before
public void setUp() {
// init
mapper = new ObjectMapper();
mapper.setMixIns(ImmutableMap.of(A.class, AMixIn.class, B.class, BMixIn.class));
writer = mapper.writer().with(SerializationFeature.INDENT_OUTPUT);
}
并更改 AMixIn
以展开 B
public interface AMixIn {
// Filter for A (implemented to filter second depth as well)
@JsonIgnore
String getA2();
@JsonUnwrapped
public BMixIn getB();
}
忘记注册 BMixIn
导致其 @JsonIgnore
永远无法使用。 @JsonUnwrapped
取消嵌套 b
所以你得到一个平面结构。