BatfishObjectMapper: add #clone(TypeReference)
* BatfishObjectMapper: add #clone(TypeReference)
Supplements #clone(Class) for generic types.
@@ -3,6 +3,7 @@ package org.batfish.common.util;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.core.util.DefaultIndenter;
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
import com.fasterxml.jackson.databind.MapperFeature;
@@ -43,6 +44,11 @@ public final class BatfishObjectMapper {
return MAPPER.readValue(WRITER.writeValueAsBytes(o), clazz);
}
+ /** Uses Jackson to clone the given object using the given type. */
+ public static <T> T clone(Object o, TypeReference<T> type) throws IOException {
+ return MAPPER.readValue(WRITER.writeValueAsBytes(o), type);
+ }
+
/**
* Returns a {@link ObjectWriter} configured to Batfish JSON standards. The JSON produced is not
* pretty-printed; see {@link #prettyWriter} for that.
