我需要一些幫助。我必須讓未來的JSON:如何在map中序列化對象值的類型屬性?
{
"433434" : {
"type" : "MULTIPLE",
"value" : [ {
"type" : "NUMBER",
"value" : 322332
}, {
"type" : "NUMBER",
"value" : 322332
} ]
}
}
但是我有這樣的:
{
"433434" : {
"value" : [ {
"type" : "NUMBER",
"value" : 322332
}, {
"type" : "NUMBER",
"value" : 322332
} ]
}
}
我使用傑克遜
。其我的主類
package com.un1acker;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.un1acker.characteristic.AbstractCharacteristic;
import com.un1acker.characteristic.MultipleCharacteristic;
import com.un1acker.characteristic.NumCharacteristic;
import java.io.IOException;
import java.io.StringWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Main {
public static void main(String[] args) throws IOException {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationFeature.INDENT_OUTPUT, true);
NumCharacteristic numCharacteristic = new NumCharacteristic();
numCharacteristic.setValue(BigInteger.valueOf(322332L));
List<AbstractCharacteristic<?>> list = new ArrayList<>();
list.add(numCharacteristic);
list.add(numCharacteristic);
StringWriter sw = new StringWriter();
MultipleCharacteristic multipleCharacteristic = new MultipleCharacteristic();
multipleCharacteristic.setValue(list);
Map<String, AbstractCharacteristic<?>> map = new HashMap<>();
map.put("433434", multipleCharacteristic);
mapper.writeValue(sw, map);
System.out.println(sw.toString());
}
}
而且我有從AbstractCharacteristic延伸的類MultipleCharateristic和NumberCharacteristic。計劃我們有map>,它包含值MultipleCharacteristic。 在NumberCharacteristic值的MultipleCharacteristic集列表中。
package com.un1acker.characteristic;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.PROPERTY,
property = "type"
)
@JsonSubTypes({ @JsonSubTypes.Type(
value = NumCharacteristic.class,
name = "NUMBER"
), @JsonSubTypes.Type(
value = MultipleCharacteristic.class,
name = "MULTIPLE")})
public abstract class AbstractCharacteristic<T>{
private static final long serialVersionUID = -6524899961842198462L;
private T value;
public AbstractCharacteristic() {
}
public T getValue() {
return this.value;
}
protected void setValue(T value) {
this.value = value;
}
}
NumCharacteristic類
package com.un1acker.characteristic;
import java.math.BigInteger;
public class NumCharacteristic extends AbstractCharacteristic<BigInteger> {
private static final long serialVersionUID = 9220460768952701281L;
public NumCharacteristic() {
}
public void setValue(BigInteger value) {
super.setValue(value);
}
}
MultipleCharacteristic類
package com.un1acker.characteristic;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.un1acker.MyCustomSerializer;
import java.util.List;
public class MultipleCharacteristic extends AbstractCharacteristic<List<? extends AbstractCharacteristic<?>>> {
@Override
public void setValue(List<? extends AbstractCharacteristic<?>> value) {
super.setValue(value);
}
@Override
@JsonSerialize(using = MyCustomSerializer.class)
public List<? extends AbstractCharacteristic<?>> getValue() {
return super.getValue();
}
}
我試過了MultipleClass與重寫方法serializeWithType但這並不工作創建自定義序列化。
我找到了決定。我爲SimpleModule爲MultipleCharacteristic添加自定義序列化器,並覆蓋下一個方法:serialize,serializeWithType,handledType。但任何感謝您的答案!=) – Alexandr