我正在嘗試升級我的項目以使用更新版本的Jackson,並遇到了使用自定義串行器和反序列化器的障礙。將Jackson從2.0.4升級到2.7.1
關於下面的解串器我得到一個構造函數問題 - 我不知道我應該傳入什麼類型。我使用的構造函數不再可用。以下是當前班級:
import com.fasterxml.jackson.databind.DeserializationContext; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.deser.std.StdKeyDeserializer;
公共類SifMapKeyDeserializer擴展StdKeyDeserializer {
protected SifMapKeyDeserializer(Class<?> cls) {
super(cls); //IT COMPLAINS. It needs a type, not sure what type?
}
@Override
protected Object _parse(String key, DeserializationContext ctxt)
throws Exception {
ObjectMapper jacksonMapper = ObjectMapperFactory.getObjectMapper();
JsonNode element = jacksonMapper.readTree(key);
String fullClassName = element.path(JacksonExtConstants.JAVA_CLASS_NAME_KEY).asText();
String nodeText = element.toString();
Object result = null;
try {
result = jacksonMapper.readValue(nodeText, Class.forName(fullClassName));
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
}
編輯我發現這個解決上述問題。
public SifMapKeyDeserializer(Class<?> cls) {
super(StdKeyDeserializer.TYPE_CLASS, cls);
}
解串器列在下面。我遇到了註釋方法和SimpleBeanPropertyDefinition的問題 - 我不確定如何解決這些問題。
import java.lang.reflect.Method;
import java.util.List;
import com.fasterxml.jackson.databind.BeanDescription;
import com.fasterxml.jackson.databind.SerializationConfig;
import com.fasterxml.jackson.databind.introspect.AnnotatedMethod;
import com.fasterxml.jackson.databind.introspect.AnnotationMap;
import com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition;
import com.fasterxml.jackson.databind.ser.BeanPropertyWriter;
import com.fasterxml.jackson.databind.ser.BeanSerializerModifier;
import com.fasterxml.jackson.databind.type.SimpleType;
import com.fasterxml.jackson.databind.util.SimpleBeanPropertyDefinition;
/**
* This custom bean serializer is to allow us add/delete any properties.
* For example for each deserialized bean we are currently adding its fully
* qualified class name.
*
*/
public class SifBeanSerializerModifier extends BeanSerializerModifier {
public List<BeanPropertyWriter> changeProperties(SerializationConfig config,
BeanDescription beanDesc, List<BeanPropertyWriter> beanProperties) {
Class cc = beanDesc.getType().getRawClass();
Method m;
try {
m = cc.getMethod("getClass", null);
AnnotatedMethod am = new AnnotatedMethod(m, _emptyAnnotationMap(), null);
BeanPropertyDefinition propDef = new SimpleBeanPropertyDefinition(am, JacksonExtConstants.JAVA_CLASS_NAME_KEY);
BeanPropertyWriter beanPropWriter = new BeanPropertyWriter(propDef,
am, null,
SimpleType.constructUnsafe(Class.class),
null, null, SimpleType.constructUnsafe(Class.class),
true, null);
beanProperties.add(beanPropWriter);
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return beanProperties;
}
private AnnotationMap _emptyAnnotationMap() {
return new AnnotationMap();
}
}
任何幫助將不勝感激。
感謝