1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

ObjectMapperを試してみた

Posted at

概要

JavaのclassとJsonの変換に関して試したことを列挙します!

利用例

JavaクラスからJson文字列への変換

import lombok.AllArgsConstructor;
import lombok.Getter;

@AllArgsConstructor
@Getter
public class Response {
    String accessToken;
    String refreshToken;
}
    @Test
    void オブジェクトをJsonに変換できる() throws Exception {
        // arrange
        var response = new Response("a", "b");

        // act, assert
        assertEquals("{\"accessToken\":\"a\",\"refreshToken\":\"b\"}",
                new ObjectMapper().writeValueAsString(response));
    }

Json文字列からJavaクラスへの変換

import lombok.*;

@AllArgsConstructor
@Getter
@EqualsAndHashCode
@NoArgsConstructor
public class Response {
    String accessToken;
    String refreshToken;
}
    @Test
    void Jsonをオブジェクトに変換できる() throws Exception {
        // arrange
        var jsonString = "{\"accessToken\":\"a\",\"refreshToken\":\"b\"}";

        // act, assert
        assertEquals(new Response("a", "b"),
                new ObjectMapper().readValue(jsonString, Response.class));
    }

JavaクラスからJson文字列への変換(スネークケース)

import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import lombok.AllArgsConstructor;
import lombok.Getter;

@JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class)
@AllArgsConstructor
@Getter
public class SnakeResponse {
    String accessToken; // フィールドをpublicにするのでもOK 
    String refreshToken;
}
    @Test
    void オブジェクトをJsonにスネークケースで変換できる() throws Exception {
        // arrange
        var response = new SnakeResponse("a", "b");

        // act, assert
        assertEquals("{\"access_token\":\"a\",\"refresh_token\":\"b\"}",
                new ObjectMapper().writeValueAsString(response));
    }

PropertyNamingStrategyのバリエーション

Strategy 結果
LowerDotCaseStrategy {"access.token":"a","refresh.token":"b"}
SnakeCaseStrategy {"access_token":"a","refresh_token":"b"}
KebabCaseStrategy {"access-token":"a","refresh-token":"b"}
LowerCaseStrategy {"accesstoken":"a","refreshtoken":"b"}
UpperCamelCaseStrategy {"AccessToken":"a","RefreshToken":"b"}

nullがある場合は表示しない

@AllArgsConstructor
@Getter
@EqualsAndHashCode
@NoArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Response {
    String accessToken;
    String refreshToken;
}

    @Test
    void オブジェクトにnullがある場合_そのプロパティは表示しない() throws Exception {
        // arrange
        var response = new Response("a", null);

        // act, assert
        assertEquals("{\"accessToken\":\"a\"}",
                new ObjectMapper().writeValueAsString(response));
    }

エラー

フィールドが見えない時のエラー

No serializer found for class Response and no properties discovered to create BeanSerializerはgetterをつければ解消できます。

@AllArgsConstructor
//@Getter // ここがないからエラー もちろん自分でgetter作ってもOK
public class Response {
    String accessToken; // フィールドをpublicにするのでもOK 
    String refreshToken;
}
    @Test
    void オブジェクトをJsonに変換できる() throws Exception {
        // arrange
        var response = new Response("a", "b");

        // act, assert
        assertEquals("{\"accessToken\":\"a\",\"refreshToken\":\"b\"}",
                new ObjectMapper().writeValueAsString(response));
    }

コンストラクタがないエラー

Cannot construct instance of Response (no Creators, like default constructor, exist): cannot deserialize from Object value

@AllArgsConstructor
@Getter
@EqualsAndHashCode
//@NoArgsConstructor // これがない
public class Response {
    String accessToken;
    String refreshToken;
}
    @Test
    void Jsonをオブジェクトに変換できる() throws Exception {
        // arrange
        var jsonString = "{\"accessToken\":\"a\",\"refreshToken\":\"b\"}";

        // act, assert
        assertEquals(new Response("a", "b"),
                new ObjectMapper().readValue(jsonString, Response.class));
    }
1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?