tbel fix_bug tbDate parse toString with Object Mapper

This commit is contained in:
nick 2023-12-04 15:26:07 +02:00
parent 34f7730542
commit 240cd25cf4
2 changed files with 22 additions and 2 deletions

View File

@ -15,6 +15,8 @@
*/
package org.thingsboard.script.api.tbel;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
@ -782,4 +784,19 @@ class TbDateTest {
Assert.assertEquals(d.getSeconds(), d.getUTCSeconds());
Assert.assertEquals(d.getMilliseconds(), d.getUTCMilliseconds());
}
@Test
public void tbDateSerializedPMapperTest() {
String stringDateUTC = "2023-09-06T01:04:05.345Z";
TbDate expectedDate = new TbDate(stringDateUTC);
String serializedTbDate = JacksonUtil.toString(expectedDate);
JsonNode tbDateNode = JacksonUtil.toJsonNode(serializedTbDate);
Assert.assertNotNull(tbDateNode);
((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,7 +52,9 @@ import java.util.regex.Pattern;
*/
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()
.enable(SerializationFeature.INDENT_OUTPUT)
.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true)
@ -64,7 +66,8 @@ public class JacksonUtil {
.build();
public static final ObjectMapper IGNORE_UNKNOWN_PROPERTIES_JSON_MAPPER = JsonMapper.builder()
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.build();
.build()
.registerModule(new JavaTimeModule());
public static ObjectMapper getObjectMapperWithJavaTimeModule() {
return new ObjectMapper().registerModule(new JavaTimeModule());