3
私は、(この例では)タイプFooのサブクラスであるオブジェクトを含むオブジェクトグラフを持っています。 Fooクラスにはbarという名前の属性があり、私はオブジェクトグラフと直列化したくありません。ですから、基本的には、Foo型のオブジェクトを直列化するたびに、bar以外のすべてを出力すると言う方法が必要です。Jackson JSONのシリアル化から属性をグローバルに削除する方法はありますか?
class Foo { // this is an external dependency
public long getBar() { return null; }
}
class Fuzz extends Foo {
public long getBiz() { return null; }
}
public static void main(String[] args) {
ObjectMapper mapper = new ObjectMapper();
// I want to set a configuration on the mapper to
// exclude bar from all things that are type Foo
Fuzz fuzz = new Fuzz();
System.out.println(mapper.writeValueAsString(fuzz));
// writes {"bar": null, "biz": null} what I want is {"biz": null}
}
おかげで、 身代金
編集:脇@JsonIgnore
からの中古StaxMan提案、私が使用して終了(バー例のためにゲッター製)するコードを含む
interface Mixin {
@JsonIgnore long getBar();
}
class Example {
public static void main() {
ObjectMapper mapper = new ObjectMapper();
mapper.getSerializationConfig().addMixInAnnotations(Foo.class, Mixin.class);
Fuzz fuzz = new Fuzz();
System.out.println(mapper.writeValueAsString(fuzz));
// writes {"biz": null} whoo!
}
}
これは私の知らないかもしれませんが、マーキングバーは一時的なものとしてどうですか? – DwB