From b6cbc7ff82a751c5510b3978a591207b274f9a1c Mon Sep 17 00:00:00 2001 From: Holger Friedrich Date: Sun, 28 Apr 2024 20:14:31 +0200 Subject: [PATCH] [mapdb] Explicitly set date format for serialization (#16657) Between Java 17 and Java 21, serialization of Date has changed due to CLDR 42 which uses a narrow non-breaking space. To ease switching JDK versions, the serialization format is explicitly set to the Java 17 format when GsonBuilder is used. Signed-off-by: Holger Friedrich --- .../persistence/mapdb/internal/MapDbPersistenceService.java | 5 +++-- .../org/openhab/persistence/mapdb/StateTypeAdapterTest.java | 4 +++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/bundles/org.openhab.persistence.mapdb/src/main/java/org/openhab/persistence/mapdb/internal/MapDbPersistenceService.java b/bundles/org.openhab.persistence.mapdb/src/main/java/org/openhab/persistence/mapdb/internal/MapDbPersistenceService.java index 7461c9ecb5d..38857b1909c 100644 --- a/bundles/org.openhab.persistence.mapdb/src/main/java/org/openhab/persistence/mapdb/internal/MapDbPersistenceService.java +++ b/bundles/org.openhab.persistence.mapdb/src/main/java/org/openhab/persistence/mapdb/internal/MapDbPersistenceService.java @@ -35,6 +35,7 @@ import org.mapdb.DBMaker; import org.openhab.core.OpenHAB; import org.openhab.core.common.ThreadPoolManager; import org.openhab.core.items.Item; +import org.openhab.core.library.types.DateTimeType; import org.openhab.core.persistence.FilterCriteria; import org.openhab.core.persistence.HistoricItem; import org.openhab.core.persistence.PersistenceItemInfo; @@ -82,8 +83,8 @@ public class MapDbPersistenceService implements QueryablePersistenceService { private @NonNullByDefault({}) DB db; private @NonNullByDefault({}) Map map; - private transient Gson mapper = new GsonBuilder().registerTypeHierarchyAdapter(State.class, new StateTypeAdapter()) - .create(); + private transient Gson mapper = new GsonBuilder().setDateFormat(DateTimeType.DATE_PATTERN_JSON_COMPAT) + .registerTypeHierarchyAdapter(State.class, new StateTypeAdapter()).create(); @Activate public void activate() { diff --git a/bundles/org.openhab.persistence.mapdb/src/test/java/org/openhab/persistence/mapdb/StateTypeAdapterTest.java b/bundles/org.openhab.persistence.mapdb/src/test/java/org/openhab/persistence/mapdb/StateTypeAdapterTest.java index 7350092adfa..ec83fa5944b 100644 --- a/bundles/org.openhab.persistence.mapdb/src/test/java/org/openhab/persistence/mapdb/StateTypeAdapterTest.java +++ b/bundles/org.openhab.persistence.mapdb/src/test/java/org/openhab/persistence/mapdb/StateTypeAdapterTest.java @@ -24,6 +24,7 @@ import java.util.stream.Stream; import org.eclipse.jdt.annotation.NonNullByDefault; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.MethodSource; +import org.openhab.core.library.types.DateTimeType; import org.openhab.core.library.types.DecimalType; import org.openhab.core.library.types.HSBType; import org.openhab.core.library.types.OnOffType; @@ -45,7 +46,8 @@ import com.google.gson.GsonBuilder; */ @NonNullByDefault public class StateTypeAdapterTest { - private Gson mapper = new GsonBuilder().registerTypeHierarchyAdapter(State.class, new StateTypeAdapter()).create(); + private Gson mapper = new GsonBuilder().setDateFormat(DateTimeType.DATE_PATTERN_JSON_COMPAT) + .registerTypeHierarchyAdapter(State.class, new StateTypeAdapter()).create(); private static final List DECIMAL_TYPE_VALUES = List.of(DecimalType.ZERO, new DecimalType(1.123), new DecimalType(10000000));