ジャクソンはマーシャリングのプロパティを無視します
1. 概要
このチュートリアルでは、Jackson 2.xを使用してignore certain fields when serializing an object to JSONする方法を示します。
これは、Jacksonのデフォルトでは不十分で、JSONにシリアル化されるものを正確に制御する必要がある場合に非常に役立ちます。また、プロパティを無視する方法はいくつかあります。
深く掘り下げて、ジャクソンでできる他のクールなことを学びたい場合は、the main Jackson tutorialに進んでください。
参考文献:
Jackson ObjectMapperの紹介
この記事では、Jacksonの中心的なObjectMapperクラス、基本的なシリアル化と逆シリアル化、および2つのプロセスの構成について説明します。
2. クラスレベルでフィールドを無視する
the @JsonIgnoreProperties annotation and specifying the fields by nameを使用して、クラスレベルで特定のフィールドを無視できます。
@JsonIgnoreProperties(value = { "intValue" })
public class MyDto {
private String stringValue;
private int intValue;
private boolean booleanValue;
public MyDto() {
super();
}
// standard setters and getters are not shown
}
オブジェクトがJSONに書き込まれた後、フィールドが実際に出力の一部ではないことをテストできます。
@Test
public void givenFieldIsIgnoredByName_whenDtoIsSerialized_thenCorrect()
throws JsonParseException, IOException {
ObjectMapper mapper = new ObjectMapper();
MyDto dtoObject = new MyDto();
String dtoAsString = mapper.writeValueAsString(dtoObject);
assertThat(dtoAsString, not(containsString("intValue")));
}
3. フィールドレベルでフィールドを無視する
the @JsonIgnore annotation directly on the fieldを介してフィールドを直接無視することもできます。
public class MyDto {
private String stringValue;
@JsonIgnore
private int intValue;
private boolean booleanValue;
public MyDto() {
super();
}
// standard setters and getters are not shown
}
これで、intValueフィールドが実際にシリアル化されたJSON出力の一部ではないことをテストできます。
@Test
public void givenFieldIsIgnoredDirectly_whenDtoIsSerialized_thenCorrect()
throws JsonParseException, IOException {
ObjectMapper mapper = new ObjectMapper();
MyDto dtoObject = new MyDto();
String dtoAsString = mapper.writeValueAsString(dtoObject);
assertThat(dtoAsString, not(containsString("intValue")));
}
4. タイプ別にすべてのフィールドを無視する
最後に、ignore all fields of a specified type, using the @JsonIgnoreType annotationを実行できます。 型を制御する場合、クラスに直接注釈を付けることができます。
@JsonIgnoreType
public class SomeType { ... }
ただし、多くの場合、クラス自体を制御することはできません。この場合、we can make good use of Jackson mixinsです。
まず、無視するタイプのMixInを定義し、代わりに@JsonIgnoreTypeで注釈を付けます。
@JsonIgnoreType
public class MyMixInForIgnoreType {}
次に、マーシャリング中にすべてのString[]タイプを置き換える(そして無視する)ために、そのミックスインを登録します。
mapper.addMixInAnnotations(String[].class, MyMixInForIgnoreType.class);
この時点で、すべての文字列配列はJSONにマーシャリングされる代わりに無視されます。
@Test
public final void givenFieldTypeIsIgnored_whenDtoIsSerialized_thenCorrect()
throws JsonParseException, IOException {
ObjectMapper mapper = new ObjectMapper();
mapper.addMixIn(String[].class, MyMixInForIgnoreType.class);
MyDtoWithSpecialField dtoObject = new MyDtoWithSpecialField();
dtoObject.setBooleanValue(true);
String dtoAsString = mapper.writeValueAsString(dtoObject);
assertThat(dtoAsString, containsString("intValue"));
assertThat(dtoAsString, containsString("booleanValue"));
assertThat(dtoAsString, not(containsString("stringValue")));
}
DTOは次のとおりです。
public class MyDtoWithSpecialField {
private String[] stringValue;
private int intValue;
private boolean booleanValue;
}
注:バージョン2.5以降–このメソッドを使用してプリミティブデータ型を無視することはできませんが、カスタムデータ型と配列には使用できます。
5. フィルタを使用してフィールドを無視する
最後に、ジャクソンのwe can also use filters to ignore specific fields。 まず、Javaオブジェクトでフィルターを定義する必要があります。
@JsonFilter("myFilter")
public class MyDtoWithFilter { ... }
次に、intValueフィールドを無視する単純なフィルターを定義します。
SimpleBeanPropertyFilter theFilter = SimpleBeanPropertyFilter
.serializeAllExcept("intValue");
FilterProvider filters = new SimpleFilterProvider()
.addFilter("myFilter", theFilter);
これで、オブジェクトをシリアル化し、intValueフィールドがJSON出力に存在しないことを確認できます。
@Test
public final void givenTypeHasFilterThatIgnoresFieldByName_whenDtoIsSerialized_thenCorrect()
throws JsonParseException, IOException {
ObjectMapper mapper = new ObjectMapper();
SimpleBeanPropertyFilter theFilter = SimpleBeanPropertyFilter
.serializeAllExcept("intValue");
FilterProvider filters = new SimpleFilterProvider()
.addFilter("myFilter", theFilter);
MyDtoWithFilter dtoObject = new MyDtoWithFilter();
String dtoAsString = mapper.writer(filters).writeValueAsString(dtoObject);
assertThat(dtoAsString, not(containsString("intValue")));
assertThat(dtoAsString, containsString("booleanValue"));
assertThat(dtoAsString, containsString("stringValue"));
System.out.println(dtoAsString);
}
6. 結論
この記事では、シリアル化でフィールドを無視する方法を説明しました。まず名前で、次に直接、最後に、MixInsでjavaタイプ全体を無視し、出力をさらに制御するためにフィルターを使用します。
これらすべての例とコードスニペットの実装は、my GitHub projectにあります。