Skip to content

Instantly share code, notes, and snippets.

@judepereira
Created September 4, 2023 13:32
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 judepereira/b190ef1ecad3bcb6a79dd6e52878974a to your computer and use it in GitHub Desktop.
Save judepereira/b190ef1ecad3bcb6a79dd6e52878974a to your computer and use it in GitHub Desktop.
private static final ObjectMapper OM = new ObjectMapper();
public static String escapeString(Object o) {
if (o == null) {
return null;
}
try {
// The string is automatically quoted. Therefore, return a new string that
// doesn't contain those quotes (since the caller appends quotes themselves).
val bytes = OM.writeValueAsBytes(o.toString());
return new String(bytes, 1, bytes.length - 2, StandardCharsets.UTF_8);
} catch (JsonProcessingException e) {
return "";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment