Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
10
Help us understand the problem. What is going on with this article?
@mdstoy

Jackson が変換・出力対象とするもの

More than 3 years have passed since last update.

Jackson の変換対象についてちょっと勘違いしていたことがあって、改めて調べ直したものをせっかくなので記事にしました。

Jackson が JSON に出力してくれるもの

以下が条件となります。(過不足があればご指摘ください)

  • public のフィールド
  • public のゲッター
    • 対応するフィールドがなくても、get で始まるメソッドがあれば出力対象となる
    • boolean の場合は、is で始まる名前も対象となる
      • can とか has などはならないっぽい
  • ここまでの条件に当てはまっていても、@JsonIgnore アノテーションをつけると対象外となる(つけるのはフィールドでもゲッターでもよい)

サンプル

百聞は一見に如かず、ということで実際にやってみましょう。

変換対象クラス

JacksonSampleObject.java
import com.fasterxml.jackson.annotation.JsonIgnore;

public class JacksonSampleObject {

    private Integer notOutputPrivate = 0;

    protected Integer notOutputProtected = 1;

    public Integer outputPublic = 2;

    private Integer outputPrivateWithPublicGetter = 3;

    public Integer getOutputPrivateWithPublicGetter() {
        return outputPrivateWithPublicGetter;
    }

    protected Integer outputProtectedWithGetter = 4;

    public Integer getOutputProtectedWithGetter() {
        return outputProtectedWithGetter;
    }

    private Integer getNotOutputPrivateGetter() {
        return 5;
    }

    protected Integer getNotOutputProtectedGetter() {
        return 6;
    }

    public Integer getOutputPublicGetter() {
        return 7;
    }

    @JsonIgnore
    public Integer notOutputPublicWithIgnoreAnnotation = 8;

    @JsonIgnore
    public Integer getNotOutputPublicGetterWithIgnoreAnnotation() {
        return 9;
    }

    private Boolean outputBoolean = true;

    public Boolean isOutputBoolean() {
        return outputBoolean;
    }

    public Integer isNotOutput() {
        return 10;
    }

    public Boolean getOutputBooleanGetter() {
        return true;
    }

    public Boolean canNotOutputBooleanMethod() {
        return false;
    }
}

実行クラス

JacksonSample.java
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

public class JacksonSample {

    public static void main(String[] args) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        mapper.enable(SerializationFeature.INDENT_OUTPUT);
        System.out.println(mapper.writeValueAsString(new JacksonSampleObject()));
    }
}

実行結果

{
  "outputPublic" : 2,
  "outputPrivateWithPublicGetter" : 3,
  "outputProtectedWithGetter" : 4,
  "outputBoolean" : true,
  "outputPublicGetter" : 7,
  "outputBooleanGetter" : true
}
10
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
mdstoy
主に「自分用おぼえがき」や「人に説明するためのメモ」を書いていきたい

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
10
Help us understand the problem. What is going on with this article?