Revert changes to JacksonUtil

This commit is contained in:
Andrii Shvaika 2023-12-05 13:22:59 +02:00
parent 6a0a5750e0
commit fc698b4f31
3 changed files with 9 additions and 15 deletions

View File

@ -15,6 +15,7 @@
*/ */
package org.thingsboard.script.api.tbel; package org.thingsboard.script.api.tbel;
import com.fasterxml.jackson.annotation.JsonValue;
import org.mvel2.ConversionException; import org.mvel2.ConversionException;
import org.thingsboard.common.util.JacksonUtil; import org.thingsboard.common.util.JacksonUtil;
import org.thingsboard.server.common.data.StringUtils; import org.thingsboard.server.common.data.StringUtils;
@ -141,6 +142,7 @@ public class TbDate implements Serializable, Cloneable {
return toLocaleString(localeStr, zoneIdUTC.getId(), (locale, options) -> DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL, FormatStyle.MEDIUM).withLocale(locale)); return toLocaleString(localeStr, zoneIdUTC.getId(), (locale, options) -> DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL, FormatStyle.MEDIUM).withLocale(locale));
} }
@JsonValue
public String toString() { public String toString() {
return toString(Locale.getDefault().getLanguage()); return toString(Locale.getDefault().getLanguage());
} }

View File

@ -37,6 +37,7 @@ import java.time.ZoneId;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
@ -799,17 +800,11 @@ class TbDateTest {
} }
@Test @Test
public void tbDateSerializedPMapperTest() { public void tbDateSerializedMapperTest() {
String stringDateUTC = "2023-09-06T01:04:05.345Z"; String stringDateUTC = "2023-09-06T01:04:05.345Z";
TbDate expectedDate = new TbDate(stringDateUTC); TbDate expectedDate = new TbDate(stringDateUTC);
String serializedTbDate = JacksonUtil.toString(expectedDate); String serializedTbDate = JacksonUtil.toJsonNode(JacksonUtil.toString(Map.of("date", expectedDate))).get("date").asText();
JsonNode tbDateNode = JacksonUtil.toJsonNode(serializedTbDate); Assert.assertNotNull(serializedTbDate);
Assert.assertNotNull(tbDateNode); Assert.assertEquals(expectedDate.toString(), serializedTbDate);
((ObjectNode) tbDateNode).put("test", (String) null);
serializedTbDate = JacksonUtil.toString(tbDateNode);
TbDate actualDate = JacksonUtil.fromStringIgnoreUnknownProperties(serializedTbDate, TbDate.class);
Assert.assertNotNull(actualDate);
Assert.assertEquals(expectedDate.toString(), actualDate.toString());
Assert.assertEquals(expectedDate.getInstant(), actualDate.getInstant());
} }
} }

View File

@ -52,9 +52,7 @@ import java.util.regex.Pattern;
*/ */
public class JacksonUtil { public class JacksonUtil {
public static final ObjectMapper OBJECT_MAPPER = new ObjectMapper() public static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
.registerModule(new JavaTimeModule())
.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
public static final ObjectMapper PRETTY_SORTED_JSON_MAPPER = JsonMapper.builder() public static final ObjectMapper PRETTY_SORTED_JSON_MAPPER = JsonMapper.builder()
.enable(SerializationFeature.INDENT_OUTPUT) .enable(SerializationFeature.INDENT_OUTPUT)
.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true) .configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true)
@ -66,8 +64,7 @@ public class JacksonUtil {
.build(); .build();
public static final ObjectMapper IGNORE_UNKNOWN_PROPERTIES_JSON_MAPPER = JsonMapper.builder() public static final ObjectMapper IGNORE_UNKNOWN_PROPERTIES_JSON_MAPPER = JsonMapper.builder()
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.build() .build();
.registerModule(new JavaTimeModule());
public static ObjectMapper getObjectMapperWithJavaTimeModule() { public static ObjectMapper getObjectMapperWithJavaTimeModule() {
return new ObjectMapper().registerModule(new JavaTimeModule()); return new ObjectMapper().registerModule(new JavaTimeModule());