Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wesleyegberto/41ec5077bcf496739231871d2be88fbb to your computer and use it in GitHub Desktop.
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
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