Add test helper for (de)serialization of arbitrary objects

This commit is contained in:
René Pfeuffer
2020-04-30 15:51:08 +02:00
parent fa8afc9fbf
commit 705a175221

View File

@@ -33,20 +33,20 @@ import java.io.StringWriter;
public class SerializationTestUtil {
static <T> T toAndFromJson(Class<T> clazz, T input) throws JsonProcessingException {
public static <T> T toAndFromJson(Class<T> clazz, T input) throws JsonProcessingException {
final ObjectMapper objectMapper = new ObjectMapper();
final String json = objectMapper.writeValueAsString(input);
return objectMapper.readValue(json, clazz);
}
static <T> T toAndFromXml(Class<T> clazz, T input) {
public static <T> T toAndFromXml(Class<T> clazz, T input) {
final StringWriter xmlWriter = new StringWriter();
JAXB.marshal(input, xmlWriter);
final StringReader xmlReader = new StringReader(xmlWriter.toString());
return JAXB.unmarshal(xmlReader, clazz);
}
static <T> T toAndFromJsonAndXml(Class<T> clazz, T input) throws JsonProcessingException {
public static <T> T toAndFromJsonAndXml(Class<T> clazz, T input) throws JsonProcessingException {
return toAndFromXml(clazz, toAndFromJson(clazz, input));
}
}