For*_*gel 9 serialization json spring-mvc jackson deserialization
我猜我有一个父类参数,它有2个子类ComboParameter和IntegerParameter
@JsonSubTypes({
@JsonSubTypes.Type(value = IntegerParameter.class, name = "integerParam"),
@JsonSubTypes.Type(value = ComboParameter.class, name = "comboParam")
})
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = As.WRAPPER_OBJECT)
public abstract class Parameter {
String regEx;
}
@JsonTypeName("integerParam")
public class IntegerParameter extends Parameter {
}
@JsonTypeName("comboParam")
public class ComboParameter extends Parameter {
List<String> values;
}
Run Code Online (Sandbox Code Playgroud)
我有一个具有属性参数的类
class A {
@JsonUnwrapped
Parameter parameter;
}
Run Code Online (Sandbox Code Playgroud)
对象的序列化A抛出异常
com.fasterxml.jackson.databind.JsonMappingException:未包装的属性需要使用类型信息:无法在不禁用的情况下序列化
SerializationFeature.FAIL_ON_UNWRAPPED_TYPE_IDENTIFIERS
如果我删除注释,@JsonUnwrapped我会有一个像那样的json
{
parameter:{
integerParam:{
regEx: regExVal
}
}
}
Run Code Online (Sandbox Code Playgroud)
而我需要的是像这样的json:
{
integerParam:{
regEx: regExVal
}
}
Run Code Online (Sandbox Code Playgroud)
NB我正在使用Jackson 2.4.4
不要认为这个问题有简单干净的解决方案。但这里有一些如何解决这个问题的想法(两种情况的要点演示):
选项 1:在顶级 bean 中添加@JsonIgnore上述属性。@JsonAnyGetter易于实现,但在 bean 中使用静态 ObjectMapper 并不好,并且必须将此代码复制到具有 Parameter 属性的每个 bean
public class A {
@JsonIgnore
Parameter parameter;
// can put ObjectMapper and most of this code in util class
// and just use JacksonUtils.toMap(parameter) as return of JsonAnyGetter
private static ObjectMapper mapper = new ObjectMapper();
/************************ Serialization ************************/
@JsonAnyGetter
private Map<String, Object> parameterAsMap(){
return mapper.convertValue(parameter, Map.class);
}
/************************ Deserialization **********************/
@JsonAnySetter
private void parameterFromMap(String key, JsonNode value) {
try {
parameter = mapper.readValue(String.format("{\"%s\":%s}", key,value),
Parameter.class);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Run Code Online (Sandbox Code Playgroud)
选项 2: 为根@JsonIgnore类设置属性并注册自定义序列化器/反序列化A器
SimpleModule module = new SimpleModule();
module.addSerializer(A.class, new ASerializer());
module.addDeserializer(A.class, new ADeserializer());
mapper.registerModule(module);
Run Code Online (Sandbox Code Playgroud)
不能使用@JsonSerialize上面的 A 类,因为 ObjectMapper 内部的序列化器和反序列化器也会使用此注释,但您需要将其设置为使用默认序列化器/反序列化器,而不是递归地使用它本身。或者,如果您确实想要注释,您可以实现类似/sf/answers/1288417091/的东西
序列化器+反序列化器看起来像这样(未优化,只是概念证明):
/************************ Serialization ************************/
public static class ASerializer extends JsonSerializer<A> {
private static ObjectMapper m = new ObjectMapper();
@Override
public void serialize(A value, JsonGenerator gen,
SerializerProvider serializers) throws IOException {
Map defaults = m.convertValue(value, Map.class);
Map params = m.convertValue(value.getParameter(), Map.class);
defaults.putAll(params);
gen.writeObject(defaults);
}
}
/************************ Deserialization **********************/
public static class ADeserializer extends JsonDeserializer<A> {
private static ObjectMapper m = new ObjectMapper();
private static String[] subtipes = {"integerParam", "comboParam"};
public ADeserializer() {
m.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
}
@Override
public A deserialize(JsonParser p, DeserializationContext ctxt)
throws IOException {
TreeNode node = m.readTree(p);
A a = m.convertValue(node, A.class);
// hardcoded , probably can be done dynamically
// with annotations inspection
for (String key : subtipes) {
TreeNode value = node.get(key);
if (value != null) {
String json = String.format("{\"%s\":%s}", key, value);
a.setParameter(m.readValue(json, Parameter.class));
break;
}
}
return a;
}
}
Run Code Online (Sandbox Code Playgroud)
通用反序列化器很难编写。但根据问题正文,这个问题无论如何都是关于序列化的。