如何反序列化 Eclipse 集合类型?
How to deserialise Eclipse Collections types?
我遇到了这个库 eclipse-collections,它适合我的用例。但不幸的是,我无法使用 Gson 或 Jackson 反序列化它 - serialization/deserialization.
的两个最受欢迎的库
这两个库中的任何一个都提供对 eclipse 集合的支持吗?如果是,那又如何?
eclipse-collections 的官方 github 回购自述文件中的一个文档提到:
Unfortunately, with new collection types comes incompatibility with normal serialization frameworks. Jackson, arguably the most popular JSON serialization framework for Java, is unable to deserialize Eclipse Collections types out-of-the-box. For this purpose, there is now a Jackson module supporting most Eclipse Collections types directly (including primitive collections).
他们不仅提到了这个问题,而且还提供了解决方案。您需要做的就是用 Jackson 的 ObjectMapper
对象注册 EclipseCollectionsModule
,如下所示:
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.eclipsecollections.EclipseCollectionsModule;
ObjectMapper mapper = new ObjectMapper().registerModule(new EclipseCollectionsModule());
完成上述操作后,您现在可以像往常一样使用 ObjectMapper。所以,
MutableIntLongMap myDeserialisedVariable = mapper.readValue(jsonString, MutableIntLongMap.class);
我遇到了这个库 eclipse-collections,它适合我的用例。但不幸的是,我无法使用 Gson 或 Jackson 反序列化它 - serialization/deserialization.
的两个最受欢迎的库这两个库中的任何一个都提供对 eclipse 集合的支持吗?如果是,那又如何?
eclipse-collections 的官方 github 回购自述文件中的一个文档提到:
Unfortunately, with new collection types comes incompatibility with normal serialization frameworks. Jackson, arguably the most popular JSON serialization framework for Java, is unable to deserialize Eclipse Collections types out-of-the-box. For this purpose, there is now a Jackson module supporting most Eclipse Collections types directly (including primitive collections).
他们不仅提到了这个问题,而且还提供了解决方案。您需要做的就是用 Jackson 的 ObjectMapper
对象注册 EclipseCollectionsModule
,如下所示:
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.eclipsecollections.EclipseCollectionsModule;
ObjectMapper mapper = new ObjectMapper().registerModule(new EclipseCollectionsModule());
完成上述操作后,您现在可以像往常一样使用 ObjectMapper。所以,
MutableIntLongMap myDeserialisedVariable = mapper.readValue(jsonString, MutableIntLongMap.class);