From be07937df9fcda1525d790b1be0ff6fe0f953dbe Mon Sep 17 00:00:00 2001 From: Sergey Matvienko Date: Tue, 23 Apr 2024 11:07:36 +0200 Subject: [PATCH] JacksonUtil merged with downstream --- .../thingsboard/common/util/JacksonUtil.java | 47 ++++++++++++++++++- 1 file changed, 45 insertions(+), 2 deletions(-) diff --git a/common/util/src/main/java/org/thingsboard/common/util/JacksonUtil.java b/common/util/src/main/java/org/thingsboard/common/util/JacksonUtil.java index 1075ec9e6e..ff5716a361 100644 --- a/common/util/src/main/java/org/thingsboard/common/util/JacksonUtil.java +++ b/common/util/src/main/java/org/thingsboard/common/util/JacksonUtil.java @@ -28,6 +28,7 @@ import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.databind.json.JsonMapper; import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.ObjectNode; +import com.fasterxml.jackson.databind.type.CollectionType; import com.fasterxml.jackson.datatype.jdk8.Jdk8Module; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import com.google.common.collect.Lists; @@ -39,6 +40,8 @@ import java.io.File; import java.io.IOException; import java.io.Reader; import java.io.Writer; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.Arrays; import java.util.HashMap; import java.util.Iterator; @@ -133,7 +136,7 @@ public class JacksonUtil { try { return bytes != null ? OBJECT_MAPPER.readValue(bytes, clazz) : null; } catch (IOException e) { - throw new IllegalArgumentException("The given string value cannot be transformed to Json object: " + Arrays.toString(bytes), e); + throw new IllegalArgumentException("The given byte[] value cannot be transformed to Json object:" + Arrays.toString(bytes), e); } } @@ -161,6 +164,15 @@ public class JacksonUtil { } } + public static String writeValueAsString(Object value) { + try { + return OBJECT_MAPPER.writeValueAsString(value); + } catch (JsonProcessingException e) { + throw new IllegalArgumentException("The given Json object value: " + + value + " cannot be transformed to a String", e); + } + } + public static String toPrettyString(Object o) { try { return PRETTY_SORTED_JSON_MAPPER.writeValueAsString(o); @@ -206,6 +218,38 @@ public class JacksonUtil { } } + public static T readValue(String file, CollectionType clazz) { + try { + return OBJECT_MAPPER.readValue(file, clazz); + } catch (IOException e) { + throw new IllegalArgumentException("Can't read file: " + file, e); + } + } + + public static T readValue(File file, TypeReference clazz) { + try { + return OBJECT_MAPPER.readValue(file, clazz); + } catch (IOException e) { + throw new IllegalArgumentException("Can't read file: " + file, e); + } + } + + public static T readValue(File file, Class clazz) { + try { + return OBJECT_MAPPER.readValue(file, clazz); + } catch (IOException e) { + throw new IllegalArgumentException("Can't read file: " + file, e); + } + } + + public static JsonNode toJsonNode(Path file) { + try { + return OBJECT_MAPPER.readTree(Files.readAllBytes(file)); + } catch (IOException e) { + throw new IllegalArgumentException("Can't read file: " + file, e); + } + } + public static JsonNode toJsonNode(File value) { try { return value != null ? OBJECT_MAPPER.readTree(value) : null; @@ -249,7 +293,6 @@ public class JacksonUtil { } } - public static JsonNode getSafely(JsonNode node, String... path) { if (node == null) { return null;