我正在尝试升级我的项目以使用更新版本的Jackson,并且遇到了自定义序列化程序和反序列化程序。
在下面的反序列化器中,我遇到了构造函数问题 - 我不确定我应该传入哪种类型。我使用的构造函数不再可用。以下是当前的课程:
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;
public class 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);
}
下面列出了解串器。我对 AnnotatedMethod 和 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();
}
}
非常感谢任何帮助。
由于
答案 0 :(得分:0)
我能够像这样修复它::
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);
TypeResolutionContext.Basic tb = new TypeResolutionContext.Basic(TypeFactory.defaultInstance(), TypeBindings.emptyBindings());
AnnotatedMethod am = new AnnotatedMethod(tb, m, _emptyAnnotationMap(), null);
BeanPropertyDefinition propDef = SimpleBeanPropertyDefinition.construct(config, 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();
}
}