LoginSignup
15
11

More than 5 years have passed since last update.

[Java][JSON] genericなクラスの配列となっているJSONをJacksonを使ってLISTへデシリアライズする方法

Last updated at Posted at 2016-02-10

genericなクラスの場合は、下記にあるTypeReferenceを使う方法ではCastExceptionが発生してしまいました。

このようにすればよいみたいです。

public static <T extends Object> List<T> deserialize(final String jsonStr, final Class<T> clazz) {
  final ObjectMapper mapper = new ObjectMapper();
  final CollectionType jt
    = mapper.getTypeFactory().constructCollectionType(List.class, clazz);

  return (List<T>) mapper.readValue(jsonStr, jt);
}

JacksonのFAQに記載ありました :laughing:
http://wiki.fasterxml.com/JacksonFAQ#Deserializing_Generic_types

15
11
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
15
11