1 package de.dlr.shepard.common.util;
2
3 import com.fasterxml.jackson.databind.ObjectMapper;
4 import io.quarkus.logging.Log;
5
6 public final class JsonConverter {
7
8 public static String convertToString(Object object) {
9 try {
10 if (object == null) return null;
11 return new ObjectMapper().writeValueAsString(object);
12 } catch (Exception e) {
13 Log.errorf("Error while converting metadata to JSON string. %s", e);
14 throw new RuntimeException(e);
15 }
16 }
17
18 public static Object convertToObject(String jsonString) {
19 try {
20 if (jsonString == null) return null;
21 return new ObjectMapper().readValue(jsonString, Object.class);
22 } catch (Exception e) {
23 Log.errorf("Error while converting JSON string to metadata object. %s", e);
24 throw new RuntimeException(e);
25 }
26 }
27
28 public static <T> T convertToObject(String jsonString, Class<T> clazz) {
29 try {
30 if (jsonString == null) return null;
31 return new ObjectMapper().readValue(jsonString, clazz);
32 } catch (Exception e) {
33 Log.errorf("Error while converting JSON string to metadata object. %s", e);
34 throw new RuntimeException(e);
35 }
36 }
37 }