Reputation: 3294
I came across this library eclipse-collections and it fits my use case. But unfortunately, I am not able to deserialize it using either Gson or Jackson - the two most popular libraries for serialization/deserialization.
Does any of the two libraries provide support for eclipse collections? If yes, then how?
Upvotes: 1
Views: 272
Reputation: 3294
One of the docs on the Readme of the official github repo of eclipse-collections mentions:
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).
Not only do they mention the issue but they also provide the solution to it. All you need to do is register the EclipseCollectionsModule
with Jackson's ObjectMapper
object like so:
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.eclipsecollections.EclipseCollectionsModule;
ObjectMapper mapper = new ObjectMapper().registerModule(new EclipseCollectionsModule());
Once you have done the above, you can now use the ObjectMapper as usual. So,
MutableIntLongMap myDeserialisedVariable = mapper.readValue(jsonString, MutableIntLongMap.class);
Upvotes: 2