LoginSignup
2
1

More than 5 years have passed since last update.

ObjectMapperでJSONをSerializeする際のイロイロな設定

Last updated at Posted at 2016-03-25

プロジェクトで使っているのがObjectMapper1系なので、1系前提。

適当なBeanを用意する。

public class TestBean {

    private int id;
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
}

Serialize時の設定

Serializeする際にnull以外のプロパティを対象にする

    @Test
    public void Serializeする際にNULLじゃないプロパティのみが対象になる() throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        mapper.setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL);
        TestBean bean = new TestBean();
        String s = mapper.writeValueAsString(bean);
        // idはデフォルト値になる
        // nameはnullなのでSerialize対象外
        assertThat(s, is("{\"id\":0}"));
    }

Serializeする際に空以外のプロパティを対象にする

    @Test
    public void Serializeする際に空じゃないプロパティのみが対象になる() throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        mapper.setSerializationInclusion(JsonSerialize.Inclusion.NON_EMPTY);
        TestBean bean = new TestBean();
        bean.setName("");
        String s = mapper.writeValueAsString(bean);
        assertThat(s, is("{\"id\":0}"));
    }

Serializeする際にデフォルト値以外のプロパティを対象にする

    @Test
    public void Serializeする際にデフォルト値じゃないプロパティのみが対象になる() throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        mapper.setSerializationInclusion(JsonSerialize.Inclusion.NON_DEFAULT);
        TestBean bean = new TestBean();
        bean.setName("tenten0213");
        String s = mapper.writeValueAsString(bean);
        assertThat(s, is("{\"name\":\"tenten0213\"}"));
    }

Deserialize時の設定もあるけど、多くて試すの面倒だから書かない。
プロジェクトでは、外部から送信されるJSONのI/Fに項目が勝手に追加される(恐ろしい…)ことがあるらしいので、知らないプロパティが来ても落ちないように以下のような設定をしている。

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);

一応pomも置いておく

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.github.tenten0213</groupId>
    <artifactId>JasonSerializeSample</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>1.9.9</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>1.9.9</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1