私には十数以上の特性を持つクラスがあります。プリミティブ型のほとんどのプロパティについては、デフォルトのBeanSerializerとBeanDeserializerなどを使用して、書く必要のある面倒なコードを減らしたいと考えています。カスタムおよび配列型の他のプロパティについては、カスタムのシリアライザ/デシリアライザを使用したいと思います。基になるJSON文字列を変更することはできません。しかし、私はアンドロイドコードに完全にアクセスできます。私はJackson 1.7.9/Ektorp 1.1.1を使用しています。
BeanDeserializerをサブクラス化しますか?私はそれに問題があります。パラメータを持たないデフォルトのコンストラクタが必要ですが、スーパーコンストラクタの呼び出し方法はわかりません。
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を検索しましたが、役立つサンプル/チュートリアルが見つかりませんでした。誰かが私に偉大になるいくつかの実用的な例を私に送ることができれば!ありがとう!