Skip to content

Instantly share code, notes, and snippets.

@purplefox
Created January 20, 2020 16:47
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 purplefox/8975123fcc97da9c7f00eb2ce5ccf8ec to your computer and use it in GitHub Desktop.
Save purplefox/8975123fcc97da9c7f00eb2ce5ccf8ec to your computer and use it in GitHub Desktop.
public <T> T deserialiseObject(final Buffer buffer, final HttpServerResponse response,
final Class<T> clazz) {
final ObjectMapper objectMapper = DatabindCodec.mapper();
try {
final T t = objectMapper.readValue(buffer.getBytes(), clazz);
} catch (UnrecognizedPropertyException e) {
sendUnrecognisedPropertyError(e.getPropertyName(), response);
return null;
} catch (MismatchedInputException e) {
final int startIndex = e.getMessage().indexOf('\'');
final int endIndex = e.getMessage().indexOf('\'', startIndex + 1);
final String propertyName = e.getMessage().substring(startIndex + 1, endIndex);
sendMissingPropertyError(propertyName, response);
return null;
} catch (JsonParseException e) {
sendInvalidJsonError(response);
return null;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment