7

ダース以上のプロパティを持つクラスがあります。プリミティブ型のほとんどのプロパティでは、デフォルトのBeanSerializerやBeanDeserializerなどを使用して、作成する必要のある面倒なコードを減らしたいと考えています。カスタム型と配列型の他のプロパティについては、カスタムシリアライザー/デシリアライザーを実行したいと思います。基になるJSON文字列を変更できないことに注意してください。しかし、私はアンドロイドコードへのフルアクセスを持っています。Jackson 1.7.9 /Ektorp1.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<String, SettableBeanProperty> backRefs,
        HashSet<String> 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を検索しましたが、役立つ例やチュートリアルが見つかりませんでした。誰かが私にいくつかの実用的な例を送ってくれるなら、それは素晴らしいことです!ありがとう!

4

1 に答える 1

4

BeanSerializer / -Deserializerをサブクラス化するには、より新しいバージョンのJacksonを使用することをお勧めします。この領域は、インスタンスの構成を変更できるBeanSerializerModifierおよびBeanDeserializerModifierによる明示的なサポートによって改善されているためです。

ただし、念のため、次のように、個々のプロパティでのみ使用するカスタムシリアライザー/デシリアライザーを指定することもできます。

class Foo {
   @JsonSerialize(using=MySerializer.class)
   public OddType getValue();
}
于 2011-10-28T15:45:26.167 に答える