[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 <mail@holger-friedrich.de>
Signed-off-by: Ciprian Pascu <contact@ciprianpascu.ro>
This commit is contained in:
Holger Friedrich 2024-04-28 20:14:31 +02:00 committed by Ciprian Pascu
parent 0a41961cc5
commit 376c5e01d8
2 changed files with 6 additions and 3 deletions

View File

@ -35,6 +35,7 @@ import org.mapdb.DBMaker;
import org.openhab.core.OpenHAB; import org.openhab.core.OpenHAB;
import org.openhab.core.common.ThreadPoolManager; import org.openhab.core.common.ThreadPoolManager;
import org.openhab.core.items.Item; import org.openhab.core.items.Item;
import org.openhab.core.library.types.DateTimeType;
import org.openhab.core.persistence.FilterCriteria; import org.openhab.core.persistence.FilterCriteria;
import org.openhab.core.persistence.HistoricItem; import org.openhab.core.persistence.HistoricItem;
import org.openhab.core.persistence.PersistenceItemInfo; import org.openhab.core.persistence.PersistenceItemInfo;
@ -82,8 +83,8 @@ public class MapDbPersistenceService implements QueryablePersistenceService {
private @NonNullByDefault({}) DB db; private @NonNullByDefault({}) DB db;
private @NonNullByDefault({}) Map<String, String> map; private @NonNullByDefault({}) Map<String, String> map;
private transient Gson mapper = new GsonBuilder().registerTypeHierarchyAdapter(State.class, new StateTypeAdapter()) private transient Gson mapper = new GsonBuilder().setDateFormat(DateTimeType.DATE_PATTERN_JSON_COMPAT)
.create(); .registerTypeHierarchyAdapter(State.class, new StateTypeAdapter()).create();
@Activate @Activate
public void activate() { public void activate() {

View File

@ -24,6 +24,7 @@ import java.util.stream.Stream;
import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.NonNullByDefault;
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource; 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.DecimalType;
import org.openhab.core.library.types.HSBType; import org.openhab.core.library.types.HSBType;
import org.openhab.core.library.types.OnOffType; import org.openhab.core.library.types.OnOffType;
@ -45,7 +46,8 @@ import com.google.gson.GsonBuilder;
*/ */
@NonNullByDefault @NonNullByDefault
public class StateTypeAdapterTest { 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<DecimalType> DECIMAL_TYPE_VALUES = List.of(DecimalType.ZERO, new DecimalType(1.123), private static final List<DecimalType> DECIMAL_TYPE_VALUES = List.of(DecimalType.ZERO, new DecimalType(1.123),
new DecimalType(10000000)); new DecimalType(10000000));