There are times when it is necessary to convert a list of user-defined objects to string and later on retrieve it as a list in its original form. Example: like storing it into a Redis cache.
How do we do that?
ObjectMapper
Steps:
1. ObjectMapper objectMapper = new ObjectMapper();
First, initialize ObjectMapper.
2. List<YourClass> listObj ;
Create and initialize your list of user-defined object.
where, YourClass is a class having multiple variables.
Example -
class YourClass{
String abc;
String xyz;
}
3. String convertedStr = objectMapper.writeValueAsString(listObj);
This is the string conversion of the list.
4. listObj = objectMapper.readValue(convertedStr, new TypeReference<List<YourClass>>();
And we are back with your list object.
Comments
Post a Comment