Bir düzineden fazla özelliği olan bir sınıfım var. İlkel türün özelliklerinin çoğunda, varsayılan BeanSerializer ve BeanDeserializer'ı veya yazmam gereken hantal kodları azaltmak için ne kullanırsam kullanırım. Özel ve dizi türlerinin diğer özellikleri için, bazı özel serializer/deserializer yapmak istiyorum. Temel JSON dizesini değiştiremediğimi unutmayın. Ama ben android koduna tam erişimim var. Jackson 1.7.9/Ektorp 1.1.1 kullanıyorum.
BeanDeserializer alt sınıfını mı kullanmalıyım? Bununla sorun yaşıyorum. Parametresiz bir varsayılan kurucuyu bekler ama süper kurucuyu nasıl arayacağımı bilmiyorum.
class MyType{
//a dozen properties with primitive types String, Int, BigDecimal
public Stirng getName();
public void setName(String name);
//properties that require custom deserializer/serializer
public CustomType getCustom();
public void setCustom(CustomType ct);
}
class MyDeserializer extends BeanDeserialzer{
//an exception is throw if I don't have default constructor.
//But BeanDeserializer doesn't have a default constructor
//It has the below constructor that I don't know how to fill in the parameters
public MyDeserializer(AnnotatedClass forClass, JavaType type,
BeanProperty property, CreatorContainer creators,
BeanPropertyMap properties,
Map backRefs,
HashSet ignorableProps, boolean ignoreAllUnknown,
SettableAnyProperty anySetter) {
super(forClass, type, property, creators, properties, backRefs, ignorableProps,
ignoreAllUnknown, anySetter);
}
@Override
public Object deserialize(JsonParser jp, DeserializationContext dc, Object bean)
throws IOException, JsonProcessingException {
super.deserialize(jp, dc, bean);
MyType c = (MyType)bean;
ObjectMapper mapper = new ObjectMapper();
JsonNode rootNode = mapper.readValue(jp, JsonNode.class);
//Use tree model to construct custom
//Is it inefficient because it needs a second pass to the JSON string to construct the tree?
c.setCustom(custom);
return c;
}
}
Google'da arama yaptım, ancak yararlı örnekleri/öğretici bulamadım. Herkes bana harika olabilecek bazı çalışma örnekleri gönderebilir! Teşekkürler!