[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>
This commit is contained in:
Holger Friedrich 2024-04-28 20:14:31 +02:00 committed by GitHub
parent 1fff7eb028
commit b6cbc7ff82
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
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.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<String, String> 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() {

View File

@ -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<DecimalType> DECIMAL_TYPE_VALUES = List.of(DecimalType.ZERO, new DecimalType(1.123),
new DecimalType(10000000));