FrankChen021 commented on code in PR #13165:
URL: https://github.com/apache/druid/pull/13165#discussion_r1000595192
##########
core/src/main/java/org/apache/druid/java/util/common/jackson/JacksonUtils.java:
##########
@@ -89,7 +95,50 @@ public static void writeObjectUsingSerializerProvider(
}
}
- private JacksonUtils()
+ /**
+ * Convert the given object to an array of bytes. Use when the object is
+ * known serializable so that the Jackson exception can be suppressed.
+ */
+ public static byte[] toBytes(ObjectMapper jsonMapper, Object obj)
{
+ try {
+ return jsonMapper.writeValueAsBytes(obj);
+ }
+ catch (JsonProcessingException e) {
+ throw new ISE("Failed to serialize " + obj.getClass().getSimpleName());
+ }
+ }
+
+ /**
+ * Deserialize an object from an array of bytes. Use when the object is
+ * known deserializable so that the Jackson exception can be suppressed.
+ */
+ public static <T> T fromBytes(ObjectMapper jsonMapper, byte[] bytes,
Class<T> clazz)
+ {
+ try {
+ return jsonMapper.readValue(bytes, clazz);
+ }
+ catch (IOException e) {
+ throw new ISE(e, "Failed to deserialize a " + clazz.getSimpleName());
+ }
+ }
+
+ /**
+ * Quick & easy implementation of {@code toString()} for objects which are
+ * primarily representations of JSON objects. Use only for cases where the
+ * {@code toString()} is for debugging: the cost of creating an object mapper
+ * every time is undesirable for production code. Also, assumes that the
+ * type can serialized using the default mapper: doesn't work for types that
+ * require custom Jackson extensions.
+ */
+ public static String toString(Object obj)
+ {
+ ObjectMapper jsonMapper = new ObjectMapper();
Review Comment:
I think we should use `DefaultObjectMapper` instead
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]