Created
April 7, 2022 20:22
-
-
Save wesleyegberto/41ec5077bcf496739231871d2be88fbb to your computer and use it in GitHub Desktop.
Snippet to parse string JSON to a generic class using Jackson lib for Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Event<T> { | |
T payload; | |
} | |
public <T> T parseJsonToClass(String json, Class<T> targetClass) throw JsonProcessingException { | |
var targetType = objectMapper | |
.getTypeFactory() | |
.constructParametricType(Event.class, targetClass); | |
return objectMapper.readValue(json, targetType); | |
} | |
var object = parseJsonToClass("{\"payload\":{}}", SavePayload.class); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment