[英]jackson json serialize specific property value of list-of-list
Good day everyone, I would like to remove a property and a property name of an object which is in a List<> when returned from another object (as property). 大家好,我想从另一个对象(作为属性)返回时,删除List <>中一个对象的属性和属性名称。
I have the following classes: 我有以下课程:
public class Base {
protected String type;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
public class A extends Base {
private double[] value;
public A() { type = "A"; }
public A(double[] value) {
this();
setValue(value);
}
public double[] getValue() {
return value;
}
public void setValue(double[] value) {
this.value = value;
}
}
public class B extends Base {
private List<A> value = new ArrayList<A>();
public B() { type = "B"; }
public B(List<A> value) {
this();
setValue(value);
}
public List<A> getValue() { return value; }
public void setValue(List<A> value) {
this.value = value;
}
}
public class C extends Base {
private List<B> value = new ArrayList<B>();
public C() { type = "C"; }
public List<B> getValue() {
return value;
}
public void setValue(List<B> value) {
this.value = value;
}
}
And use them like: 并像这样使用它们:
A a = new A();
a.setValue(new double[]{1,2});
B b = new B();
List<A> aList = new ArrayList<A>();
aList.add(new A(new double[]{1,2}));
aList.add(new A(new double[]{3,4}));
b.setValue(aList);
C c = new C();
List<B> bList = new ArrayList<B>();
List<A> aList1 = new ArrayList<A>();
List<A> aList2 = new ArrayList<A>();
aList1.add(new A(new double[]{1,2}));
aList1.add(new A(new double[]{3,4}));
aList2.add(new A(new double[]{5,6}));
bList.add(new B(aList1));
bList.add(new B(aList2));
c.setValue(bList);
ObjectMapper jsonMapper = new ObjectMapper();
jsonMapper.enable(SerializationFeature.INDENT_OUTPUT);
try {
System.out.println("=== com.example.A ===");
System.out.println(jsonMapper.writeValueAsString(a));
System.out.println("=== com.example.B ===");
System.out.println(jsonMapper.writeValueAsString(b));
System.out.println("=== com.example.C ===");
System.out.println(jsonMapper.writeValueAsString(c));
} catch (JsonProcessingException e) {
e.printStackTrace();
}
Where I get: 我在哪里:
=== com.example.A ===
{
"type" : "A",
"value" : [ 1.0, 2.0 ]
}
=== com.example.B ===
{
"type" : "B",
"value" : [ {
"type" : "A",
"value" : [ 1.0, 2.0 ]
}, {
"type" : "A",
"value" : [ 3.0, 4.0 ]
} ]
}
=== com.example.C ===
{
"type" : "C",
"value" : [ {
"type" : "B",
"value" : [ {
"type" : "A",
"value" : [ 1.0, 2.0 ]
}, {
"type" : "A",
"value" : [ 3.0, 4.0 ]
} ]
}, {
"type" : "B",
"value" : [ {
"type" : "A",
"value" : [ 5.0, 6.0 ]
} ]
} ]
}
But I want to have it like: 但我想像这样:
=== com.example.A ===
{
"type" : "A",
"value" : [ 1.0, 2.0 ]
}
=== com.example.B ===
{
"type" : "B",
"value" : [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ]
}
=== com.example.C ===
{
"type" : "C",
"value" : [ [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ], [ [ 5.0, 6.0 ] ] ]
}
I was able to remove the property "type" of A when returned from B and property "type" of B from C using: 我可以使用以下命令从B返回A的属性“ type”,从C删除B的属性“ type”:
@JsonIgnoreProperties( { "type" })
public List<A> getValue() {
return value;
}
...
@JsonIgnoreProperties( { "type" })
public List<B> getValue() {
return value;
}
But I'm still getting the property "value" name and the curly braces... 但是我仍然得到属性“值”的名称和花括号。
=== com.example.A ===
{
"type" : "A",
"value" : [ 1.0, 2.0 ]
}
=== com.example.B ===
{
"type" : "B",
"value" : [ {
"value" : [ 1.0, 2.0 ]
}, {
"value" : [ 3.0, 4.0 ]
} ]
}
=== com.example.C ===
{
"type" : "C",
"value" : [ {
"value" : [ {
"value" : [ 1.0, 2.0 ]
}, {
"value" : [ 3.0, 4.0 ]
} ]
}, {
"value" : [ {
"value" : [ 5.0, 6.0 ]
} ]
} ]
}
What annotation can I use to get the desired output? 我可以使用什么注释来获得所需的输出? Or do I have to redesign my classes?
还是我必须重新设计课程?
public class A extends Base {
private double[] value;
public A() {
type = "A";
}
public A(double[] value) {
this();
setValue(value);
}
public double[] getValue() {
return value;
}
public void setValue(double[] value) {
this.value = value;
}
}
public class B extends Base {
private List<A> value = new ArrayList<A>();
public B() {
type = "B";
}
public B(List<A> value) {
this();
setValue(value);
}
@JsonIgnoreProperties({"type"})
public List<A> getValue() {
return value;
}
@JsonGetter("value")
@JsonIgnoreProperties({"type"})
public List<double[]> getA() {
List<double[]> res = new ArrayList<>();
for (A a : value) {
res.add(a.getValue());
}
return res;
}
public void setValue(List<A> value) {
this.value = value;
}
@Override
public String toString() {
return "" + value;
}
}
public class C extends Base {
private List<B> value = new ArrayList<B>();
public C() {
type = "C";
}
@JsonGetter("value")
@JsonIgnoreProperties({"type"})
public List<List<double[]>> getB() {
List<List<double[]>> res1 = new ArrayList<>();
for (B b : value) {
List<double[]> res2 = new ArrayList<>();
for (A a : b.getValue()) {
res2.add(a.getValue());
}
res1.add(res2);
}
return res1;
}
public List<B> getValue() {
return value;
}
public void setValue(List<B> value) {
this.value = value;
}
}
Modify the B and C classes as below: 修改B和C类,如下所示:
Added @JsonIgnore
annotation to value to avoid value being printed and created a new method getValueForJson()
with return type List<double[]>
whose output is a deep parsed result with values of class A
. 为值添加
@JsonIgnore
注释以避免打印值,并创建了一个新方法getValueForJson()
,其返回类型为List<double[]>
其输出是具有A
类值的深层解析结果。 This new method is added with annotation @JsonProperty
with value as "value"
so as to get this treated as a member named value
. 此新方法添加了
@JsonProperty
批注,其值作为"value"
以便将其视为名为value
的成员。
class B extends Base {
@JsonIgnore
private List<A> value = new ArrayList<A>();
public B() {
type = "B";
}
public B(List<A> value) {
this();
setValue(value);
}
public List<A> getValue() {
return value;
}
public void setValue(List<A> value) {
this.value = value;
}
@JsonProperty("value")
public List<double[]> getValueForJson(){
List<double[]> res = new ArrayList<double[]>();
value.forEach(a -> res.add(a.getValue()));
return res;
}
}
class C extends Base {
@JsonIgnore
private List<B> value = new ArrayList<B>();
public C() {
type = "C";
}
public List<B> getValue() {
return value;
}
public void setValue(List<B> value) {
this.value = value;
}
@JsonProperty("value")
public List<double[]> getValueForJson(){
List<double[]> res = new ArrayList<double[]>();
value.forEach(b -> b.getValue().forEach(a -> res.add(a.getValue())));
return res;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.