Jackson - Deserialising JSON string - TypeReference vs TypeFactory.constructCollectionType

前端 未结 2 1284
無奈伤痛
無奈伤痛 2021-02-01 13:38

To deserialise JSON string to a list of class, different ways listed at StackOverflow question

Type 1 (docs link):

List someClassList =          


        
相关标签:
2条回答
  • 2021-02-01 14:21

    One more way you can achieve this is by :

    List<SomeClass> list = Arrays.asList(mapper.readValue(jsonString, SomeClass[].class));

    0 讨论(0)
  • 2021-02-01 14:34

    After constructing JavaType, both call same deserialization functionality, so the only difference is the way generic type is handled.

    Second one is fully static, so type must be known in compile type, and can not vary. So it is similar to using basic Class literal.

    First one is dynamic, so it can be used to construct things that vary regarding their parameterization.

    Personally I prefer first alternative for all cases (it avoids creation of one more anonymous inner classes), but second one may be more readable.

    0 讨论(0)
提交回复
热议问题