Created
May 10, 2018 16:30
-
-
Save ques0942/9d81b3db57bcd3d9dfbb142d4167ae10 to your computer and use it in GitHub Desktop.
Jacksonよくある使い方メモ ref: https://qiita.com/ques0942/items/c4a20a21c9e1f0963591
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SampleDTO { | |
private String field1; | |
private List<String> field2; | |
private SomeClass field3; | |
public String getField1(){return field1}; | |
public List<String> getField2(){return field2}; | |
public SomeClass getField3(){return field3}; | |
public String getField3Text(){return field3.getText()}; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SampleDTO dto = new SampleDTO(); | |
ObjectMapper mapper = new ObjectMapper(); | |
String json = mapper.writeValueAsString(dto); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SampleDTO dto = new SampleDTO(); | |
ObjectMapper mapper = new ObjectMapper(); | |
ObjectNode jsonNode = mapper.valueToTree(dto); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SampleDTO dto = new SampleDTO(); | |
ObjectMapper mapper = new ObjectMapper(); | |
ObjectReader reader = mapper.readerForUpdating(dto); | |
reader.readValue(json); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@JsonIgnore | |
private String field1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@JsonProperty("fieldA") | |
private String field1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@JsonProperty("fieldA") | |
public String getField1(){ | |
... | |
} | |
// JsonPropertyで設定した値とコンフリクトするメソッドが存在する場合は | |
// 不要な方にJsonIgnoreを付与する。 | |
@JsonIgnore | |
public String getFieldA(){ | |
... | |
} | |
// この場合setterも合わせて定義しないとうまく相互変換できない | |
@JsonSetter("fieldA") | |
public void setField1(String field1){ | |
... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private String sampleField; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ObjectMapper mapper = new ObjectMapper(); | |
// 命名変更規則の設定 | |
mapper.setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 出力するJSON | |
mapper.enable(SerializationConfig.Feature.INDENT_OUTPUT); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment